recursion-schemes

Haskell: Labeling an AST with type information using Algorithm W

自古美人都是妖i 提交于 2019-12-07 18:41:22
问题 We have an AST definition: data Term a = Lam String a | App a a | Var String deriving(Read,Show,Eq,Functor,Foldable,Traversable) And an F-Algebra for the type inference: type Wrapped m a = Enviroment -> m a type Result m = Wrapped (Type,Substitution) w :: (MonadState Int, MonadError TypeError) => Term (Result m) -> Result m w term env = ... We can get the result of running the inference using cata : infer :: (MonadState Int, MonadError TypeError) => Fix Term -> Result m infer ast = cata hm

Haskell: Labeling an AST with type information using Algorithm W

馋奶兔 提交于 2019-12-06 06:21:37
We have an AST definition: data Term a = Lam String a | App a a | Var String deriving(Read,Show,Eq,Functor,Foldable,Traversable) And an F-Algebra for the type inference: type Wrapped m a = Enviroment -> m a type Result m = Wrapped (Type,Substitution) w :: (MonadState Int, MonadError TypeError) => Term (Result m) -> Result m w term env = ... We can get the result of running the inference using cata : infer :: (MonadState Int, MonadError TypeError) => Fix Term -> Result m infer ast = cata hm ast But now I want the result to be the original AST annotated with type information for each expression,

Histomorphisms, Zygomorphisms and Futumorphisms specialised to lists

喜你入骨 提交于 2019-12-03 17:53:48
问题 I ended up figuring it out. See the video and slides of a talk I gave: slides/pdf video Original question: In my effort to understand generic recursion schemes (i.e., that use Fix ) I have found it useful to write list-only versions of the various schemes. It makes it much easier to understand the actual schemes (without the additional overhead of the Fix stuff). However, I have not yet figured out how to define list-only versions of zygo and futu . Here are my specialised definitions so far:

What's the type of a catamorphism (fold) for non-regular recursive types?

五迷三道 提交于 2019-12-02 21:44:36
Many catamorphisms seem to be simple enough, mostly replacing each data constructor with a custom function, e.g. data Bool = False | True foldBool :: r -- False constructor -> r -- True constructor -> Bool -> r data Maybe a = Nothing | Just a foldMaybe :: b -- Nothing constructor -> (a -> b) -- Just constructor -> Maybe a -> b data List a = Empty | Cons a (List a) foldList :: b -- Empty constructor -> (a -> b -> b) -- Cons constructor -> List a -> b However, what's not clear to me is what happens if the same type constructor is used, but with a different type argument. E.g. instead of passing

Recursion schemes for dummies?

混江龙づ霸主 提交于 2019-11-30 10:03:16
问题 I'm looking for some really simple, easy-to-grasp explanations of recursion schemes and corecursion schemes (catamorphisms, anamorphisms, hylomorphisms etc.) which do not require following lots of links, or opening a category theory textbook. I'm sure I've reinvented many of these schemes unconsciously and "applied" them in my head during the process of coding (I'm sure many of us have), but I have no clue what the (co)recursion schemes I use are called. (OK, I lied. I've just been reading

Once I have an F-Algebra, can I define Foldable and Traversable in terms of it?

若如初见. 提交于 2019-11-30 08:48:32
I have defined an F-Algebra , as per Bartosz Milewski's articles ( one , two ): (This is not to say my code is an exact embodiment of Bartosz's ideas, it's merely my limited understanding of them, and any faults are mine alone.) module Algebra where data Expr a = Branch [a] | Leaf Int instance Functor Expr where fmap f (Branch xs) = Branch (fmap f xs) fmap _ (Leaf i ) = Leaf i newtype Fix a = Fix { unFix :: a (Fix a) } branch = Fix . Branch leaf = Fix . Leaf -- | This is an example algebra. evalSum (Branch xs) = sum xs evalSum (Leaf i ) = i cata f = f . fmap (cata f) . unFix I can now do

Recursion schemes for dummies?

若如初见. 提交于 2019-11-29 19:03:41
I'm looking for some really simple, easy-to-grasp explanations of recursion schemes and corecursion schemes (catamorphisms, anamorphisms, hylomorphisms etc.) which do not require following lots of links, or opening a category theory textbook. I'm sure I've reinvented many of these schemes unconsciously and "applied" them in my head during the process of coding (I'm sure many of us have), but I have no clue what the (co)recursion schemes I use are called. (OK, I lied. I've just been reading about a few of them, which prompted this question. But before today, I had no clue.) I think diffusion of

Histomorphisms, Zygomorphisms and Futumorphisms specialised to lists

时间秒杀一切 提交于 2019-11-28 15:28:48
I ended up figuring it out. See the video and slides of a talk I gave: slides/pdf video Original question: In my effort to understand generic recursion schemes (i.e., that use Fix ) I have found it useful to write list-only versions of the various schemes. It makes it much easier to understand the actual schemes (without the additional overhead of the Fix stuff). However, I have not yet figured out how to define list-only versions of zygo and futu . Here are my specialised definitions so far: cataL :: (a -> b -> b) -> b -> [a] -> b cataL f b (a : as) = f a (cataL f b as) cataL _ b [] = b paraL

What does “coalgebra” mean in the context of programming?

筅森魡賤 提交于 2019-11-28 14:53:51
I have heard the term "coalgebras" several times in functional programming and PLT circles, especially when the discussion is about objects, comonads, lenses, and such. Googling this term gives pages that give mathematical description of these structures which is pretty much incomprehensible to me. Can anyone please explain what coalgebras mean in the context of programming, what is their significance, and how they relate to objects and comonads? Algebras I think the place to start would be to understand the idea of an algebra . This is just a generalization of algebraic structures like groups

What is a catamorphism and can it be implemented in C# 3.0?

梦想与她 提交于 2019-11-27 17:44:13
I'm trying to learn about catamorphisms and I've read the Wikipedia article and the first couple posts in the series of the topic for F# on the Inside F# blog. I understand that it's a generalization of folds (i.e., mapping a structure of many values to one value, including a list of values to another list). And I gather that the fold-list and fold-tree is a canonical example. Can this be shown to be done in C#, using LINQ's Aggregate operator or some other higher-order method? Brian LINQ's Aggregate() is just for IEnumerables . Catamorphisms in general refer to the pattern of folding for an