free-foil-0.6.1: Efficient Type-Safe Capture-Avoiding Substitution for Free (Scoped Monads)
Safe HaskellNone
LanguageHaskell2010

Control.Monad.Free.Foil.Internal

Description

The implementation of Control.Monad.Free.Foil, together with the definitions it does not export, such as the loops behind α-equivalence. Use Control.Monad.Free.Foil instead, unless you need those.

Since: 0.6.1

Synopsis

Documentation

data ScopedAST (binder :: S -> S -> Type) (sig :: Type -> Type -> Type) (n :: S) where Source #

Scoped term under a binder, which may be a pattern binding several names.

Since: 0.0.1

Constructors

ScopedAST :: forall (binder :: S -> S -> Type) (n :: S) (l :: S) (sig :: Type -> Type -> Type). binder n l -> AST binder sig l -> ScopedAST binder sig n 

Instances

Instances details
(Bifunctor sig, CoSinkable binder, SinkableK binder) => SinkableK (ScopedAST binder sig :: S -> Type) Source # 
Instance details

Defined in Control.Monad.Free.Foil.Internal

Methods

sinkabilityProofK :: forall (as :: LoT (S -> Type)) (bs :: LoT (S -> Type)) r. RenamingsK as bs -> (ScopedAST binder sig :@@: as) -> (forall (cs :: LoT (S -> Type)). RenamingsK as cs -> (ScopedAST binder sig :@@: cs) -> r) -> r Source #

(Bifunctor sig, CoSinkable binder, SinkableK binder) => Sinkable (ScopedAST binder sig) Source # 
Instance details

Defined in Control.Monad.Free.Foil.Internal

Methods

sinkabilityProof :: forall (n :: S) (l :: S). (Name n -> Name l) -> ScopedAST binder sig n -> ScopedAST binder sig l Source #

GenericK (ScopedAST binder sig :: S -> Type) Source # 
Instance details

Defined in Control.Monad.Free.Foil.Internal

Associated Types

type RepK (ScopedAST binder sig :: S -> Type) 
Instance details

Defined in Control.Monad.Free.Foil.Internal

type RepK (ScopedAST binder sig :: S -> Type) = Exists S (Field ((('Kon binder :: Atom (S -> S -> Type) (S -> S -> Type)) ':@: (Var1 :: Atom (S -> S -> Type) S)) ':@: (Var0 :: Atom (S -> S -> Type) S)) :*: Field (((('Kon AST :: Atom (S -> S -> Type) ((S -> S -> Type) -> (Type -> Type -> Type) -> S -> Type)) ':@: ('Kon binder :: Atom (S -> S -> Type) (S -> S -> Type))) ':@: ('Kon sig :: Atom (S -> S -> Type) (Type -> Type -> Type))) ':@: (Var0 :: Atom (S -> S -> Type) S)))

Methods

fromK :: forall (x :: LoT (S -> Type)). (ScopedAST binder sig :@@: x) -> RepK (ScopedAST binder sig) x #

toK :: forall (x :: LoT (S -> Type)). RepK (ScopedAST binder sig) x -> ScopedAST binder sig :@@: x #

(forall (x :: S) (y :: S). Binary (binder x y), forall (l :: S). Binary (AST binder sig l)) => Binary (ScopedAST binder sig n) Source #

The binder and the body, one after the other. Decoding mints the scope under the binder. See the module documentation.

Instance details

Defined in Control.Monad.Free.Foil.Binary

Methods

put :: ScopedAST binder sig n -> Put #

get :: Get (ScopedAST binder sig n) #

putList :: [ScopedAST binder sig n] -> Put #

(forall (x :: S) (y :: S). NFData (binder x y), forall (l :: S). NFData (AST binder sig l)) => NFData (ScopedAST binder sig n) Source # 
Instance details

Defined in Control.Monad.Free.Foil.Internal

Methods

rnf :: ScopedAST binder sig n -> () #

type RepK (ScopedAST binder sig :: S -> Type) Source # 
Instance details

Defined in Control.Monad.Free.Foil.Internal

type RepK (ScopedAST binder sig :: S -> Type) = Exists S (Field ((('Kon binder :: Atom (S -> S -> Type) (S -> S -> Type)) ':@: (Var1 :: Atom (S -> S -> Type) S)) ':@: (Var0 :: Atom (S -> S -> Type) S)) :*: Field (((('Kon AST :: Atom (S -> S -> Type) ((S -> S -> Type) -> (Type -> Type -> Type) -> S -> Type)) ':@: ('Kon binder :: Atom (S -> S -> Type) (S -> S -> Type))) ':@: ('Kon sig :: Atom (S -> S -> Type) (Type -> Type -> Type))) ':@: (Var0 :: Atom (S -> S -> Type) S)))

data AST (binder :: S -> S -> Type) (sig :: Type -> Type -> Type) (n :: S) where Source #

A term, generated by a signature Bifunctor sig, with (free) variables in scope n.

Since: 0.0.1

Constructors

Var :: forall (n :: S) (binder :: S -> S -> Type) (sig :: Type -> Type -> Type). !(Name n) -> AST binder sig n

A (free) variable in scope n.

Node :: forall (sig :: Type -> Type -> Type) (binder :: S -> S -> Type) (n :: S). sig (ScopedAST binder sig n) (AST binder sig n) -> AST binder sig n

