Skip to content

Commit dc615a3

Browse files
committed
feat: meta code for the category_theory port (#755)
Co-authored-by: Scott Morrison <scott.morrison@gmail.com>
1 parent 3a3b30d commit dc615a3

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

Mathlib.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ import Mathlib.Lean.Expr.ReplaceRec
145145
import Mathlib.Lean.Expr.Traverse
146146
import Mathlib.Lean.LocalContext
147147
import Mathlib.Lean.Meta
148+
import Mathlib.Lean.Meta.Simp
148149
import Mathlib.Logic.Basic
149150
import Mathlib.Logic.Embedding.Basic
150151
import Mathlib.Logic.Equiv.Basic

Mathlib/Lean/Meta.lean

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,27 @@ def existsi (mvar : MVarId) (es : List Expr) : MetaM MVarId := do
5454

5555
end Lean.MVarId
5656

57+
namespace Lean.Meta
58+
59+
/--
60+
Given a monadic function `F` that takes a type and a term of that type and produces a new term,
61+
lifts this to the monadic function that opens a `∀` telescope, applies `F` to the body,
62+
and then builds the lambda telescope term for the new term.
63+
-/
64+
def mapForallTelescope' (F : Expr → Expr → MetaM Expr) (forallTerm : Expr) : MetaM Expr := do
65+
forallTelescope (← Meta.inferType forallTerm) fun xs ty => do
66+
Meta.mkLambdaFVars xs (← F ty (mkAppN forallTerm xs))
67+
68+
/--
69+
Given a monadic function `F` that takes a term and produces a new term,
70+
lifts this to the monadic function that opens a `∀` telescope, applies `F` to the body,
71+
and then builds the lambda telescope term for the new term.
72+
-/
73+
def mapForallTelescope (F : Expr → MetaM Expr) (forallTerm : Expr) : MetaM Expr := do
74+
mapForallTelescope' (fun _ e => F e) forallTerm
75+
76+
end Lean.Meta
77+
5778
namespace Lean.Elab.Tactic
5879

5980
/-- Analogue of `liftMetaTactic` for tactics that do not return any goals. -/

Mathlib/Lean/Meta/Simp.lean

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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

Comments
 (0)