Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - feat: (↑) notation for coercions #199

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Mathlib/Data/Subtype.lean
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ f x
lemma restrict_apply {α} {β : α → Type _} (f : ∀x, β x) (p : α → Prop) (x : Subtype p) :
restrict f p x = f x.1 := rfl

lemma restrict_def {α β} (f : α → β) (p : α → Prop) : restrict f p = f ∘ CoeHead.coe := rfl
lemma restrict_def {α β} (f : α → β) (p : α → Prop) : restrict f p = f ∘ (↑) := rfl

lemma restrict_injective {α β} {f : α → β} (p : α → Prop) (h : injective f) :
injective (restrict f p) :=
Expand Down
26 changes: 15 additions & 11 deletions Mathlib/Tactic/Coe.lean
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@ Author: Gabriel Ebner
-/
import Lean

open Lean Elab Term
open Lean Elab Term Meta

/-!
Redefine the ↑-notation to elaborate in the same way as type annotations
(i.e., unfolding the coercion instance).
Define a `(↑)` notation for coercions equivalent to the eta-reduction of `(↑ ·)`.
-/

namespace Lean.Elab.Term.CoeImpl

scoped elab "coe%" x:term : term <= expectedType => do
tryPostponeIfMVar expectedType
let x ← elabTerm x none
synthesizeSyntheticMVarsUsingDefault
ensureHasType expectedType x

macro_rules
| `(↑ $x) => `(coe% $x)
elab "(" "↑" ")" : term <= expectedType => do
let expectedType ← instantiateMVars expectedType
let Expr.forallE _ a b .. := expectedType | do
tryPostpone
throwError "(↑) must have a function type, not{indentExpr expectedType}"
if b.hasLooseBVars then
tryPostpone
throwError "(↑) must have a non-dependent function type, not{indentExpr expectedType}"
if a.hasExprMVar then tryPostpone
if b.hasExprMVar then tryPostpone
let f ← withLocalDeclD `x a fun x => do
mkLambdaFVars #[x] (← mkCoe b a x)
return f.etaExpanded?.getD f