quox/lib/Quox/Syntax/Subst.idr

172 lines
4.4 KiB
Idris
Raw Permalink Normal View History

2021-07-20 16:05:19 -04:00
module Quox.Syntax.Subst
2023-07-12 16:56:35 -04:00
import Quox.Thin
import Quox.Loc
2021-07-20 16:05:19 -04:00
2023-07-12 16:56:35 -04:00
import Data.DPair
2021-07-20 16:05:19 -04:00
import Data.List
2023-03-26 10:09:47 -04:00
import Data.SnocVect
import Derive.Prelude
2021-07-20 16:05:19 -04:00
%default total
%language ElabReflection
2021-07-20 16:05:19 -04:00
public export
2023-07-12 16:56:35 -04:00
Subst : (Nat -> Type) -> Nat -> Nat -> Type
Subst env from to = SnocVect from (Lazy (Thinned env to))
2021-07-20 16:05:19 -04:00
public export
2023-07-12 16:56:35 -04:00
Subst2 : (Nat -> Nat -> Type) -> Nat -> Nat -> Nat -> Type
Subst2 env d from to = SnocVect from (Lazy (Thinned2 env d to))
2021-07-20 16:05:19 -04:00
2023-07-12 16:56:35 -04:00
public export
get : Subst env f t -> Fin f -> Thinned env t
get (sx :< x) FZ = x
get (sx :< x) (FS i) = get sx i
2021-07-20 16:05:19 -04:00
public export
2023-07-12 16:56:35 -04:00
interface FromVar (0 term : Nat -> Type) where
var : Loc -> term 1
2021-07-20 16:05:19 -04:00
public export
2023-07-12 16:56:35 -04:00
0 FromVar2 : (Nat -> Nat -> Type) -> Type
FromVar2 t = FromVar (t 0)
2023-05-01 21:06:25 -04:00
2023-07-12 16:56:35 -04:00
public export
varT : FromVar term => Fin n -> Loc -> Thinned term n
varT i loc = Th (one' i) (var loc)
2021-07-20 16:05:19 -04:00
public export
2023-07-12 16:56:35 -04:00
varT2 : FromVar2 term => Fin n -> Loc -> Thinned2 term d n
varT2 i loc = Th2 zero (one' i) (var loc)
2021-07-20 16:05:19 -04:00
2023-07-12 16:56:35 -04:00
infixl 8 //
namespace CanSubstSelf
public export
interface FromVar term => CanSubstSelf term where
(//) : {f : Nat} -> Thinned term f -> Subst term f t -> Thinned term t
2021-07-20 16:05:19 -04:00
2023-07-12 16:56:35 -04:00
namespace CanSubstSelf2
public export
interface FromVar2 term => CanSubstSelf2 term where
(//) : {f : Nat} -> Thinned2 term d f ->
Subst2 term d f t -> Thinned2 term d t
2021-07-20 16:05:19 -04:00
2023-07-12 16:56:35 -04:00
public export
(.) : {mid : Nat} -> CanSubstSelf f =>
Subst f from mid -> Subst f mid to -> Subst f from to
th . ph = map (\(Delay x) => x // ph) th
2023-07-12 16:56:35 -04:00
infixr 9 .%
2021-07-20 16:05:19 -04:00
public export
2023-07-12 16:56:35 -04:00
(.%) : {mid : Nat} -> CanSubstSelf2 f =>
Subst2 f d from mid -> Subst2 f d mid to -> Subst2 f d from to
th .% ph = map (\(Delay x) => x // ph) th
2021-07-20 16:05:19 -04:00
public export
2023-07-12 16:56:35 -04:00
tabulate : (n : Nat) -> SnocVect n (Fin n)
tabulate n = go n id where
go : (n : Nat) -> (Fin n -> Fin n') -> SnocVect n (Fin n')
go 0 f = [<]
go (S n) f = go n (f . FS) :< f FZ
2021-07-20 16:05:19 -04:00
public export
2023-07-12 16:56:35 -04:00
id : FromVar term => {n : Nat} -> (under : Nat) -> Loc ->
Subst term n (n + under)
id under loc =
map (\i => delay $ varT (weakenN under i) loc) (tabulate n)
2021-07-20 16:05:19 -04:00
2023-07-12 16:56:35 -04:00
public export
id2 : FromVar2 term => {n : Nat} -> Loc -> Subst2 term d n n
id2 loc = map (\i => delay $ varT2 i loc) $ tabulate n
2021-07-20 16:05:19 -04:00
2023-07-12 16:56:35 -04:00
export
select : {n, mask : Nat} -> (0 ope : OPE m n mask) ->
SnocVect n a -> SnocVect m a
select ope sx with %syntactic (view ope)
select _ [<] | StopV = [<]
select _ (sx :< x) | DropV _ ope = select ope sx
select _ (sx :< x) | KeepV _ ope = select ope sx :< x
2021-07-20 16:05:19 -04:00
2023-07-12 16:56:35 -04:00
export
opeToFins : {n, mask : Nat} ->
(0 ope : OPE m n mask) -> SnocVect m (Fin n)
opeToFins ope = select ope $ tabulate n
export
shift : FromVar term => {from : Nat} ->
(n : Nat) -> Loc -> Subst term from (n + from)
shift n loc = map (\i => delay $ varT (shift n i) loc) $ tabulate from
2023-01-22 21:22:50 -05:00
2021-09-09 17:56:10 -04:00
public export
2023-07-12 16:56:35 -04:00
pushN : CanSubstSelf term => {to : Nat} -> (by : Nat) ->
Subst term from to -> Loc -> Subst term (by + from) (by + to)
pushN by th loc =
rewrite plusCommutative by from in
(th . shift by loc) ++ id to loc
2021-09-09 17:56:10 -04:00
2023-07-12 16:56:35 -04:00
public export %inline
push : CanSubstSelf f => {to : Nat} ->
Subst f from to -> Loc -> Subst f (S from) (S to)
push = pushN 1
2021-07-20 16:05:19 -04:00
2023-01-26 13:54:46 -05:00
2022-02-26 19:46:44 -05:00
public export %inline
2023-07-12 16:56:35 -04:00
one : Thinned f n -> Subst f 1 n
one x = [< x]
2022-02-26 19:46:44 -05:00
2023-07-12 16:56:35 -04:00
||| whether two substitutions with the same codomain have the same domain
export
2023-07-12 16:56:35 -04:00
cmpShape : SnocVect m a -> SnocVect n a -> Either Ordering (m = n)
cmpShape [<] [<] = Right Refl
cmpShape [<] (sx :< _) = Left LT
cmpShape (sx :< _) [<] = Left GT
cmpShape (sx :< _) (sy :< _) = cong S <$> cmpShape sx sy
public export
record WithSubst tm env n where
constructor Sub
term : tm from
2023-07-12 16:56:35 -04:00
subst : Subst env from n
2023-07-12 16:56:35 -04:00
{-
export
2023-07-12 16:56:35 -04:00
(forall n. Eq (env n), forall n. Eq (tm n)) =>
Eq (WithSubst tm env n) where
Sub t1 s1 == Sub t2 s2 =
2023-05-01 21:06:25 -04:00
case cmpShape s1 s2 of
Left _ => False
2023-07-12 16:56:35 -04:00
Right Refl =>
t1 == t2 && concat @{All} (zipWith ((==) `on` force) s1 s2)
2023-05-01 21:06:25 -04:00
export
2023-07-12 16:56:35 -04:00
(forall n. Ord (env n), forall n. Ord (tm n)) =>
Ord (WithSubst tm env n) where
2023-05-01 21:06:25 -04:00
Sub t1 s1 `compare` Sub t2 s2 =
case cmpShape s1 s2 of
Left o => o
2023-07-12 16:56:35 -04:00
Right Refl =>
compare t1 t2 <+> concat (zipWith (compare `on` force) s1 s2)
export %hint
2023-07-12 16:56:35 -04:00
ShowWithSubst : {n : Nat} ->
(forall n. Show (env n), forall n. Show (tm n)) =>
Show (WithSubst tm env n)
2023-07-12 16:56:35 -04:00
ShowWithSubst = deriveShow where
Show (Lazy (Thinned env n)) where showPrec d = showPrec d . force
-}
public export
record WithSubst2 tm env d n where
constructor Sub2
term : tm d from
subst : Subst2 env d from n