2022-04-23 18:21:30 -04:00
|
|
|
module Quox.Syntax.Term.Subst
|
|
|
|
|
|
|
|
import Quox.Syntax.Term.Base
|
|
|
|
|
2022-05-02 16:38:37 -04:00
|
|
|
%default total
|
|
|
|
|
2022-04-23 18:21:30 -04:00
|
|
|
|
2023-01-08 14:44:25 -05:00
|
|
|
export FromVar (Elim q d) where fromVar = B
|
|
|
|
export FromVar (Term q d) where fromVar = E . fromVar
|
2022-04-23 18:21:30 -04:00
|
|
|
|
|
|
|
||| does the minimal reasonable work:
|
|
|
|
||| - deletes the closure around a free name since it doesn't do anything
|
|
|
|
||| - deletes an identity substitution
|
|
|
|
||| - composes (lazily) with an existing top-level closure
|
|
|
|
||| - immediately looks up a bound variable
|
|
|
|
||| - otherwise, wraps in a new closure
|
|
|
|
export
|
2023-01-08 14:44:25 -05:00
|
|
|
CanSubst (Elim q d) (Elim q d) where
|
2022-04-23 18:21:30 -04:00
|
|
|
F x // _ = F x
|
|
|
|
B i // th = th !! i
|
|
|
|
CloE e ph // th = assert_total CloE e $ ph . th
|
|
|
|
e // th = case force th of
|
|
|
|
Shift SZ => e
|
|
|
|
th => CloE e th
|
|
|
|
|
|
|
|
||| does the minimal reasonable work:
|
|
|
|
||| - deletes the closure around an atomic constant like `TYPE`
|
|
|
|
||| - deletes an identity substitution
|
|
|
|
||| - composes (lazily) with an existing top-level closure
|
|
|
|
||| - goes inside `E` in case it is a simple variable or something
|
|
|
|
||| - otherwise, wraps in a new closure
|
|
|
|
export
|
2023-01-08 14:44:25 -05:00
|
|
|
CanSubst (Elim q d) (Term q d) where
|
2022-04-23 18:21:30 -04:00
|
|
|
TYPE l // _ = TYPE l
|
|
|
|
E e // th = E $ e // th
|
|
|
|
CloT s ph // th = CloT s $ ph . th
|
|
|
|
s // th = case force th of
|
|
|
|
Shift SZ => s
|
|
|
|
th => CloT s th
|
|
|
|
|
|
|
|
export
|
2023-01-08 14:44:25 -05:00
|
|
|
CanSubst (Elim q d) (ScopeTerm q d) where
|
2022-04-23 18:21:30 -04:00
|
|
|
TUsed body // th = TUsed $ body // push th
|
|
|
|
TUnused body // th = TUnused $ body // th
|
|
|
|
|
2023-01-08 14:44:25 -05:00
|
|
|
export CanSubst Var (Term q d) where s // th = s // map (B {q, d}) th
|
|
|
|
export CanSubst Var (Elim q d) where e // th = e // map (B {q, d}) th
|
|
|
|
export CanSubst Var (ScopeTerm q d) where s // th = s // map (B {q, d}) th
|
2022-04-23 18:21:30 -04:00
|
|
|
|
|
|
|
|
|
|
|
infixl 8 //., ///
|
|
|
|
mutual
|
|
|
|
namespace Term
|
|
|
|
||| applies a term substitution with a less ambiguous type
|
|
|
|
export
|
2023-01-08 14:44:25 -05:00
|
|
|
(//.) : Term q d from -> TSubst q d from to -> Term q d to
|
2022-04-23 18:21:30 -04:00
|
|
|
t //. th = t // th
|
|
|
|
|
|
|
|
||| applies a dimension substitution with the same behaviour as `(//)`
|
|
|
|
||| above
|
|
|
|
export
|
2023-01-08 14:44:25 -05:00
|
|
|
(///) : Term q dfrom n -> DSubst dfrom dto -> Term q dto n
|
2022-04-23 18:21:30 -04:00
|
|
|
TYPE l /// _ = TYPE l
|
|
|
|
E e /// th = E $ e /// th
|
|
|
|
DCloT s ph /// th = DCloT s $ ph . th
|
|
|
|
s /// Shift SZ = s
|
|
|
|
s /// th = DCloT s th
|
|
|
|
|
|
|
|
||| applies a term and dimension substitution
|
|
|
|
public export %inline
|
2023-01-08 14:44:25 -05:00
|
|
|
subs : Term q dfrom from -> DSubst dfrom dto -> TSubst q dto from to ->
|
|
|
|
Term q dto to
|
2022-04-23 18:21:30 -04:00
|
|
|
subs s th ph = s /// th // ph
|
|
|
|
|
|
|
|
namespace Elim
|
|
|
|
||| applies a term substitution with a less ambiguous type
|
|
|
|
export
|
2023-01-08 14:44:25 -05:00
|
|
|
(//.) : Elim q d from -> TSubst q d from to -> Elim q d to
|
2022-04-23 18:21:30 -04:00
|
|
|
e //. th = e // th
|
|
|
|
|
|
|
|
||| applies a dimension substitution with the same behaviour as `(//)`
|
|
|
|
||| above
|
|
|
|
export
|
2023-01-08 14:44:25 -05:00
|
|
|
(///) : Elim q dfrom n -> DSubst dfrom dto -> Elim q dto n
|
2022-04-23 18:21:30 -04:00
|
|
|
F x /// _ = F x
|
|
|
|
B i /// _ = B i
|
|
|
|
DCloE e ph /// th = DCloE e $ ph . th
|
|
|
|
e /// Shift SZ = e
|
|
|
|
e /// th = DCloE e th
|
|
|
|
|
|
|
|
||| applies a term and dimension substitution
|
|
|
|
public export %inline
|
2023-01-08 14:44:25 -05:00
|
|
|
subs : Elim q dfrom from -> DSubst dfrom dto -> TSubst q dto from to ->
|
|
|
|
Elim q dto to
|
2022-04-23 18:21:30 -04:00
|
|
|
subs e th ph = e /// th // ph
|
|
|
|
|
|
|
|
namespace ScopeTerm
|
|
|
|
||| applies a term substitution with a less ambiguous type
|
|
|
|
export
|
2023-01-08 14:44:25 -05:00
|
|
|
(//.) : ScopeTerm q d from -> TSubst q d from to -> ScopeTerm q d to
|
2022-04-23 18:21:30 -04:00
|
|
|
body //. th = body // th
|
|
|
|
|
|
|
|
||| applies a dimension substitution with the same behaviour as `(//)`
|
|
|
|
||| above
|
|
|
|
export
|
2023-01-08 14:44:25 -05:00
|
|
|
(///) : ScopeTerm q dfrom n -> DSubst dfrom dto -> ScopeTerm q dto n
|
2022-04-23 18:21:30 -04:00
|
|
|
TUsed body /// th = TUsed $ body /// th
|
|
|
|
TUnused body /// th = TUnused $ body /// th
|
|
|
|
|
|
|
|
||| applies a term and dimension substitution
|
|
|
|
public export %inline
|
2023-01-08 14:44:25 -05:00
|
|
|
subs : ScopeTerm q dfrom from -> DSubst dfrom dto -> TSubst q dto from to ->
|
|
|
|
ScopeTerm q dto to
|
2022-04-23 18:21:30 -04:00
|
|
|
subs body th ph = body /// th // ph
|
|
|
|
|
2023-01-08 14:44:25 -05:00
|
|
|
export CanShift (Term q d) where s // by = s //. Shift by
|
|
|
|
export CanShift (Elim q d) where e // by = e //. Shift by
|
|
|
|
export CanShift (ScopeTerm q d) where s // by = s //. Shift by
|
2022-04-23 18:21:30 -04:00
|
|
|
|
|
|
|
|
|
|
|
export %inline
|
2023-01-08 14:44:25 -05:00
|
|
|
comp' : DSubst dfrom dto -> TSubst q dfrom from mid -> TSubst q dto mid to ->
|
|
|
|
TSubst q dto from to
|
2022-04-23 18:21:30 -04:00
|
|
|
comp' th ps ph = map (/// th) ps . ph
|
|
|
|
|
|
|
|
|
|
|
|
export
|
2023-01-08 14:44:25 -05:00
|
|
|
fromDScopeTerm : DScopeTerm q d n -> Term q (S d) n
|
2022-04-23 18:21:30 -04:00
|
|
|
fromDScopeTerm (DUsed body) = body
|
|
|
|
fromDScopeTerm (DUnused body) = body /// shift 1
|
|
|
|
|
|
|
|
export
|
2023-01-08 14:44:25 -05:00
|
|
|
fromScopeTerm : ScopeTerm q d n -> Term q d (S n)
|
2022-04-23 18:21:30 -04:00
|
|
|
fromScopeTerm (TUsed body) = body
|
|
|
|
fromScopeTerm (TUnused body) = body //. shift 1
|