Skip to content

Commit

Permalink
feat: expose Kernel.check for debugging purposes
Browse files Browse the repository at this point in the history
along `Kernel.isDefEq` and `Kernel.whnf`.
  • Loading branch information
nomeata committed Sep 21, 2024
1 parent a6830f9 commit 86d2e0d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Lean/Environment.lean
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,13 @@ def isDefEqGuarded (env : Environment) (lctx : LocalContext) (a b : Expr) : Bool
@[extern "lean_kernel_whnf"]
opaque whnf (env : Environment) (lctx : LocalContext) (a : Expr) : Except KernelException Expr

/--
Kernel typecheck function. We use it mainly for debugging purposes.
Recall that the Kernel type checker does not support metavariables.
When implementing automation, consider using the `MetaM` methods. -/
@[extern "lean_kernel_check"]
opaque check (env : Environment) (lctx : LocalContext) (a : Expr) : Except KernelException Expr

end Kernel

class MonadEnv (m : TypeType) where
Expand Down
6 changes: 6 additions & 0 deletions src/kernel/type_checker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1203,6 +1203,12 @@ extern "C" LEAN_EXPORT lean_object * lean_kernel_whnf(lean_object * env, lean_ob
});
}

extern "C" LEAN_EXPORT lean_object * lean_kernel_check(lean_object * env, lean_object * lctx, lean_object * a) {
return catch_kernel_exceptions<object*>([&]() {
return type_checker(environment(env), local_ctx(lctx)).check_ignore_undefined_universes(expr(a)).steal();
});
}

inline static expr * new_persistent_expr_const(name const & n) {
expr * e = new expr(mk_const(n));
mark_persistent(e->raw());
Expand Down
10 changes: 10 additions & 0 deletions tests/lean/run/kernel2.lean
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ def whnf (a : Name) : CoreM Unit := do
let r ← ofExceptKernelException (Kernel.whnf env {} a)
IO.println (toString a ++ " ==> " ++ toString r)

def check (a : Name) : CoreM Unit := do
let env ← getEnv
let a := mkConst a
let r ← ofExceptKernelException (Kernel.check env {} a)
IO.println (toString a ++ " ==> " ++ toString r)

partial def fact : Nat → Nat
| 0 => 1
| (n+1) => (n+1)*fact n
Expand Down Expand Up @@ -93,3 +99,7 @@ def c12 : Nat := 'a'.toNat
/-- info: c12 ==> 97 -/
#guard_msgs in
#eval whnf `c12

/-- info: c11 ==> Bool -/
#guard_msgs in
#eval check `c11

0 comments on commit 86d2e0d

Please sign in to comment.