| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
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
- data ScopedAST (binder :: S -> S -> Type) (sig :: Type -> Type -> Type) (n :: S) where
- data AST (binder :: S -> S -> Type) (sig :: Type -> Type -> Type) (n :: S) where
- 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
- 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
- substitutePattern :: 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 -> Substitution (AST binder sig) n o -> binder' n i -> [AST binder sig o] -> AST binder sig i -> AST binder sig o
- foldAST :: forall binder sig r s (n :: S). Bifunctor sig => (forall (m :: S). Name m -> r m) -> (forall (m :: S) (l :: S). binder m l -> r l -> s m) -> (forall (m :: S). sig (s m) (r m) -> r m) -> AST binder sig n -> r n
- 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
- 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
- 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
- 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
- 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
- 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
- renamedId :: forall (n :: S) (a :: S) (b :: S). (NameBinder n a -> NameBinder n b) -> Name a -> Int
- 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
- alphaEquivEnv :: 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 -> AST binder sig n1 -> AST binder sig n2 -> Bool
- 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
- data AlphaEquivLoops (binder :: S -> S -> Type) (sig :: Type -> Type -> Type) = AlphaEquivLoops {
- aeAlphaEquiv :: forall (n :: S). Distinct n => Scope n -> AST binder sig n -> AST binder sig n -> Bool
- aeAlphaEquivScoped :: forall (n :: S). Distinct n => Scope n -> ScopedAST binder sig n -> ScopedAST binder sig n -> Bool
- aeBindPairs :: forall (m :: S) (l1 :: S) (l2 :: S). Distinct m => Int -> IntMap Int -> IntMap Int -> [(Int, Int)] -> Scope m -> AST binder sig l1 -> AST binder sig l2 -> Bool
- aeAlphaEquivEnv :: forall (n :: S) (n1 :: S) (n2 :: S). Distinct n => Int -> IntMap Int -> IntMap Int -> Scope n -> AST binder sig n1 -> AST binder sig n2 -> Bool
- aeAlphaEquivScopedEnv :: forall (n :: S) (n1 :: S) (n2 :: S). Distinct n => Int -> IntMap Int -> IntMap Int -> Scope n -> ScopedAST binder sig n1 -> ScopedAST binder sig n2 -> Bool
- alphaEquivLoops :: forall (sig :: Type -> Type -> Type) (binder :: S -> S -> Type). (ZipMatchK sig, UnifiablePattern binder) => AlphaEquivLoops binder sig
- 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
- 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
- data EqLoops (binder :: S -> S -> Type) (sig :: Type -> Type -> Type) = EqLoops {}
- unsafeEqLoops :: forall (sig :: Type -> Type -> Type) (binder :: S -> S -> Type). (ZipMatchK sig, UnifiablePattern binder) => EqLoops binder sig
- 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
- 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
- data EqInLoops (binder :: S -> S -> Type) (sig :: Type -> Type -> Type) = EqInLoops {}
- unsafeEqInLoops :: forall (sig :: Type -> Type -> Type) (binder :: S -> S -> Type). (ZipMatchK sig, UnifiablePattern binder) => EqInLoops binder sig
- data UnresolvedName rawIdent = UnresolvedName {
- unresolvedIdent :: rawIdent
- unresolvedInScope :: [rawIdent]
- unresolvedNames :: forall sig binder rawIdent rawTerm rawPattern rawScopedTerm (n :: S). (Distinct n, Bifoldable sig, Ord rawIdent, CoSinkable binder) => (rawTerm -> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm)) -> (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) -> (rawScopedTerm -> rawTerm) -> Scope n -> Map rawIdent (Name n) -> rawTerm -> [UnresolvedName rawIdent]
- unresolvedNamesWith :: forall sig binder rawIdent rawTerm rawPattern rawScopedTerm (n :: S). (Distinct n, Bifoldable sig, Ord rawIdent, CoSinkable binder) => (rawTerm -> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm)) -> (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) -> (rawScopedTerm -> rawTerm) -> Scope n -> Map rawIdent (Name n) -> Map rawIdent (AST binder sig n) -> rawTerm -> [UnresolvedName rawIdent]
- tryConvertToAST :: 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)) -> (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) -> (rawScopedTerm -> rawTerm) -> Scope n -> Map rawIdent (Name n) -> rawTerm -> Either (UnresolvedName rawIdent) (AST binder sig n)
- tryConvertToASTWith :: 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)) -> (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) -> (rawScopedTerm -> rawTerm) -> Scope n -> Map rawIdent (Name n) -> Map rawIdent (AST binder sig n) -> rawTerm -> Either (UnresolvedName rawIdent) (AST binder sig n)
- unsafeConvertToAST :: forall (n :: S) sig rawIdent binder rawTerm rawPattern rawScopedTerm. (Distinct n, Bifunctor sig, Ord rawIdent, CoSinkable binder) => (rawTerm -> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm)) -> (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) -> (rawScopedTerm -> rawTerm) -> Scope n -> Map rawIdent (Name n) -> rawTerm -> AST binder sig n
- unsafeConvertToScopedAST :: forall (n :: S) sig rawIdent binder rawTerm rawPattern rawScopedTerm. (Distinct n, Bifunctor sig, Ord rawIdent, CoSinkable binder) => (rawTerm -> Either rawIdent (sig (rawPattern, rawScopedTerm) rawTerm)) -> (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) -> (rawScopedTerm -> rawTerm) -> Scope n -> Map rawIdent (Name n) -> (rawPattern, rawScopedTerm) -> ScopedAST binder sig n
- convertFromAST :: forall sig rawPattern rawScopedTerm rawTerm rawIdent binder (n :: S). Bifunctor sig => (sig (rawPattern, rawScopedTerm) rawTerm -> rawTerm) -> (rawIdent -> rawTerm) -> (forall (x :: S) (y :: S). binder x y -> rawPattern) -> (rawTerm -> rawScopedTerm) -> (Int -> rawIdent) -> AST binder sig n -> rawTerm
- convertFromScopedAST :: forall sig rawPattern rawScopedTerm rawTerm rawIdent binder (n :: S). Bifunctor sig => (sig (rawPattern, rawScopedTerm) rawTerm -> rawTerm) -> (rawIdent -> rawTerm) -> (forall (x :: S) (y :: S). binder x y -> rawPattern) -> (rawTerm -> rawScopedTerm) -> (Int -> rawIdent) -> ScopedAST binder sig n -> (rawPattern, rawScopedTerm)
- convertFromASTWith :: forall sig binder rawIdent rawTerm rawPattern rawScopedTerm (n :: S). (Bifunctor sig, Distinct n, CoSinkable binder) => (sig (rawPattern, rawScopedTerm) rawTerm -> rawTerm) -> (rawIdent -> rawTerm) -> (forall (x :: S) (y :: S). binder x y -> rawPattern) -> (rawTerm -> rawScopedTerm) -> (Name n -> rawIdent) -> (Int -> rawIdent) -> AST binder sig n -> rawTerm
- supportOf :: forall (n :: S) (binder :: S -> S -> Type) (sig :: Type -> Type -> Type). (Distinct n, CoSinkable binder, Bifoldable sig) => AST binder sig n -> NameSet n
- supportOfScopedAST :: forall (n :: S) (binder :: S -> S -> Type) (sig :: Type -> Type -> Type). (Distinct n, CoSinkable binder, Bifoldable sig) => ScopedAST binder sig n -> NameSet n
- 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
- 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)
- freeVarsOf :: forall (n :: S) (binder :: S -> S -> Type) (sig :: Type -> Type -> Type). (Distinct n, CoSinkable binder, Bifoldable sig) => AST binder sig n -> [Name n]
- freeVarsOfScopedAST :: forall (n :: S) (binder :: S -> S -> Type) (sig :: Type -> Type -> Type). (Distinct n, CoSinkable binder, Bifoldable sig) => ScopedAST binder sig n -> [Name n]
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
| (Bifunctor sig, CoSinkable binder, SinkableK binder) => SinkableK (ScopedAST binder sig :: S -> Type) Source # | |||||
Defined in Control.Monad.Free.Foil.Internal | |||||
| (Bifunctor sig, CoSinkable binder, SinkableK binder) => Sinkable (ScopedAST binder sig) Source # | |||||
Defined in Control.Monad.Free.Foil.Internal | |||||
| GenericK (ScopedAST binder sig :: S -> Type) Source # | |||||
Defined in Control.Monad.Free.Foil.Internal Associated Types
| |||||
| (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. | ||||
| (forall (x :: S) (y :: S). NFData (binder x y), forall (l :: S). NFData (AST binder sig l)) => NFData (ScopedAST binder sig n) Source # | |||||
Defined in Control.Monad.Free.Foil.Internal | |||||
| type RepK (ScopedAST binder sig :: S -> Type) Source # | |||||
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 |
| 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 |
Instances
| (Bifunctor sig, CoSinkable binder, SinkableK binder) => RelMonad Name (AST binder sig) Source # | |||||
| (Bifunctor sig, CoSinkable binder, SinkableK binder) => SinkableK (AST binder sig :: S -> Type) Source # | |||||
Defined in Control.Monad.Free.Foil.Internal | |||||
| Show (Expr n) Source # | Use | ||||
| (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. | ||||
Defined in Control.Monad.Free.Foil.Internal | |||||
| InjectName (AST binder sig) Source # | |||||
Defined in Control.Monad.Free.Foil.Internal | |||||
| (Bifunctor sig, CoSinkable binder, SinkableK binder) => Sinkable (AST binder sig) Source # | |||||
Defined in Control.Monad.Free.Foil.Internal | |||||
| GenericK (AST binder sig :: S -> Type) Source # | |||||
Defined in Control.Monad.Free.Foil.Internal Associated Types
| |||||
| (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. | ||||
| (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 # |
| ||||
Defined in Control.Monad.Free.Foil.Internal | |||||
| Generic (AST binder sig n) Source # | |||||
Defined in Control.Monad.Free.Foil.Internal Associated Types
| |||||
| type RepK (AST binder sig :: S -> Type) Source # | |||||
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 # | |||||
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
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 |
| -> binder' n i | Binders that extend scope |
| -> [AST binder sig o] | Terms for the bound names, in binding order ( |
| -> 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
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 for a node count.Const Int
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
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 |
| -> 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
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
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
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 |
| -> 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
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 |
| -> 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
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 |
| -> 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
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 |
| -> 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
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 |
| -> 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 |
| -> (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
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
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
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
that answers exactly the question.Name x -> Maybe ( Name n )
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. comes back with it, so
the term can be Ext m nsinked 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