-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | A lightweight implementation of 'bound' -- -- An abstraction for representing bound variables. Most of this code has -- been extracted from bound, with the purpose of providing a -- mostly self-contained library for implementing embedded languages. @package bound-simple @version 0.2.0.0 module Data.Functor.Classes.Generic -- | Lifting of the Eq class to unary type constructors. class Eq1 (f :: Type -> Type) -- | Lift an equality test through the type constructor. -- -- The function will usually be applied to an equality function, but the -- more general type ensures that the implementation uses it to compare -- elements of the first container with elements of the second. liftEq :: Eq1 f => (a -> b -> Bool) -> f a -> f b -> Bool -- | A suitable implementation of Eq1’s liftEq for Generic1 types. genericLiftEq :: (Generic1 f, GEq1 (Rep1 f)) => (a -> b -> Bool) -> f a -> f b -> Bool -- | Lifting of the Ord class to unary type constructors. class Eq1 f => Ord1 (f :: Type -> Type) -- | Lift a compare function through the type constructor. -- -- The function will usually be applied to a comparison function, but the -- more general type ensures that the implementation uses it to compare -- elements of the first container with elements of the second. liftCompare :: Ord1 f => (a -> b -> Ordering) -> f a -> f b -> Ordering -- | A suitable implementation of Ord1’s liftCompare for Generic1 types. genericLiftCompare :: (Generic1 f, GOrd1 (Rep1 f)) => (a -> b -> Ordering) -> f a -> f b -> Ordering -- | Lifting of the Show class to unary type constructors. class Show1 (f :: Type -> Type) -- | showsPrec function for an application of the type constructor -- based on showsPrec and showList functions for the -- argument type. liftShowsPrec :: Show1 f => (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> f a -> ShowS -- | showList function for an application of the type constructor -- based on showsPrec and showList functions for the -- argument type. The default implementation using standard list syntax -- is correct for most types. liftShowList :: Show1 f => (Int -> a -> ShowS) -> ([a] -> ShowS) -> [f a] -> ShowS newtype GShow1Options GShow1Options :: Bool -> GShow1Options [optionsUseRecordSyntax] :: GShow1Options -> Bool defaultGShow1Options :: GShow1Options -- | A suitable implementation of Show1’s liftShowsPrec for Generic1 types. genericLiftShowsPrec :: (Generic1 f, GShow1 (Rep1 f)) => (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> f a -> ShowS -- | A suitable implementation of Show1’s liftShowsPrec for Generic1 types. genericLiftShowsPrecWithOptions :: (Generic1 f, GShow1 (Rep1 f)) => GShow1Options -> (Int -> a -> ShowS) -> ([a] -> ShowS) -> Int -> f a -> ShowS -- | Used with the DerivingVia extension to provide fast -- derivations for Eq1, Show1, and Ord1. newtype Generically f a Generically :: f a -> Generically f a [unGenerically] :: Generically f a -> f a instance (GHC.Generics.Generic1 f, Data.Functor.Classes.Generic.GEq1 (GHC.Generics.Rep1 f)) => Data.Functor.Classes.Eq1 (Data.Functor.Classes.Generic.Generically f) instance (GHC.Generics.Generic1 f, Data.Functor.Classes.Generic.GEq1 (GHC.Generics.Rep1 f), Data.Functor.Classes.Generic.GOrd1 (GHC.Generics.Rep1 f)) => Data.Functor.Classes.Ord1 (Data.Functor.Classes.Generic.Generically f) instance (GHC.Generics.Generic1 f, Data.Functor.Classes.Generic.GShow1 (GHC.Generics.Rep1 f)) => Data.Functor.Classes.Show1 (Data.Functor.Classes.Generic.Generically f) instance (GHC.Generics.Constructor c, Data.Functor.Classes.Generic.GShow1Body f) => Data.Functor.Classes.Generic.GShow1 (GHC.Generics.M1 GHC.Generics.C c f) instance Data.Functor.Classes.Generic.GShow1Body GHC.Generics.U1 instance (GHC.Generics.Selector s, Data.Functor.Classes.Generic.GShow1 f) => Data.Functor.Classes.Generic.GShow1Body (GHC.Generics.M1 GHC.Generics.S s f) instance (Data.Functor.Classes.Generic.GShow1Body f, Data.Functor.Classes.Generic.GShow1Body g) => Data.Functor.Classes.Generic.GShow1Body (f GHC.Generics.:*: g) instance Data.Functor.Classes.Generic.GShow1 GHC.Generics.U1 instance Data.Functor.Classes.Generic.GShow1 GHC.Generics.Par1 instance GHC.Show.Show c => Data.Functor.Classes.Generic.GShow1 (GHC.Generics.K1 i c) instance Data.Functor.Classes.Show1 f => Data.Functor.Classes.Generic.GShow1 (GHC.Generics.Rec1 f) instance Data.Functor.Classes.Generic.GShow1 f => Data.Functor.Classes.Generic.GShow1 (GHC.Generics.M1 GHC.Generics.D c f) instance Data.Functor.Classes.Generic.GShow1 f => Data.Functor.Classes.Generic.GShow1 (GHC.Generics.M1 GHC.Generics.S c f) instance (Data.Functor.Classes.Generic.GShow1 f, Data.Functor.Classes.Generic.GShow1 g) => Data.Functor.Classes.Generic.GShow1 (f GHC.Generics.:+: g) instance (Data.Functor.Classes.Generic.GShow1 f, Data.Functor.Classes.Generic.GShow1 g) => Data.Functor.Classes.Generic.GShow1 (f GHC.Generics.:*: g) instance (Data.Functor.Classes.Show1 f, Data.Functor.Classes.Generic.GShow1 g) => Data.Functor.Classes.Generic.GShow1 (f GHC.Generics.:.: g) instance Data.Functor.Classes.Generic.GOrd1 GHC.Generics.U1 instance Data.Functor.Classes.Generic.GOrd1 GHC.Generics.Par1 instance GHC.Classes.Ord c => Data.Functor.Classes.Generic.GOrd1 (GHC.Generics.K1 i c) instance Data.Functor.Classes.Ord1 f => Data.Functor.Classes.Generic.GOrd1 (GHC.Generics.Rec1 f) instance Data.Functor.Classes.Generic.GOrd1 f => Data.Functor.Classes.Generic.GOrd1 (GHC.Generics.M1 i c f) instance (Data.Functor.Classes.Generic.GOrd1 f, Data.Functor.Classes.Generic.GOrd1 g) => Data.Functor.Classes.Generic.GOrd1 (f GHC.Generics.:+: g) instance (Data.Functor.Classes.Generic.GOrd1 f, Data.Functor.Classes.Generic.GOrd1 g) => Data.Functor.Classes.Generic.GOrd1 (f GHC.Generics.:*: g) instance (Data.Functor.Classes.Ord1 f, Data.Functor.Classes.Generic.GOrd1 g) => Data.Functor.Classes.Generic.GOrd1 (f GHC.Generics.:.: g) instance Data.Functor.Classes.Generic.GEq1 GHC.Generics.U1 instance Data.Functor.Classes.Generic.GEq1 GHC.Generics.Par1 instance GHC.Classes.Eq c => Data.Functor.Classes.Generic.GEq1 (GHC.Generics.K1 i c) instance Data.Functor.Classes.Eq1 f => Data.Functor.Classes.Generic.GEq1 (GHC.Generics.Rec1 f) instance Data.Functor.Classes.Generic.GEq1 f => Data.Functor.Classes.Generic.GEq1 (GHC.Generics.M1 i c f) instance (Data.Functor.Classes.Generic.GEq1 f, Data.Functor.Classes.Generic.GEq1 g) => Data.Functor.Classes.Generic.GEq1 (f GHC.Generics.:+: g) instance (Data.Functor.Classes.Generic.GEq1 f, Data.Functor.Classes.Generic.GEq1 g) => Data.Functor.Classes.Generic.GEq1 (f GHC.Generics.:*: g) instance (Data.Functor.Classes.Eq1 f, Data.Functor.Classes.Generic.GEq1 g) => Data.Functor.Classes.Generic.GEq1 (f GHC.Generics.:.: g) -- |

Example

-- -- The whnf function in this example shows how to beta-reduce a -- term of the untyped lambda calculus. -- -- Note : the Show instance of Exp depends on its Show1 instance (since -- Exp has one type parameter), which can be derived Generically. -- This works on most recent versions of GHC (>= 8.6.1). -- -- Note 2 : the example below requires language extensions -- DeriveFunctor, DeriveFoldable, -- DeriveTraversable and DerivingVia. -- --
--   import Bound.Simple (Scope, Bound(..), abstract1, instantiate1)
--   import Data.Functor.Classes (Show1)
--   import Data.Functor.Classes.Generic (Generically(..))
--   
--   import GHC.Generics (Generic1)
--   
--   infixl 9 :@
--   data Exp a = V a | Exp a :@ Exp a | Lam (Scope () Exp a)
--     deriving (Show, Functor, Foldable, Traversable, Generic1)
--     deriving (Show1) via Generically Exp
--   
--   instance Applicative Exp where pure = V; k <*> m = ap k m
--   
--   instance Monad Exp where
--     return = V
--     V a      >>= f = f a
--     (x :@ y) >>= f = (x >>= f) :@ (y >>= f)
--     Lam e    >>= f = Lam (e >>>= f)
--   
--   lam :: Eq a => a -> Exp a -> Exp a
--   lam v b = Lam (abstract1 v b)
--   
--   whnf :: Exp a -> Exp a
--   whnf (e1 :@ e2) = case whnf e1 of
--     Lam b -> whnf (instantiate1 e2 b)
--     f'    -> f' :@ e2
--   whnf e = e
--   
--   main :: IO ()
--   main = do
--     let term = lam x (V x) :@ V y
--     print term         -- Lam (Scope (V (B ()))) :@ V y
--     print $ whnf term  -- V y
--   
module Bound.Simple class Bound t -- | Perform substitution -- -- If t is an instance of MonadTrans and you are -- compiling on GHC >= 7.4, then this gets the default definition: -- --
--   m >>>= f = m >>= lift . f
--   
(>>>=) :: (Bound t, Monad f) => t f a -> (a -> f c) -> t f c -- | Perform substitution -- -- If t is an instance of MonadTrans and you are -- compiling on GHC >= 7.4, then this gets the default definition: -- --
--   m >>>= f = m >>= lift . f
--   
(>>>=) :: (Bound t, MonadTrans t, Monad f, Monad (t f)) => t f a -> (a -> f c) -> t f c -- | Scope b f a is an f expression with bound -- variables in b, and free variables in a data Scope b f a -- | toScope is just another name for Scope toScope :: f (Var b a) -> Scope b f a -- | fromScope is just another name for unscope fromScope :: Scope b f a -> f (Var b a) data Var b a -- | Capture some free variables in an expression to yield a Scope -- with bound variables in b -- --
--   >>> :m + Data.List
--   
--   >>> abstract (`elemIndex` "bar") "barry"
--   Scope [B 0,B 1,B 2,B 2,F 'y']
--   
abstract :: Functor f => (a -> Maybe b) -> f a -> Scope b f a -- | Abstract over a single variable -- --
--   >>> abstract1 'x' "xyz"
--   Scope [B (),F 'y',F 'z']
--   
abstract1 :: (Functor f, Eq a) => a -> f a -> Scope () f a -- | Enter a scope, instantiating all bound variables -- --
--   >>> :m + Data.List
--   
--   >>> instantiate (\x -> [toEnum (97 + x)]) $ abstract (`elemIndex` "bar") "barry"
--   "abccy"
--   
instantiate :: Monad f => (b -> f a) -> Scope b f a -> f a -- | Enter a Scope that binds one variable, instantiating it -- --
--   >>> instantiate1 "x" $ Scope [B (),F 'y',F 'z']
--   "xyz"
--   
instantiate1 :: Monad f => f a -> Scope n f a -> f a -- | Return a list of occurences of the variables bound by this -- Scope. bindings :: Foldable f => Scope b f a -> [b] hoistScope :: (f (Var b a) -> g (Var b a)) -> Scope b f a -> Scope b g a -- | If a term has no free variables, you can freely change the type of -- free variables it is parameterized on. -- --
--   >>> closed [12]
--   Nothing
--   
-- --
--   >>> closed ""
--   Just []
--   
-- --
--   >>> :t closed ""
--   closed "" :: Maybe [b]
--   
closed :: Traversable f => f a -> Maybe (f b) -- | substitute a p w replaces the free variable a -- with p in w. -- --
--   >>> substitute "hello" ["goodnight","Gracie"] ["hello","!!!"]
--   ["goodnight","Gracie","!!!"]
--   
substitute :: (Monad f, Eq a) => a -> f a -> f a -> f a -- | substituteVar a b w replaces a free variable -- a with another free variable b in w. -- --
--   >>> substituteVar "Alice" "Bob" ["Alice","Bob","Charlie"]
--   ["Bob","Bob","Charlie"]
--   
substituteVar :: (Functor f, Eq a) => a -> a -> f a -> f a -- | A closed term has no free variables. -- --
--   >>> isClosed []
--   True
--   
-- --
--   >>> isClosed [1,2,3]
--   False
--   
isClosed :: Foldable f => f a -> Bool -- | Used with the DerivingVia extension to provide fast -- derivations for Eq1, Show1, and Ord1. newtype Generically f a Generically :: f a -> Generically f a [unGenerically] :: Generically f a -> f a instance Data.Traversable.Traversable (Bound.Simple.Var b) instance Data.Foldable.Foldable (Bound.Simple.Var b) instance GHC.Base.Functor (Bound.Simple.Var b) instance (GHC.Show.Show b, GHC.Show.Show a) => GHC.Show.Show (Bound.Simple.Var b a) instance (GHC.Classes.Eq b, GHC.Classes.Eq a) => GHC.Classes.Eq (Bound.Simple.Var b a) instance (GHC.Base.Functor f, Data.Functor.Classes.Eq1 f, GHC.Classes.Eq b) => Data.Functor.Classes.Eq1 (Bound.Simple.Scope b f) instance (GHC.Base.Functor f, Data.Functor.Classes.Show1 f, GHC.Show.Show b) => Data.Functor.Classes.Show1 (Bound.Simple.Scope b f) instance GHC.Base.Functor f => GHC.Generics.Generic1 (Bound.Simple.Scope b f) instance Data.Functor.Classes.Eq1 Bound.Simple.Exp instance Data.Functor.Classes.Show1 Bound.Simple.Exp instance GHC.Generics.Generic1 Bound.Simple.Exp instance Data.Traversable.Traversable Bound.Simple.Exp instance Data.Foldable.Foldable Bound.Simple.Exp instance GHC.Base.Functor Bound.Simple.Exp instance GHC.Classes.Eq a => GHC.Classes.Eq (Bound.Simple.Exp a) instance GHC.Show.Show a => GHC.Show.Show (Bound.Simple.Exp a) instance Bound.Simple.Bound (Bound.Simple.Scope b) instance (GHC.Classes.Eq e, GHC.Base.Functor m, Data.Functor.Classes.Eq1 m, GHC.Classes.Eq a) => GHC.Classes.Eq (Bound.Simple.Scope e m a) instance (GHC.Show.Show e, GHC.Base.Functor m, Data.Functor.Classes.Show1 m, GHC.Show.Show a) => GHC.Show.Show (Bound.Simple.Scope e m a) instance GHC.Base.Functor f => GHC.Base.Functor (Bound.Simple.Scope b f) instance Data.Foldable.Foldable f => Data.Foldable.Foldable (Bound.Simple.Scope b f) instance Data.Traversable.Traversable f => Data.Traversable.Traversable (Bound.Simple.Scope b f) instance GHC.Base.Monad f => GHC.Base.Applicative (Bound.Simple.Scope b f) instance GHC.Base.Monad f => GHC.Base.Monad (Bound.Simple.Scope b f) instance Data.Functor.Classes.Eq2 Bound.Simple.Var instance GHC.Classes.Eq b => Data.Functor.Classes.Eq1 (Bound.Simple.Var b) instance Data.Functor.Classes.Show2 Bound.Simple.Var instance GHC.Show.Show b => Data.Functor.Classes.Show1 (Bound.Simple.Var b)