Catamorphism?
See original GitHub issueWould it be possible to add a requirement for all ADTs that conform to the Fantasy Land spec that they expose a cata
/fold
/[adt]
method (where [adt]
is a lower case version of the name of the ADT)? This is not as necessary in, say, Haskell, because an ADT comes with a set of type destructors (the type constructors when appearing in a pattern match).
This would allow APIs like that suggested in #151, where a method that takes an ADT and depends on a fold method being present.
Unfortunately, I do not think the general version is typeable and describing it in the language of the spec may be hard as well. Here’s an attempt at a plain English definition of the Haskell equivalent:
The
fold
method takes one argument for each data constructor of the data type. Each argument must have the same arity as its corresponding data constructor. Other than the result type, its signature will match that of the data constructor with all type variables replaced with their value in the concrete type. Its result type may be any variablec
that is the same across all arguments tofold
.
I don’t know if there’s a way to get at this idea without the “hidden model” of type theory that unifies the laws of the methods. Even given that, I can imagine many may be against the idea. I thought it was probably worthwhile writing this issue and getting feedback
Issue Analytics
- State:
- Created 7 years ago
- Reactions:3
- Comments:6 (6 by maintainers)
Top GitHub Comments
It would be possible to type
fold
if it’s not variadic:But I dunno if all
fold
implementations out there are for ADTs with two variants.I bet @puffnfresh wouldn’t mind donating daggy to fantasy-land tbh.