quox/lib/Quox/Typing.idr

150 lines
4.6 KiB
Idris
Raw Normal View History

2021-12-23 13:05:50 -05:00
module Quox.Typing
2023-03-13 16:41:57 -04:00
import public Quox.Typing.Context as Typing
import public Quox.Typing.EqMode as Typing
import public Quox.Typing.Error as Typing
2021-12-23 13:05:50 -05:00
import public Quox.Syntax
2022-08-22 04:17:08 -04:00
import public Quox.Definition
import public Quox.Reduce
2021-12-23 13:05:50 -05:00
2023-04-15 09:13:01 -04:00
import Language.Reflection
2023-03-31 13:23:30 -04:00
import Control.Eff
%default total
2023-04-15 09:13:01 -04:00
%language ElabReflection
private TTName : Type
TTName = TT.Name
%hide TT.Name
2021-12-23 13:05:50 -05:00
2022-04-23 18:21:30 -04:00
public export
2023-04-01 13:16:43 -04:00
CheckResult' : Nat -> Type
2023-02-19 11:51:44 -05:00
CheckResult' = QOutput
2022-04-23 18:21:30 -04:00
public export
2023-04-01 13:16:43 -04:00
CheckResult : DimEq d -> Nat -> Type
CheckResult eqs n = IfConsistent eqs $ CheckResult' n
2023-02-19 11:51:44 -05:00
public export
2023-04-01 13:16:43 -04:00
record InferResult' d n where
2022-04-23 18:21:30 -04:00
constructor InfRes
2023-04-01 13:16:43 -04:00
type : Term d n
qout : QOutput n
2022-04-23 18:21:30 -04:00
2023-02-19 11:51:44 -05:00
public export
InferResult : DimEq d -> TermLike
2023-04-01 13:16:43 -04:00
InferResult eqs d n = IfConsistent eqs $ InferResult' d n
2023-02-19 11:51:44 -05:00
2022-04-23 18:21:30 -04:00
2023-02-10 15:40:44 -05:00
export
lookupFree : Has ErrorEff fs => Name -> Definitions -> Eff fs Definition
lookupFree x defs = maybe (throw $ NotInScope x) pure $ lookup x defs
2023-02-10 15:40:44 -05:00
2023-02-12 15:30:08 -05:00
public export
2023-04-01 13:16:43 -04:00
substCasePairRet : Term d n -> ScopeTerm d n -> Term d (2 + n)
2023-02-10 15:40:44 -05:00
substCasePairRet dty retty =
2023-03-31 13:26:55 -04:00
let arg = Pair (BVT 1) (BVT 0) :# (dty // fromNat 2) in
2023-02-10 15:40:44 -05:00
retty.term // (arg ::: shift 2)
2023-02-11 12:15:50 -05:00
2023-03-26 08:40:54 -04:00
public export
2023-04-01 13:16:43 -04:00
substCaseSuccRet : ScopeTerm d n -> Term d (2 + n)
2023-03-31 13:26:55 -04:00
substCaseSuccRet retty = retty.term // (Succ (BVT 1) :# Nat ::: shift 2)
2023-03-26 08:40:54 -04:00
2023-03-31 13:11:35 -04:00
public export
2023-04-01 13:16:43 -04:00
substCaseBoxRet : Term d n -> ScopeTerm d n -> Term d (S n)
2023-03-31 13:11:35 -04:00
substCaseBoxRet dty retty =
2023-04-15 09:13:01 -04:00
retty.term // (Box (BVT 0) :# weakT 1 dty ::: shift 1)
2023-02-11 12:15:50 -05:00
2023-03-31 13:23:30 -04:00
parameters (defs : Definitions) {auto _ : Has ErrorEff fs}
2023-04-15 09:13:01 -04:00
namespace TyContext
parameters (ctx : TyContext d n)
export covering
whnf : {0 isRedex : RedexTest tm} -> Whnf tm isRedex =>
tm d n -> Eff fs (NonRedex tm d n defs)
2023-04-15 09:13:01 -04:00
whnf = let Val n = ctx.termLen; Val d = ctx.dimLen in
rethrow . whnf defs (toWhnfContext ctx)
2023-04-15 09:13:01 -04:00
private covering %macro
expect : (forall d, n. NameContexts d n -> Term d n -> Error) ->
TTImp -> TTImp -> Elab (Term d n -> Eff fs a)
expect k l r = do
f <- check `(\case ~(l) => Just ~(r); _ => Nothing)
pure $ \t => maybe (throw $ k ctx.names t) pure . f . fst =<< whnf t
2023-02-11 12:15:50 -05:00
export covering %inline
2023-04-01 13:16:43 -04:00
expectTYPE : Term d n -> Eff fs Universe
2023-04-15 09:13:01 -04:00
expectTYPE = expect ExpectedTYPE `(TYPE l) `(l)
export covering %inline
2023-04-01 13:16:43 -04:00
expectPi : Term d n -> Eff fs (Qty, Term d n, ScopeTerm d n)
2023-04-15 09:13:01 -04:00
expectPi = expect ExpectedPi `(Pi {qty, arg, res}) `((qty, arg, res))
export covering %inline
2023-04-01 13:16:43 -04:00
expectSig : Term d n -> Eff fs (Term d n, ScopeTerm d n)
2023-04-15 09:13:01 -04:00
expectSig = expect ExpectedSig `(Sig {fst, snd}) `((fst, snd))
export covering %inline
2023-04-01 13:16:43 -04:00
expectEnum : Term d n -> Eff fs (SortedSet TagVal)
2023-04-15 09:13:01 -04:00
expectEnum = expect ExpectedEnum `(Enum ts) `(ts)
export covering %inline
2023-04-01 13:16:43 -04:00
expectEq : Term d n -> Eff fs (DScopeTerm d n, Term d n, Term d n)
2023-04-15 09:13:01 -04:00
expectEq = expect ExpectedEq `(Eq {ty, l, r}) `((ty, l, r))
2023-03-26 08:40:54 -04:00
export covering %inline
2023-04-01 13:16:43 -04:00
expectNat : Term d n -> Eff fs ()
2023-04-15 09:13:01 -04:00
expectNat = expect ExpectedNat `(Nat) `(())
2023-03-26 08:40:54 -04:00
2023-03-31 13:11:35 -04:00
export covering %inline
2023-04-01 13:16:43 -04:00
expectBOX : Term d n -> Eff fs (Qty, Term d n)
2023-04-15 09:13:01 -04:00
expectBOX = expect ExpectedBOX `(BOX {qty, ty}) `((qty, ty))
namespace EqContext
parameters (ctx : EqContext n)
export covering
whnf : {0 isRedex : RedexTest tm} -> Whnf tm isRedex =>
tm 0 n -> Eff fs (NonRedex tm 0 n defs)
whnf = let Val n = ctx.termLen in rethrow . whnf defs (toWhnfContext ctx)
2023-03-31 13:11:35 -04:00
2023-04-15 09:13:01 -04:00
private covering %macro
expect : (forall d, n. NameContexts d n -> Term d n -> Error) ->
TTImp -> TTImp -> Elab (Term 0 n -> Eff fs a)
expect k l r = do
f <- check `(\case ~(l) => Just ~(r); _ => Nothing)
pure $ \t =>
let err = throw $ k ctx.names (t // shift0 ctx.dimLen) in
maybe err pure . f . fst =<< whnf t
export covering %inline
2023-04-15 09:13:01 -04:00
expectTYPE : Term 0 n -> Eff fs Universe
expectTYPE = expect ExpectedTYPE `(TYPE l) `(l)
export covering %inline
2023-04-15 09:13:01 -04:00
expectPi : Term 0 n -> Eff fs (Qty, Term 0 n, ScopeTerm 0 n)
expectPi = expect ExpectedPi `(Pi {qty, arg, res}) `((qty, arg, res))
export covering %inline
2023-04-15 09:13:01 -04:00
expectSig : Term 0 n -> Eff fs (Term 0 n, ScopeTerm 0 n)
expectSig = expect ExpectedSig `(Sig {fst, snd}) `((fst, snd))
export covering %inline
2023-04-15 09:13:01 -04:00
expectEnum : Term 0 n -> Eff fs (SortedSet TagVal)
expectEnum = expect ExpectedEnum `(Enum ts) `(ts)
export covering %inline
2023-04-15 09:13:01 -04:00
expectEq : Term 0 n -> Eff fs (DScopeTerm 0 n, Term 0 n, Term 0 n)
expectEq = expect ExpectedEq `(Eq {ty, l, r}) `((ty, l, r))
2023-03-26 08:40:54 -04:00
export covering %inline
2023-04-15 09:13:01 -04:00
expectNat : Term 0 n -> Eff fs ()
expectNat = expect ExpectedNat `(Nat) `(())
2023-03-31 13:11:35 -04:00
export covering %inline
2023-04-15 09:13:01 -04:00
expectBOX : Term 0 n -> Eff fs (Qty, Term 0 n)
expectBOX = expect ExpectedBOX `(BOX {qty, ty}) `((qty, ty))