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-03-13 16:41:57 -04:00
|
|
|
|
2023-03-13 22:22:26 -04:00
|
|
|
%default total
|
|
|
|
|
2023-03-13 16:41:57 -04:00
|
|
|
|
2023-03-15 10:54:51 -04:00
|
|
|
public export
|
|
|
|
QContext : Type -> Nat -> Type
|
|
|
|
QContext = Context'
|
|
|
|
|
2023-03-13 16:41:57 -04:00
|
|
|
public export
|
|
|
|
TContext : Type -> Nat -> Nat -> Type
|
|
|
|
TContext q d = Context (Term q d)
|
|
|
|
|
|
|
|
public export
|
|
|
|
QOutput : Type -> Nat -> Type
|
|
|
|
QOutput = Context'
|
|
|
|
|
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
|
|
|
|
record TyContext q d n where
|
|
|
|
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
|
|
|
|
dnames : NContext d
|
|
|
|
tctx : TContext q d n
|
|
|
|
tnames : NContext n
|
2023-03-15 10:54:51 -04:00
|
|
|
qtys : QContext q n -- only used for printing
|
2023-03-13 16:41:57 -04:00
|
|
|
%name TyContext ctx
|
2023-03-13 22:22:26 -04:00
|
|
|
|
|
|
|
|
|
|
|
public export
|
|
|
|
record EqContext q n where
|
|
|
|
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
|
|
|
|
dnames : NContext dimLen -- only used for printing
|
2023-03-13 22:22:26 -04:00
|
|
|
tctx : TContext q 0 n
|
|
|
|
tnames : NContext n
|
2023-03-15 10:54:51 -04:00
|
|
|
qtys : QContext q n -- only used for printing
|
2023-03-13 22:22:26 -04:00
|
|
|
%name EqContext ctx
|
|
|
|
|
|
|
|
|
|
|
|
namespace TContext
|
|
|
|
export %inline
|
|
|
|
pushD : TContext q d n -> TContext q (S d) n
|
|
|
|
pushD tel = map (// shift 1) tel
|
|
|
|
|
|
|
|
export %inline
|
|
|
|
zeroFor : IsQty q => Context tm n -> QOutput q n
|
|
|
|
zeroFor ctx = zero <$ ctx
|
|
|
|
|
2023-03-25 17:41:30 -04:00
|
|
|
private
|
|
|
|
extendLen : Telescope a from to -> Singleton from -> Singleton to
|
|
|
|
extendLen [<] x = x
|
|
|
|
extendLen (tel :< _) x = [|S $ extendLen tel x|]
|
|
|
|
|
2023-03-13 22:22:26 -04:00
|
|
|
namespace TyContext
|
|
|
|
public export %inline
|
|
|
|
empty : TyContext q 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
|
|
|
|
|
|
|
export %inline
|
2023-03-15 10:54:51 -04:00
|
|
|
extendTyN : Telescope (\n => (q, BaseName, Term q d n)) from to ->
|
2023-03-13 22:22:26 -04:00
|
|
|
TyContext q d from -> TyContext q d to
|
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
|
|
|
|
|
|
|
export %inline
|
2023-03-15 10:54:51 -04:00
|
|
|
extendTy : q -> BaseName -> Term q d n -> TyContext q d n ->
|
|
|
|
TyContext q d (S n)
|
|
|
|
extendTy q x s = extendTyN [< (q, x, s)]
|
2023-03-13 22:22:26 -04:00
|
|
|
|
|
|
|
export %inline
|
|
|
|
extendDim : BaseName -> TyContext q d n -> TyContext q (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
|
|
|
|
eqDim : Dim d -> Dim d -> TyContext q d n -> TyContext q d n
|
2023-03-25 17:41:30 -04:00
|
|
|
eqDim p q = {dctx $= set p q, dimLen $= id, termLen $= id}
|
|
|
|
|
|
|
|
export
|
|
|
|
injectT : TyContext q d n -> Term q 0 0 -> Term q d n
|
|
|
|
injectT (MkTyContext {dimLen = Val d, termLen = Val n, _}) tm =
|
|
|
|
tm // shift0 d // shift0 n
|
|
|
|
|
|
|
|
export
|
|
|
|
injectE : TyContext q d n -> Elim q 0 0 -> Elim q d n
|
|
|
|
injectE (MkTyContext {dimLen = Val d, termLen = Val n, _}) el =
|
|
|
|
el // shift0 d // shift0 n
|
2023-03-13 22:22:26 -04:00
|
|
|
|
|
|
|
|
|
|
|
namespace QOutput
|
|
|
|
parameters {auto _ : IsQty q}
|
|
|
|
export %inline
|
|
|
|
(+) : QOutput q n -> QOutput q n -> QOutput q n
|
|
|
|
(+) = zipWith (+)
|
|
|
|
|
|
|
|
export %inline
|
|
|
|
(*) : q -> QOutput q n -> QOutput q n
|
|
|
|
(*) pi = map (pi *)
|
|
|
|
|
|
|
|
export %inline
|
|
|
|
zeroFor : TyContext q _ n -> QOutput q n
|
|
|
|
zeroFor ctx = zeroFor ctx.tctx
|
|
|
|
|
|
|
|
|
|
|
|
export
|
|
|
|
makeDAssign : DSubst d 0 -> DimAssign d
|
|
|
|
makeDAssign (Shift SZ) = [<]
|
|
|
|
makeDAssign (K e ::: th) = makeDAssign th :< e
|
|
|
|
|
|
|
|
export
|
2023-03-15 10:54:51 -04:00
|
|
|
makeEqContext' : {d : Nat} -> TyContext q d n -> DSubst d 0 -> EqContext q n
|
|
|
|
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
|
|
|
|
makeEqContext : TyContext q d n -> DSubst d 0 -> EqContext q n
|
|
|
|
makeEqContext ctx@(MkTyContext {dnames, _}) th =
|
|
|
|
let (d' ** Refl) = lengthPrf0 dnames in makeEqContext' ctx th
|
|
|
|
|
2023-03-13 22:22:26 -04:00
|
|
|
namespace EqContext
|
|
|
|
public export %inline
|
|
|
|
empty : EqContext q 0
|
2023-03-15 10:54:51 -04:00
|
|
|
empty = MkEqContext {
|
|
|
|
dassign = [<], dnames = [<], tctx = [<], tnames = [<], qtys = [<]
|
|
|
|
}
|
2023-03-13 22:22:26 -04:00
|
|
|
|
|
|
|
export %inline
|
2023-03-15 10:54:51 -04:00
|
|
|
extendTyN : Telescope (\n => (q, BaseName, Term q 0 n)) from to ->
|
2023-03-13 22:22:26 -04:00
|
|
|
EqContext q from -> EqContext q to
|
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
|
|
|
|
|
|
|
export %inline
|
2023-03-15 10:54:51 -04:00
|
|
|
extendTy : q -> BaseName -> Term q 0 n -> EqContext q n -> EqContext q (S n)
|
|
|
|
extendTy q x s = extendTyN [< (q, x, s)]
|
2023-03-13 22:22:26 -04:00
|
|
|
|
|
|
|
export %inline
|
|
|
|
extendDim : BaseName -> DimConst -> EqContext q n -> EqContext q 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-03-15 10:54:51 -04:00
|
|
|
toTyContext : (e : EqContext q n) -> TyContext q e.dimLen n
|
|
|
|
toTyContext (MkEqContext {dimLen, dassign, dnames, tctx, tnames, qtys}) =
|
|
|
|
MkTyContext {
|
|
|
|
dctx = fromGround dassign,
|
|
|
|
tctx = map (// shift0 dimLen) tctx,
|
|
|
|
dnames, tnames, qtys
|
|
|
|
}
|
|
|
|
|
2023-03-25 17:41:30 -04:00
|
|
|
export
|
|
|
|
injectT : EqContext q n -> Term q 0 0 -> Term q 0 n
|
|
|
|
injectT (MkEqContext {termLen = Val n, _}) tm = tm // shift0 n
|
|
|
|
|
|
|
|
export
|
|
|
|
injectE : EqContext q n -> Elim q 0 0 -> Elim q 0 n
|
|
|
|
injectE (MkEqContext {termLen = Val n, _}) el = el // shift0 n
|
|
|
|
|
2023-03-15 10:54:51 -04:00
|
|
|
|
2023-03-26 08:41:36 -04:00
|
|
|
private
|
|
|
|
data CtxBinder a = MkCtxBinder BaseName a
|
|
|
|
|
|
|
|
PrettyHL a => PrettyHL (CtxBinder a) where
|
|
|
|
prettyM (MkCtxBinder x t) = pure $
|
|
|
|
sep [hsep [!(pretty0M $ TV x), colonD], !(pretty0M t)]
|
|
|
|
|
2023-03-18 18:27:27 -04:00
|
|
|
parameters {auto _ : (Eq q, PrettyHL q, IsQty q)} (unicode : Bool)
|
2023-03-25 15:54:31 -04:00
|
|
|
private
|
|
|
|
pipeD : Doc HL
|
|
|
|
pipeD = hl Syntax "|"
|
|
|
|
|
2023-03-15 10:54:51 -04:00
|
|
|
export covering
|
|
|
|
prettyTContext : QContext q n -> NContext n -> TContext q d n -> Doc HL
|
|
|
|
prettyTContext qs ns ctx = separate comma $ toList $ go qs ns ctx where
|
|
|
|
go : QContext q m -> NContext m -> TContext q d m -> SnocList (Doc HL)
|
|
|
|
go [<] [<] [<] = [<]
|
|
|
|
go (qs :< q) (xs :< x) (ctx :< t) =
|
2023-03-26 08:41:36 -04:00
|
|
|
let bind = MkWithQty q $ MkCtxBinder x t in
|
2023-03-18 18:27:27 -04:00
|
|
|
go qs xs ctx :< runPretty unicode (pretty0M bind)
|
2023-03-15 10:54:51 -04:00
|
|
|
|
|
|
|
export covering
|
|
|
|
prettyTyContext : TyContext q d n -> Doc HL
|
|
|
|
prettyTyContext (MkTyContext dctx dnames tctx tnames qtys) =
|
2023-03-26 08:41:36 -04:00
|
|
|
case dctx of
|
|
|
|
C [<] => prettyTContext qtys tnames tctx
|
|
|
|
_ => sep [prettyDimEq dnames dctx <++> pipeD,
|
|
|
|
prettyTContext qtys tnames tctx]
|
2023-03-15 10:54:51 -04:00
|
|
|
|
|
|
|
export covering
|
|
|
|
prettyEqContext : EqContext q n -> Doc HL
|
|
|
|
prettyEqContext (MkEqContext dassign dnames tctx tnames qtys) =
|
2023-03-26 08:41:36 -04:00
|
|
|
case dassign of
|
|
|
|
[<] => prettyTContext qtys tnames tctx
|
|
|
|
_ => sep [prettyDimEq dnames (fromGround dassign) <++> pipeD,
|
|
|
|
prettyTContext qtys tnames tctx]
|