lean4-htt/src/Lean/Meta/Eval.lean
Leonardo de Moura 98b8e300e1 feat: add evalTerm and Meta.evalExpr
These functions were in Mathlib 4.
2022-06-28 19:14:40 -07:00

34 lines
1.2 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/-
Copyright (c) 2022 Sebastian Ullrich. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sebastian Ullrich, Leonardo de Moura
-/
import Lean.Meta.Check
namespace Lean.Meta
unsafe def evalExprCore (α) (value : Expr) (checkType : Expr → MetaM Unit) (safety := DefinitionSafety.safe) : MetaM α :=
withoutModifyingEnv do
let name ← mkFreshUserName `_tmp
let type ← inferType value
checkType type
let decl := Declaration.defnDecl {
name, levelParams := [], type
value, hints := ReducibilityHints.opaque,
safety
}
addAndCompile decl
evalConst α name
unsafe def evalExpr' (α) (typeName : Name) (value : Expr) (safety := DefinitionSafety.safe) : MetaM α :=
evalExprCore (safety := safety) α value fun type => do
let type ← whnfD type
unless type.isConstOf typeName do
throwError "unexpected type at evalExpr{indentExpr type}"
unsafe def evalExpr (α) (expectedType : Expr) (value : Expr) (safety := DefinitionSafety.safe) : MetaM α :=
evalExprCore (safety := safety) α value fun type => do
unless ← isDefEq type expectedType do
throwError "unexpected type at `evalExpr` {← mkHasTypeButIsExpectedMsg type expectedType}"
end Lean.Meta