Tags: Haskell, EADT, Variant | October 28, 2018 |
A short post to announce that I have just released version 2.0.1 of my haskus-utils-variant
package (containing Variant and EADT).
The documentation has also been greatly enhanced thanks to the feedback I’ve received:
As a short introduction, a Variant is a generic sum type (i.e. like Either
but supporting any number of value types instead of just 2):
w,x,y,z :: V '[String,Int,Float,Maybe String]
w = V "test"
x = V @Int 10
y = V @Float 5.0
z = V (Just "Hello World")
Similarly, an EADT is an extensible algebraic data type (ADT). Compared to Variant, it supports recursively defined ADT. For instance a list EADT can be defined with:
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE TypeFamilies #-}
data ConsF a l = ConsF a l deriving (Functor)
data NilF l = NilF deriving (Functor)
eadtPattern 'ConsF "Cons"
eadtPattern 'NilF "Nil"
type List a = EADT '[ConsF a, NilF]
x :: List String
x = Cons "Hello" (Cons "World" Nil)
Of course the whole point of EADT is not to redefine lists in a weird way but to help with codes where we would like to add or remove ADT constructors (e.g. different abstract syntax trees (AST) in a compiler) or to support generic recursive object hierarchies (e.g. extensible GUI widget hierarchies).
Any feedback appreciated!