|
| 1 | +/- |
| 2 | +Copyright (c) 2022 Scott Morrison. All rights reserved. |
| 3 | +Released under Apache 2.0 license as described in the file LICENSE. |
| 4 | +Authors: Scott Morrison |
| 5 | +-/ |
| 6 | +import Lean |
| 7 | + |
| 8 | +/-! |
| 9 | +# Helper functions for using the simplifier. |
| 10 | +-/ |
| 11 | + |
| 12 | +open Lean Elab.Tactic |
| 13 | + |
| 14 | +namespace Lean.Meta |
| 15 | + |
| 16 | +/-- Construct a `SimpTheorems` from a list of names. (i.e. as with `simp only`). -/ |
| 17 | +def simpTheoremsOfNames (lemmas : List Name) : MetaM SimpTheorems := do |
| 18 | + lemmas.foldlM (·.addConst ·) (← simpOnlyBuiltins.foldlM (·.addConst ·) {}) |
| 19 | + |
| 20 | +/-- Simplify an expression using only a list of lemmas specified by name. -/ |
| 21 | +-- TODO We need to write a `mkSimpContext` in `MetaM` |
| 22 | +-- that supports all the bells and whistles in `simp`. |
| 23 | +-- It should generalize this, and another partial implementation in `Tactic.Simps.Basic`. |
| 24 | +def simpOnlyNames (lemmas : List Name) (e : Expr) : MetaM Simp.Result := do |
| 25 | + (·.1) <$> simp e |
| 26 | + { simpTheorems := #[← simpTheoremsOfNames lemmas], congrTheorems := ← getSimpCongrTheorems } |
| 27 | + |
| 28 | +/-- |
| 29 | +Given a simplifier `S : Expr → MetaM Simp.Result`, |
| 30 | +and an expression `e : Expr`, run `S` on the type of `e`, and then |
| 31 | +convert `e` into that simplified type, using a combination of type hints and `Eq.mp`. |
| 32 | +-/ |
| 33 | +def simpType (S : Expr → MetaM Simp.Result) (e : Expr) : MetaM Expr := do |
| 34 | + match (← S (← inferType e)) with |
| 35 | + | ⟨ty', none, _⟩ => mkExpectedTypeHint e ty' |
| 36 | + -- We use `mkExpectedTypeHint` in this branch as well, in order to preserve the binder types. |
| 37 | + | ⟨ty', some prf, _⟩ => mkExpectedTypeHint (← mkEqMP prf e) ty' |
| 38 | + |
| 39 | +end Lean.Meta |
0 commit comments