Print `Control.Alternative.Free.Alt` type

77 Views Asked by At

Given,

{-# LANGUAGE DeriveFunctor #-}
import Control.Alternative.Free (Alt, liftAlt)
import Control.Applicative ((<|>), (*>), (<*), many)

data Primitive a = Primitive Char a
  deriving (Show, Functor)

type RegExp = Alt Primitive

charAs :: Char -> a -> RegExp a
charAs c x = liftAlt (Primitive c x)

char :: Char -> RegExp Char
char c = charAs c c

string :: String -> RegExp String
string = traverse char

How would you actually print it? I get

> string "ASF"

<interactive>:67:1: error:
    * No instance for (Show (RegExp String))

source: https://blog.jle.im/entry/free-alternative-regexp.html

1

There are 1 best solutions below

2
amalloy On

There's no particular reason to assume it would be printable. How would you expect it to print? An Alt f a will contain functions, which can't be printed. If you want printable regexes, you will have to construct a type which contains both a function and also a printable representation.