A non-variable syntactic construction specified by the signature Bifunctor sig.

Instances

Instances details
(Bifunctor sig, CoSinkable binder, SinkableK binder) => RelMonad Name (AST binder sig) Source #

AST binder sig is a monad relative to Name.

Instance details

Defined in Control.Monad.Free.Foil.Internal

Methods

rreturn :: forall (a :: S). Name a -> AST binder sig a Source #

rbind :: forall (b :: S) (a :: S). Distinct b => Scope b -> AST binder sig a -> (Name a -> AST binder sig b) -> AST binder sig b Source #

(Bifunctor sig, CoSinkable binder, SinkableK binder) => SinkableK (AST binder sig :: S -> Type) Source # 
Instance details

Defined in Control.Monad.Free.Foil.Internal

Methods

sinkabilityProofK :: forall (as :: LoT (S -> Type)) (bs :: LoT (S -> Type)) r. RenamingsK as bs -> (AST binder sig :@@: as) -> (forall (cs :: LoT (S -> Type)). RenamingsK as cs -> (AST binder sig :@@: cs) -> r) -> r Source #

Show (Expr n) Source #

Use ppExpr to show \(\lambda\)-terms.

Instance details

Defined in Control.Monad.Free.Foil.Example

Methods

showsPrec :: Int -> Expr n -> ShowS #

show :: Expr n -> String #

showList :: [Expr n] -> ShowS #

(ZipMatchK sig, UnifiablePattern binder) => AlphaEquiv (AST binder sig) Source #

A term is a scope-indexed value that can be compared up to α, which is what a pattern carrying terms as payloads needs of them.

Instance details

Defined in Control.Monad.Free.Foil.Internal

Methods

alphaEquivIn :: forall (n :: S). Distinct n => Scope n -> AST binder sig n -> AST binder sig n -> Bool Source #

InjectName (AST binder sig) Source # 
Instance details

Defined in Control.Monad.Free.Foil.Internal

Methods

injectName :: forall (n :: S). Name n -> AST binder sig n Source #

(Bifunctor sig, CoSinkable binder, SinkableK binder) => Sinkable (AST binder sig) Source # 
Instance details

Defined in Control.Monad.Free.Foil.Internal

Methods

sinkabilityProof :: forall (n :: S) (l :: S). (Name n -> Name l) -> AST binder sig n -> AST binder sig l Source #

GenericK (AST binder sig :: S -> Type) Source # 
Instance details

Defined in Control.Monad.Free.Foil.Internal

Associated Types

type RepK (AST binder sig :: S -> Type) 
Instance details

Defined in Control.Monad.Free.Foil.Internal

type RepK (AST binder sig :: S -> Type) = Field (Name :$: (Var0 :: Atom (S -> Type) S)) :+: Field ((sig :$: (((('Kon ScopedAST :: Atom (S -> Type) ((S -> S -> Type) -> (Type -> Type -> Type) -> S -> Type)) ':@: ('Kon binder :: Atom (S -> Type) (S -> S -> Type))) ':@: ('Kon sig :: Atom (S -> Type) (Type -> Type -> Type))) ':@: (Var0 :: Atom (S -> Type) S))) ':@: (((('Kon AST :: Atom (S -> Type) ((S -> S -> Type) -> (Type -> Type -> Type) -> S -> Type)) ':@: ('Kon binder :: Atom (S -> Type) (S -> S -> Type))) ':@: ('Kon sig :: Atom (S -> Type) (Type -> Type -> Type))) ':@: (Var0 :: Atom (S -> Type) S)))

Methods

fromK :: forall (x :: LoT (S -> Type)). (AST binder sig :@@: x) -> RepK (AST binder sig) x #

toK :: forall (x :: LoT (S -> Type)). RepK (AST binder sig) x -> AST binder sig :@@: x #

(forall (x :: S) (y :: S). Binary (binder x y), forall scope term. (Binary scope, Binary term) => Binary (sig scope term)) => Binary (AST binder sig n) Source #

A tag byte, then the name or the node.

Instance details

Defined in Control.Monad.Free.Foil.Binary

Methods

put :: AST binder sig n -> Put #

get :: Get (AST binder sig n) #

putList :: [AST binder sig n] -> Put #

(forall (x :: S) (y :: S). NFData (binder x y), forall scope term. (NFData scope, NFData term) => NFData (sig scope term)) => NFData (AST binder sig n) Source #

rnf forces the names, the binders, and the nodes through the NFData instance of the signature. GHC specialises it where the binder and the signature are known. On the λ-terms of the nfdata benchmark, the specialised rnf is about four times faster than the general one.

Instance details

Defined in Control.Monad.Free.Foil.Internal

Methods

rnf :: AST binder sig n -> () #

Generic (AST binder sig n) Source # 
Instance details

Defined in Control.Monad.Free.Foil.Internal

Associated Types

type Rep (AST binder sig n) 
Instance details

Defined in Control.Monad.Free.Foil.Internal

type Rep (AST binder sig n) = D1 ('MetaData "AST" "Control.Monad.Free.Foil.Internal" "free-foil-0.6.1-GKcf2CraGZ7DvDGF7Om32K" 'False) (C1 ('MetaCons "Var" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 (Name n))) :+: C1 ('MetaCons "Node" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (sig (ScopedAST binder sig n) (AST binder sig n)))))

