I understand the regular fixed-point type combinator and I think I understand the higher-order fixed-n type combinators, but HFix eludes me. Could you give an example of a set of data-types and their (manually derived) fixed points that you can apply HFix to.
I understand the regular fixed-point type combinator and I think I understand the higher-order
Share
The natural reference is the paper Generic programming with fixed points for mutually recursive datatypes
where the multirec package is explained.
HFixis a fixpoint type combinator for mutually recursive data types.It is well explained in Section 3.2 in the paper, but the idea is
to generalise this pattern:
to
To restrict how many types it does a fixed point over, they use type constructors
instead of *^n. They give an example of an AST data type, mutually recursive over
three types in the paper. I offer you perhaps the simplest example instead. Let
us HFix this data type:
Let us introduce the family specific GADT for this datatype as is done in section 4.1
EO Evenwill mean that we are carrying around an even number.We need El instances for this to work, which says which specific constructor
we are refering to when writing
EO EvenandEO Oddrespectively.These are used as constraints for the
HFunctorinstancefor I.
Let us now define the pattern functor for the even and odd data type.
We use the combinators from the library. The
:>:type constructor tagsa value with its type index:
Now we can use
HFixto tie the knot around this pattern functor:These are now isomorphic to EO Even and EO Odd, and we can use the
hfromandhtofunctionsif we make it an instance of
Fam:A simple little test:
Another silly test with an Algebra turning
EvenandOdds to theirIntvalue: