recursion-schemes

Optimizing a Free Monad

牧云@^-^@ 提交于 2021-02-19 03:44:10
问题 If I have a value a: Free[Op, A] , is it possible to "flatten" the structure of a so that two Op s that are bound together by the free monad may be collapsed into one? Context: I'd like to perform this as an optimization step before interpretation because a semantic of Op is that its operations are idempotent. So if two appear "in a row", the second can be eliminated at no cost to the semantics of the program. 回答1: As far as I understand there is no way for this kind introspection of Free

What is the difference between Fix, Mu and Nu in Ed Kmett's recursion scheme package

做~自己de王妃 提交于 2020-06-09 09:25:54
问题 In Ed Kmett's recursion-scheme package, there are three declarations: newtype Fix f = Fix (f (Fix f)) newtype Mu f = Mu (forall a. (f a -> a) -> a) data Nu f where Nu :: (a -> f a) -> a -> Nu f What is the difference between those three data types? 回答1: Mu represents a recursive type as its fold, and Nu represents it as its unfold. In Haskell, these are isomorphic, and are different ways to represent the same type. If you pretend that Haskell doesn't have arbitrary recursion, the distinction

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

爱⌒轻易说出口 提交于 2020-01-10 14:08:57
问题 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.

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

╄→гoц情女王★ 提交于 2019-12-20 10:10:48
问题 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

Generate lexicographic series efficiently in Python

柔情痞子 提交于 2019-12-20 05:15:19
问题 I want to generate a lexicographic series of numbers such that for each number the sum of digits is a given constant. It is somewhat similar to 'subset sum problem'. For example if I wish to generate 4-digit numbers with sum = 3 then I have a series like: [3 0 0 0] [2 1 0 0] [2 0 1 0] [2 0 0 1] [1 2 0 0] ... and so on. I was able to do it successfully in Python with the following code: import numpy as np M = 4 # No. of digits N = 3 # Target sum a = np.zeros((1,M), int) b = np.zeros((1,M), int

Generate lexicographic series efficiently in Python

我只是一个虾纸丫 提交于 2019-12-20 05:15:06
问题 I want to generate a lexicographic series of numbers such that for each number the sum of digits is a given constant. It is somewhat similar to 'subset sum problem'. For example if I wish to generate 4-digit numbers with sum = 3 then I have a series like: [3 0 0 0] [2 1 0 0] [2 0 1 0] [2 0 0 1] [1 2 0 0] ... and so on. I was able to do it successfully in Python with the following code: import numpy as np M = 4 # No. of digits N = 3 # Target sum a = np.zeros((1,M), int) b = np.zeros((1,M), int

How to work with AST with Cofree annotation?

拈花ヽ惹草 提交于 2019-12-17 23:15:46
问题 I have this simple Expr AST and I can easily convert it to String . import Prelude hiding (Foldable) import qualified Prelude import Data.Foldable as F import Data.Functor.Foldable import Data.Monoid import Control.Comonad.Cofree data ExprF r = Const Int | Add r r deriving ( Show, Eq, Ord, Functor, Prelude.Foldable ) type Expr = Fix ExprF testExpr = Fix $ Add (Fix (Const 1)) (Fix (Const 2)) convertToString :: Expr -> String convertToString = cata $ \case e@(Const x) -> show x e@(Add x y) ->

How to define the fibonacci sequence using a fold for natural numbers?

大兔子大兔子 提交于 2019-12-12 19:14:28
问题 I am currently learning folds in the sense of structural recursion/catamorphisms. I implemented power and factorial using a fold for natural numbers. Please note that I barely know Haskell, so the code is probably awkward: foldNat zero succ = go where go n = if (n <= 0) then zero else succ (go (n - 1)) pow n = foldNat 1 (n*) fact n = foldNat 1 (n*) n Next I wanted to adapt the fibonacci sequence: fib n = go n (0,1) where go !n (!a, !b) | n==0 = a | otherwise = go (n-1) (b, a+b) With fib I

List filter using an anamorphism

人走茶凉 提交于 2019-12-08 15:32:10
问题 I implemented a broken filter function using an anamorphism from recursion-schemes Hackage library: import Data.Functor.Foldable xfilter :: (a -> Bool) -> [a] -> [a] xfilter f = ana $ project . phi f phi :: (a -> Bool) -> [a] -> [a] phi f (h : t) | not (f h) = t phi f l = l The function is not a faithful implementation of filter : xfilter odd [1..5] works, but xfilter odd [0,0] doesn't. I tried to implement "retries" by using explicit recursion in phi and then reimplemented that with a