Methods

from :: AST binder sig n -> Rep (AST binder sig n) x #

to :: Rep (AST binder sig n) x -> AST binder sig n #

type RepK (AST binder sig :: S -> Type) Source # 
Instance details

Defined in Control.Monad.Free.Foil.Internal

type RepK (AST binder sig :: S -> Type) = Field (Name :$: (Var0 :: Atom (S -> Type) S)) :+: Field ((sig :$: (((('Kon ScopedAST :: Atom (S -> Type) ((S -> S -> Type) -> (Type -> Type -> Type) -> S -> Type)) ':@: ('Kon binder :: Atom (S -> Type) (S -> S -> Type))) ':@: ('Kon sig :: Atom (S -> Type) (Type -> Type -> Type))) ':@: (Var0 :: Atom (S -> Type) S))) ':@: (((('Kon AST :: Atom (S -> Type) ((S -> S -> Type) -> (Type -> Type -> Type) -> S -> Type)) ':@: ('Kon binder :: Atom (S -> Type) (S -> S -> Type))) ':@: ('Kon sig :: Atom (S -> Type) (Type -> Type -> Type))) ':@: (Var0 :: Atom (S -> Type) S)))
type Rep (AST binder sig n) Source # 
Instance details

Defined in Control.Monad.Free.Foil.Internal

type Rep (AST binder sig n) = D1 ('MetaData "AST" "Control.Monad.Free.Foil.Internal" "free-foil-0.6.1-GKcf2CraGZ7DvDGF7Om32K" 'False) (C1 ('MetaCons "Var" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 (Name n))) :+: C1 ('MetaCons "Node" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (sig (ScopedAST binder sig n) (AST binder sig n)))))

Substitution

substitute :: forall (sig :: Type -> Type -> Type) (binder :: S -> S -> Type) (o :: S) (i :: S). (Bifunctor sig, Distinct o, CoSinkable binder, SinkableK binder) => Scope o -> Substitution (AST binder sig) i o -> AST binder sig i -> AST binder sig o Source #

Substitution for free (scoped monads).

Since: 0.0.1

substituteRefreshed :: forall (sig :: Type -> Type -> Type) (o :: S) (binder :: S -> S -> Type) (i :: S). (Bifunctor sig, Distinct o, CoSinkable binder, SinkableK binder) => Scope o -> Substitution (AST binder sig) i o -> AST binder sig i -> AST binder sig o Source #

A version of substitute that refreshes every binder of the input term.

The substituted terms are inserted as they are, so their binders are not refreshed. Apply refreshAST to the result to normalize all of its binders.

In general, substitute is more efficient since it does not always refresh binders.

Since: 0.0.3

substitutePattern Source #

Arguments

:: forall (sig :: Type -> Type -> Type) (o :: S) binder' (binder :: S -> S -> Type) (n :: S) (i :: S). (Bifunctor sig, Distinct o, CoSinkable binder', CoSinkable binder, SinkableK binder) 
=> Scope o

Resulting scope.

-> Substitution (AST binder sig) n o

Environment mapping names in scope n.

-> binder' n i

Binders that extend scope n to scope i.

-> [AST binder sig o]

Terms for the bound names, in binding order (error if too few, extras ignored).

-> AST binder sig i 
-> AST binder sig o 

Substitute terms for the names a pattern binds in a body, and apply an environment to the body's other names.

Since: 0.2.0

Transformations between signatures

foldAST Source #

Arguments

:: forall binder sig r s (n :: S). Bifunctor sig 
=> (forall (m :: S). Name m -> r m)

A variable.

-> (forall (m :: S) (l :: S). binder m l -> r l -> s m)

A scoped subterm, from its binder and the fold of its body.

-> (forall (m :: S). sig (s m) (r m) -> r m)

A node, its subterms folded.

-> AST binder sig n 
-> r n 

Fold a term bottom-up into a scope-indexed carrier r. A variable goes through the first function, a scoped subterm through the second, given its binder and the fold of its body, and a node through the third, once its subterms are folded. The scope indices stay as they are, so the carrier can be another kind of term, as in hoistAST, or anything indexed by the scope, such as Const Int for a node count.

To change the binder type as well, map it in the second function:

foldAST Var (\binder body -> ScopedAST (toBinder binder) body) Node

Since: 0.6.0

hoistAST :: forall sig sig' (binder :: S -> S -> Type) (n :: S). Bifunctor sig => (forall a b. sig a b -> sig' a b) -> AST binder sig n -> AST binder sig' n Source #

Change the signature of a term by a natural transformation of signatures, keeping the binders, as hoistFree of the free package does for a free monad. Each node is rebuilt, so this costs a traversal.

Since: 0.6.0

\(\alpha\)-equivalence

refreshAST :: forall (sig :: Type -> Type -> Type) (n :: S) (binder :: S -> S -> Type). (Bifunctor sig, Distinct n, CoSinkable binder, SinkableK binder) => Scope n -> AST binder sig n -> AST binder sig n Source #

Refresh (force) all binders in a term, minimizing the used indices.

Since: 0.0.3

