scott-encoding

More efficient tail of church encoded list

风格不统一 提交于 2019-12-09 14:42:44
问题 This is a literate haskell post. Simply save it as "ChurchList.lhs" to run it. > {-# LANGUAGE Rank2Types #-} A Church encoded list is a way of representing a list via a function. It resembles both folding and continuation passing style. > newtype ChurchList a = CList {runCList :: forall r. (a -> r -> r) -> r -> r} For illustration as to how this corresponds to a list, here is a O(n) isomorphism > fromList :: [a] -> ChurchList a > fromList xs = CList $ \cons empty -> foldr cons empty xs >

How do I use the Church encoding for Free Monads?

心已入冬 提交于 2019-12-09 09:33:43
问题 I've been using the Free datatype in Control.Monad.Free from the free package. Now I'm trying to convert it to use F in Control.Monad.Free.Church but can't figure out how to map the functions. For example, a simple pattern matching function using Free would look like this - -- Pattern match Free matchFree :: (a -> r) -> (f (Free f a) -> r) -> Free f a -> r matchFree kp _ (Pure a) = kp a matchFree _ kf (Free f) = kf f I can easily convert it to a function that uses F by converting to/from Free

How do you represent nested types using the Scott Encoding?

浪子不回头ぞ 提交于 2019-12-07 17:03:46
问题 An ADT can be represented using the Scott Encoding by replacing products by tuples and sums by matchers. For example: data List a = Cons a (List a) | Nil Can be encoded using the Scott Encoding as: cons = (λ h t c n . c h t) nil = (λ c n . n) But I couldn't find how nested types can be encoded using SE: data Tree a = Node (List (Tree a)) | Leaf a How can it be done? 回答1: If the Wikipedia article is correct, then data Tree a = Node (List (Tree a)) | Leaf a has Scott encoding node = λ a . λ

How do you represent nested types using the Scott Encoding?

不羁岁月 提交于 2019-12-06 01:37:20
An ADT can be represented using the Scott Encoding by replacing products by tuples and sums by matchers. For example: data List a = Cons a (List a) | Nil Can be encoded using the Scott Encoding as: cons = (λ h t c n . c h t) nil = (λ c n . n) But I couldn't find how nested types can be encoded using SE: data Tree a = Node (List (Tree a)) | Leaf a How can it be done? If the Wikipedia article is correct, then data Tree a = Node (List (Tree a)) | Leaf a has Scott encoding node = λ a . λ node leaf . node a leaf = λ a . λ node leaf . leaf a It looks like the Scott encoding is indifferent to (nested

More efficient tail of church encoded list

早过忘川 提交于 2019-12-04 02:13:30
This is a literate haskell post. Simply save it as "ChurchList.lhs" to run it. > {-# LANGUAGE Rank2Types #-} A Church encoded list is a way of representing a list via a function. It resembles both folding and continuation passing style. > newtype ChurchList a = CList {runCList :: forall r. (a -> r -> r) -> r -> r} For illustration as to how this corresponds to a list, here is a O(n) isomorphism > fromList :: [a] -> ChurchList a > fromList xs = CList $ \cons empty -> foldr cons empty xs > toList :: ChurchList a -> [a] > toList cl = runCList cl (:) [] > instance Show a => Show (ChurchList a)

How do I use the Church encoding for Free Monads?

戏子无情 提交于 2019-12-03 12:22:13
I've been using the Free datatype in Control.Monad.Free from the free package. Now I'm trying to convert it to use F in Control.Monad.Free.Church but can't figure out how to map the functions. For example, a simple pattern matching function using Free would look like this - -- Pattern match Free matchFree :: (a -> r) -> (f (Free f a) -> r) -> Free f a -> r matchFree kp _ (Pure a) = kp a matchFree _ kf (Free f) = kf f I can easily convert it to a function that uses F by converting to/from Free - -- Pattern match F (using toF and fromF) matchF :: Functor f => (a -> r) -> (f (F f a) -> r) -> F f