quox/src/Quox/Typechecker.idr

135 lines
4.2 KiB
Idris
Raw Normal View History

2022-04-23 18:21:30 -04:00
module Quox.Typechecker
import public Quox.Syntax
import public Quox.Typing
import Quox.Error
%default total
private covering %inline
expectTYPE : MonadThrow Typing.Error m => Term d n -> m Universe
expectTYPE s =
case (whnfT s).fst of
TYPE l => pure l
_ => throw $ ExpectedTYPE s
private covering %inline
expectPi : MonadThrow Typing.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)
_ => throw $ ExpectedPi ty
2022-04-23 18:21:30 -04:00
private %inline
expectEqualQ : MonadThrow Equal.Error m =>
(expect, actual : Qty) -> m ()
expectEqualQ pi rh = unless (pi == rh) $ throw $ ClashQ pi rh
private %inline
popQ : MonadThrow Equal.Error m => Qty -> QOutput (S n) -> m (QOutput n)
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
mutual
-- [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
2022-04-27 15:58:09 -04:00
--
-- [todo] probably need to check that pi is 1 or 0 like atkey said
2022-04-23 18:21:30 -04:00
export covering %inline
check : MonadThrows [Typing.Error, Equal.Error] m => {d, n : Nat} ->
2022-04-27 15:58:09 -04:00
(ctx : TyContext d n) -> (pi : Qty) ->
(subj : Term d n) -> (ty : Term d n) ->
2022-04-23 18:21:30 -04:00
m (CheckResult n)
2022-04-27 15:58:09 -04:00
check ctx pi subj ty = check' ctx pi (pushSubstsT subj) ty
2022-04-23 18:21:30 -04:00
export covering %inline
infer : MonadThrows [Typing.Error, Equal.Error] m => {d, n : Nat} ->
2022-04-27 15:58:09 -04:00
(ctx : TyContext d n) -> (pi : Qty) -> (subj : Elim d n) ->
2022-04-23 18:21:30 -04:00
m (InferResult d n)
2022-04-27 15:58:09 -04:00
infer ctx pi subj = infer' ctx pi (pushSubstsE subj)
2022-04-23 18:21:30 -04:00
export covering
check' : MonadThrows [Typing.Error, Equal.Error] m => {d, n : Nat} ->
2022-04-27 15:58:09 -04:00
(ctx : TyContext d n) -> (pi : Qty) ->
2022-04-23 18:21:30 -04:00
(subj : NotCloTerm d n) -> (ty : Term d n) ->
m (CheckResult n)
2022-04-27 15:58:09 -04:00
check' ctx pi (Element (TYPE l) _) ty = do
2022-04-23 18:21:30 -04:00
l' <- expectTYPE ty
2022-04-27 15:58:09 -04:00
expectEqualQ zero pi
2022-04-23 18:21:30 -04:00
unless (l < l') $ throw $ BadUniverse l l'
2022-04-27 15:58:09 -04:00
pure zero
2022-04-23 18:21:30 -04:00
-- [todo] factor this stuff out
2022-04-27 15:58:09 -04:00
check' ctx pi (Element (Pi qty x arg res) _) ty = do
2022-04-23 18:21:30 -04:00
l <- expectTYPE ty
2022-04-27 15:58:09 -04:00
expectEqualQ zero pi
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
check' ctx pi (Element (Lam x body) _) ty = do
(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 15:58:09 -04:00
qout <- check (extendTy arg (pi * qty) ctx) pi body res
popQ qty qout
check' ctx pi (Element (E e) _) ty = do
infres <- infer ctx pi e
ignore $ check ctx zero ty (TYPE UAny)
2022-04-23 18:21:30 -04:00
infres.type `subT` ty
2022-04-27 15:58:09 -04:00
pure infres.qout
2022-04-23 18:21:30 -04:00
export covering
infer' : MonadThrows [Typing.Error, Equal.Error] m => {d, n : Nat} ->
2022-04-27 15:58:09 -04:00
(ctx : TyContext d n) -> (pi : Qty) -> (subj : NotCloElim d n) ->
2022-04-23 18:21:30 -04:00
m (InferResult d n)
2022-04-27 15:58:09 -04:00
infer' ctx pi (Element (F x) _) =
-- [todo] check that global is erased ==> pi = zero
2022-04-23 18:21:30 -04:00
case lookup x ctx.globals of
2022-04-27 15:58:09 -04:00
Just g => pure $ InfRes {type = g.type, qout = zero}
2022-04-23 18:21:30 -04:00
Nothing => throw $ NotInScope x
2022-04-27 15:58:09 -04:00
infer' ctx pi (Element (B i) _) =
pure $ lookupBound pi i ctx
infer' ctx pi (Element (fun :@ arg) _) = do
funres <- infer ctx pi fun
(qty, argty, res) <- expectPi funres.type
argout <- check ctx (pi * qty) arg argty
pure $ InfRes {type = fromScopeTerm res //. ((arg :# argty) ::: id),
qout = funres.qout + argout}
infer' ctx pi (Element (tm :# ty) _) = do
ignore $ check ctx zero ty (TYPE UAny)
qout <- check ctx pi tm ty
pure $ InfRes {type = ty, qout}