refreshScopedAST :: forall (sig :: Type -> Type -> Type) (n :: S) (binder :: S -> S -> Type). (Bifunctor sig, Distinct n, CoSinkable binder, SinkableK binder) => Scope n -> ScopedAST binder sig n -> ScopedAST binder sig n Source #

Similar to refreshAST, but for scoped terms.

Since: 0.0.3

alphaEquivRefreshed :: forall (sig :: Type -> Type -> Type) (n :: S) (binder :: S -> S -> Type). (Bifunctor sig, ZipMatchK sig, Distinct n, UnifiablePattern binder, SinkableK binder) => Scope n -> AST binder sig n -> AST binder sig n -> Bool Source #

\(\alpha\)-equivalence check for two terms in one scope via normalization of bound identifiers (via refreshAST).

Compared to alphaEquiv, this function renames every binder on both sides unconditionally, so it does strictly more work. It remains as an independent implementation of the same test. Like alphaEquiv, it compares patterns with unifyPatternsIn, so the payloads of a pattern are compared as well.

Since: 0.0.3

alphaEquiv :: forall (sig :: Type -> Type -> Type) (n :: S) (binder :: S -> S -> Type). (ZipMatchK sig, Distinct n, UnifiablePattern binder) => Scope n -> AST binder sig n -> AST binder sig n -> Bool Source #

\(\alpha\)-equivalence check for two terms in one scope via unification of bound variables (via unifyNameBinders).

When two matching binders coincide, comparison continues with no work at all. When they differ, the prescribed renaming is /threaded down the recursion/ (see alphaEquivEnv) and consulted at variables only, so nothing is ever copied. Applying the renaming eagerly instead would materialise a renamed copy of the remaining body at every mismatched binder, which is quadratic on a chain of them.

Since: 0.0.3

alphaEquivScoped :: forall (sig :: Type -> Type -> Type) (binder :: S -> S -> Type) (n :: S). (ZipMatchK sig, Distinct n, UnifiablePattern binder) => Scope n -> ScopedAST binder sig n -> ScopedAST binder sig n -> Bool Source #

Same as alphaEquiv but for scoped terms.

While the binders of the two sides coincide, this runs with no renaming state at all. The first pair that differs switches to alphaEquivEnv, which threads the renamings down and switches back when they empty out.

Since: 0.0.3

renamedId :: forall (n :: S) (a :: S) (b :: S). (NameBinder n a -> NameBinder n b) -> Name a -> Int Source #

The raw name a verdict's renaming assigns to a pattern's name.

Since: 0.4.0

bindPairs :: forall (sig :: Type -> Type -> Type) (binder :: S -> S -> Type) (m :: S) (l1 :: S) (l2 :: S). (ZipMatchK sig, Distinct m, UnifiablePattern binder) => Int -> IntMap Int -> IntMap Int -> [(Int, Int)] -> Scope m -> AST binder sig l1 -> AST binder sig l2 -> Bool Source #

Bind the paired names of a binder pair. A pair whose names coincide shadows both sides identically and is deleted from the environments. A pair whose names differ binds both to one fresh level. Continues with alphaEquivEnv on the bodies.

Since: 0.4.0

alphaEquivEnv Source #

Arguments

:: forall (sig :: Type -> Type -> Type) (binder :: S -> S -> Type) (n :: S) (n1 :: S) (n2 :: S). (ZipMatchK sig, Distinct n, UnifiablePattern binder) 
=> Int

Next fresh level.

-> IntMap Int

Left renaming: raw name to the level that bound it.

-> IntMap Int

Right renaming.

-> Scope n

Scope along the unified path (consulted by unifyPatternsIn).

-> AST binder sig n1 
-> AST binder sig n2 
-> Bool 

The renaming-threading worker behind alphaEquiv: compare two terms under partial renamings of their names into shared levels.

Each environment maps a raw name to the level of the binder pair that bound it on the comparison path, and a name outside its environment stands for itself. A variable occurrence then compares as a level against a level, or as a raw name against a raw name, and the two can never be conflated. This is what makes threading sound where applying a raw renaming at the variables would not be, since a renamed name could collide with one that passes through unchanged and happens to share the target spelling. Levels are also why no capture check is needed: a level is never a name, so there is nothing for a binder to capture.

A binder pair whose names coincide deletes those names from both environments, the pair shadowing both sides identically. When the environments empty out the comparison drops back to the stateless alphaEquiv, so only the region of the terms below a mismatched binder, and above the point where the mismatch is shadowed away, pays for the threading at all.

The indices of the two terms are deliberately independent, in the style of unsafeEqAST: the terms are never renamed into a common scope, so no common index exists to give them.

Since: 0.4.0

alphaEquivScopedEnv :: forall (sig :: Type -> Type -> Type) (binder :: S -> S -> Type) (n :: S) (n1 :: S) (n2 :: S). (ZipMatchK sig, Distinct n, UnifiablePattern binder) => Int -> IntMap Int -> IntMap Int -> Scope n -> ScopedAST binder sig n1 -> ScopedAST binder sig n2 -> Bool Source #

The scoped half of alphaEquivEnv: get the verdict from unifyPatternsIn, extend the environments as it prescribes, and recurse into the bodies as they stand.

Since: 0.4.0

