tschuchort
04/18/2020, 11:08 AMraulraja
04/18/2020, 11:54 AMpakoito
04/18/2020, 12:09 PMtschuchort
04/18/2020, 12:19 PMdata Exp = Const Int | Add Exp Exp
Later on you want to have another data type, that is very similar to your existing data type, except you want to add some information. For example wrap all value constructors in another type to annotate every "node" of the "tree" that is an instance of this data type. If the data type were not recursive you could just do:`data LineNr a = LineNr Int a` But this annotates only one level of the tree. All inner nodes will still be just Exp
not LineNr
. The core idea is to solve this by wrapping Exp
in LineNr
before the recursive knot is tied, and then do the recursion later with `Fix`:
data ExpF a = Const Int | Add a a
data LineNr a = LineNr Int a
type Exp = Fix (LineNr ExpF)
The rest of "data types a la carte" is to define each case as it's own type (instead of a type constructor in one larger type) and combine them using a coproduct type. Interpreters for each case are defined as a type class. You can then get an interpreter for any arbitrary coproduct using the initial F-algebra IIRC.raulraja
04/18/2020, 2:39 PMJannis
04/18/2020, 2:58 PM