Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

Commit 5ed5f59

Browse files
robertylewisfpvandoorn
authored andcommitted
feat(tactic/delta_instance): handle parameters and use in library (#1483)
* feat(tactic/core): improve delta_instance handler * feat(*): use delta_instance derive handler * feat(tactic/delta_instance): cleaner handling of application under binders * Revert "feat(tactic/delta_instance): cleaner handling of application under binders" This reverts commit 4005a2f. * comment apply_under_pis * properly get unused name * feat(tactic/delta_instance): run with high priority and don't run on inductives * Update src/tactic/core.lean Co-Authored-By: Floris van Doorn <fpvdoorn@gmail.com>
1 parent 800dba4 commit 5ed5f59

File tree

8 files changed

+58
-39
lines changed

8 files changed

+58
-39
lines changed

src/category_theory/comma.lean

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,13 @@ end comma
190190

191191
omit 𝒜 ℬ
192192

193+
@[derive category]
193194
def over (X : T) := comma.{v₃ 0 v₃} (𝟭 T) (functor.of.obj X)
194195

195196
namespace over
196197

197198
variables {X : T}
198199

199-
instance category : category (over X) := by delta over; apply_instance
200-
201200
@[extensionality] lemma over_morphism.ext {X : T} {U V : over X} {f g : U ⟶ V}
202201
(h : f.left = g.left) : f = g :=
203202
by tidy
@@ -254,14 +253,13 @@ end
254253

255254
end over
256255

256+
@[derive category]
257257
def under (X : T) := comma.{0 v₃ v₃} (functor.of.obj X) (𝟭 T)
258258

259259
namespace under
260260

261261
variables {X : T}
262262

263-
instance : category (under X) := by delta under; apply_instance
264-
265263
@[extensionality] lemma under_morphism.ext {X : T} {U V : under X} {f g : U ⟶ V}
266264
(h : f.right = g.right) : f = g :=
267265
by tidy

src/data/nat/modeq.lean

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace nat
1111

1212
/-- Modular equality. `modeq n a b`, or `a ≡ b [MOD n]`, means
1313
that `a - b` is a multiple of `n`. -/
14+
@[derive decidable]
1415
def modeq (n a b : ℕ) := a % n = b % n
1516

1617
notation a ` ≡ `:50 b ` [MOD `:50 n `]`:0 := modeq n a b
@@ -24,8 +25,6 @@ variables {n m a b c d : ℕ}
2425

2526
@[trans] protected theorem trans : a ≡ b [MOD n] → b ≡ c [MOD n] → a ≡ c [MOD n] := eq.trans
2627

27-
instance : decidable (a ≡ b [MOD n]) := by unfold modeq; apply_instance
28-
2928
theorem modeq_zero_iff : a ≡ 0 [MOD n] ↔ n ∣ a :=
3029
by rw [modeq, zero_mod, dvd_iff_mod_eq_zero]
3130

src/data/rel.lean

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,13 @@ import tactic.basic data.set.lattice order.complete_lattice
99

1010
variables {α : Type*} {β : Type*} {γ : Type*}
1111

12+
@[derive lattice.complete_lattice]
1213
def rel (α : Type*) (β : Type*) := α → β → Prop
1314

1415
namespace rel
1516

1617
variables {δ : Type*} (r : rel α β)
1718

18-
instance : lattice.complete_lattice (rel α β) :=
19-
by unfold rel; apply_instance
20-
2119
def inv : rel β α := flip r
2220

2321
lemma inv_def (x : α) (y : β) : r.inv y x ↔ r x y := iff.rfl

src/field_theory/mv_polynomial.lean

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,9 @@ namespace mv_polynomial
200200
universe u
201201
variables (σ : Type u) (α : Type u) [fintype σ] [discrete_field α] [fintype α]
202202

203+
@[derive [add_comm_group, vector_space α]]
203204
def R : Type u := restrict_degree σ α (fintype.card α - 1)
204205

205-
instance R.add_comm_group : add_comm_group (R σ α) := by dunfold R; apply_instance
206-
instance R.vector_space : vector_space α (R σ α) := by dunfold R; apply_instance
207-
208206
noncomputable instance decidable_restrict_degree (m : ℕ) :
209207
decidable_pred (λn, n ∈ {n : σ →₀ ℕ | ∀i, n i ≤ m }) :=
210208
by simp only [set.mem_set_of_eq]; apply_instance

src/linear_algebra/dual.lean

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,12 @@ namespace module
1414
variables (R : Type*) (M : Type*)
1515
variables [comm_ring R] [add_comm_group M] [module R M]
1616

17-
def dual := M →ₗ[R] R
17+
@[derive [add_comm_group, module R]] def dual := M →ₗ[R] R
1818

1919
namespace dual
2020

2121
instance : has_coe_to_fun (dual R M) := ⟨_, linear_map.to_fun⟩
2222

23-
instance : add_comm_group (dual R M) :=
24-
by delta dual; apply_instance
25-
26-
instance : module R (dual R M) :=
27-
by delta dual; apply_instance
28-
2923
def eval : M →ₗ[R] (dual R (dual R M)) := linear_map.id.flip
3024

3125
lemma eval_apply (v : M) (a : dual R M) : (eval R M v) a = a v :=
@@ -43,7 +37,7 @@ variables {K : Type u} {V : Type v} {ι : Type w}
4337
variables [discrete_field K] [add_comm_group V] [vector_space K V]
4438
open vector_space module module.dual submodule linear_map cardinal function
4539

46-
instance dual.vector_space : vector_space K (dual K V) := {..dual.module K V}
40+
instance dual.vector_space : vector_space K (dual K V) := { ..module.dual.inst K V }
4741

4842
variables [decidable_eq ι]
4943
variables {B : ι → V} (h : is_basis K B)
@@ -205,7 +199,6 @@ begin
205199
rcases exists_is_basis_fintype h with ⟨b, hb, ⟨hf⟩⟩,
206200
resetI,
207201
rw [← hb.to_dual_to_dual, range_comp, hb.to_dual_range, map_top, to_dual_range _],
208-
delta dual_basis,
209202
apply_instance
210203
end
211204

src/tactic/core.lean

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,8 +1154,24 @@ do e ← get_env,
11541154
since it is expensive to execute `get_mathlib_dir` many times. -/
11551155
meta def is_in_mathlib (n : name) : tactic bool :=
11561156
do ml ← get_mathlib_dir, e ← get_env, return $ e.is_prefix_of_file ml n
1157+
1158+
/-- auxiliary function for apply_under_pis -/
1159+
private meta def apply_under_pis_aux (func arg : pexpr) : ℕ → expr → pexpr
1160+
| n (expr.pi nm bi tp bd) := expr.pi nm bi (pexpr.of_expr tp) (apply_under_pis_aux (n+1) bd)
1161+
| n _ :=
1162+
let vars := ((list.range n).reverse.map (@expr.var ff)),
1163+
bd := vars.foldl expr.app arg.mk_explicit in
1164+
func bd
1165+
1166+
/--
1167+
Assumes `pi_expr` is of the form `Π x1 ... xn, _`.
1168+
Creates a pexpr of the form `Π x1 ... xn, func (arg x1 ... xn)`.
1169+
All arguments (implicit and explicit) to `arg` should be supplied. -/
1170+
meta def apply_under_pis (func arg : pexpr) (pi_expr : expr) : pexpr :=
1171+
apply_under_pis_aux func arg 0 pi_expr
1172+
11571173
/--
1158-
Tries to derive unary instances by unfolding the newly introduced type.
1174+
Tries to derive instances by unfolding the newly introduced type and applying type class resolution.
11591175
11601176
For example,
11611177
```
@@ -1164,21 +1180,32 @@ For example,
11641180
adds an instance `ring new_int`, defined to be the instance of `ring ℤ` found by `apply_instance`.
11651181
11661182
Multiple instances can be added with `@[derive [ring, module ℝ]]`.
1183+
1184+
This derive handler applies only to declarations made using `def`, and will fail on such a
1185+
declaration if it is unable to derive an instance. It is run with higher priority than the built-in
1186+
handlers, which will fail on `def`s.
11671187
-/
1168-
@[derive_handler] meta def delta_instance : derive_handler :=
1169-
λ cls tp,
1170-
(do tp' ← mk_const tp,
1171-
tgt ← to_expr ``(%%cls %%tp'),
1172-
(_, v) ← solve_aux tgt (delta_target [tp] >> apply_instance >> done),
1173-
v ← instantiate_mvars v,
1174-
nm ← get_unused_name $ tp ++
1175-
match tgt with
1176-
| expr.app (expr.const nm _) _ := nm
1188+
@[derive_handler, priority 2000] meta def delta_instance : derive_handler :=
1189+
λ cls new_decl_name,
1190+
do env ← get_env,
1191+
if env.is_inductive new_decl_name then return ff else
1192+
do new_decl_type ← declaration.type <$> get_decl new_decl_name,
1193+
new_decl_pexpr ← resolve_name new_decl_name,
1194+
tgt ← to_expr $ apply_under_pis cls new_decl_pexpr new_decl_type,
1195+
(_, inst) ← solve_aux tgt
1196+
(intros >> reset_instance_cache >> delta_target [new_decl_name] >> apply_instance >> done),
1197+
inst ← instantiate_mvars inst,
1198+
tgt ← instantiate_mvars tgt,
1199+
nm ← get_unused_decl_name $ new_decl_name ++
1200+
match cls with
1201+
-- the postfix is needed because we can't protect this name. using nm.last directly can
1202+
-- conflict with open namespaces
1203+
| (expr.const nm _) := (nm.last ++ "_1" : string)
11771204
| _ := "inst"
11781205
end,
1179-
add_decl $ mk_definition nm [] tgt v,
1206+
add_decl $ mk_definition nm inst.collect_univ_params tgt inst,
11801207
set_basic_attribute `instance nm tt,
1181-
return tt) <|> return ff
1208+
return tt
11821209

