recursion-schemes-ext 0.1.1.0 → 0.1.1.1
raw patch · 5 files changed
+55/−34 lines, 5 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
- Data.Functor.Foldable.Extensions: coswitch :: CoSubHom f g a b => a -> a
- Data.Functor.Foldable.Examples: AddF :: r_aguv -> r_aguv -> BertF r_aguv
+ Data.Functor.Foldable.Examples: AddF :: r_aguW -> r_aguW -> BertF r_aguW
- Data.Functor.Foldable.Examples: BertF :: Ernie -> BertF r_aguv
+ Data.Functor.Foldable.Examples: BertF :: Ernie -> BertF r_aguW
- Data.Functor.Foldable.Examples: ErnieF :: Bert -> ErnieF r_agfl
+ Data.Functor.Foldable.Examples: ErnieF :: Bert -> ErnieF r_agfM
- Data.Functor.Foldable.Examples: ListF :: [r_agfl] -> ErnieF r_agfl
+ Data.Functor.Foldable.Examples: ListF :: [r_agfM] -> ErnieF r_agfM
- Data.Functor.Foldable.Examples: MultiplyF :: r_agfl -> r_agfl -> ErnieF r_agfl
+ Data.Functor.Foldable.Examples: MultiplyF :: r_agfM -> r_agfM -> ErnieF r_agfM
- Data.Functor.Foldable.Examples: NumF :: Integer -> BertF r_aguv
+ Data.Functor.Foldable.Examples: NumF :: Integer -> BertF r_aguW
- Data.Functor.Foldable.Examples: StringF :: String -> BertF r_aguv
+ Data.Functor.Foldable.Examples: StringF :: String -> BertF r_aguW
- Data.Functor.Foldable.Examples: data BertF r_aguv
+ Data.Functor.Foldable.Examples: data BertF r_aguW
- Data.Functor.Foldable.Examples: data ErnieF r_agfl
+ Data.Functor.Foldable.Examples: data ErnieF r_agfM
- Data.Functor.Foldable.Extensions: chema :: (CoSubHom (Base t) (Base t') a b, SubType b, Corecursive t') => (a -> Base t a) -> (b -> Base t' b) -> b -> t'
+ Data.Functor.Foldable.Extensions: chema :: (CoSubHom (Base t) (Base t') a b, SubType b, Corecursive t') => t -> (a -> Base t a) -> (b -> Base t' b) -> b -> t'
Files
- README.md +7/−1
- recursion-schemes-ext.cabal +2/−1
- src/Data/Functor/Foldable/Examples.hs +11/−10
- src/Data/Functor/Foldable/Extensions.hs +22/−19
- src/Data/Functor/Foldable/Extensions/TH.hs +13/−3
README.md view
@@ -9,6 +9,13 @@ ## Pitch +### Monadic Functions++This package provides `cataM`, `anaM`, and `hyloM`. That means you can have+(co)algebras that return a monadic value.++### Dendromorphisms etc.+ Let's say you want to collapse a syntax tree. Suppose further that it's a relatively involved syntax tree, and you have some data types that encapsulate others. Here's a simple-minded example, where we collapse using traditional@@ -64,7 +71,6 @@ bertAlgebra x = embed x ernieAlgebra :: ErnieF Ernie -> Ernie-ernieAlgebra (ErnieF (Bert e)) = e ernieAlgebra (MultiplyF (Ernie (Num i)) (Ernie (Num j))) = Ernie . Num $ i * j ernieAlgebra x = embed x
recursion-schemes-ext.cabal view
@@ -1,5 +1,5 @@ name: recursion-schemes-ext-version: 0.1.1.0+version: 0.1.1.1 synopsis: Amateur addenda to recursion-schemes description: This package provides some exotic recursion schemes that I miss when I leave Idris. homepage: https://hub.darcs.net/vmchale/recursion-schemes-ext#readme@@ -10,6 +10,7 @@ copyright: Copyright: (c) 2017 Vanessa McHale category: Control build-type: Simple+stability: experimental extra-source-files: README.md , stack.yaml , .travis.yml
src/Data/Functor/Foldable/Examples.hs view
@@ -9,10 +9,13 @@ {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TypeFamilies #-} -module Data.Functor.Foldable.Examples ( Bert (..)+-- | This module contains an example used by the test suite.+module Data.Functor.Foldable.Examples ( -- * Data Types+ Bert (..) , Ernie (..) , BertF (..) , ErnieF (..)+ -- * Catamorphisms , collapseErnieSyntaxTree , collapseErnieSyntaxTree' , collapseBertSyntaxTree@@ -38,13 +41,9 @@ | List [Ernie] deriving (Show, Eq, Generic, NFData) --- want: entangleBaseFunctors function to do this automatically!- makeBaseFunctor ''Ernie makeBaseFunctor ''Bert --- TODO default/dummy? Also infer dummy from applicative + dummy underlying type- instance Dummy Bert where dummy = Num 3 @@ -54,31 +53,33 @@ entanglePair ''Ernie ''Bert entanglePair ''Bert ''Ernie +-- | BertF-algebra bertAlgebra :: BertF Bert -> Bert bertAlgebra (AddF (Num i) (Num j)) = Num $ i + j bertAlgebra x = embed x +-- | ErnieF-algebra ernieAlgebra :: ErnieF Ernie -> Ernie-ernieAlgebra (ErnieF (Bert e)) = e ernieAlgebra (MultiplyF (Ernie (Num i)) (Ernie (Num j))) = Ernie . Num $ i * j ernieAlgebra x = embed x --- | Dendromorphism collapsing the tree.+-- | Dendromorphism collapsing the tree. Note that we can use the same+-- F-algebras here as we would in a normal catamorphism. collapseErnieSyntaxTree :: (Recursive Ernie) => Ernie -> Ernie collapseErnieSyntaxTree = dendro (dummy :: Bert) bertAlgebra ernieAlgebra --- | We get two dendromorphisms for the price of one!+-- | We can generate two functions by swapping the F-algebras and the dummy+-- type. collapseBertSyntaxTree :: (Recursive Bert) => Bert -> Bert collapseBertSyntaxTree = dendro (dummy :: Ernie) ernieAlgebra bertAlgebra --- | Catamorphism, which collapses the tree, but not very well.+-- | Catamorphism, which collapses the tree the usual way. collapseErnieSyntaxTree' :: (Recursive Ernie) => Ernie -> Ernie collapseErnieSyntaxTree' = cata algebra where algebra (ErnieF e) = Ernie $ collapseBertSyntaxTree' e algebra (MultiplyF (Ernie (Num i)) (Ernie (Num j))) = Ernie . Num $ i * j algebra x = embed x --- | Another catamorphism that is stupid and lame. collapseBertSyntaxTree' :: (Recursive Bert) => Bert -> Bert collapseBertSyntaxTree' = cata algebra where algebra (BertF e) = Bert $ collapseErnieSyntaxTree' e
src/Data/Functor/Foldable/Extensions.hs view
@@ -4,23 +4,28 @@ {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE TemplateHaskell #-} +-- | Several extensions to Edward Kmett's recursion schemes package. The monadic+-- recursion schemes and exotic recursion schemes should be stable, but the+-- recursion schemes for interdependent data type (and their attendant+-- typeclasses) are experimental. module Data.Functor.Foldable.Extensions- ( -- | Functions- dicata+ ( -- * Classes+ SubHom (..)+ , SubType (..)+ , CoSubHom (..)+ , Dummy (..)+ -- * Monadic recursion schemes+ , cataM+ , anaM+ , hyloM+ -- * Recursion schemes for interdependent data types , dendro , dendroTri- , micro , symplecto , chema- -- | Monadic recursion schemes- , cataM- , anaM- , hyloM- -- | Classes- , SubHom (..)- , SubType (..)- , CoSubHom (..)- , Dummy (..)+ -- * Exotic recursion schemes+ , dicata+ , micro ) where import Control.Arrow@@ -45,9 +50,6 @@ -- | Homomorphism of g-coalgebras paramterized by an f-coalgebra homoCo :: (a -> f a) -> (b -> g b) -> (b -> g b) - -- | Resolve nested functions- coswitch :: a -> a- -- | We need this class to make type resolution work. class Dummy t where dummy :: t@@ -65,10 +67,11 @@ -- Entangle two anamorphisms. chema :: (CoSubHom (Base t) (Base t') a b, SubType b, Corecursive t')- => (a -> Base t a) -- A (Base t)-coalgebra+ => t -- ^ dummy type+ -> (a -> Base t a) -- A (Base t)-coalgebra -> (b -> Base t' b) -- A (Base t')-coalgebra -> b -> t'-chema = pseudoana .* homoCo+chema = const (pseudoana .* homoCo) where pseudoana g = a where a = embed . fmap a . g . switch -- | A dendromorphism allows us to entangle two catamorphisms@@ -78,7 +81,7 @@ -> (Base t' b -> b) -- ^ A (Base t')-algebra -> t' -> b dendro = const (pseudocata .* homo)- where pseudocata f = c where c = switch . f . fmap c . project+ where pseudocata f = c where c = switch . f . fmap (switch . c) . project -- | Entangle three base functors. dendroTri :: (SubHom (Base t) (Base t') a b, SubType b, Recursive t', SubHom (Base t'') (Base t) c a, SubType a, Recursive t)@@ -88,7 +91,7 @@ -> (Base t a -> a) -- A (Base t)-algebra -> (Base t' b -> b) -- A (Base t')-algebra -> t' -> b-dendroTri = const . (switch .** homo -.* ((.) . dendro))+dendroTri = fmap const (switch .** homo -.* (fmap <$> dendro)) -- | Catamorphism collapsing along two data types simultaneously. Basically a fancy zygomorphism. dicata :: (Recursive a) => (Base a (b, a) -> b) -> (Base a (b, a) -> a) -> a -> b
src/Data/Functor/Foldable/Extensions/TH.hs view
@@ -4,8 +4,10 @@ {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE TemplateHaskell #-} +-- | Module containing Template Haskell functions to automically intertwine the+-- base functors of the given types. module Data.Functor.Foldable.Extensions.TH- ( -- | Template Haskell helpers+ ( -- * Template Haskell helpers entangleFunctors , entanglePair ) where@@ -13,11 +15,19 @@ import Data.Functor.Foldable.Extensions import Language.Haskell.TH --- | Make the abscissae a subtype of the ordinates.+-- | Entangle a list of functors. As an example,+--+-- > entangleFunctors [(''Data, ''Codata)]+--+-- will generate+--+-- > instance SubHom DataF CodataF Data Codata+-- > instance SubType Codata entangleFunctors :: [(Name, Name)] -> Q [Dec] entangleFunctors = fmap concat . traverse (uncurry entanglePair) --- | Entangle two functors, creating a 'SubHom' instance. Note that this is rather strict with regards to naming.+-- | Entangle types, creating a 'SubHom' instance with their base functors.+-- Note that this is rather strict with regards to naming. entanglePair :: Name -> Name -> Q [Dec] entanglePair sub top = pure [subHomInstance, subTypeInstance] where