yaya-0.6.2.0: Total recursion schemes.
Safe HaskellSafe
LanguageHaskell2010

Yaya.Retrofit

Description

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

Documentation

class Corecursive c t f | t -> f where Source #

Coinductive (potentially-infinite) structures that guarantee _productivity_ rather than termination.

Methods

ana :: Coalgebra c f a -> a `c` t Source #

Instances

Instances details
Corecursive (->) (Nu f :: Type) (f :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

ana :: forall (a :: k). Coalgebra (->) f a -> a -> Nu f Source #

Functor f => Corecursive (->) (Cofix f :: Type) (f :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold.Native.Internal

Methods

ana :: forall (a :: k). Coalgebra (->) f a -> a -> Cofix f Source #

Corecursive (->) (NonEmpty a :: Type) (AndMaybe a :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold.Native

Methods

ana :: forall (a0 :: k). Coalgebra (->) (AndMaybe a) a0 -> a0 -> NonEmpty a Source #

Corecursive (->) ([a] :: Type) (XNor a :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold.Native

Methods

ana :: forall (a0 :: k). Coalgebra (->) (XNor a) a0 -> a0 -> [a] Source #

Corecursive (->) (Maybe a :: Type) (Const (Maybe a) :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

ana :: forall (a0 :: k). Coalgebra (->) (Const (Maybe a)) a0 -> a0 -> Maybe a Source #

Functor f => Corecursive (->) (Cofree f a :: Type) (EnvT a f :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold.Native

Methods

ana :: forall (a0 :: k). Coalgebra (->) (EnvT a f) a0 -> a0 -> Cofree f a Source #

Functor f => Corecursive (->) (Free f a :: Type) (FreeF f a :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold.Native

Methods

ana :: forall (a0 :: k). Coalgebra (->) (FreeF f a) a0 -> a0 -> Free f a Source #

Corecursive (->) (Either a b :: Type) (Const (Either a b) :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

ana :: forall (a0 :: k). Coalgebra (->) (Const (Either a b)) a0 -> a0 -> Either a b Source #

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).

Methods

project :: Coalgebra c f t Source #

Instances

Instances details
Projectable (->) Void Identity Source # 
Instance details

Defined in Yaya.Fold

Projectable (->) Natural Maybe Source # 
Instance details

Defined in Yaya.Fold

Functor f => Projectable (->) (Mu f :: Type) (f :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

project :: Coalgebra (->) f (Mu f) Source #

Functor f => Projectable (->) (Nu f :: Type) (f :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

project :: Coalgebra (->) f (Nu f) Source #

Projectable (->) (Fix f :: Type) (f :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold.Native

Methods

project :: Coalgebra (->) f (Fix f) Source #

Projectable (->) (Cofix f :: Type) (f :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold.Native.Internal

Methods

project :: Coalgebra (->) f (Cofix f) Source #

Projectable (->) (NonEmpty a :: Type) (AndMaybe a :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

project :: Coalgebra (->) (AndMaybe a) (NonEmpty a) Source #

Projectable (->) ([a] :: Type) (XNor a :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

project :: Coalgebra (->) (XNor a) [a] Source #

Projectable (->) (Maybe a :: Type) (Const (Maybe a) :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

project :: Coalgebra (->) (Const (Maybe a)) (Maybe a) Source #

Projectable (->) (Cofree f a :: Type) (EnvT a f :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

project :: Coalgebra (->) (EnvT a f) (Cofree f a) Source #

Projectable (->) (Free f a :: Type) (FreeF f a :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

project :: Coalgebra (->) (FreeF f a) (Free f a) Source #

Projectable (->) (Either a b :: Type) (Const (Either a b) :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

project :: Coalgebra (->) (Const (Either a b)) (Either a b) 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.

Methods

cata :: Algebra c f a -> t `c` a Source #

Instances

Instances details
Recursive (->) Void Identity Source # 
Instance details

Defined in Yaya.Fold

Methods

cata :: forall (a :: k1). Algebra (->) Identity a -> Void -> a Source #

Recursive (->) Natural Maybe Source # 
Instance details

Defined in Yaya.Fold.Native

Methods

cata :: forall (a :: k1). Algebra (->) Maybe a -> Natural -> a Source #

Recursive (->) (Mu f :: Type) (f :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

cata :: forall (a :: k1). Algebra (->) f a -> Mu f -> a Source #

Functor f => Recursive (->) (Fix f :: Type) (f :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold.Native

Methods

cata :: forall (a :: k1). Algebra (->) f a -> Fix f -> a Source #

Recursive (->) (Maybe a :: Type) (Const (Maybe a) :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

cata :: forall (a0 :: k1). Algebra (->) (Const (Maybe a)) a0 -> Maybe a -> a0 Source #

Recursive (->) (Either a b :: Type) (Const (Either a b) :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

cata :: forall (a0 :: k1). Algebra (->) (Const (Either a b)) a0 -> Either a b -> a0 Source #

class Projectable c t f => Steppable c t f | t -> f where Source #

Structures you can walk through step-by-step.

Methods

embed :: Algebra c f t Source #

Instances

Instances details
Steppable (->) Void Identity Source # 
Instance details

Defined in Yaya.Fold

Steppable (->) Natural Maybe Source # 
Instance details

Defined in Yaya.Fold

Functor f => Steppable (->) (Mu f :: Type) (f :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

embed :: Algebra (->) f (Mu f) Source #

Functor f => Steppable (->) (Nu f :: Type) (f :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

embed :: Algebra (->) f (Nu f) Source #

Steppable (->) (Fix f :: Type) (f :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold.Native

Methods

embed :: Algebra (->) f (Fix f) Source #

Steppable (->) (Cofix f :: Type) (f :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold.Native.Internal

Methods

embed :: Algebra (->) f (Cofix f) Source #

Steppable (->) (NonEmpty a :: Type) (AndMaybe a :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

embed :: Algebra (->) (AndMaybe a) (NonEmpty a) Source #

Steppable (->) ([a] :: Type) (XNor a :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

embed :: Algebra (->) (XNor a) [a] Source #

Steppable (->) (Maybe a :: Type) (Const (Maybe a) :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

embed :: Algebra (->) (Const (Maybe a)) (Maybe a) Source #

Steppable (->) (Cofree f a :: Type) (EnvT a f :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

embed :: Algebra (->) (EnvT a f) (Cofree f a) Source #

Steppable (->) (Free f a :: Type) (FreeF f a :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

embed :: Algebra (->) (FreeF f a) (Free f a) Source #

Steppable (->) (Either a b :: Type) (Const (Either a b) :: Type -> Type) Source # 
Instance details

Defined in Yaya.Fold

Methods

embed :: Algebra (->) (Const (Either a b)) (Either a b) Source #

recursiveCompare :: (Recursive (->) t f, Steppable (->) u f, Functor f, Foldable f, Ord1 f) => t -> u -> Ordering Source #

An implementation of == for any Recursive instance. Note that this is actually more general than Ord’s compare, as it can compare between different fixed-point representations of the same functor.

NB: Use recursiveCompare' if you need to use a custom comparator for f.

Since: 0.6.1.0

recursiveCompare' :: (Recursive (->) t f, Steppable (->) u f, Functor f, Foldable f) => (f () -> f () -> Ordering) -> t -> u -> Ordering Source #

Like recursiveCompare, but allows you to provide a custom comparator for f.

Since: 0.6.1.0

recursiveEq :: (Recursive (->) t f, Steppable (->) u f, Functor f, Foldable f, Eq1 f) => t -> u -> Bool Source #

An implementation of == for any Recursive instance. Note that this is actually more general than Eq’s ==, as it can compare between different fixed-point representations of the same functor.

NB: Use recursiveEq' if you need to use a custom comparator for f.

recursiveEq' :: (Recursive (->) t f, Steppable (->) u f, Functor f, Foldable f) => (f () -> f () -> Bool) -> t -> u -> Bool Source #

Like recursiveEq, but allows you to provide a custom comparator for f.

Since: 0.6.1.0

recursiveShowsPrec :: (Recursive (->) t f, Show1 f) => Int -> t -> ShowS Source #

An implementation of showsPrec for any Recursive instance.

recursiveShowsPrec' :: Recursive (->) t f => Algebra (->) f (Int -> ShowS) -> Int -> t -> ShowS Source #

Like recursiveShowsPrec, but allows you to provide a custom display function for f.

Since: 0.6.1.0

steppableReadPrec :: (Steppable (->) t f, Read1 f) => ReadPrec t Source #

An implementation of readPrec for any Steppable instance.

NB: Use steppableReadPrec' if you need to use a custom parsing function for f.

NB: This only requires Steppable, but the inverse operation is recursiveShowsPrec, which requires Recursive instead.

Since: 0.6.1.0

steppableReadPrec' :: Steppable (->) t f => (ReadPrec t -> ReadPrec [t] -> ReadPrec (f t)) -> ReadPrec t Source #

Like steppableReadPrec, but allows you to provide a custom display function for f.

Since: 0.6.1.0

data PatternFunctorRules Source #

Rules of renaming data names

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 stock (Show)

extractPatternFunctor defaultRules ''Expr

will create

data ExprF a x
    = LitF a
    | AddF x x
    | x :*$ [x]
  deriving stock (Functor, Foldable, Traversable)

instance Projectable (->) (Expr a) (ExprF a) where
  project (Lit x)   = LitF x
  project (Add x y) = AddF x y
  project (x :* y)  = x :*$ y

instance Steppable (->) (Expr a) (ExprF a) where
  embed (LitF x)   = Lit x
  embed (AddF x y) = Add x y
  embed (x :*$ y)  = x :* y

instance Recursive (->) (Expr a) (ExprF a) where
  cata φ = φ . fmap (cata φ) . project

instance Corecursive (->) (Expr a) (ExprF a) where
  ana ψ = 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 and Corecursive 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).