11831210
/-- `find_private_decl n none` finds a private declaration named `n` in any of the imported files.
11841211
@@ -1207,9 +1234,9 @@ do env ← get_env,
12071234

12081235
open lean.parser interactive
12091236

1210-
/-- `import_private foo from bar` finds a private declaration `foo` in the same file as `bar`
1211-
and creates a local notation to refer to it.
1212-
1237+
/-- `import_private foo from bar` finds a private declaration `foo` in the same file as `bar`
1238+
and creates a local notation to refer to it.
1239+
12131240
`import_private foo`, looks for `foo` in all imported files. -/
12141241
@[user_command]
12151242
meta def import_private_cmd (_ : parse $ tk "import_private") : lean.parser unit :=

src/topology/uniform_space/compare_reals.lean

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,8 @@ namespace compare_reals
9191
instead of the metric space one. We proved in rat.uniform_space_eq that they are equal,
9292
but they are not definitionaly equal, so it would confuse the type class system (and probably
9393
also human readers). -/
94-
def Q := ℚ
94+
@[derive comm_ring] def Q := ℚ
9595

96-
instance : comm_ring Q := by unfold Q ; apply_instance
9796
instance : uniform_space Q := is_absolute_value.uniform_space (abs : ℚ → ℚ)
9897

9998
/-- Real numbers constructed as in Bourbaki. -/

test/delta_instance.lean

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ Copyright (c) 2019 Robert Y. Lewis. All rights reserved.
33
Released under Apache 2.0 license as described in the file LICENSE.
44
Authors: Robert Y. Lewis
55
-/
6+
import data.set
67

7-
import tactic.core
8+
@[derive has_coe_to_sort] def X : Type := set ℕ
89

910
@[derive ring] def T := ℤ
1011

@@ -15,3 +16,9 @@ instance : binclass ℤ ℤ := ⟨_, _⟩
1516
@[derive [ring, binclass ℤ]] def U := ℤ
1617

1718
@[derive λ α, binclass α ℤ] def V := ℤ
19+
20+
@[derive ring] def id_ring (α) [ring α] : Type := α
21+
22+
@[derive decidable_eq] def S := ℕ
23+
24+
@[derive decidable_eq] inductive P | a | b | c

0 commit comments

Comments
 (0)