2023-03-13 16:41:57 -04:00
|
|
|
module Quox.Typing.Context
|
|
|
|
|
|
|
|
import Quox.Syntax
|
|
|
|
import Quox.Context
|
2023-03-15 10:54:51 -04:00
|
|
|
import Quox.Pretty
|
2023-03-25 17:41:30 -04:00
|
|
|
import public Data.Singleton
|
2023-09-17 13:09:54 -04:00
|
|
|
import Derive.Prelude
|
2023-03-13 16:41:57 -04:00
|
|
|
|
2023-03-13 22:22:26 -04:00
|
|
|
%default total
|
2023-09-17 13:09:54 -04:00
|
|
|
%language ElabReflection
|
2023-03-13 22:22:26 -04:00
|
|
|
|
2023-03-13 16:41:57 -04:00
|
|
|
|
2023-03-15 10:54:51 -04:00
|
|
|
public export
|
2023-04-01 13:16:43 -04:00
|
|
|
QContext : Nat -> Type
|
|
|
|
QContext = Context' Qty
|
2023-03-15 10:54:51 -04:00
|
|
|
|
2023-03-13 16:41:57 -04:00
|
|
|
public export
|
2023-04-01 13:16:43 -04:00
|
|
|
TContext : TermLike
|
|
|
|
TContext d = Context (Term d)
|
2023-03-13 16:41:57 -04:00
|
|
|
|
|
|
|
public export
|
2023-04-01 13:16:43 -04:00
|
|
|
QOutput : Nat -> Type
|
|
|
|
QOutput = Context' Qty
|
2023-03-13 16:41:57 -04:00
|
|
|
|
2023-03-13 22:22:26 -04:00
|
|
|
public export
|
|
|
|
DimAssign : Nat -> Type
|
|
|
|
DimAssign = Context' DimConst
|
|
|
|
|
2023-03-13 16:41:57 -04:00
|
|
|
|
|
|
|
public export
|
2023-04-01 13:16:43 -04:00
|
|
|
record TyContext d n where
|
2023-03-13 16:41:57 -04:00
|
|
|
constructor MkTyContext
|
2023-03-25 17:41:30 -04:00
|
|
|
{auto dimLen : Singleton d}
|
|
|
|
{auto termLen : Singleton n}
|
2023-03-13 22:22:26 -04:00
|
|
|
dctx : DimEq d
|
2023-05-01 21:06:25 -04:00
|
|
|
dnames : BContext d
|
2023-04-01 13:16:43 -04:00
|
|
|
tctx : TContext d n
|
2023-09-12 03:56:49 -04:00
|
|
|
tnames : BContext n -- only used for printing
|
2023-04-01 13:16:43 -04:00
|
|
|
qtys : QContext n -- only used for printing
|
2023-03-13 16:41:57 -04:00
|
|
|
%name TyContext ctx
|
2023-09-17 13:09:54 -04:00
|
|
|
%runElab deriveIndexed "TyContext" [Show]
|
2023-03-13 22:22:26 -04:00
|
|
|
|
|
|
|
|
|
|
|
public export
|
2023-04-01 13:16:43 -04:00
|
|
|
record EqContext n where
|
2023-03-13 22:22:26 -04:00
|
|
|
constructor MkEqContext
|
2023-03-15 10:54:51 -04:00
|
|
|
{dimLen : Nat}
|
2023-03-25 17:41:30 -04:00
|
|
|
{auto termLen : Singleton n}
|
2023-03-15 10:54:51 -04:00
|
|
|
dassign : DimAssign dimLen -- only used for printing
|
2023-05-01 21:06:25 -04:00
|
|
|
dnames : BContext dimLen -- only used for printing
|
2023-04-01 13:16:43 -04:00
|
|
|
tctx : TContext 0 n
|
2023-09-12 03:56:49 -04:00
|
|
|
tnames : BContext n -- only used for printing
|
2023-04-01 13:16:43 -04:00
|
|
|
qtys : QContext n -- only used for printing
|
2023-03-13 22:22:26 -04:00
|
|
|
%name EqContext ctx
|
2023-09-17 13:09:54 -04:00
|
|
|
%runElab deriveIndexed "EqContext" [Show]
|
2023-03-13 22:22:26 -04:00
|
|
|
|
|
|
|
|
2023-04-15 09:13:01 -04:00
|
|
|
public export
|
|
|
|
record WhnfContext d n where
|
|
|
|
constructor MkWhnfContext
|
2023-10-15 10:23:38 -04:00
|
|
|
{auto dimLen : Singleton d}
|
|
|
|
{auto termLen : Singleton n}
|
2023-05-01 21:06:25 -04:00
|
|
|
dnames : BContext d
|
|
|
|
tnames : BContext n
|
2023-04-15 09:13:01 -04:00
|
|
|
tctx : TContext d n
|
|
|
|
%name WhnfContext ctx
|
2023-09-17 13:09:54 -04:00
|
|
|
%runElab deriveIndexed "WhnfContext" [Show]
|
2023-04-15 09:13:01 -04:00
|
|
|
|
2023-03-13 22:22:26 -04:00
|
|
|
namespace TContext
|
|
|
|
export %inline
|
2023-04-01 13:16:43 -04:00
|
|
|
pushD : TContext d n -> TContext (S d) n
|
2023-03-13 22:22:26 -04:00
|
|
|
pushD tel = map (// shift 1) tel
|
|
|
|
|
|
|
|
export %inline
|
2023-04-01 13:16:43 -04:00
|
|
|
zeroFor : Context tm n -> QOutput n
|
|
|
|
zeroFor ctx = Zero <$ ctx
|
2023-03-13 22:22:26 -04:00
|
|
|
|
2023-09-30 12:37:44 -04:00
|
|
|
public export
|
2023-06-23 12:32:05 -04:00
|
|
|
extendLen : Telescope a n1 n2 -> Singleton n1 -> Singleton n2
|
2023-03-25 17:41:30 -04:00
|
|
|
extendLen [<] x = x
|
|
|
|
extendLen (tel :< _) x = [|S $ extendLen tel x|]
|
|
|
|
|
2023-04-03 11:46:23 -04:00
|
|
|
|
|
|
|
public export
|
|
|
|
CtxExtension : Nat -> Nat -> Nat -> Type
|
2023-05-01 21:06:25 -04:00
|
|
|
CtxExtension d = Telescope ((Qty, BindName,) . Term d)
|
2023-04-03 11:46:23 -04:00
|
|
|
|
2023-09-12 03:56:49 -04:00
|
|
|
public export
|
|
|
|
CtxExtension0 : Nat -> Nat -> Nat -> Type
|
|
|
|
CtxExtension0 d = Telescope ((BindName,) . Term d)
|
|
|
|
|
2023-03-13 22:22:26 -04:00
|
|
|
namespace TyContext
|
|
|
|
public export %inline
|
2023-04-01 13:16:43 -04:00
|
|
|
empty : TyContext 0 0
|
2023-03-15 10:54:51 -04:00
|
|
|
empty =
|
|
|
|
MkTyContext {dctx = new, dnames = [<], tctx = [<], tnames = [<], qtys = [<]}
|
2023-03-13 22:22:26 -04:00
|
|
|
|
2023-03-26 10:10:39 -04:00
|
|
|
public export %inline
|
2023-04-01 13:16:43 -04:00
|
|
|
null : TyContext d n -> Bool
|
2023-03-26 10:10:39 -04:00
|
|
|
null ctx = null ctx.dnames && null ctx.tnames
|
|
|
|
|
2023-03-13 22:22:26 -04:00
|
|
|
export %inline
|
2023-06-23 12:32:05 -04:00
|
|
|
extendTyN : CtxExtension d n1 n2 -> TyContext d n1 -> TyContext d n2
|
2023-03-25 17:41:30 -04:00
|
|
|
extendTyN xss (MkTyContext {termLen, dctx, dnames, tctx, tnames, qtys}) =
|
2023-03-15 10:54:51 -04:00
|
|
|
let (qs, xs, ss) = unzip3 xss in
|
2023-03-25 17:41:30 -04:00
|
|
|
MkTyContext {
|
|
|
|
dctx, dnames,
|
|
|
|
termLen = extendLen xss termLen,
|
|
|
|
tctx = tctx . ss,
|
|
|
|
tnames = tnames . xs,
|
|
|
|
qtys = qtys . qs
|
|
|
|
}
|
2023-03-13 22:22:26 -04:00
|
|
|
|
2023-09-12 03:56:49 -04:00
|
|
|
export %inline
|
|
|
|
extendTyN0 : CtxExtension0 d n1 n2 -> TyContext d n1 -> TyContext d n2
|
|
|
|
extendTyN0 xss = extendTyN (map (Zero,) xss)
|
|
|
|
|
2023-03-13 22:22:26 -04:00
|
|
|
export %inline
|
2023-05-01 21:06:25 -04:00
|
|
|
extendTy : Qty -> BindName -> Term d n -> TyContext d n -> TyContext d (S n)
|
2023-03-15 10:54:51 -04:00
|
|
|
extendTy q x s = extendTyN [< (q, x, s)]
|
2023-03-13 22:22:26 -04:00
|
|
|
|
2023-09-12 03:56:49 -04:00
|
|
|
export %inline
|
|
|
|
extendTy0 : BindName -> Term d n -> TyContext d n -> TyContext d (S n)
|
|
|
|
extendTy0 = extendTy Zero
|
|
|
|
|
2023-03-13 22:22:26 -04:00
|
|
|
export %inline
|
2023-05-01 21:06:25 -04:00
|
|
|
extendDim : BindName -> TyContext d n -> TyContext (S d) n
|
2023-03-25 17:41:30 -04:00
|
|
|
extendDim x (MkTyContext {dimLen, dctx, dnames, tctx, tnames, qtys}) =
|
|
|
|
MkTyContext {
|
|
|
|
dctx = dctx :<? Nothing,
|
|
|
|
dnames = dnames :< x,
|
|
|
|
dimLen = [|S dimLen|],
|
|
|
|
tctx = pushD tctx,
|
|
|
|
tnames, qtys
|
|
|
|
}
|
2023-03-13 22:22:26 -04:00
|
|
|
|
|
|
|
export %inline
|
2023-04-01 13:16:43 -04:00
|
|
|
eqDim : Dim d -> Dim d -> TyContext d n -> TyContext d n
|
2023-03-25 17:41:30 -04:00
|
|
|
eqDim p q = {dctx $= set p q, dimLen $= id, termLen $= id}
|
|
|
|
|
2023-04-15 09:13:01 -04:00
|
|
|
export
|
|
|
|
toWhnfContext : TyContext d n -> WhnfContext d n
|
|
|
|
toWhnfContext (MkTyContext {dnames, tnames, tctx, _}) =
|
|
|
|
MkWhnfContext {dnames, tnames, tctx}
|
|
|
|
|
2023-03-13 22:22:26 -04:00
|
|
|
|
|
|
|
namespace QOutput
|
2023-04-01 13:16:43 -04:00
|
|
|
export %inline
|
|
|
|
(+) : QOutput n -> QOutput n -> QOutput n
|
|
|
|
(+) = zipWith (+)
|
2023-03-13 22:22:26 -04:00
|
|
|
|
2023-04-01 13:16:43 -04:00
|
|
|
export %inline
|
|
|
|
(*) : Qty -> QOutput n -> QOutput n
|
|
|
|
(*) pi = map (pi *)
|
2023-03-13 22:22:26 -04:00
|
|
|
|
2023-04-01 13:16:43 -04:00
|
|
|
export %inline
|
|
|
|
zeroFor : TyContext _ n -> QOutput n
|
|
|
|
zeroFor ctx = zeroFor ctx.tctx
|
2023-03-13 22:22:26 -04:00
|
|
|
|
|
|
|
|
|
|
|
export
|
|
|
|
makeDAssign : DSubst d 0 -> DimAssign d
|
2023-05-01 21:06:25 -04:00
|
|
|
makeDAssign (Shift SZ) = [<]
|
|
|
|
makeDAssign (K e _ ::: th) = makeDAssign th :< e
|
2023-03-13 22:22:26 -04:00
|
|
|
|
|
|
|
export
|
2023-04-01 13:16:43 -04:00
|
|
|
makeEqContext' : {d : Nat} -> TyContext d n -> DSubst d 0 -> EqContext n
|
2023-03-15 10:54:51 -04:00
|
|
|
makeEqContext' ctx th = MkEqContext {
|
2023-03-25 17:41:30 -04:00
|
|
|
termLen = ctx.termLen,
|
2023-03-13 22:22:26 -04:00
|
|
|
dassign = makeDAssign th,
|
|
|
|
dnames = ctx.dnames,
|
|
|
|
tctx = map (// th) ctx.tctx,
|
2023-03-15 10:54:51 -04:00
|
|
|
tnames = ctx.tnames,
|
|
|
|
qtys = ctx.qtys
|
2023-03-13 22:22:26 -04:00
|
|
|
}
|
|
|
|
|
2023-03-15 10:54:51 -04:00
|
|
|
export
|
2023-04-01 13:16:43 -04:00
|
|
|
makeEqContext : TyContext d n -> DSubst d 0 -> EqContext n
|
2023-03-15 10:54:51 -04:00
|
|
|
makeEqContext ctx@(MkTyContext {dnames, _}) th =
|
2023-09-12 03:56:49 -04:00
|
|
|
let Val d = lengthPrf0 dnames in makeEqContext' ctx th
|
2023-03-15 10:54:51 -04:00
|
|
|
|
2023-03-13 22:22:26 -04:00
|
|
|
namespace EqContext
|
|
|
|
public export %inline
|
2023-04-01 13:16:43 -04:00
|
|
|
empty : EqContext 0
|
2023-03-15 10:54:51 -04:00
|
|
|
empty = MkEqContext {
|
|
|
|
dassign = [<], dnames = [<], tctx = [<], tnames = [<], qtys = [<]
|
|
|
|
}
|
2023-03-13 22:22:26 -04:00
|
|
|
|
2023-03-26 10:10:39 -04:00
|
|
|
public export %inline
|
2023-04-01 13:16:43 -04:00
|
|
|
null : EqContext n -> Bool
|
2023-03-26 10:10:39 -04:00
|
|
|
null ctx = null ctx.dnames && null ctx.tnames
|
|
|
|
|
2023-03-13 22:22:26 -04:00
|
|
|
export %inline
|
2023-06-23 12:32:05 -04:00
|
|
|
extendTyN : CtxExtension 0 n1 n2 -> EqContext n1 -> EqContext n2
|
2023-03-25 17:41:30 -04:00
|
|
|
extendTyN xss (MkEqContext {termLen, dassign, dnames, tctx, tnames, qtys}) =
|
|
|
|
let (qs, xs, ss) = unzip3 xss in
|
|
|
|
MkEqContext {
|
|
|
|
termLen = extendLen xss termLen,
|
|
|
|
tctx = tctx . ss,
|
|
|
|
tnames = tnames . xs,
|
|
|
|
qtys = qtys . qs,
|
|
|
|
dassign, dnames
|
|
|
|
}
|
2023-03-13 22:22:26 -04:00
|
|
|
|
2023-09-12 03:56:49 -04:00
|
|
|
export %inline
|
|
|
|
extendTyN0 : CtxExtension0 0 n1 n2 -> EqContext n1 -> EqContext n2
|
|
|
|
extendTyN0 xss = extendTyN (map (Zero,) xss)
|
|
|
|
|
2023-03-13 22:22:26 -04:00
|
|
|
export %inline
|
2023-05-01 21:06:25 -04:00
|
|
|
extendTy : Qty -> BindName -> Term 0 n -> EqContext n -> EqContext (S n)
|
2023-03-15 10:54:51 -04:00
|
|
|
extendTy q x s = extendTyN [< (q, x, s)]
|
2023-03-13 22:22:26 -04:00
|
|
|
|
2023-09-12 03:56:49 -04:00
|
|
|
export %inline
|
|
|
|
extendTy0 : BindName -> Term 0 n -> EqContext n -> EqContext (S n)
|
|
|
|
extendTy0 = extendTy Zero
|
|
|
|
|
2023-03-13 22:22:26 -04:00
|
|
|
export %inline
|
2023-05-01 21:06:25 -04:00
|
|
|
extendDim : BindName -> DimConst -> EqContext n -> EqContext n
|
2023-03-25 17:41:30 -04:00
|
|
|
extendDim x e (MkEqContext {dassign, dnames, tctx, tnames, qtys}) =
|
|
|
|
MkEqContext {dassign = dassign :< e, dnames = dnames :< x,
|
|
|
|
tctx, tnames, qtys}
|
2023-03-13 22:22:26 -04:00
|
|
|
|
|
|
|
export
|
2023-04-01 13:16:43 -04:00
|
|
|
toTyContext : (e : EqContext n) -> TyContext e.dimLen n
|
2023-03-15 10:54:51 -04:00
|
|
|
toTyContext (MkEqContext {dimLen, dassign, dnames, tctx, tnames, qtys}) =
|
|
|
|
MkTyContext {
|
|
|
|
dctx = fromGround dassign,
|
|
|
|
tctx = map (// shift0 dimLen) tctx,
|
|
|
|
dnames, tnames, qtys
|
|
|
|
}
|
|
|
|
|
2023-04-15 09:13:01 -04:00
|
|
|
export
|
|
|
|
toWhnfContext : (ectx : EqContext n) -> WhnfContext 0 n
|
|
|
|
toWhnfContext (MkEqContext {tnames, tctx, _}) =
|
|
|
|
MkWhnfContext {dnames = [<], tnames, tctx}
|
|
|
|
|
2023-10-15 10:23:38 -04:00
|
|
|
export
|
|
|
|
injElim : WhnfContext d n -> Elim 0 0 -> Elim d n
|
|
|
|
injElim ctx e =
|
|
|
|
let Val d = ctx.dimLen; Val n = ctx.termLen in
|
|
|
|
e // shift0 d // shift0 n
|
|
|
|
|
2023-04-15 09:13:01 -04:00
|
|
|
namespace WhnfContext
|
|
|
|
public export %inline
|
|
|
|
empty : WhnfContext 0 0
|
|
|
|
empty = MkWhnfContext [<] [<] [<]
|
|
|
|
|
|
|
|
export
|
2023-05-01 21:06:25 -04:00
|
|
|
extendDimN : {s : Nat} -> BContext s -> WhnfContext d n ->
|
2023-04-15 09:13:01 -04:00
|
|
|
WhnfContext (s + d) n
|
2023-10-15 10:23:38 -04:00
|
|
|
extendDimN ns (MkWhnfContext {dnames, tnames, tctx, dimLen}) =
|
2023-04-15 09:13:01 -04:00
|
|
|
MkWhnfContext {
|
2023-10-15 10:23:38 -04:00
|
|
|
dimLen = [|Val s + dimLen|],
|
2023-04-15 09:13:01 -04:00
|
|
|
dnames = dnames ++ toSnocVect' ns,
|
|
|
|
tctx = dweakT s <$> tctx,
|
|
|
|
tnames
|
|
|
|
}
|
|
|
|
|
|
|
|
export
|
2023-05-01 21:06:25 -04:00
|
|
|
extendDim : BindName -> WhnfContext d n -> WhnfContext (S d) n
|
2023-04-15 09:13:01 -04:00
|
|
|
extendDim i = extendDimN [< i]
|
|
|
|
|
2023-03-15 10:54:51 -04:00
|
|
|
|
2023-03-26 08:41:36 -04:00
|
|
|
private
|
2023-05-14 13:58:46 -04:00
|
|
|
prettyTContextElt : {opts : _} ->
|
|
|
|
BContext d -> BContext n ->
|
|
|
|
Qty -> BindName -> Term d n -> Eff Pretty (Doc opts)
|
|
|
|
prettyTContextElt dnames tnames q x s =
|
|
|
|
pure $ hsep [hcat [!(prettyQty q), !dotD, !(prettyTBind x)], !colonD,
|
|
|
|
!(withPrec Outer $ prettyTerm dnames tnames s)]
|
|
|
|
|
|
|
|
private
|
|
|
|
prettyTContext' : {opts : _} ->
|
|
|
|
BContext d -> QContext n -> BContext n ->
|
|
|
|
TContext d n -> Eff Pretty (SnocList (Doc opts))
|
|
|
|
prettyTContext' _ [<] [<] [<] = pure [<]
|
|
|
|
prettyTContext' dnames (qtys :< q) (tnames :< x) (tys :< t) =
|
|
|
|
[|prettyTContext' dnames qtys tnames tys :<
|
|
|
|
prettyTContextElt dnames tnames q x t|]
|
|
|
|
|
|
|
|
export
|
|
|
|
prettyTContext : {opts : _} ->
|
|
|
|
BContext d -> QContext n -> BContext n ->
|
|
|
|
TContext d n -> Eff Pretty (Doc opts)
|
|
|
|
prettyTContext dnames qtys tnames tys =
|
|
|
|
separateTight !commaD <$> prettyTContext' dnames qtys tnames tys
|
|
|
|
|
|
|
|
export
|
|
|
|
prettyTyContext : {opts : _} -> TyContext d n -> Eff Pretty (Doc opts)
|
|
|
|
prettyTyContext (MkTyContext dctx dnames tctx tnames qtys) =
|
|
|
|
case dctx of
|
|
|
|
C [<] => prettyTContext dnames qtys tnames tctx
|
|
|
|
_ => pure $
|
|
|
|
sep [!(prettyDimEq dnames dctx) <++> !pipeD,
|
|
|
|
!(prettyTContext dnames qtys tnames tctx)]
|
|
|
|
|
|
|
|
export
|
|
|
|
prettyEqContext : {opts : _} -> EqContext n -> Eff Pretty (Doc opts)
|
|
|
|
prettyEqContext ctx = prettyTyContext $ toTyContext ctx
|