I'd like to see a Coq version of the Bananas, Lenses, etc. They are built up in the excellent series of blog posts at sumtypeofway Introduction to Recursion schemes
However, the blog post is in Haskell, which permits infinite non-terminating recursion, and thus is perfectly content with the Y
combinator. Which Coq isn't.
In particular, the definitions depend on the type
newtype Term f = In { out :: f (Term f) }
which builds inifinite types f (f (f (f ...)))
. Term f
permits very pretty and succinct definitions for catamorphisms, paramorphisms, anamorphisms, etc., using the Term family of types.
Trying to port this to Coq as
Inductive Term f : Type := {out:f (Term f)}.
gives me the expected
Error: Non strictly positive occurrence of "Term" in "f (Term f) -> Term f".
Q: What would be a good way to formalize the above Haskell Term type in Coq?
Above f
is of type Type->Type
, but perhaps it is too general, and there could be some way to restrict us to inductive types such that each application of f
is decreasing?
Perhaps someone has already implemented the recursion schemes from Banans, Lenses, Envelopes in Coq?