2022-04-23 18:21:30 -04:00
|
|
|
module Quox.Typechecker
|
|
|
|
|
|
|
|
import public Quox.Syntax
|
|
|
|
import public Quox.Typing
|
2022-08-22 23:43:23 -04:00
|
|
|
import public Quox.Equal
|
|
|
|
import public Control.Monad.Either
|
2022-04-23 18:21:30 -04:00
|
|
|
|
|
|
|
%default total
|
|
|
|
|
|
|
|
|
|
|
|
private covering %inline
|
2022-05-06 15:23:58 -04:00
|
|
|
expectTYPE : MonadError Error m => Term d n -> m Universe
|
2022-04-23 18:21:30 -04:00
|
|
|
expectTYPE s =
|
|
|
|
case (whnfT s).fst of
|
|
|
|
TYPE l => pure l
|
2022-05-06 15:23:58 -04:00
|
|
|
_ => throwError $ ExpectedTYPE s
|
2022-04-23 18:21:30 -04:00
|
|
|
|
|
|
|
private covering %inline
|
2022-05-06 15:23:58 -04:00
|
|
|
expectPi : MonadError Error m => Term d n ->
|
2022-04-27 15:58:09 -04:00
|
|
|
m (Qty, Term d n, ScopeTerm d n)
|
2022-04-23 18:21:30 -04:00
|
|
|
expectPi ty =
|
|
|
|
case (whnfT ty).fst of
|
2022-04-27 15:58:09 -04:00
|
|
|
Pi qty _ arg res => pure (qty, arg, res)
|
2022-05-06 15:23:58 -04:00
|
|
|
_ => throwError $ ExpectedPi ty
|
2022-04-23 18:21:30 -04:00
|
|
|
|
|
|
|
private %inline
|
2022-05-06 15:23:58 -04:00
|
|
|
expectEqualQ : MonadError Error m =>
|
2022-04-23 18:21:30 -04:00
|
|
|
(expect, actual : Qty) -> m ()
|
2022-05-06 15:23:58 -04:00
|
|
|
expectEqualQ pi rh =
|
2022-08-22 23:43:23 -04:00
|
|
|
unless (pi == rh) $ throwError $ ClashQ pi rh
|
2022-04-23 18:21:30 -04:00
|
|
|
|
|
|
|
|
|
|
|
private %inline
|
2022-05-06 15:23:58 -04:00
|
|
|
popQ : MonadError Error m => Qty -> QOutput (S n) -> m (QOutput n)
|
2022-04-23 18:21:30 -04:00
|
|
|
popQ pi (qctx :< rh) = expectEqualQ pi rh $> qctx
|
|
|
|
|
|
|
|
|
|
|
|
private %inline
|
|
|
|
tail : TyContext d (S n) -> TyContext d n
|
|
|
|
tail = {tctx $= tail, qctx $= tail}
|
|
|
|
|
|
|
|
|
|
|
|
private %inline
|
|
|
|
weakI : InferResult d n -> InferResult d (S n)
|
2022-04-27 15:58:09 -04:00
|
|
|
weakI = {type $= weakT, qout $= (:< zero)}
|
2022-04-23 18:21:30 -04:00
|
|
|
|
|
|
|
private
|
2022-04-27 15:58:09 -04:00
|
|
|
lookupBound : {n : Nat} -> Qty -> Var n -> TyContext d n -> InferResult d n
|
|
|
|
lookupBound pi VZ (MkTyContext {tctx = _ :< ty, _}) =
|
|
|
|
InfRes {type = weakT ty, qout = zero :< pi}
|
|
|
|
lookupBound pi (VS i) ctx =
|
|
|
|
weakI $ lookupBound pi i (tail ctx)
|
2022-04-23 18:21:30 -04:00
|
|
|
|
|
|
|
|
2022-04-27 16:57:56 -04:00
|
|
|
private %inline
|
|
|
|
subjMult : Qty -> Qty -> Subset Qty IsSubj
|
|
|
|
subjMult sg qty =
|
|
|
|
if sg == Zero || qty == Zero
|
|
|
|
then Element Zero %search
|
|
|
|
else Element One %search
|
|
|
|
|
|
|
|
|
2022-08-22 23:43:23 -04:00
|
|
|
public export
|
|
|
|
CanTC : (Type -> Type) -> Type
|
|
|
|
CanTC m = (MonadError Error m, MonadReader Definitions m)
|
|
|
|
|
|
|
|
parameters {auto _ : CanTC m}
|
|
|
|
mutual
|
2022-04-23 18:21:30 -04:00
|
|
|
-- [todo] it seems like the options here for dealing with substitutions are
|
|
|
|
-- to either push them or parametrise the whole typechecker over ambient
|
|
|
|
-- substitutions. both of them seem like the same amount of work for the
|
|
|
|
-- computer but pushing is less work for the me
|
|
|
|
|
|
|
|
export covering %inline
|
2022-08-22 23:43:23 -04:00
|
|
|
check : {d, n : Nat} ->
|
|
|
|
(ctx : TyContext d n) -> (sg : Qty) -> (0 _ : IsSubj sg) =>
|
2022-04-27 15:58:09 -04:00
|
|
|
(subj : Term d n) -> (ty : Term d n) ->
|
2022-04-23 18:21:30 -04:00
|
|
|
m (CheckResult n)
|
2022-04-27 16:57:56 -04:00
|
|
|
check ctx sg subj ty = check' ctx sg (pushSubstsT subj) ty
|
2022-04-23 18:21:30 -04:00
|
|
|
|
|
|
|
export covering %inline
|
2022-08-22 23:43:23 -04:00
|
|
|
infer : {d, n : Nat} ->
|
|
|
|
(ctx : TyContext d n) -> (sg : Qty) -> (0 _ : IsSubj sg) =>
|
2022-04-27 16:57:56 -04:00
|
|
|
(subj : Elim d n) ->
|
2022-04-23 18:21:30 -04:00
|
|
|
m (InferResult d n)
|
2022-04-27 16:57:56 -04:00
|
|
|
infer ctx sg subj = infer' ctx sg (pushSubstsE subj)
|
2022-04-23 18:21:30 -04:00
|
|
|
|
|
|
|
|
|
|
|
export covering
|
2022-08-22 23:43:23 -04:00
|
|
|
check' : {d, n : Nat} ->
|
|
|
|
(ctx : TyContext d n) -> (sg : Qty) -> (0 _ : IsSubj sg) =>
|
2022-04-23 18:21:30 -04:00
|
|
|
(subj : NotCloTerm d n) -> (ty : Term d n) ->
|
|
|
|
m (CheckResult n)
|
|
|
|
|
2022-04-27 16:57:56 -04:00
|
|
|
check' ctx sg (Element (TYPE l) _) ty = do
|
2022-04-23 18:21:30 -04:00
|
|
|
l' <- expectTYPE ty
|
2022-04-27 16:57:56 -04:00
|
|
|
expectEqualQ zero sg
|
2022-05-06 15:23:58 -04:00
|
|
|
unless (l < l') $ throwError $ BadUniverse l l'
|
2022-04-27 15:58:09 -04:00
|
|
|
pure zero
|
2022-04-23 18:21:30 -04:00
|
|
|
|
2022-04-27 16:57:56 -04:00
|
|
|
check' ctx sg (Element (Pi qty x arg res) _) ty = do
|
2022-04-23 18:21:30 -04:00
|
|
|
l <- expectTYPE ty
|
2022-04-27 16:57:56 -04:00
|
|
|
expectEqualQ zero sg
|
2022-04-27 15:58:09 -04:00
|
|
|
ignore $ check ctx zero arg (TYPE l)
|
|
|
|
case res of
|
|
|
|
TUsed res => ignore $ check (extendTy arg zero ctx) zero res (TYPE l)
|
|
|
|
TUnused res => ignore $ check ctx zero res (TYPE l)
|
|
|
|
pure zero
|
|
|
|
|
2022-04-27 16:57:56 -04:00
|
|
|
check' ctx sg (Element (Lam x body) _) ty = do
|
2022-04-27 15:58:09 -04:00
|
|
|
(qty, arg, res) <- expectPi ty
|
2022-04-23 18:21:30 -04:00
|
|
|
-- [todo] do this properly?
|
|
|
|
let body = fromScopeTerm body; res = fromScopeTerm res
|
2022-04-27 16:57:56 -04:00
|
|
|
qout <- check (extendTy arg (sg * qty) ctx) sg body res
|
2022-04-27 15:58:09 -04:00
|
|
|
popQ qty qout
|
|
|
|
|
2022-04-27 16:57:56 -04:00
|
|
|
check' ctx sg (Element (E e) _) ty = do
|
|
|
|
infres <- infer ctx sg e
|
2022-04-27 15:58:09 -04:00
|
|
|
ignore $ check ctx zero ty (TYPE UAny)
|
2022-08-22 23:43:23 -04:00
|
|
|
subT infres.type ty
|
2022-04-27 15:58:09 -04:00
|
|
|
pure infres.qout
|
2022-04-23 18:21:30 -04:00
|
|
|
|
|
|
|
export covering
|
2022-08-22 23:43:23 -04:00
|
|
|
infer' : {d, n : Nat} ->
|
|
|
|
(ctx : TyContext d n) -> (sg : Qty) -> (0 _ : IsSubj sg) =>
|
2022-04-27 16:57:56 -04:00
|
|
|
(subj : NotCloElim d n) ->
|
2022-04-23 18:21:30 -04:00
|
|
|
m (InferResult d n)
|
|
|
|
|
2022-08-22 23:43:23 -04:00
|
|
|
infer' ctx sg (Element (F x) _) = do
|
|
|
|
Just g <- asks $ lookup x | Nothing => throwError $ NotInScope x
|
|
|
|
when (isZero g) $ expectEqualQ sg Zero
|
2023-01-08 09:07:01 -05:00
|
|
|
pure $ InfRes {type = g.type.get, qout = zero}
|
2022-04-23 18:21:30 -04:00
|
|
|
|
2022-04-27 16:57:56 -04:00
|
|
|
infer' ctx sg (Element (B i) _) =
|
|
|
|
pure $ lookupBound sg i ctx
|
2022-04-27 15:58:09 -04:00
|
|
|
|
2022-04-27 16:57:56 -04:00
|
|
|
infer' ctx sg (Element (fun :@ arg) _) = do
|
|
|
|
funres <- infer ctx sg fun
|
2022-04-27 15:58:09 -04:00
|
|
|
(qty, argty, res) <- expectPi funres.type
|
2022-04-27 16:57:56 -04:00
|
|
|
let Element sg' _ = subjMult sg qty
|
|
|
|
argout <- check ctx sg' arg argty
|
2022-04-27 15:58:09 -04:00
|
|
|
pure $ InfRes {type = fromScopeTerm res //. ((arg :# argty) ::: id),
|
|
|
|
qout = funres.qout + argout}
|
|
|
|
|
2022-04-27 16:57:56 -04:00
|
|
|
infer' ctx sg (Element (tm :# ty) _) = do
|
2022-04-27 15:58:09 -04:00
|
|
|
ignore $ check ctx zero ty (TYPE UAny)
|
2022-04-27 16:57:56 -04:00
|
|
|
qout <- check ctx sg tm ty
|
2022-04-27 15:58:09 -04:00
|
|
|
pure $ InfRes {type = ty, qout}
|