yaya-unsafe 0.1.1.2 → 0.2.0.0
raw patch · 5 files changed
+70/−69 lines, 5 filesdep ~yaya
Dependency ranges changed: yaya
Files
- CHANGELOG.md +8/−0
- src/Yaya/Unsafe/Fold.hs +26/−32
- src/Yaya/Unsafe/Fold/Instances.hs +12/−13
- src/Yaya/Unsafe/Zoo.hs +22/−22
- yaya-unsafe.cabal +2/−2
CHANGELOG.md view
@@ -4,6 +4,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to the [Haskell Package Versioning Policy](https://pvp.haskell.org/). +## 0.2.0.0 – 2020–05–14+### Changed+- updated in sync with polykinding changes in yaya-0.3.0.0++## 0.1.1.3 – 2020–05–14+### Changed+- enabled and fixed warnings+ ## 0.1.1.2 – 2019–11–08 ### Changed - improved documentation
src/Yaya/Unsafe/Fold.hs view
@@ -4,68 +4,62 @@ import Control.Arrow import Control.Comonad-import Control.Comonad.Cofree-import Control.Comonad.Env import Control.Lens import Control.Monad-import Control.Monad.Trans.Free-import Data.Either.Combinators-import Data.Function import Data.Functor.Compose-import Data.Functor.Identity import Yaya.Fold -- | This can’t be implemented in a total fashion. There is a _similar_ approach--- that can be total – with `ψ :: CoalgebraM m f a`, `ana (Compose . ψ)`+-- that can be total – with `ψ :: CoalgebraM (->) m f a`, `ana (Compose . ψ)` -- results in something like `Nu (Compose m f)` which is akin to an effectful -- stream.-anaM :: (Monad m, Steppable t f, Traversable f) => CoalgebraM m f a -> a -> m t+anaM :: (Monad m, Steppable (->) t f, Traversable f) => CoalgebraM (->) m f a -> a -> m t anaM = hyloM (pure . embed) ganaM- :: (Monad m, Monad n, Traversable n, Steppable t f, Traversable f)- => DistributiveLaw n f- -> GCoalgebraM m n f a+ :: (Monad m, Monad n, Traversable n, Steppable (->) t f, Traversable f)+ => DistributiveLaw (->) n f+ -> GCoalgebraM (->) m n f a -> a -> m t ganaM k ψ = anaM (lowerCoalgebraM k ψ) . pure -- | Fusion of an 'ana' and 'cata'.-hylo :: Functor f => Algebra f b -> Coalgebra f a -> a -> b+hylo :: Functor f => Algebra (->) f b -> Coalgebra (->) f a -> a -> b hylo φ ψ = go where go = φ . fmap go . ψ ghylo :: (Comonad w, Monad m, Functor f)- => DistributiveLaw f w- -> DistributiveLaw m f- -> GAlgebra w f b- -> GCoalgebra m f a+ => DistributiveLaw (->) f w+ -> DistributiveLaw (->) m f+ -> GAlgebra (->) w f b+ -> GCoalgebra (->) m f a -> a -> b ghylo w m φ ψ = extract . hylo (lowerAlgebra w φ) (lowerCoalgebra m ψ) . pure hyloM :: (Monad m, Traversable f)- => AlgebraM m f b- -> CoalgebraM m f a+ => AlgebraM (->) m f b+ -> CoalgebraM (->) m f a -> a -> m b hyloM φ ψ = hylo (φ <=< sequenceA <=< getCompose) (Compose . ψ) ghyloM :: (Comonad w, Traversable w, Monad m, Traversable f, Monad n, Traversable n)- => DistributiveLaw f w- -> DistributiveLaw n f- -> GAlgebraM m w f b- -> GCoalgebraM m n f a+ => DistributiveLaw (->) f w+ -> DistributiveLaw (->) n f+ -> GAlgebraM (->) m w f b+ -> GCoalgebraM (->) m n f a -> a -> m b ghyloM w n φ ψ = fmap extract . hyloM (lowerAlgebraM w φ) (lowerCoalgebraM n ψ) . pure stream'- :: (Projectable t f, Steppable u g, Functor g)- => CoalgebraM Maybe g b+ :: (Projectable (->) t f, Steppable (->) u g, Functor g)+ => CoalgebraM (->) Maybe g b -> (b -> ((b -> b, t) -> u) -> f t -> u) -> b -> t -> u@@ -79,27 +73,27 @@ -- | Gibbons’ metamorphism. It lazily folds a (necessarily infinite) value, -- incrementally re-expanding that value into some new representation. streamAna- :: (Projectable t f, Steppable u g, Functor g)- => CoalgebraM Maybe g b- -> AlgebraM ((,) (b -> b)) f t+ :: (Projectable (->) t f, Steppable (->) u g, Functor g)+ => CoalgebraM (->) Maybe g b+ -> AlgebraM (->) ((,) (b -> b)) f t -> b -> t -> u-streamAna ψ φ = stream' ψ (\c f -> f . φ)+streamAna ψ φ = stream' ψ (\_ f -> f . φ) -- | Another form of Gibbons’ metamorphism. This one can be applied to non- -- infinite inputs and takes an additional “flushing” coalgebra to be applied -- after all the input has been consumed. streamGApo- :: (Projectable t f, Steppable u g, Corecursive u g, Functor g)- => Coalgebra g b- -> CoalgebraM Maybe g b+ :: (Projectable (->) t f, Steppable (->) u g, Corecursive (->) u g, Functor g)+ => Coalgebra (->) g b+ -> CoalgebraM (->) Maybe g b -> (f t -> Maybe (b -> b, t)) -> b -> t -> u streamGApo ψ' ψ φ = stream' ψ (\c f -> maybe (ana ψ' c) f . φ) corecursivePrism- :: (Steppable t f, Recursive t f, Corecursive t f, Traversable f)+ :: (Steppable (->) t f, Recursive (->) t f, Corecursive (->) t f, Traversable f) => CoalgebraPrism f a -> Prism' a t corecursivePrism alg = prism (cata (review alg)) (anaM (matching alg))
src/Yaya/Unsafe/Fold/Instances.hs view
@@ -1,3 +1,5 @@+{-# options_ghc -Wno-orphans #-}+ -- | Type class instances that use direct recursion in a potentially partial -- way. This is separated from the rest of `Yaya.Unsafe.Fold` because you can -- neither control nor qualify the import of instances. Therefore this module@@ -9,11 +11,8 @@ -- to terminate. module Yaya.Unsafe.Fold.Instances where -import Control.Arrow-import Control.Comonad import Control.Comonad.Cofree import Control.Comonad.Env-import Control.Monad import Control.Monad.Trans.Free import Data.Functor.Classes import Data.List.NonEmpty@@ -23,7 +22,7 @@ import Yaya.Pattern import qualified Yaya.Unsafe.Fold as Unsafe -instance Functor f => Recursive (Fix f) f where+instance Functor f => Recursive (->) (Fix f) f where cata = flip Unsafe.hylo project instance (Functor f, Foldable f, Eq1 f) => Eq (Fix f) where@@ -32,10 +31,10 @@ instance (Functor f, Show1 f) => Show (Fix f) where showsPrec = recursiveShowsPrec -instance Functor f => Corecursive (Mu f) f where+instance Functor f => Corecursive (->) (Mu f) f where ana = Unsafe.hylo embed -instance Functor f => Recursive (Nu f) f where+instance Functor f => Recursive (->) (Nu f) f where cata = flip Unsafe.hylo project instance (Functor f, Foldable f, Eq1 f) => Eq (Nu f) where@@ -44,24 +43,24 @@ instance (Functor f, Show1 f) => Show (Nu f) where showsPrec = recursiveShowsPrec -instance Recursive [a] (XNor a) where+instance Recursive (->) [a] (XNor a) where cata = flip Unsafe.hylo project -instance Recursive (NonEmpty a) (AndMaybe a) where+instance Recursive (->) (NonEmpty a) (AndMaybe a) where cata = flip Unsafe.hylo project -instance Functor f => Recursive (Cofree f a) (EnvT a f) where+instance Functor f => Recursive (->) (Cofree f a) (EnvT a f) where cata = flip Unsafe.hylo project -instance Functor f => Recursive (Free f a) (FreeF f a) where+instance Functor f => Recursive (->) (Free f a) (FreeF f a) where cata = flip Unsafe.hylo project --- TODO: If we can generalize this to an arbitrary 'Recursive t (FreeF h a)'+-- TODO: If we can generalize this to an arbitrary 'Recursive (->) t (FreeF h a)' -- then it would no longer be unsafe. seqFreeT :: (Functor f, Functor h)- => DistributiveLaw h f- -> DistributiveLaw (Free h) f+ => DistributiveLaw (->) h f+ -> DistributiveLaw (->) (Free h) f seqFreeT k = cata (\case
src/Yaya/Unsafe/Zoo.hs view
@@ -16,63 +16,63 @@ chrono :: Functor f- => GAlgebra (Cofree f) f b- -> GCoalgebra (Free f) f a+ => GAlgebra (->) (Cofree f) f b+ -> GCoalgebra (->) (Free f) f a -> a -> b chrono = Unsafe.ghylo (distCofreeT id) (Unsafe.seqFreeT id) -codyna :: Functor f => Algebra f b -> GCoalgebra (Free f) f a -> a -> b+codyna :: Functor f => Algebra (->) f b -> GCoalgebra (->) (Free f) f a -> a -> b codyna φ = Unsafe.ghylo distIdentity (Unsafe.seqFreeT id) (φ . fmap runIdentity) -- | [Recursion Schemes for Dynamic Programming](https://www.researchgate.net/publication/221440162_Recursion_Schemes_for_Dynamic_Programming)-dyna :: Functor f => GAlgebra (Cofree f) f b -> Coalgebra f a -> a -> b+dyna :: Functor f => GAlgebra (->) (Cofree f) f b -> Coalgebra (->) f a -> a -> b dyna φ ψ = Unsafe.ghylo (distCofreeT id) seqIdentity φ (fmap Identity . ψ) -- | Unlike most `Unsafe.hylo`s, `elgot` composes an algebra and coalgebra in a -- way that allows information to move between them. The coalgebra can return, -- effectively, a pre-folded branch, short-circuiting parts of the process.-elgot :: Functor f => Algebra f b -> ElgotCoalgebra (Either b) f a -> a -> b+elgot :: Functor f => Algebra (->) f b -> ElgotCoalgebra (->) (Either b) f a -> a -> b elgot φ ψ = Unsafe.hylo ((id ||| φ) . getCompose) (Compose . ψ) -- | The dual of `elgot`, `coelgot` allows the /algebra/ to short-circuit in -- some cases – operating directly on a part of the seed.-coelgot :: Functor f => ElgotAlgebra ((,) a) f b -> Coalgebra f a -> a -> b+coelgot :: Functor f => ElgotAlgebra (->) ((,) a) f b -> Coalgebra (->) f a -> a -> b coelgot φ ψ = Unsafe.hylo (φ . getCompose) (Compose . (id &&& ψ)) -futu :: (Corecursive t f, Functor f) => GCoalgebra (Free f) f a -> a -> t+futu :: (Corecursive (->) t f, Functor f) => GCoalgebra (->) (Free f) f a -> a -> t futu = gana (Unsafe.seqFreeT id) gprepro- :: (Steppable t f, Recursive t f, Functor f, Comonad w)- => DistributiveLaw f w- -> GAlgebra w f a- -> (forall a. f a -> f a)+ :: (Steppable (->) t f, Recursive (->) t f, Functor f, Comonad w)+ => DistributiveLaw (->) f w+ -> GAlgebra (->) w f a+ -> (forall x. f x -> f x) -> t -> a gprepro k φ e = Unsafe.ghylo k seqIdentity φ (fmap (Identity . cata (embed . e)) . project) gpostpro- :: (Steppable t f, Corecursive t f, Functor f, Monad m)- => DistributiveLaw m f- -> (forall a. f a -> f a)- -> GCoalgebra m f a+ :: (Steppable (->) t f, Corecursive (->) t f, Functor f, Monad m)+ => DistributiveLaw (->) m f+ -> (forall x. f x -> f x)+ -> GCoalgebra (->) m f a -> a -> t gpostpro k e = Unsafe.ghylo distIdentity k (embed . fmap (ana (e . project) . runIdentity)) -- | The metamorphism definition from Gibbons’ paper.-stream :: Coalgebra (XNor c) b -> (b -> a -> b) -> b -> [a] -> [c]+stream :: Coalgebra (->) (XNor c) b -> (b -> a -> b) -> b -> [a] -> [c] stream f g = fstream f g (const Neither) -- | Basically the definition from Gibbons’ paper, except the flusher (@h@) is a -- `Coalgebra` instead of an `unfold`. fstream- :: Coalgebra (XNor c) b+ :: Coalgebra (->) (XNor c) b -> (b -> a -> b)- -> Coalgebra (XNor c) b+ -> Coalgebra (->) (XNor c) b -> b -> [a] -> [c]@@ -94,9 +94,9 @@ -- TODO: Weaken `Monad` constraint to `Applicative`. cotraverse- :: ( Steppable t (f a)- , Steppable u (f b)- , Corecursive u (f b)+ :: ( Steppable (->) t (f a)+ , Steppable (->) u (f b)+ , Corecursive (->) u (f b) , Bitraversable f , Traversable (f b) , Monad m)@@ -107,7 +107,7 @@ -- | Zygohistomorphic prepromorphism – everyone’s favorite recursion scheme joke. zygoHistoPrepro- :: (Steppable t f, Recursive t f, Functor f)+ :: (Steppable (->) t f, Recursive (->) t f, Functor f) => (f b -> b) -> (f (EnvT b (Cofree f) a) -> a) -> (forall c. f c -> f c)
yaya-unsafe.cabal view
@@ -1,5 +1,5 @@ name: yaya-unsafe-version: 0.1.1.2+version: 0.2.0.0 synopsis: Non-total extensions to the Yaya recursion scheme library. description: Yaya is designed as a _total_ library. However, it is often expedient to use partial operations in some cases, and this@@ -33,7 +33,7 @@ , either , free , lens- , yaya >= 0.1.0+ , yaya >= 0.3.0 default-extensions: ConstraintKinds , DeriveTraversable , FlexibleContexts