data AlphaEquivLoops (binder :: S -> S -> Type) (sig :: Type -> Type -> Type) Source #

The mutually recursive loops behind α-equivalence, one field per function above.

Since: 0.6.1

Constructors

AlphaEquivLoops 

Fields

alphaEquivLoops :: forall (sig :: Type -> Type -> Type) (binder :: S -> S -> Type). (ZipMatchK sig, UnifiablePattern binder) => AlphaEquivLoops binder sig Source #

The loops behind α-equivalence. They are local and take the instances from this function, so once it is inlined at a known signature, they use that signature's instances directly. A recursive function with class constraints would instead be split by GHC into a wrapper and a worker (some of its dictionaries, such as Distinct, are never used at runtime), and a client's SPECIALIZE pragma would reach only the wrapper.

Since: 0.6.1

Unsafe equality checks

unsafeEqAST :: forall (sig :: Type -> Type -> Type) (binder :: S -> S -> Type) (n :: S) (l :: S). (ZipMatchK sig, UnifiablePattern binder, Distinct n, Distinct l) => AST binder sig n -> AST binder sig l -> Bool Source #

Unsafe equality check for two terms. Names are compared by their raw identifiers, and patterns by unsafeEqPattern, which uses the binder-only unifyPatterns. Thus α-equivalent terms whose binders differ compare unequal, so normalize both sides with refreshAST first.

Since: 0.0.3

unsafeEqScopedAST :: forall (sig :: Type -> Type -> Type) (binder :: S -> S -> Type) (n :: S) (l :: S). (ZipMatchK sig, UnifiablePattern binder, Distinct n, Distinct l) => ScopedAST binder sig n -> ScopedAST binder sig l -> Bool Source #

A version of unsafeEqAST for scoped terms.

Since: 0.0.3

data EqLoops (binder :: S -> S -> Type) (sig :: Type -> Type -> Type) Source #

The two mutually recursive loops behind unsafeEqAST and unsafeEqScopedAST.

Since: 0.6.1

Constructors

EqLoops 

Fields

unsafeEqLoops :: forall (sig :: Type -> Type -> Type) (binder :: S -> S -> Type). (ZipMatchK sig, UnifiablePattern binder) => EqLoops binder sig Source #

The loops behind unsafeEqAST and unsafeEqScopedAST, local for the same reason as those of alphaEquivLoops.

Since: 0.6.1

unsafeEqASTIn :: forall (sig :: Type -> Type -> Type) (binder :: S -> S -> Type) (n :: S). (ZipMatchK sig, UnifiablePattern binder, Distinct n) => Scope n -> AST binder sig n -> AST binder sig n -> Bool Source #

Unsafe equality check for two terms in one scope. Names are compared directly, and patterns by unifyPatternsIn, which must find their binders the same. Thus α-equivalent terms whose binders differ compare unequal, so normalize both sides with refreshAST first, as alphaEquivRefreshed does.

Unlike unsafeEqAST, this compares the payloads of patterns, which unifyPatternsIn needs the scope for.

Since: 0.6.1

unsafeEqScopedASTIn :: forall (sig :: Type -> Type -> Type) (binder :: S -> S -> Type) (n :: S). (ZipMatchK sig, UnifiablePattern binder, Distinct n) => Scope n -> ScopedAST binder sig n -> ScopedAST binder sig n -> Bool Source #

A version of unsafeEqASTIn for scoped terms.

Since: 0.6.1

data EqInLoops (binder :: S -> S -> Type) (sig :: Type -> Type -> Type) Source #

The two mutually recursive loops behind unsafeEqASTIn and unsafeEqScopedASTIn.

Since: 0.6.1

Constructors

EqInLoops 

Fields

unsafeEqInLoops :: forall (sig :: Type -> Type -> Type) (binder :: S -> S -> Type). (ZipMatchK sig, UnifiablePattern binder) => EqInLoops binder sig Source #

The loops behind unsafeEqASTIn and unsafeEqScopedASTIn, local for the same reason as those of alphaEquivLoops.

Since: 0.6.1

Converting to and from free foil

Convert to free foil

data UnresolvedName rawIdent Source #

An identifier a raw term mentions that the names given for conversion do not account for.

Note what this does and does not carry. It cannot carry a position: the conversion functions are generic in the raw term and only ever see it through toSig, so a source location, if the syntax has one, is not theirs to read. What they do know, and a caller checking names beforehand does not, is which names were in scope at the occurrence, the binders passed on the way down included. That is what a "did you mean" needs.

Since: 0.4.0

Constructors

UnresolvedName 

Fields

Instances

Instances details
Functor UnresolvedName Source # 
Instance details

Defined in Control.Monad.Free.Foil.Internal

Methods

fmap :: (a -> b) -> UnresolvedName a -> UnresolvedName b #

(<$) :: a -> UnresolvedName b -> UnresolvedName a #

Foldable UnresolvedName Source # 
Instance details

Defined in Control.Monad.Free.Foil.Internal

Methods

fold :: Monoid m => UnresolvedName m -> m #

foldMap :: Monoid m => (a -> m) -> UnresolvedName a -> m #

foldMap' :: Monoid m => (a -> m) -> UnresolvedName a -> m #

foldr :: (a -> b -> b) -> b -> UnresolvedName a -> b #

