question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Would 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 variable c that is the same across all arguments to fold.

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:open
  • Created 7 years ago
  • Reactions:3
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
robotlolitacommented, Sep 6, 2016

It would be possible to type fold if it’s not variadic:

-- e.g.: either.fold(whenLeft, whenRight)
fold :: t a b -> (a -> c) -> (b -> c) -> c
-- e.g.: trilean.fold3(whenTrue, whenFalse, whenUnknown)
fold3 :: t a b c -> (a -> d) -> (b -> d) -> (c -> d) -> d
-- e.g.: future.fold4(whenPending, whenCancelled, whenRejected, whenResolved)
fold4 :: t a b c d -> (a -> e) -> (b -> e) -> (c -> e) -> (d -> e) -> e

But I dunno if all fold implementations out there are for ADTs with two variants.

0reactions
SimonRichardsoncommented, Sep 13, 2016

I bet @puffnfresh wouldn’t mind donating daggy to fantasy-land tbh.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Catamorphism
In functional programming, catamorphisms provide generalizations of folds of lists to arbitrary algebraic data types, which can be described as ...
Read more >
Catamorphisms - HaskellWiki
Catamorphisms are generalizations of the concept of a fold in functional programming. A catamorphism deconstructs a data structure with an ...
Read more >
Catamorphisms - ploeh blog
A catamorphism is a general abstraction that enables you to handle multiple values, for example in order to reduce them to a single...
Read more >
catamorphism in nLab
In the study of recursion schemes, a catamorphism is the simple case of a fold over an inductive data type. In category theoretic...
Read more >
catamorphism
(computer science) A generalization of the folds on lists known from functional programming to arbitrary abstract data types that can be described as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found