r/purescript • • Aug 10 '16

Approximating GADTs in PureScript

http://code.slipthrough.net/2016/08/10/approximating-gadts-in-purescript/
20 Upvotes

7 comments sorted by

View all comments

Show parent comment

2

u/gasche Aug 10 '16

I more meant that I couldn't think of a good example of it in a somewhat concrete but easy to show example

For an easy example, you can just extend my example above with pairs:

data T a where
  B :: Bool -> T Bool
  I :: Integer -> T Integer
  P :: (T a, T b) -> T (a, b)

add :: T a -> T a -> T a
add (B b1) (B b2) = B (b1 || b2)
add (I n1) (I n2) = I (n1 + n2)
add (P (a1, b1)) (P (a2, b2)) = P (add a1 a2, add b1 b2)

As Oleg likes to say, eval :: Expr a -> a is not a very good example of GADTs, you must use binary functions to really see the power and difficulties.