r/purescript • u/gb__ • Aug 10 '16
Approximating GADTs in PureScript
http://code.slipthrough.net/2016/08/10/approximating-gadts-in-purescript/
20
Upvotes
2
u/rtpg Aug 11 '16
This is pretty similar to how you solve a lack of existential types: You carry around all the operations you might do on the type inside the data.
data ShapeContainer a = C {
data :: a,
draw :: a -> Bitmap -> Bitmap
}
-- Exists from purescript-exists
data Shape = Exists ShapeContainer
[...]
shapes :: List Shape
shapes = [
circle center radius,
bmp origin bitmapData,
rectangle origin width height
]
addShape :: Bitmap -> Shape -> Bitmap
addShape startingImage shape = runExists shape
(\(C shape) -> shape.draw shape.data startingImage)
-- final image is the result of successively applying each shape drawing
finalImage = foldl addShape blankBitmap shapes1
u/nikita-volkov Aug 10 '16
I just love when the problems get solved without extending the language. This is beautiful!
1
u/gasche Aug 10 '16
The problem with this encoding with equality is that most encoding of type equalities are bad at reasoning with inequalities or, worse, constructor injectivity. How would you implement, for example
?