foldr' :: (a -> b -> b) -> b -> UnresolvedName a -> b #

foldl :: (b -> a -> b) -> b -> UnresolvedName a -> b #

foldl' :: (b -> a -> b) -> b -> UnresolvedName a -> b #

foldr1 :: (a -> a -> a) -> UnresolvedName a -> a #

foldl1 :: (a -> a -> a) -> UnresolvedName a -> a #

toList :: UnresolvedName a -> [a] #

null :: UnresolvedName a -> Bool #

length :: UnresolvedName a -> Int #

elem :: Eq a => a -> UnresolvedName a -> Bool #

maximum :: Ord a => UnresolvedName a -> a #

minimum :: Ord a => UnresolvedName a -> a #

sum :: Num a => UnresolvedName a -> a #

product :: Num a => UnresolvedName a -> a #

Traversable UnresolvedName Source # 
Instance details

Defined in Control.Monad.Free.Foil.Internal

Methods

traverse :: Applicative f => (a -> f b) -> UnresolvedName a -> f (UnresolvedName b) #

sequenceA :: Applicative f => UnresolvedName (f a) -> f (UnresolvedName a) #

mapM :: Monad m => (a -> m b) -> UnresolvedName a -> m (UnresolvedName b) #

sequence :: Monad m => UnresolvedName (m a) -> m (UnresolvedName a) #

Show rawIdent => Show (UnresolvedName rawIdent) Source # 
Instance details

Defined in Control.Monad.Free.Foil.Internal

Methods

showsPrec :: Int -> UnresolvedName rawIdent -> ShowS #

show :: UnresolvedName rawIdent -> String #

showList :: [UnresolvedName rawIdent] -> ShowS #

Eq rawIdent => Eq (UnresolvedName rawIdent) Source # 
Instance details

Defined in Control.Monad.Free.Foil.Internal

Methods

(==) :: UnresolvedName rawIdent -> UnresolvedName rawIdent -> Bool #

(/=) :: UnresolvedName rawIdent -> UnresolvedName rawIdent -> Bool #

unresolvedNames Source #

Arguments

:: forall sig binder rawIdent rawTerm rawPattern rawScopedTerm (n :: S). (Distinct n, Bifoldable sig, Ord rawIdent, CoSinkable binder) 
=> (rawTerm -> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm))

Unpeel one syntax node (or a variable) from a raw term.

-> (forall (x :: S) z. Distinct x => Scope x -> Map rawIdent (Name x) -> rawPattern -> (forall (y :: S). DExt x y => binder x y -> Map rawIdent (Name y) -> z) -> z)

Convert raw pattern into a scope-safe pattern.

-> (rawScopedTerm -> rawTerm)

Extract a term from a scoped term (or crash).

-> Scope n

Resulting scope of the constructed term.

-> Map rawIdent (Name n)

Known names of free variables in scope n.

-> rawTerm

Raw term.

-> [UnresolvedName rawIdent] 

The identifiers a raw term mentions that a set of names cannot resolve, in the order they occur.

This is unsafeConvertToAST with the conversion left out, so it descends under binders in the same way and accounts for what they bind. A caller that converts with tryConvertToASTWith should use unresolvedNamesWith instead, so that its constants do not count as unresolved.

Since: 0.4.0

unresolvedNamesWith Source #

Arguments

:: forall sig binder rawIdent rawTerm rawPattern rawScopedTerm (n :: S). (Distinct n, Bifoldable sig, Ord rawIdent, CoSinkable binder) 
=> (rawTerm -> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm))

Unpeel one syntax node (or a variable) from a raw term.

-> (forall (x :: S) z. Distinct x => Scope x -> Map rawIdent (Name x) -> rawPattern -> (forall (y :: S). DExt x y => binder x y -> Map rawIdent (Name y) -> z) -> z)

Convert raw pattern into a scope-safe pattern.

-> (rawScopedTerm -> rawTerm)

Extract a term from a scoped term (or crash).

-> Scope n

Resulting scope of the constructed term.

-> Map rawIdent (Name n)

Known names of free variables in scope n.

-> Map rawIdent (AST binder sig n)

Identifiers that denote a term rather than a variable.

-> rawTerm

Raw term.

-> [UnresolvedName rawIdent] 

The identifiers a raw term mentions that neither a set of names nor a table of constants can resolve, in the order they occur. This is unresolvedNames for the arguments of tryConvertToASTWith: given the same names and constants, it reports every identifier that tryConvertToASTWith would fail on, and the first of them is the one that tryConvertToASTWith reports.

Since: 0.6.1

tryConvertToAST Source #

Arguments

:: forall sig binder rawIdent rawTerm rawPattern rawScopedTerm (n :: S). (Distinct n, Bitraversable sig, Ord rawIdent, CoSinkable binder, SinkableK binder) 
=> (rawTerm -> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm))

Unpeel one syntax node (or a variable) from a raw term.

-> (forall (x :: S) z. Distinct x => Scope x -> Map rawIdent (Name x) -> rawPattern -> (forall (y :: S). DExt x y => binder x y -> Map rawIdent (Name y) -> z) -> z)

Convert raw pattern into a scope-safe pattern.

-> (rawScopedTerm -> rawTerm)

