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

1

u/gb__ Aug 10 '16

Yeah, that's definitely an ugly consequence of this approach - in the example above it's not too hard for a human to figure out that the missing cases won't occur, but it could be much harder in a less contrived case.

And thanks for the injectivity example - I did remember that from the paper, I more meant that I couldn't think of a good example of it in a somewhat concrete but easy to show example. A lack of imagination on my part no doubt.

I would love to have real GADTs in PureScript (it would help with the situation for handling existentials also), but for now this is still a potentially useful technique when the basic equality is all you need. I liked this application of Leibniz too, as I found it helpful to get my head around how it might actually be used in practice.

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.