ƪ      !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~(C) 2012 Edward Kmett BSD-style (see the file LICENSE)Edward Kmett <ekmett@gmail.com> experimentalportable Trustworthy05"I am not a number, I am a  free monad!"A  b a+ is a variable that may either be "bound" ( ) or "free" ().H(It is also technically a free monad in the same near-trivial sense as .)this is a bound variablethis is a free variableThis provides a Prism that can be used with lens library to access a bound .  :: Prism (Var b a) (Var b' a) b b'@ This provides a Prism that can be used with lens library to access a free .  :: Prism (Var b a) (Var b a') a a'@   (C) 2012 Edward Kmett BSD-style (see the file LICENSE)Edward Kmett <ekmett@gmail.com> experimentalportableSafe%% a p w replaces the free variable a with p in w.9substitute "hello" ["goodnight","Gracie"] ["hello","!!!"]["goodnight","Gracie","!!!"]&& a b w replaces a free variable a with another free variable b in w.5substituteVar "Alice" "Bob" ["Alice","Bob","Charlie"]["Bob","Bob","Charlie"]'jIf 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]($A closed term has no free variables. isClosed []TrueisClosed [1,2,3]False%&'(%&'(%&('%&'((C) 2012-2015 Edward Kmett BSD-style (see the file LICENSE)Edward Kmett <ekmett@gmail.com> experimentalportableSafe6) Instances of )# generate left modules over monads.2This means they should satisfy the following laws: m *  "a m m * ( x ! k x  h) "a (m * k) * h This guarantees that a typical Monad instance for an expression type where Bound instances appear will satisfy the Monad laws (see doc/BoundLaws.hs).If instances of ) are monad transformers, then m * f "a m    f@ implies the above laws, and is in fact the default definition.This is useful for types like expression lists, case alternatives, schemas, etc. that may not be expressions in their own right, but often contain expressions.Note:  V isn't "really" a monad transformer, even if the kind matches. Therefore there isn't )   instance.*Perform substitutionIf t is an instance of  MonadTransM and you are compiling on GHC >= 7.4, then this gets the default definition: m * f = m    f+A flipped version of (*). (+) =  (*) )*+,-./01234)*+)**+ )**+,-./01234*1+1(C) 2012-2013 Edward Kmett BSD-style (see the file LICENSE)Edward Kmett <ekmett@gmail.com> experimentalportable Trustworthy/0:OT55 b f a is an f$ expression with bound variables in b, and free variables in aaWe store bound variables as their generalized de Bruijn representation in that we're allowed to  (using n) an entire tree rather than only succ individual variables, but we're still only allowed to do so once per 5. Weakening trees permits O(1)^ weakening and permits more sharing opportunities. Here the deBruijn 0 is represented by the  constructor of , while the de Bruijn : (which may be applied to an entire tree!) is handled by .6NB: equality and comparison quotient out the distinct  placements allowed by the generalized de Bruijn representation and return the same result as a traditional de Bruijn representation would.FLogically you can think of this as if the shape were the traditional  f (Var b a), but the extra f a inside permits us a cheaper .89Capture some free variables in an expression to yield a 5 with bound variables in b:m + Data.List$abstract (`elemIndex` "bar") "barry"Scope [B 0,B 1,B 2,B 2,F "y"]9Abstract over a single variableabstract1 'x' "xyz"Scope [B (),F "y",F "z"]:0Enter a scope, instantiating all bound variables:m + Data.ListLinstantiate (\x -> [toEnum (97 + x)]) $ abstract (`elemIndex` "bar") "barry""abccy";Enter a 5* that binds one variable, instantiating it+instantiate1 "x" $ Scope [B (),F "y",F "z"]"xyz"<<* quotients out the possible placements of  in 5x by distributing them all to the leaves. This yields a more traditional de Bruijn indexing scheme for bound variables.Since, <  = "a  we know that <  =  < "a <and therefore (= . <) is idempotent.=DConvert from traditional de Bruijn to generalized de Bruijn indices.#This requires a full tree traversal>;Perform substitution on both bound and free variables in a 5.?;Return a list of occurences of the variables bound by this 5.@1Perform a change of variables on bound variables.AIPerform a change of variables, reassigning both bound and free variables.B>Perform a change of variables on bound variables given only a  instanceC A version of A) that can be used when you only have the  instanceD>Obtain a result by collecting information from bound variablesEMObtain a result by collecting information from both bound and free variablesF the bound variables in a 5.G? both the variables bound by this scope and any free variables.H,mapM_ over the variables bound by this scopeIA G' that can be used when you only have a  instanceJ the bound variables in a 5.K&Traverse both bound and free variablesL'mapM over both bound and free variablesMA K' that can be used when you only have a  instancePThis allows you to  a 5.Q#This is a higher-order analogue of .S9instantiate bound variables using a list of new variablesT#Lift a natural transformation from f to g into one between scopes.gSThe monad permits substitution on free variables, while preserving bound variablesj; is provides a list (with duplicates) of the free variables756789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijk 56789:;<=>?@ABCDEFGHIJKLMNOPQRST 56789:;<=>?@ABCDEFGHIJKLMNOTPRQS556789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijk(C) 2012 Edward Kmett BSD-style (see the file LICENSE)Edward Kmett <ekmett@gmail.com> experimentalportable Trustworthy05mWe track the choice of m n% as a forgettable property that does not affect the result of () or .)To compare names rather than values, use (   o) instead.o Extract the o.pThis provides an Iso+ that can be used to access the parts of a m. p :: Iso (m n a) (m m b) (n, a) (m, b) q-Abstraction, capturing named bound variables.rAbstract over a single variablesfEnter a scope, instantiating all bound variables, but discarding (comonadic) meta data, like its nametEnter a 53 that binds one (named) variable, instantiating it. t = ;"mnopqrstuvwxyz{|}~mnopqrstmnpoqrst!mnopqrstuvwxyz{|}~(C) 2013 Edward Kmett BSD-style (see the file LICENSE)Edward Kmett <ekmett@gmail.com> experimentalportable Trustworthy/0:OT b f a is an f$ expression with bound variables in b, and free variables in a5This implements traditional de Bruijn indices, while   + implements generalized de Bruijn indices.[These traditional indices can be used to test the performance gain of generalized indices.While this type  is identical to 3 this module focuses on a drop-in replacement for   .WAnother use case is for syntaxes not stable under substitution, therefore with only a  instance and no  instance.9Capture some free variables in an expression to yield a  with bound variables in b:m + Data.List$abstract (`elemIndex` "bar") "barry"Scope [B 0,B 1,B 2,B 2,F 'y']Abstract over a single variableabstract1 'x' "xyz"Scope [B (),F 'y',F 'z']0Enter a scope, instantiating all bound variables:m + Data.ListLinstantiate (\x -> [toEnum (97 + x)]) $ abstract (`elemIndex` "bar") "barry""abccy"Enter a * that binds one variable, instantiating it+instantiate1 "x" $ Scope [B (),F 'y',F 'z']"xyz" is just another name for  and is exported to mimick . In particular no  constraint is required. is just another name for  and is exported to mimick . In particular no  constraint is required.;Perform substitution on both bound and free variables in a .;Return a list of occurences of the variables bound by this .1Perform a change of variables on bound variables.IPerform a change of variables, reassigning both bound and free variables.>Perform a change of variables on bound variables given only a  instance A version of ) that can be used when you only have the  instanceMObtain a result by collecting information from both bound and free variablesMObtain a result by collecting information from both bound and free variables the bound variables in a .? both the variables bound by this scope and any free variables.,mapM_ over the variables bound by this scopeA ' that can be used when you only have a  instance&Traverse both bound and free variables&Traverse both bound and free variablesThis allows you to  a .#This is a higher-order analogue of .9instantiate bound variables using a list of new variables'mapM over both bound and free variablesA ' that can be used when you only have a  instanceSThe monad permits substitution on free variables, while preserving bound variables; is provides a list (with duplicates) of the free variables6 4(C) 2012-2013 Edward Kmett BSD-style (see the file LICENSE)Edward Kmett <ekmett@gmail.com> experimentalportableNoneM&number tells how many layers are thereUse to automatically derive  and  instances for your datatype.9Also works for components that are lists or instances of <, but still does not work for a great deal of other things.deriving-compat# package may be used to derive the Show1 and Read1 instances {-# LANGUAGE DeriveFunctor #-} {-# LANGUAGE TemplateHaskell #-} import Bound (Scope, makeBound) import Data.Functor.Classes (Show1, Read1, shosPrec1, readsPrec1) import Data.Deriving (deriveShow1, deriveRead1) data Exp a = V a | App (Exp a) (Exp a) | Lam (Scope () Exp a) | ND [Exp a] | I Int deriving (Functor) makeBound ''Exp deriveShow1 ''Exp deriveRead1 ''Exp instance Read a => Read (Exp a) where readsPrec = readsPrec1 instance Show a => Show (Exp a) where showsPrec = showsPrec1  and in GHCi =ghci> :set -XDeriveFunctor ghci> :set -XTemplateHaskell ghci> import Bound (Scope, makeBound) ghci> import Data.Functor.Classes (Show1, Read1, showsPrec1, readsPrec1) ghci> import Data.Deriving (deriveShow1, deriveRead1) ghci> :{ ghci| data Exp a = V a | App (Exp a) (Exp a) | Lam (Scope () Exp a) | ND [Exp a] | I Int deriving (Functor) ghci| makeBound ''Exp ghci| deriveShow1 ''Exp ghci| deriveRead1 ''Exp ghci| instance Read a => Read (Exp a) where readsPrec = readsPrec1 ghci| instance Show a => Show (Exp a) where showsPrec = showsPrec1 ghci| :}  and # instances can be derived similarly import Data.Functor.Classes (Eq1, Ord1, eq1, compare1) import Data.Deriving (deriveEq1, deriveOrd1) deriveEq1 ''Exp deriveOrd1 ''Exp instance Eq a => Eq (Exp a) where (==) = eq1 instance Ord a => Ord (Exp a) where compare = compare1  or in GHCi: &ghci> import Data.Functor.Classes (Eq1, Ord1, eq1, compare1) ghci> import Data.Deriving (deriveEq1, deriveOrd1) ghci> :{ ghci| deriveEq1 ''Exp ghci| deriveOrd1 ''Exp ghci| instance Eq a => Eq (Exp a) where (==) = eq1 ghci| instance Ord a => Ord (Exp a) where compare = compare1 ghci| :} We cannot automatically derive  and > using the standard GHC mechanism, because instances require Exp to be a : {instance (Monad f, Eq b, Eq1 f, Eq a) => Eq (Scope b f a) instance (Monad f, Ord b, Ord1 f, Ord a) => Ord (Scope b f a) Extraty type variables&Apply arguments to a type constructor.  (C) 2012 Edward Kmett BSD-style (see the file LICENSE)Edward Kmett <ekmett@gmail.com> experimentalportableNone%'()*+56789:;<=%('56789:;)**+<= !"#$%&'()*+,-./0123456789: ;<=>?@ABCDE  FGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyzz{|}~  FGHIJaKLMNOPQRSTUVWX]^`_YZ[\bcdeghionpqjklmrstuvwxy bound-2-7IewuMnEPFmDtWuvR0VXbb Bound.Var Bound.Term Bound.Class Bound.Scope Bound.NameBound.Scope.SimpleBound.THControl.Monad.FreeFree Data.FunctiononBoundScopeControl.Monad.TransEitherT fromScopetoScopeVarBFunvar_B_F $fNFDataVar $fRead1Var $fShow1Var $fOrd1Var$fEq1Var $fRead2Var $fShow2Var $fOrd2Var$fEq2Var$fBitraversableVar$fBifoldableVar$fBifunctorVar $fMonadVar$fApplicativeVar$fTraversableVar $fFoldableVar $fFunctorVar$fSerializeVar $fBinaryVar $fSerialVar $fSerial1Var $fSerial2Var $fHashableVar$fHashable1Var$fHashable2Var$fEqVar$fOrdVar $fShowVar $fReadVar $fDataVar $fGenericVar substitute substituteVarclosedisClosed>>>==<<<$fBoundWriterT $fBoundStateT$fBoundReaderT $fBoundRWST $fBoundMaybeT $fBoundListT$fBoundIdentityT $fBoundErrorT $fBoundContTunscopeabstract abstract1 instantiate instantiate1splatbindingsmapBoundmapScope liftMBound liftMScope foldMapBound foldMapScopetraverseBound_traverseScope_ mapMBound_ mapMScope_ traverseBound traverseScope mapMBound mapMScopeserializeScopedeserializeScopebitraverseScopetransverseScopebitransverseScopeinstantiateVars hoistScope$fSerializeScope $fBinaryScope $fSerialScope$fSerial1Scope $fNFDataScope$fHashableScope$fHashable1Scope $fBoundScope $fRead1Scope $fShow1Scope $fOrd1Scope $fEq1Scope $fShowScope $fReadScope $fOrdScope $fEqScope$fMFunctorScope$fMonadTransScope $fMonadScope$fApplicativeScope$fTraversableScope$fFoldableScope$fFunctorScope $fDataScopeNamename_Name abstractName abstract1NameinstantiateNameinstantiate1Name $fNFDataName$fSerializeName $fBinaryName $fSerialName $fSerial1Name $fSerial2Name $fRead1Name $fShow1Name $fOrd1Name $fEq1Name $fRead2Name $fShow2Name $fOrd2Name $fEq2Name $fComonadName$fBitraversableName$fBifoldableName$fBifunctorName$fTraversableName$fFoldableName $fFunctorName $fOrdName$fHashableName$fHashable1Name$fHashable2Name$fEqName $fShowName $fReadName $fDataName $fGenericName makeBound $fShowProp$fShowComponentsbase Data.EitherEither distinguisherGHC.Basereturn>>=transformers-0.5.2.0Control.Monad.Trans.Classlift.flipGHC.EnumsuccidMonad Data.Foldable traverse_Data.Traversabletraverse'bifunctors-5.4.2-FZrmj8WklPbIT4LBQhjbnYData.Bitraversable bitraversetoListghc-prim GHC.Classes==compareFunctorFunktor ApplicativeEqOrdtypeVarsconAppsT Components ComponentVariablePropKonstExp makeBound' constructBind construct interpret stripLast2getNamegetPure varBindName