Safe Haskell | None |
---|---|
Language | Haskell2010 |
This module re-exports a subset of Fold
, intended for when you want
to define recursion scheme instances for your existing recursive types.
This is not the recommended way to use Yaya, but it solves some real problems: 1. you have existing directly-recursive types and you want to start taking advantage of recursion schemes without having to rewrite your existing code, or 2. a directly-recursive type has been imposed on you by some other library and you want to take advantage of recursion schemes.
The distinction between these two cases is whether you have control of the
data
declaration. In the first case, you probably do. In that case, you
should only generate the safe instances, and ensure that all the
recursive type references are strict (if you want a Recursive
instance). If you don't have control, then you may need to generate all
instances.
Another difference when you have control is that it means you may migrate away from direct recursion entirely, at which point this import should disappear.
Synopsis
- class Corecursive c t f | t -> f where
- class Recursive c t f | t -> f where
- class Projectable c t f => Steppable c t f | t -> f where
- class Projectable c t f | t -> f where
- recursiveEq :: (Recursive (->) t f, Steppable (->) u f, Functor f, Foldable f, Eq1 f) => t -> u -> Bool
- recursiveShowsPrec :: (Recursive (->) t f, Show1 f) => Int -> t -> ShowS
- data PatternFunctorRules = PatternFunctorRules {
- patternType :: Name -> Name
- patternCon :: Name -> Name
- patternField :: Name -> Name
- defaultRules :: PatternFunctorRules
- extractPatternFunctor :: PatternFunctorRules -> Name -> Q [Dec]
Documentation
class Corecursive c t f | t -> f where Source #
Coinductive (potentially-infinite) structures that guarantee _productivity_ rather than termination.
Instances
Corecursive ((->) :: Type -> Type -> Type) (Nu f :: Type) (f :: Type -> Type) Source # | |
Functor f => Corecursive ((->) :: Type -> Type -> Type) (Fix f :: Type) (f :: Type -> Type) Source # | |
Corecursive ((->) :: Type -> Type -> Type) ([a] :: Type) (XNor a :: Type -> Type) Source # | |
Corecursive ((->) :: Type -> Type -> Type) (NonEmpty a :: Type) (AndMaybe a :: Type -> Type) Source # | |
Corecursive ((->) :: Type -> Type -> Type) (Maybe a :: Type) (Const (Maybe a) :: Type -> Type) Source # | |
Corecursive ((->) :: Type -> Type -> Type) (Either a b :: Type) (Const (Either a b) :: Type -> Type) Source # | |
Functor f => Corecursive ((->) :: Type -> Type -> Type) (Cofree f a :: Type) (EnvT a f :: Type -> Type) Source # | |
Functor f => Corecursive ((->) :: Type -> Type -> Type) (Free f a :: Type) (FreeF f a :: Type -> Type) Source # | |
class Recursive c t f | t -> f where Source #
Inductive structures that can be reasoned about in the way we usually do – with pattern matching.
Instances
Recursive ((->) :: Type -> Type -> Type) Natural Maybe Source # | |
Recursive ((->) :: Type -> Type -> Type) Void Identity Source # | |
Recursive ((->) :: Type -> Type -> Type) (Mu f :: Type) (f :: Type -> Type) Source # | |
Recursive ((->) :: Type -> Type -> Type) (Maybe a :: Type) (Const (Maybe a) :: Type -> Type) Source # | |
Recursive ((->) :: Type -> Type -> Type) (Either a b :: Type) (Const (Either a b) :: Type -> Type) Source # | |
class Projectable c t f => Steppable c t f | t -> f where Source #
Structures you can walk through step-by-step.
Instances
class Projectable c t f | t -> f where Source #
This type class is lawless on its own, but there exist types that can’t
implement the corresponding embed
operation. Laws are induced by
implementing either Steppable
(which extends this) or Corecursive
(which doesn’t).
Instances
recursiveEq :: (Recursive (->) t f, Steppable (->) u f, Functor f, Foldable f, Eq1 f) => t -> u -> Bool Source #
data PatternFunctorRules Source #
Rules of renaming data names
PatternFunctorRules | |
|
defaultRules :: PatternFunctorRules Source #
Default PatternFunctorRules
: append F
or $
to data type, constructors and field names.
extractPatternFunctor :: PatternFunctorRules -> Name -> Q [Dec] Source #
Extract a pattern functor and relevant instances from a simply recursive type.
e.g.
data Expr a = Lit a | Add (Expr a) (Expr a) | Expr a :* [Expr a] deriving (Show)extractPatternFunctor
defaultRules
''Expr
will create
data ExprF a x = LitF a | AddF x x | x :*$ [x] deriving (Functor
,Foldable
,Traversable
) instanceProjectable
(->) (Expr a) (ExprF a) whereproject
(Lit x) = LitF xproject
(Add x y) = AddF x yproject
(x :* y) = x :*$ y instanceSteppable
(->) (Expr a) (ExprF a) whereembed
(LitF x) = Lit xembed
(AddF x y) = Add x yembed
(x :*$ y) = x :* y instanceRecursive
(->) (Expr a) (ExprF a) wherecata
φ = φ .fmap
(cata
φ) .project
instanceCorecursive
(->) (Expr a) (ExprF a) whereana
ψ =embed
.fmap
(ana
ψ) . ψ
Notes:
extractPatternFunctor
works properly only with ADTs. Existentials and GADTs aren't supported, as we don't try to do better than GHC's DeriveFunctor.- we always generate both
Recursive
andCorecursive
instances, but one of these is always unsafe. In future, we should check the strictness of the recursive parameter and generate only the appropriate one (unless overridden by a rule).