-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathForward.lean
697 lines (611 loc) · 14.4 KB
/
Forward.lean
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
/-
Copyright (c) 2024 Jannis Limperg. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jannis Limperg
-/
import Aesop
set_option aesop.check.all true
set_option aesop.smallErrorMessages true
set_option pp.mvars false
/--
info: Try this:
have fwd : γ₁ ∧ γ₂ := r₁ a b
have fwd_1 : δ₁ ∧ δ₂ := r₂ a
---
error: unsolved goals
α : Sort u_1
β : Sort u_2
γ₁ γ₂ δ₁ δ₂ : Prop
a : α
b : β
r₁ : α → β → γ₁ ∧ γ₂
r₂ : α → δ₁ ∧ δ₂
fwd : γ₁ ∧ γ₂
fwd_1 : δ₁ ∧ δ₂
⊢ γ₁ ∧ γ₂ ∧ δ₁ ∧ δ₂
-/
#guard_msgs in
example (a : α) (b : β) (r₁ : (a : α) → (b : β) → γ₁ ∧ γ₂)
(r₂ : (a : α) → δ₁ ∧ δ₂) : γ₁ ∧ γ₂ ∧ δ₁ ∧ δ₂ := by
saturate? [*]
/--
info: Try this:
have fwd : β := h₁ h₃
have fwd_1 : γ := h₂ fwd
---
error: unsolved goals
α β γ : Prop
h₁ : α → β
h₂ : β → γ
h₃ : α
fwd : β
fwd_1 : γ
⊢ γ
-/
#guard_msgs in
example {α β γ : Prop} (h₁ : α → β) (h₂ : β → γ) (h₃ : α) : γ := by
saturate? [*]
/--
info: Try this:
have fwd : β := h₁ h₃
---
error: unsolved goals
α β γ : Prop
h₁ : α → β
h₂ : β → γ
h₃ : α
fwd : β
⊢ γ
-/
#guard_msgs in
example {α β γ : Prop} (h₁ : α → β) (h₂ : β → γ) (h₃ : α) : γ := by
forward? [*]
/--
info: Try this:
have fwd : β := h₁ h₃
---
error: unsolved goals
α β γ : Prop
h₁ : α → β
h₂ : β → γ
h₃ : α
fwd : β
⊢ γ
-/
#guard_msgs in
example {α β γ : Prop} (h₁ : α → β) (h₂ : β → γ) (h₃ : α) : γ := by
saturate? 1 [*]
/--
info: Try this:
have fwd : β := h₁ h₄
have fwd_1 : γ := h₂ fwd
---
error: unsolved goals
α β γ δ : Prop
h₁ : α → β
h₂ : β → γ
h₃ : γ → δ
h₄ : α
fwd : β
fwd_1 : γ
⊢ δ
-/
#guard_msgs in
example {α β γ δ : Prop} (h₁ : α → β) (h₂ : β → γ) (h₃ : γ → δ) (h₄ : α) : δ := by
saturate? 2 [*]
/--
info: Try this:
have fwd : β := h₁ h₄
have fwd_1 : γ := h₂ h₄
---
error: unsolved goals
α β γ δ : Prop
h₁ : α → β
h₂ : α → γ
h₃ : β → γ → δ
h₄ : α
fwd : β
fwd_1 : γ
⊢ δ
-/
#guard_msgs in
example {α β γ δ : Prop} (h₁ : α → β) (h₂ : α → γ) (h₃ : β → γ → δ) (h₄ : α) : δ := by
saturate? 1 [*]
example {P : Nat → Prop} (hP : P 0) (hPn : ∀ n, P n → P (n + 1)) : P 20 := by
saturate 20 [*]
assumption
section
axiom A : Type
axiom B : Type
axiom C : Type
@[local aesop safe forward]
axiom ab : A → B
@[local aesop norm forward]
axiom bc : B → C
/--
info: Try this:
have fwd : P := rule P (Q ∧ R) h
-/
#guard_msgs in
example (rule : ∀ α β, α ∧ β → α) (h : P ∧ Q ∧ R) : P := by
forward? [*]
guard_hyp fwd : P
assumption
/--
info: Try this:
have fwd : B := ab a
have fwd_1 : C := bc fwd
---
error: unsolved goals
a : A
fwd : B
fwd_1 : C
⊢ C
-/
#guard_msgs in
noncomputable example : A → C := by
intro a
saturate?
end
/--
info: Try this:
have fwd : R a b := h₁ a b h₂ h₃
---
error: unsolved goals
α : Sort u_1
β : Sort u_2
a : α
b : β
P Q R : α → β → Prop
h₁ : ∀ (a : α) (b : β), P a b → Q a b → R a b
h₂ : P a b
h₃ : Q a b
fwd : R a b
⊢ R a b
-/
#guard_msgs in
example {P Q R : α → β → Prop} (h₁ : ∀ a b, P a b → Q a b → R a b)
(h₂ : P a b) (h₃ : Q a b) : R a b := by
saturate? [h₁]
/--
info: Try this:
have fwd : R a b := h₁ a b h₂ h₄
---
error: unsolved goals
α : Sort u_1
a b : α
P Q R : α → α → Prop
h₁ : ∀ (a b : α), P a b → Q b a → R a b
h₂ : P a b
h₃ : Q a b
h₄ : Q b a
fwd : R a b
⊢ R a b
-/
#guard_msgs in
example {P Q R : α → α → Prop} (h₁ : ∀ a b, P a b → Q b a → R a b)
(h₂ : P a b) (h₃ : Q a b) (h₄ : Q b a) : R a b := by
saturate? [*]
/--
error: unsolved goals
α : Sort u_1
a b : α
P Q R : α → α → Prop
h₁ : ∀ (a b : α), P a b → Q b a → R a b
h₂ : P a b
h₃ : Q a b
⊢ R a b
-/
#guard_msgs in
example {P Q R : α → α → Prop} (h₁ : ∀ a b, P a b → Q b a → R a b)
(h₂ : P a b) (h₃ : Q a b) : R a b := by
saturate [*]
/--
info: Try this:
have fwd : R b a := h₁ b a h₄ h₃
---
error: unsolved goals
α : Sort u_1
c d a b : α
P Q R : α → α → Prop
h₁ : ∀ (a b : α), P a b → Q b a → R a b
h₂ : P c d
h₃ : Q a b
h₄ : P b a
fwd : R b a
⊢ R b a
-/
#guard_msgs in
example {P Q R : α → α → Prop} (h₁ : ∀ a b, P a b → Q b a → R a b)
(h₂ : P c d) (h₃ : Q a b) (h₄ : P b a) : R b a := by
saturate? [*]
/--
info: Try this:
have fwd : R c c := h₁ c d d c h₂ h₅
have fwd_1 : R c b := h₁ c d a b h₂ h₃
have fwd_2 : R b c := h₁ b a d c h₄ h₅
have fwd_3 : R b b := h₁ b a a b h₄ h₃
---
error: unsolved goals
α : Sort u_1
c d a b : α
P Q R : α → α → Prop
h₁ : ∀ (a b c d : α), P a b → Q c d → R a d
h₂ : P c d
h₃ : Q a b
h₄ : P b a
h₅ : Q d c
fwd : R c c
fwd_1 : R c b
fwd_2 : R b c
fwd_3 : R b b
⊢ R c b
-/
#guard_msgs in
example {P Q R : α → α → Prop} (h₁ : ∀ a b c d, P a b → Q c d → R a d)
(h₂ : P c d) (h₃ : Q a b) (h₄ : P b a) (h₅ : Q d c) : R c b := by
saturate? [*]
/--
info: Try this:
have fwd : S a d := h₁ a b c d h₂ h₃ h₄
have fwd_1 : S a c := h₁ a b d c h₂ h₃ h₅
---
error: unsolved goals
α : Sort u_1
a b c d : α
P Q R S : α → α → Prop
h₁ : ∀ (a b c d : α), P a b → Q b a → R c d → S a d
h₂ : P a b
h₃ : Q b a
h₄ : R c d
h₅ : R d c
fwd : S a d
fwd_1 : S a c
⊢ S a d
-/
#guard_msgs in
example {P Q R S : α → α → Prop} (h₁ : ∀ a b c d, P a b → Q b a → R c d → S a d)
(h₂ : P a b) (h₃ : Q b a) (h₄ : R c d) (h₅ : R d c) : S a d := by
saturate? [*]
/--
info: Try this:
have fwd : R b a := h₁ a b h₂ h₃ h₄
---
error: unsolved goals
α : Sort u_1
a b : α
P : α → Prop
Q R : α → α → Prop
h₁ : ∀ (a b : α), P a → P b → Q a b → R b a
h₂ : P a
h₃ : P b
h₄ : Q a b
fwd : R b a
⊢ Q b a
-/
#guard_msgs in
example {P : α → Prop} {Q R : α → α → Prop}
(h₁ : ∀ a b, P a → P b → Q a b → R b a)
(h₂ : P a) (h₃ : P b) (h₄ : Q a b) : Q b a := by
saturate? [*]
/--
info: Try this:
have fwd : R b a := h₁ a b h₆ h₅ h₄
---
error: unsolved goals
α : Sort u_1
c d a b : α
P : α → Prop
Q R : α → α → Prop
h₁ : ∀ (a b : α), P a → P b → Q a b → R b a
h₂ : P c
h₃ : P d
h₄ : Q a b
h₅ : P b
h₆ : P a
fwd : R b a
⊢ Q b a
-/
#guard_msgs in
example {P : α → Prop} {Q R : α → α → Prop}
(h₁ : ∀ a b, P a → P b → Q a b → R b a)
(h₂ : P c) (h₃ : P d) (h₄ : Q a b) (h₅ : P b) (h₆ : P a) : Q b a := by
saturate? [*]
/--
info: Try this:
have fwd : R c d := h₁ d c h₃ h₂ h₇
have fwd_1 : R b a := h₁ a b h₆ h₅ h₄
---
error: unsolved goals
α : Sort u_1
c d a b : α
P : α → Prop
Q R : α → α → Prop
h₁ : ∀ (a b : α), P a → P b → Q a b → R b a
h₂ : P c
h₃ : P d
h₄ : Q a b
h₅ : P b
h₆ : P a
h₇ : Q d c
fwd : R c d
fwd_1 : R b a
⊢ Q b a
-/
#guard_msgs in
example {P : α → Prop} {Q R : α → α → Prop}
(h₁ : ∀ a b, P a → P b → Q a b → R b a)
(h₂ : P c) (h₃ : P d) (h₄ : Q a b) (h₅ : P b) (h₆ : P a) (h₇ : Q d c) : Q b a := by
saturate? [*]
example (a : α) (b : β) (r₁ : (a : α) → (b : β) → γ₁ ∧ γ₂)
(r₂ : (a : α) → δ₁ ∧ δ₂) : γ₁ ∧ γ₂ ∧ δ₁ ∧ δ₂ := by
aesop (add safe [forward r₁, forward (immediate := [a]) r₂])
section MatchRedundancy
-- Complete matches are considered redundant (and hence do not produce new
-- hypotheses) if they agree on all variables that appear in the conclusion.
/--
error: unsolved goals
γ : Sort u_1
α : Prop
β : Type
r : α → β → γ
a₁ a₂ : α
b₁ b₂ : β
fwd : γ
⊢ True
-/
#guard_msgs in
example {α : Prop} {β : Type} (r : α → β → γ) (a₁ a₂ : α) (b₁ b₂ : β) : True := by
saturate [r]
-- Only one new hypothesis.
/--
error: unsolved goals
α : Sort u_1
a₁ a₂ : α
P Q : α → Prop
r : ∀ (a : α), P a → Q a
p₁ : P a₁
p₂ p₂' : P a₂
fwd : Q a₁
fwd_1 : Q a₂
⊢ True
-/
#guard_msgs in
example {P Q : α → Prop} (r : ∀ a, P a → Q a) (p₁ : P a₁) (p₂ : P a₂)
(p₂' : P a₂) : True := by
saturate [r]
-- Two new hypotheses, one for `a₁` and one for `a₂` (but not two).
-- When a hypothesis already exists in the context, it is not added again.
/--
error: unsolved goals
α : Sort u_1
β : Sort u_2
r₁ r₂ : α → β
a₁ a₂ : α
fwd : β
⊢ True
-/
#guard_msgs in
example (r₁ r₂ : α → β) (a₁ a₂ : α) : True := by
saturate [r₁, r₂]
-- Two new hypotheses (but not four).
end MatchRedundancy
/--
info: Try this:
have fwd : γ₁ ∧ γ₂ := r₁ a b
simp_all only [and_self, implies_true, true_and]
obtain ⟨left, right⟩ := fwd
have fwd : δ₁ ∧ δ₂ := r₂ a
simp_all only [and_self, implies_true]
-/
#guard_msgs in
example (a : α) (b : β) (r₁ : (a : α) → (b : β) → γ₁ ∧ γ₂)
(r₂ : (a : α) → δ₁ ∧ δ₂) : γ₁ ∧ γ₂ ∧ δ₁ ∧ δ₂ := by
aesop? (add safe [forward r₁, forward (immediate := [a]) r₂])
-- `destruct` rules only clear propositional hypotheses. So this succeeds:
example (a : α) (b : β) (r₁ : (a : α) → (b : β) → γ)
(r₂ : (a : α) → δ) : γ ∧ δ := by
aesop (add safe [destruct r₁, destruct (immediate := [a]) r₂])
(config := { enableSimp := false, terminal := true })
-- ... but this fails:
/-- error: tactic 'aesop' failed, failed to prove the goal after exhaustive search. -/
#guard_msgs in
example {α : Prop} (a : α) (b : β) (r₁ : (a : α) → (b : β) → γ)
(r₂ : (a : α) → δ) : γ ∧ δ := by
aesop (add safe [destruct r₁, destruct (immediate := [a]) r₂])
(config := { enableSimp := false, terminal := true })
-- Same examples with `saturate`. Note: We currently can't make local `saturate`
-- rules into `destruct` rules.
namespace SaturateEx₁
axiom α : Type
axiom β : Type
axiom γ₁ : Prop
axiom γ₂ : Prop
axiom δ₁ : Prop
axiom δ₂ : Prop
@[aesop safe destruct]
axiom r₁ : α → β → γ₁ ∧ γ₂
@[aesop safe destruct]
axiom r₂ : α → δ₁ ∧ δ₂
/--
error: unsolved goals
a : α
b : β
fwd : γ₁ ∧ γ₂
fwd_1 : δ₁ ∧ δ₂
⊢ γ₁ ∧ γ₂ ∧ δ₁ ∧ δ₂
-/
#guard_msgs in
example (a : α) (b : β) : γ₁ ∧ γ₂ ∧ δ₁ ∧ δ₂ := by
saturate
end SaturateEx₁
namespace SaturateEx₂
axiom α : Prop
axiom β : Type
axiom γ₁ : Prop
axiom γ₂ : Prop
axiom δ₁ : Prop
axiom δ₂ : Prop
@[aesop safe destruct]
axiom r₁ : α → β → γ₁ ∧ γ₂
@[aesop safe destruct]
axiom r₂ : α → δ₁ ∧ δ₂
/--
error: unsolved goals
b : β
fwd : γ₁ ∧ γ₂
⊢ γ₁ ∧ γ₂ ∧ δ₁ ∧ δ₂
-/
#guard_msgs in
example (a : α) (b : β) : γ₁ ∧ γ₂ ∧ δ₁ ∧ δ₂ := by
saturate
end SaturateEx₂
example (a : α) (b : β) (r₁ : (a : α) → (b : β) → γ₁ ∧ γ₂)
(r₂ : (a : α) → δ₁ ∧ δ₂) : γ₁ ∧ γ₂ ∧ δ₁ ∧ δ₂ := by
aesop (add safe [forward r₁], 90% destruct r₂)
/--
warning: aesop: failed to prove the goal after exhaustive search.
---
error: unsolved goals
α β γ : Prop
h₁ : α
h₂ : β
fwd : γ
⊢ False
-/
#guard_msgs in
example {α β γ : Prop} (h : α → β → γ) (h₁ : α) (h₂ : β) : False := by
aesop (add norm -1 forward h)
-- In the following example, `h` does not apply because `simp_all` discharges
-- the premises `α` and `β`. The stateful implementation of forward reasoning
-- can't reasonably deal with local rules whose types change during the course
-- of the search; the best we can do is try to detect when this happens.
/--
warning: aesop: failed to prove the goal after exhaustive search.
---
error: unsolved goals
α β γ : Prop
h : γ
h₁ : α
h₂ : β
⊢ False
-/
#guard_msgs in
example {α β γ : Prop} (h : α → β → γ) (h₁ : α) (h₂ : β) : False := by
aesop (add safe forward h)
section Computation
-- Stateful forward reasoning sees through `reducible` definitions...
abbrev rid (x : α) : α := x
example {P Q : α → Prop} (h₁ : ∀ a, P a → Q a → X) (h₂ : P (rid a)) (h₃ : Q a) : X := by
saturate [h₁]
exact fwd
-- ... but not through semireducible ones.
/--
error: unsolved goals
α : Sort _
X : Sort _
a : α
P Q : α → Prop
h₁ : (a : α) → P a → Q a → X
h₂ : P (id a)
h₃ : Q a
⊢ X
-/
#guard_msgs in
example {P Q : α → Prop} (h₁ : ∀ a, P a → Q a → X) (h₂ : P (id a)) (h₃ : Q a) : X := by
saturate [h₁]
end Computation
namespace Immediate
axiom α : Type
axiom P : α → Prop
axiom Q : α → Prop
axiom R : α → Prop
@[aesop safe forward (immediate := [h₂])]
axiom foo : ∀ a (h₁ : P a) (h₂ : Q a), R a
/--
error: unsolved goals
a : α
h : Q a
fwd : P a → R a
⊢ False
-/
#guard_msgs in
example (h : Q a) : False := by
saturate
end Immediate
namespace Instance
class Foo (α : Type) : Prop
axiom β : Type
@[aesop safe forward]
axiom foo : ∀ (α : Type) (a : α) [Foo α], β
/--
error: unsolved goals
α : Type
inst✝ : Foo α
a : α
fwd : β
⊢ False
-/
#guard_msgs in
example [Foo α] (a : α) : False := by
saturate
axiom γ : Type
instance : Foo γ where
/--
error: unsolved goals
c : γ
fwd : β
⊢ False
-/
#guard_msgs in
example (c : γ) : False := by
saturate
@[aesop safe forward (immediate := [a])]
axiom bar : ∀ α β (a : α) (b : β) [Foo β], γ
/--
error: unsolved goals
α : Sort u_1
a : α
⊢ False
-/
#guard_msgs in
example (a : α) : False := by
saturate
/--
error: unsolved goals
c : γ
fwd : β
⊢ False
-/
#guard_msgs in
example (c : γ) : False := by
saturate
end Instance
namespace ConstForwardRule
axiom α : Type
@[local aesop safe forward]
axiom a : α
noncomputable example : α := by
aesop
/--
error: unsolved goals
fwd : α
⊢ α
-/
#guard_msgs in
noncomputable example : α := by
saturate
end ConstForwardRule
section MultipleUniverses
-- Here we test the handling of rules with multiple universe parameters.
axiom α.{u} : Type u
axiom β.{v} : Type v
axiom γ.{w} : Type w
axiom P.{u, v, w} : α.{u} → β.{v} → γ.{w} → Prop
axiom Q.{u, v, w} : β.{v} → α.{u} → γ.{w} → Prop
@[local aesop safe forward]
axiom foo {a b c} : P a b c → Q b a c
example (h : P a b c) : Q b a c := by
saturate
assumption
end MultipleUniverses