Extract a term from a scoped term (or crash).

-> Scope n

Resulting scope of the constructed term.

-> Map rawIdent (Name n)

Known names of free variables in scope n.

-> rawTerm

Raw term.

-> Either (UnresolvedName rawIdent) (AST binder sig n) 

Convert a raw term into a scope-safe term, reporting the first identifier that does not resolve.

One pass, short-circuiting at the first failure, so a term that resolves costs no more than unsafeConvertToAST does. The report is complete for that one identifier, since unresolvedInScope is built where the conversion fails and is never computed on the way through.

A caller wanting every unresolved identifier rather than the first pays a second pass for it, with unresolvedNames. The successful path stays fast that way, and a failure can afford to be walked again for a better message.

Since: 0.4.0

tryConvertToASTWith Source #

Arguments

:: forall sig binder rawIdent rawTerm rawPattern rawScopedTerm (n :: S). (Distinct n, Bitraversable sig, Ord rawIdent, CoSinkable binder, SinkableK binder) 
=> (rawTerm -> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm))

Unpeel one syntax node (or a variable) from a raw term.

-> (forall (x :: S) z. Distinct x => Scope x -> Map rawIdent (Name x) -> rawPattern -> (forall (y :: S). DExt x y => binder x y -> Map rawIdent (Name y) -> z) -> z)

Convert raw pattern into a scope-safe pattern.

-> (rawScopedTerm -> rawTerm)

Extract a term from a scoped term (or crash).

-> Scope n

Resulting scope of the constructed term.

-> Map rawIdent (Name n)

Known names of free variables in scope n.

-> Map rawIdent (AST binder sig n)

Identifiers that denote a term rather than a variable.

-> rawTerm

Raw term.

-> Either (UnresolvedName rawIdent) (AST binder sig n) 

Convert a raw term into a scope-safe term, resolving some identifiers to whole terms rather than to variables.

The extra table is what a language with constants needs: an identifier that denotes a top-level declaration, a primitive, or an abbreviation stands for something that is not a variable, and resolving it during conversion is the only place where the binders are known. Doing it in a pass of one's own means writing a binder-aware traversal of the raw syntax by hand.

Variables win: the table of names is consulted first, so a binder shadows an entry here, and an entry here shadows nothing. The table is sunk when going under a binder, exactly as the names are, so its entries may mention anything in scope where conversion started and need not be closed.

Since: 0.4.0

unsafeConvertToAST Source #

Arguments

:: forall (n :: S) sig rawIdent binder rawTerm rawPattern rawScopedTerm. (Distinct n, Bifunctor sig, Ord rawIdent, CoSinkable binder) 
=> (rawTerm -> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm))

Unpeel one syntax node (or a variable) from a raw term.

-> (forall (x :: S) z. Distinct x => Scope x -> Map rawIdent (Name x) -> rawPattern -> (forall (y :: S). DExt x y => binder x y -> Map rawIdent (Name y) -> z) -> z)

Convert raw pattern into a scope-safe pattern.

-> (rawScopedTerm -> rawTerm)

Extract a term from a scoped term (or crash).

-> Scope n

Resulting scope of the constructed term.

-> Map rawIdent (Name n)

Known names of free variables in scope n.

-> rawTerm

Raw term.

-> AST binder sig n 

Convert a raw term into a scope-safe term, calling error on an identifier that does not resolve.

Prefer tryConvertToAST, which reports such identifiers. This is for callers that have already established that every name resolves.

Since: 0.4.0

unsafeConvertToScopedAST Source #

Arguments

:: forall (n :: S) sig rawIdent binder rawTerm rawPattern rawScopedTerm. (Distinct n, Bifunctor sig, Ord rawIdent, CoSinkable binder) 
=> (rawTerm -> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm))

Unpeel one syntax node (or a variable) from a raw term.

-> (forall (x :: S) z. Distinct x => Scope x -> Map rawIdent (Name x) -> rawPattern -> (forall (y :: S). DExt x y => binder x y -> Map rawIdent (Name y) -> z) -> z)

Convert raw pattern into a scope-safe pattern.

-> (rawScopedTerm -> rawTerm)

Extract a term from a scoped term (or crash).

-> Scope n

Resulting scope of the constructed term.

-> Map rawIdent (Name n)

Known names of free variables in scope n.

-> (rawPattern, rawScopedTerm)

A pair of a pattern and a corresponding scoped term.

-> ScopedAST binder sig n 

Same as unsafeConvertToAST but for scoped terms.

Since: 0.4.0

Convert from free foil

convertFromAST Source #

Arguments

:: forall sig rawPattern rawScopedTerm rawTerm rawIdent binder (n :: S). Bifunctor sig 
=> (sig (rawPattern, rawScopedTerm) rawTerm -> rawTerm)

Peel back one layer of syntax.

-> (rawIdent -> rawTerm)

Convert identifier into a raw variable term.

-> (forall (x :: S) (y :: S). binder x y -> rawPattern)

Convert scope-safe pattern into a raw pattern.

-> (rawTerm -> rawScopedTerm)

Wrap raw term into a scoped term.

-> (Int -> rawIdent)

Name a variable, bound or free, from its underlying integer identifier.

-> AST binder sig n

Scope-safe term.

-> rawTerm 

