This repository was archived by the owner on Jul 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 289
Expand file tree
/
Copy pathelementwise.lean
More file actions
238 lines (201 loc) · 8.89 KB
/
Copy pathelementwise.lean
File metadata and controls
238 lines (201 loc) · 8.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/-
Copyright (c) 2021 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Scott Morrison
-/
import category_theory.concrete_category.basic
import tactic.fresh_names
import tactic.reassoc_axiom
import tactic.slice
/-!
# Tools to reformulate category-theoretic lemmas in concrete categories
## The `elementwise` attribute
The `elementwise` attribute can be applied to a lemma
```lean
@[elementwise]
lemma some_lemma {C : Type*} [category C]
{X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z) (h : X ⟶ Z) (w : ...) : f ≫ g = h := ...
```
and will produce
```lean
lemma some_lemma_apply {C : Type*} [category C] [concrete_category C]
{X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z) (h : X ⟶ Z) (w : ...) (x : X) : g (f x) = h x := ...
```
Here `X` is being coerced to a type via `concrete_category.has_coe_to_sort` and
`f`, `g`, and `h` are being coerced to functions via `concrete_category.has_coe_to_fun`.
Further, we simplify the type using `concrete_category.coe_id : ((𝟙 X) : X → X) x = x` and
`concrete_category.coe_comp : (f ≫ g) x = g (f x)`,
replacing morphism composition with function composition.
The name of the produced lemma can be specified with `@[elementwise other_lemma_name]`.
If `simp` is added first, the generated lemma will also have the `simp` attribute.
## Implementation
This closely follows the implementation of the `@[reassoc]` attribute, due to Simon Hudon.
Thanks to Gabriel Ebner for help diagnosing universe issues.
-/
namespace tactic
open interactive lean.parser category_theory
/--
From an expression `f = g`,
where `f g : X ⟶ Y` for some objects `X Y : V` with `[S : category V]`,
extract the expression for `S`.
-/
meta def extract_category : expr → tactic expr
| `(@eq (@quiver.hom ._ (@category_struct.to_quiver _
(@category.to_category_struct _ %%S)) _ _) _ _) := pure S
| _ := failed
/-- (internals for `@[elementwise]`)
Given a lemma of the form `f = g`, where `f g : X ⟶ Y` and `X Y : V`,
proves a new lemma of the form
`∀ (x : X), f x = g x`
if we are already in a concrete category, or
`∀ [concrete_category.{w} V] (x : X), f x = g x`
otherwise.
Returns the type and proof of this lemma,
and the universe parameter `w` for the `concrete_category` instance, if it was not synthesized.
-/
-- This is closely modelled on `reassoc_axiom`.
meta def prove_elementwise (h : expr) : tactic (expr × expr × option name) :=
do
(vs,t) ← infer_type h >>= open_pis,
(f, g) ← match_eq t,
S ← extract_category t <|> fail "no morphism equation found in statement",
`(@quiver.hom _ %%H %%X %%Y) ← infer_type f,
C ← infer_type X,
CC_type ← to_expr ``(@concrete_category %%C %%S),
(CC, CC_found) ← (do CC ← mk_instance CC_type, pure (CC, tt)) <|>
(do CC ← mk_local' `I binder_info.inst_implicit CC_type, pure (CC, ff)),
-- This is need to fill in universe levels fixed by `mk_instance`:
CC_type ← instantiate_mvars CC_type,
x_type ← to_expr ``(@coe_sort %%C _
(@category_theory.concrete_category.has_coe_to_sort %%C %%S %%CC) %%X),
x ← mk_local_def `x x_type,
t' ← to_expr ``(@coe_fn (@quiver.hom %%C %%H %%X %%Y) _
(@category_theory.concrete_category.has_coe_to_fun %%C %%S %%CC %%X %%Y) %%f %%x =
@coe_fn (@quiver.hom %%C %%H %%X %%Y) _
(@category_theory.concrete_category.has_coe_to_fun %%C %%S %%CC %%X %%Y) %%g %%x),
let c' := h.mk_app vs,
(_,pr) ← solve_aux t' (rewrite_target c'; reflexivity),
-- The codomain of forget lives in a new universe, which may be now a universe metavariable
-- if we didn't synthesize an instance:
[w, _, _] ← pure CC_type.get_app_fn.univ_levels,
-- We unify that with a fresh universe parameter.
n ← match w with
| level.mvar _ := (do
n ← get_unused_name_reserved [`w] mk_name_set,
unify (expr.sort (level.param n)) (expr.sort w),
pure (option.some n))
| _ := pure option.none
end,
t' ← instantiate_mvars t',
CC ← instantiate_mvars CC,
x ← instantiate_mvars x,
-- Now the key step: replace morphism composition with function composition,
-- and identity morphisms with nothing.
let s := simp_lemmas.mk,
s ← s.add_simp ``id_apply,
s ← s.add_simp ``comp_apply,
(t'', pr', _) ← simplify s [] t' {fail_if_unchanged := ff},
pr' ← mk_eq_mp pr' pr,
-- Further, if we're in `Type`, get rid of the coercions entirely.
let s := simp_lemmas.mk,
s ← s.add_simp ``concrete_category.has_coe_to_fun_Type,
(t'', pr'', _) ← simplify s [] t'' {fail_if_unchanged := ff},
pr'' ← mk_eq_mp pr'' pr',
t'' ← pis (vs ++ (if CC_found then [x] else [CC, x])) t'',
pr'' ← lambdas (vs ++ (if CC_found then [x] else [CC, x])) pr'',
pure (t'', pr'', n)
/-- (implementation for `@[elementwise]`)
Given a declaration named `n` of the form `∀ ..., f = g`, proves a new lemma named `n'`
of the form `∀ ... [concrete_category V] (x : X), f x = g x`.
-/
meta def elementwise_lemma (n : name) (n' : name := n.append_suffix "_apply") : tactic unit :=
do d ← get_decl n,
let c := @expr.const tt n d.univ_levels,
(t'',pr',l') ← prove_elementwise c,
let params := l'.to_list ++ d.univ_params,
add_decl $ declaration.thm n' params t'' (pure pr'),
copy_attribute `simp n n'
/--
The `elementwise` attribute can be applied to a lemma
```lean
@[elementwise]
lemma some_lemma {C : Type*} [category C]
{X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z) (h : X ⟶ Z) (w : ...) : f ≫ g = h := ...
```
and will produce
```lean
lemma some_lemma_apply {C : Type*} [category C] [concrete_category C]
{X Y Z : C} (f : X ⟶ Y) (g : Y ⟶ Z) (h : X ⟶ Z) (w : ...) (x : X) : g (f x) = h x := ...
```
Here `X` is being coerced to a type via `concrete_category.has_coe_to_sort` and
`f`, `g`, and `h` are being coerced to functions via `concrete_category.has_coe_to_fun`.
Further, we simplify the type using `concrete_category.coe_id : ((𝟙 X) : X → X) x = x` and
`concrete_category.coe_comp : (f ≫ g) x = g (f x)`,
replacing morphism composition with function composition.
The `[concrete_category C]` argument will be omitted if it is possible to synthesize an instance.
The name of the produced lemma can be specified with `@[elementwise other_lemma_name]`.
If `simp` is added first, the generated lemma will also have the `simp` attribute.
-/
@[user_attribute]
meta def elementwise_attr : user_attribute unit (option name) :=
{ name := `elementwise,
descr := "create a companion lemma for a morphism equation applied to an element",
parser := optional ident,
after_set := some (λ n _ _,
do some n' ← elementwise_attr.get_param n | elementwise_lemma n (n.append_suffix "_apply"),
elementwise_lemma n $ n.get_prefix ++ n' ) }
add_tactic_doc
{ name := "elementwise",
category := doc_category.attr,
decl_names := [`tactic.elementwise_attr],
tags := ["category theory"] }
namespace interactive
setup_tactic_parser
/--
`elementwise h`, for assumption `w : ∀ ..., f ≫ g = h`, creates a new assumption
`w : ∀ ... (x : X), g (f x) = h x`.
`elementwise! h`, does the same but deletes the initial `h` assumption.
(You can also add the attribute `@[elementwise]` to lemmas to generate new declarations generalized
in this way.)
-/
meta def elementwise (del : parse (tk "!")?) (ns : parse ident*) : tactic unit :=
do ns.mmap' (λ n,
do h ← get_local n,
(t,pr,u) ← prove_elementwise h,
assertv n t pr,
when del.is_some (tactic.clear h) )
end interactive
/-- Auxiliary definition for `category_theory.elementwise_of`. -/
meta def derive_elementwise_proof : tactic unit :=
do `(calculated_Prop %%v %%h) ← target,
(t,pr,n) ← prove_elementwise h,
unify v t,
exact pr
end tactic
/--
With `w : ∀ ..., f ≫ g = h` (with universal quantifiers tolerated),
`elementwise_of w : ∀ ... (x : X), g (f x) = h x`.
The type and proof of `elementwise_of h` is generated by `tactic.derive_elementwise_proof`
which makes `elementwise_of` meta-programming adjacent. It is not called as a tactic but as
an expression. The goal is to avoid creating assumptions that are dismissed after one use:
```lean
example (M N K : Mon.{u}) (f : M ⟶ N) (g : N ⟶ K) (h : M ⟶ K) (w : f ≫ g = h) (m : M) :
g (f m) = h m :=
begin
rw elementwise_of w,
end
```
-/
theorem category_theory.elementwise_of {α} (hh : α) {β}
(x : tactic.calculated_Prop β hh . tactic.derive_elementwise_proof) : β := x
/--
With `w : ∀ ..., f ≫ g = h` (with universal quantifiers tolerated),
`elementwise_of w : ∀ ... (x : X), g (f x) = h x`.
Although `elementwise_of` is not a tactic or a meta program, its type is generated
through meta-programming to make it usable inside normal expressions.
-/
add_tactic_doc
{ name := "category_theory.elementwise_of",
category := doc_category.tactic,
decl_names := [`category_theory.elementwise_of],
tags := ["category theory"] }