2023-08-24 12:42:26 -04:00
|
|
|
module Quox.Whnf.ComputeElimType
|
|
|
|
|
|
|
|
import Quox.Whnf.Interface
|
|
|
|
import Quox.Displace
|
|
|
|
|
|
|
|
%default total
|
|
|
|
|
|
|
|
|
|
|
|
||| performs the minimum work required to recompute the type of an elim.
|
|
|
|
|||
|
|
|
|
||| - assumes the elim is already typechecked
|
|
|
|
||| - the return value is not reduced
|
|
|
|
export covering
|
|
|
|
computeElimType : CanWhnf Term Interface.isRedexT =>
|
|
|
|
CanWhnf Elim Interface.isRedexE =>
|
|
|
|
{d, n : Nat} ->
|
|
|
|
(defs : Definitions) -> WhnfContext d n ->
|
|
|
|
(e : Elim d n) -> (0 ne : No (isRedexE defs e)) =>
|
|
|
|
Eff Whnf (Term d n)
|
2023-09-17 07:54:26 -04:00
|
|
|
|
|
|
|
|
|
|
|
||| computes a type and then reduces it to whnf
|
|
|
|
export covering
|
|
|
|
computeWhnfElimType0 : CanWhnf Term Interface.isRedexT =>
|
|
|
|
CanWhnf Elim Interface.isRedexE =>
|
|
|
|
{d, n : Nat} ->
|
|
|
|
(defs : Definitions) -> WhnfContext d n ->
|
|
|
|
(e : Elim d n) -> (0 ne : No (isRedexE defs e)) =>
|
|
|
|
Eff Whnf (Term d n)
|
|
|
|
|
2023-08-24 12:42:26 -04:00
|
|
|
computeElimType defs ctx e {ne} =
|
|
|
|
case e of
|
2023-09-17 07:54:26 -04:00
|
|
|
F x u loc => do
|
2023-08-24 12:42:26 -04:00
|
|
|
let Just def = lookup x defs
|
|
|
|
| Nothing => throw $ NotInScope loc x
|
2023-08-28 13:59:36 -04:00
|
|
|
pure $ def.typeAt u
|
2023-08-24 12:42:26 -04:00
|
|
|
|
2023-09-17 07:54:26 -04:00
|
|
|
B i _ =>
|
2023-08-24 12:42:26 -04:00
|
|
|
pure $ ctx.tctx !! i
|
|
|
|
|
2023-09-17 07:54:26 -04:00
|
|
|
App f s loc =>
|
|
|
|
case !(computeWhnfElimType0 defs ctx f {ne = noOr1 ne}) of
|
|
|
|
Pi {arg, res, _} => pure $ sub1 res $ Ann s arg loc
|
|
|
|
t => throw $ ExpectedPi loc ctx.names t
|
2023-08-24 12:42:26 -04:00
|
|
|
|
|
|
|
CasePair {pair, ret, _} =>
|
|
|
|
pure $ sub1 ret pair
|
|
|
|
|
|
|
|
CaseEnum {tag, ret, _} =>
|
|
|
|
pure $ sub1 ret tag
|
|
|
|
|
|
|
|
CaseNat {nat, ret, _} =>
|
|
|
|
pure $ sub1 ret nat
|
|
|
|
|
|
|
|
CaseBox {box, ret, _} =>
|
|
|
|
pure $ sub1 ret box
|
|
|
|
|
2023-09-17 07:54:26 -04:00
|
|
|
DApp {fun = f, arg = p, loc} =>
|
|
|
|
case !(computeWhnfElimType0 defs ctx f {ne = noOr1 ne}) of
|
|
|
|
Eq {ty, _} => pure $ dsub1 ty p
|
|
|
|
t => throw $ ExpectedEq loc ctx.names t
|
2023-08-24 12:42:26 -04:00
|
|
|
|
|
|
|
Ann {ty, _} =>
|
|
|
|
pure ty
|
|
|
|
|
|
|
|
Coe {ty, q, _} =>
|
|
|
|
pure $ dsub1 ty q
|
|
|
|
|
|
|
|
Comp {ty, _} =>
|
|
|
|
pure ty
|
|
|
|
|
|
|
|
TypeCase {ret, _} =>
|
|
|
|
pure ret
|
2023-09-17 07:54:26 -04:00
|
|
|
|
|
|
|
computeWhnfElimType0 defs ctx e =
|
|
|
|
computeElimType defs ctx e >>= whnf0 defs ctx
|