Convert a scope-safe term back into a raw term. Use convertFromASTWith to name free variables separately from bound ones.

Since: 0.0.3

convertFromScopedAST Source #

Arguments

:: forall sig rawPattern rawScopedTerm rawTerm rawIdent binder (n :: S). Bifunctor sig 
=> (sig (rawPattern, rawScopedTerm) rawTerm -> rawTerm)

Peel back one layer of syntax.

-> (rawIdent -> rawTerm)

Convert identifier into a raw variable term.

-> (forall (x :: S) (y :: S). binder x y -> rawPattern)

Convert scope-safe pattern into a raw pattern.

-> (rawTerm -> rawScopedTerm)

Wrap raw term into a scoped term.

-> (Int -> rawIdent)

Name a variable, bound or free, from its underlying integer identifier.

-> ScopedAST binder sig n

Scope-safe scoped term.

-> (rawPattern, rawScopedTerm) 

Same as convertFromAST but for scoped terms.

Since: 0.0.3

convertFromASTWith Source #

Arguments

:: forall sig binder rawIdent rawTerm rawPattern rawScopedTerm (n :: S). (Bifunctor sig, Distinct n, CoSinkable binder) 
=> (sig (rawPattern, rawScopedTerm) rawTerm -> rawTerm)

Peel back one layer of syntax.

-> (rawIdent -> rawTerm)

Convert identifier into a raw variable term.

-> (forall (x :: S) (y :: S). binder x y -> rawPattern)

Convert scope-safe pattern into a raw pattern.

-> (rawTerm -> rawScopedTerm)

Wrap raw term into a scoped term.

-> (Name n -> rawIdent)

Name a variable that is free in the whole term.

-> (Int -> rawIdent)

Name a bound variable, from its underlying integer identifier.

-> AST binder sig n

Scope-safe term.

-> rawTerm 

Convert a scope-safe term back into a raw term, naming the variables that occur free in the whole term separately from the bound ones.

convertFromAST applies one naming function to every variable it meets, bound or free, and gives it only a raw name. That is often not enough, since raw names are not unique across scope indices: a binder inside a term may share one with a name of the ambient scope, so naming by raw name alone can print a bound variable as whatever the ambient scope calls that name.

Keeping the typed name is what distinguishes them, and unsinkNamePattern is the operation for it: composing one per binder on the way down builds a Name x -> Maybe ( Name n ) that answers exactly the question.

Since: 0.4.0

Unsinking AST

supportOf :: forall (n :: S) (binder :: S -> S -> Type) (sig :: Type -> Type -> Type). (Distinct n, CoSinkable binder, Bifoldable sig) => AST binder sig n -> NameSet n Source #

The support of a term: exactly the names that occur free in it.

This is the annotation that co-de-Bruijn syntax carries intrinsically and that the foil does not need for weakening, since sink is a coercion. Computing it traverses the whole term, so a client that restricts often should cache it.

Since: 0.4.0

supportOfScopedAST :: forall (n :: S) (binder :: S -> S -> Type) (sig :: Type -> Type -> Type). (Distinct n, CoSinkable binder, Bifoldable sig) => ScopedAST binder sig n -> NameSet n Source #

The support of a scoped term, in the scope outside its binder.

Since: 0.4.0

withRelevantScope :: forall (n :: S) (binder :: S -> S -> Type) (sig :: Type -> Type -> Type) r. (Distinct n, CoSinkable binder, Bifoldable sig) => AST binder sig n -> (forall (m :: S). (Ext m n, Distinct m) => Scope m -> AST binder sig m -> r) -> r Source #

Cut a term down to the scope of exactly the names it uses.

The term's support is computed with supportOf, as in unsinkAST, but nothing is tested and nothing can fail, since the term inhabits the restricted scope by construction. Ext m n comes back with it, so the term can be sinked to where it came from for free.

Verifying a declared dependency, such as a uses clause or a module's parameters, is this plus a comparison: compute the scope a term really inhabits, and check the declared one against it.

Since: 0.4.0

unsinkAST :: forall (n :: S) (l :: S) (binder :: S -> S -> Type) (sig :: Type -> Type -> Type). (DExt n l, CoSinkable binder, Bifoldable sig) => Scope n -> AST binder sig l -> Maybe (AST binder sig n) Source #

Restrict a term from scope l to a smaller scope n, if every name that occurs free in it is in n.

The scope l must extend n, since names are compared by their raw identifiers, which identify a name only within one chain of scope extensions. On success the term is returned unchanged, since restricting a term that does inhabit n is a coercion.

Since: 0.3.0

freeVarsOf :: forall (n :: S) (binder :: S -> S -> Type) (sig :: Type -> Type -> Type). (Distinct n, CoSinkable binder, Bifoldable sig) => AST binder sig n -> [Name n] Source #

Get the free variables of an AST.

These come from supportOf, so they are distinct and in ascending order of their identifiers.

Since: 0.3.0

freeVarsOfScopedAST :: forall (n :: S) (binder :: S -> S -> Type) (sig :: Type -> Type -> Type). (Distinct n, CoSinkable binder, Bifoldable sig) => ScopedAST binder sig n -> [Name n] Source #

Get the free variables of a scoped AST, in the scope outside its binder.

Since: 0.3.0