forked from leanprover-community/mathlib4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCorecursion.lean
More file actions
595 lines (539 loc) · 22 KB
/
Corecursion.lean
File metadata and controls
595 lines (539 loc) · 22 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
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
/-
Copyright (c) 2026 Vasilii Nesterov. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Vasilii Nesterov
-/
module
public import Mathlib.Topology.MetricSpace.PiNat
public import Mathlib.Topology.MetricSpace.UniformConvergence
public import Mathlib.Topology.MetricSpace.Contracting
public import Mathlib.Data.Seq.Defs
public import Mathlib.Tactic.ENatToNat
/-!
# Non-primitive corecursion for sequences
Primitive corecursive definition of the form
```
def foo (x : X) := hd x :: foo (tlArg x)
```
(where hd and tlArg are arbitrary functions) can be encoded via the corecursor `Seq.corec`.
It is not enough, however, to define multiplication and `powser` operation for multiseries.
This file implements a more general form of corecursion in the spirit of [blanchette2015].
This is a bare minimum that needed for the tactic, it justifies a weaker class of
corecursive definitions than [blanchette2015] does, and only works for `Seq`.
A function `f : Seq α → Seq α` is called *friendly* if for all `n : ℕ` the `n`-prefix of its result
`f s` depends only on the `n`-prefix of its input `s`.
In this file we develop a theory that justifies corecursive definitions of the form
```
def foo (x : X) := hd x :: f (foo (tlArg x))
```
where f is friendly.
## Main definitions
* `FriendlyOperation f` means that `f` is friendly.
* `FriendlyOperationClass` is a typeclass meaning that some indexed family of operations
are friendly.
* `gcorec`: a generalization of `Seq.corec` that allows a corecursive call to be guarded by
a friendly function.
* `FriendlyOperation.coind`, `FriendlyOperation.coind_comp_friend_left`,
`FriendlyOperation.coind_comp_friend_right`: coinduction principles for proving that an operation
is friendly.
* `FriendlyOperation.eq_of_bisim`: a generalisation of `Seq.eq_of_bisim'` that allows using a
friendly operation in the tail of the sequences.
## Implementation details
To prove that the definition of the form
```
def foo (x : X) := hd x :: f (foo (tlArg x))
```
is correct we prove that there exists a function satisfying this equation. For that we employ a
Banach fixed point theorem. We treat `Seq α` as a metric space here with the metric
`d(s, t) := 2 ^ (-n)` where `n` is the minimal index where `s` and `t` differ.
Then `f` is friendly iff it is `1`-Lipschitz.
-/
@[expose] public section
namespace Tactic.ComputeAsymptotics.Seq
open Stream' Seq
open scoped UniformConvergence
variable {α β γ γ' : Type*}
/-- Metric space structure on `Stream' α` considering `α` as a discrete metric space. -/
noncomputable local instance : MetricSpace (Stream' α) :=
@PiNat.metricSpace (fun _ ↦ α) (fun _ ↦ ⊥) (fun _ ↦ discreteTopology_bot _)
/-- Metric space structure on `Seq α` considering `α` as a discrete metric space. -/
noncomputable local instance : MetricSpace (Seq α) :=
Subtype.metricSpace
local instance : CompleteSpace (Stream' α) :=
@PiNat.completeSpace _ (fun _ ↦ ⊥) (fun _ ↦ discreteTopology_bot _)
local instance : CompleteSpace (Seq α) := by
suffices IsClosed (X := Stream' (Option α))
(fun x ↦ ∀ {n : ℕ}, x n = none → x (n + 1) = none) by
apply IsClosed.completeSpace_coe
rw [isClosed_iff_clusterPt]
intro s hs n hn
rw [clusterPt_principal_iff] at hs
obtain ⟨t, hts, ht⟩ := hs (Metric.ball s ((1 / 2 : ℝ) ^ (n + 1)))
(Metric.ball_mem_nhds _ (by positivity))
simp only [Metric.ball, Set.mem_setOf_eq] at hts
rw [← PiNat.apply_eq_of_dist_lt hts (by simp)] at hn
rw [← PiNat.apply_eq_of_dist_lt hts (by rfl)]
exact ht hn
set_option backward.isDefEq.respectTransparency false in
theorem Stream'.dist_le_one (s t : Stream' α) : dist s t ≤ 1 := by
by_cases h : s = t
· simp [h]
rw [PiNat.dist_eq_of_ne h]
bound
@[simp]
theorem dist_le_one (s t : Seq α) : dist s t ≤ 1 := PiNat.dist_le_one _ _
local instance : BoundedSpace (Stream' α) :=
@PiNat.boundedSpace _ (fun _ ↦ ⊥) (fun _ ↦ discreteTopology_bot _)
local instance : BoundedSpace (Seq α) :=
instBoundedSpaceSubtype
set_option backward.isDefEq.respectTransparency false in
theorem dist_eq_two_inv_pow {s t : Seq α} (h : s ≠ t) : ∃ n, dist s t = 2⁻¹ ^ n := by
rw [Subtype.dist_eq, PiNat.dist_eq_of_ne (Subtype.coe_ne_coe.mpr h)]
simp
set_option backward.isDefEq.respectTransparency false in
@[simp]
theorem dist_cons_cons (x : α) (s t : Seq α) : dist (cons x s) (cons x t) = 2⁻¹ * dist s t := by
by_cases! h : s = t
· simp [h]
have h' : cons x s ≠ cons x t := by
simpa
rw [Subtype.dist_eq, Subtype.dist_eq, PiNat.dist_eq_of_ne (Subtype.coe_ne_coe.mpr h),
PiNat.dist_eq_of_ne (Subtype.coe_ne_coe.mpr h')]
simp only [show (1 / 2 : ℝ) = 2⁻¹ by simp, ← pow_succ']
congr
simp only [val_cons, PiNat.firstDiff, ne_eq, Classical.dite_not, Subtype.coe_ne_coe.mpr h,
not_false_eq_true, ↓reduceDIte, val_eq_get]
split_ifs with h_if
· contrapose! h'
apply_fun Subtype.val using Subtype.val_injective
simpa
· convert Nat.find_comp_succ _ _ _
simp [Stream'.cons]
theorem dist_eq_half_of_head {s t : Seq α} (h : s.head = t.head) :
dist s t = 2⁻¹ * dist s.tail t.tail := by
cases s <;> cases t <;> simp at h <;> simp [h]
set_option backward.isDefEq.respectTransparency false in
theorem dist_eq_one_of_head {s t : Seq α} (h : s.head ≠ t.head) : dist s t = 1 := by
rw [Subtype.dist_eq, PiNat.dist_eq_of_ne]
· convert pow_zero _
simp only [PiNat.firstDiff, ne_eq, Classical.dite_not, dite_eq_left_iff,
Nat.find_eq_zero]
intro h'
simpa [Stream'.cons]
· rw [Subtype.coe_ne_coe]
contrapose! h
simp [h]
theorem dist_cons_cons_eq_one {x y : α} {s t : Seq α} (h : x ≠ y) :
dist (cons x s) (cons y t) = 1 := by
apply dist_eq_one_of_head
simpa
@[simp]
theorem dist_cons_nil (x : α) (s : Seq α) : dist (cons x s) nil = 1 := by
apply dist_eq_one_of_head
simp
@[simp]
theorem dist_nil_cons (x : α) (s : Seq α) : dist nil (cons x s) = 1 := by
rw [dist_comm]
simp
/-- A function on sequences is called a "friend" if any `n`-prefix of its output depends only on
the `n`-prefix of the input. Such functions can be used in the tail of (non-primitive) corecursive
definitions. -/
def FriendlyOperation (op : Seq α → Seq α) : Prop := LipschitzWith 1 op
/-- A family of friendly operations on sequences indexed by a type `γ`. -/
class FriendlyOperationClass (F : γ → Seq α → Seq α) : Prop where
friend : ∀ c : γ, FriendlyOperation (F c)
theorem FriendlyOperation.id : FriendlyOperation (id : Seq α → Seq α) :=
LipschitzWith.id
theorem FriendlyOperation.comp {op op' : Seq α → Seq α}
(h : FriendlyOperation op) (h' : FriendlyOperation op') :
FriendlyOperation (op ∘ op') := by
rw [FriendlyOperation] at h h' ⊢
convert h.comp h'
simp
theorem FriendlyOperation.const {s : Seq α} : FriendlyOperation (fun _ ↦ s) := by
simp [FriendlyOperation, lipschitzWith_iff_dist_le_mul]
theorem FriendlyOperationClass.comp (F : γ → Seq α → Seq α) (g : γ' → γ)
[h : FriendlyOperationClass F] : FriendlyOperationClass (fun c ↦ F (g c)) := by
grind [FriendlyOperationClass]
theorem FriendlyOperation.ite {op₁ op₂ : Seq α → Seq α}
(h₁ : FriendlyOperation op₁) (h₂ : FriendlyOperation op₂)
{P : Option α → Prop} [DecidablePred P] :
FriendlyOperation (fun s ↦ if P s.head then op₁ s else op₂ s) := by
rw [FriendlyOperation, lipschitzWith_iff_dist_le_mul, NNReal.coe_one] at h₁ h₂ ⊢
intro s t
by_cases! h_head : s.head ≠ t.head
· simp [dist_eq_one_of_head h_head]
grind
theorem FriendlyOperation.dist_le {op : Seq α → Seq α} (h : FriendlyOperation op)
{s t : Seq α} : dist (op s) (op t) ≤ dist s t := by
rw [FriendlyOperation, lipschitzWith_iff_dist_le_mul] at h
simpa using h s t
theorem exists_fixed_point_of_contractible (F : (β →ᵤ Seq α) → (β →ᵤ Seq α))
(h : LipschitzWith 2⁻¹ F) :
∃ f : β → Seq α, Function.IsFixedPt F f := by
have hF : ContractingWith 2⁻¹ F := by
constructor
· norm_num
· exact h
let f := hF.fixedPoint _
use f
exact hF.fixedPoint_isFixedPt
set_option backward.isDefEq.respectTransparency false in
/-- Main theorem of this file. It shows that there exists a funcion satisfying the corecursive
definition of the form `def foo (x : X) := hd x :: op (foo (tlArg x))` where `f` is friendly. -/
theorem FriendlyOperation.exists_fixed_point (F : β → Option (α × γ × β)) (op : γ → Seq α → Seq α)
[h : FriendlyOperationClass op] :
∃ f : β → Seq α, ∀ b : β,
match F b with
| none => f b = nil
| some (a, c, b') => f b = Seq.cons a (op c (f b')) := by
let T : (β →ᵤ Seq α) → (β →ᵤ Seq α) := fun f b =>
match F b with
| none => nil
| some (a, c, b') => Seq.cons a (op c (f b'))
have hT : LipschitzWith 2⁻¹ T := by
rw [lipschitzWith_iff_dist_le_mul]
intro f g
rw [UniformFun.dist_le (by positivity)]
intro b
simp only [UniformFun.toFun, UniformFun.ofFun, Equiv.coe_fn_symm_mk, NNReal.coe_inv,
NNReal.coe_ofNat, T]
cases F b with
| none => simp
| some v =>
obtain ⟨a, c, b'⟩ := v
simp
calc
_ ≤ dist (f b') (g b') := by
have := h.friend c
rw [FriendlyOperation, lipschitzWith_iff_dist_le_mul] at this
specialize this (f b') (g b')
simpa using this
_ ≤ _ := by
simp only [UniformFun.dist_def]
apply le_ciSup (f := fun b ↦ dist (f b) (g b))
have : ∃ C, ∀ (a b : Seq α), dist a b ≤ C := by
rw [← Metric.boundedSpace_iff]
infer_instance
obtain ⟨C, hC⟩ := this
use C
simp [upperBounds]
grind
obtain ⟨f, hf⟩ := exists_fixed_point_of_contractible T hT
use f
intro b
rw [← hf]
simp only [T]
cases hb : F b with
| none =>
simp
| some v =>
obtain ⟨a, c, b'⟩ := v
simp only [cons_eq_cons, true_and]
congr
change f b' = T f b'
rw [hf]
/-- (General) non-primitive corecursor for `Seq α` that allows using a friendly operation in the
tail of the corecursive definition. -/
noncomputable def gcorec (F : β → Option (α × γ × β)) (op : γ → Seq α → Seq α)
[FriendlyOperationClass op] :
β → Seq α := (FriendlyOperation.exists_fixed_point F op).choose
theorem gcorec_nil {F : β → Option (α × γ × β)} {op : γ → Seq α → Seq α}
[FriendlyOperationClass op] {b : β}
(h : F b = none) :
gcorec F op b = nil := by
have := (FriendlyOperation.exists_fixed_point F op).choose_spec b
simpa [h] using this
theorem gcorec_some {F : β → Option (α × γ × β)} {op : γ → Seq α → Seq α}
[FriendlyOperationClass op] {b : β}
{a : α} {c : γ} {b' : β}
(h : F b = some (a, c, b')) :
gcorec F op b = Seq.cons a (op c (gcorec F op b')) := by
have := (FriendlyOperation.exists_fixed_point F op).choose_spec b
simpa [h] using this
/-- The operation `cons hd ·` is friendly. -/
theorem FriendlyOperation.cons (hd : α) : FriendlyOperation (cons hd) := by
simp only [FriendlyOperation, lipschitzWith_iff_dist_le_mul, dist_cons_cons, NNReal.coe_one,
one_mul]
intro x y
linarith [dist_nonneg (x := x) (y := y)]
/-- The operation `(op (.cons hd ·)).tail` is friendly if `op` is friendly. -/
theorem FriendlyOperation.cons_tail {op : Seq α → Seq α} {hd : α} (h : FriendlyOperation op) :
FriendlyOperation (fun s ↦ (op (.cons hd s)).tail) := by
simp_rw [FriendlyOperation, lipschitzWith_iff_dist_le_mul, NNReal.coe_one, one_mul] at h ⊢
intro x y
specialize h (.cons hd x) (.cons hd y)
simp only [dist_cons_cons] at h
cases hx : op (.cons hd x) with
| nil =>
cases hy : op (.cons hd y) with
| nil => simp
| cons y_hd y_tl =>
contrapose! h
grw [hx, hy, dist_le_one]
norm_num
| cons x_hd x_tl =>
cases hy : op (.cons hd y) with
| nil =>
contrapose! h
grw [hx, hy, dist_le_one]
norm_num
| cons y_hd y_tl =>
by_cases! h_hd : x_hd ≠ y_hd
· contrapose! h
grw [hx, hy, dist_cons_cons_eq_one h_hd, dist_le_one]
norm_num
simpa [hx, hy, h_hd] using h
/-- The first element of `op (a :: s)` depends only on `a`. -/
theorem FriendlyOperation.op_cons_head_eq {op : Seq α → Seq α} (h : FriendlyOperation op) {a : α}
{s t : Seq α} : (op <| .cons a s).head = (op <| .cons a t).head := by
rw [FriendlyOperation, lipschitzWith_iff_dist_le_mul] at h
specialize h (.cons a s) (.cons a t)
simp only [NNReal.coe_one, dist_cons_cons, one_mul] at h
replace h : dist (op (.cons a s)) (op (.cons a t)) ≤ 2⁻¹ := by
apply h.trans
simp
cases hs : op (.cons a s) with
| nil =>
cases ht : op (.cons a t) with
| nil => simp
| cons t_hd t_tl => norm_num [hs, ht] at h
| cons s_hd s_tl =>
cases ht : op (.cons a t) with
| nil => norm_num [hs, ht] at h
| cons t_hd t_tl =>
simp only [Seq.head_cons, Option.some.injEq]
by_contra! h_hd
rw [hs, ht, dist_cons_cons_eq_one h_hd] at h
norm_num at h
/-- Decomposes a friendly operation by the head of the input sequence. Returns `none` if the output
is `nil`, or `some (out_hd, op')` where `out_hd` is the head of the output and `op'` is a friendly
operation mapping the tail of the input to the tail of the output. See
`destruct_apply_eq_unfold` for the correctness statement. -/
def FriendlyOperation.unfold {op : Seq α → Seq α} (h : FriendlyOperation op) (hd? : Option α) :
Option (α × Subtype (@FriendlyOperation α)) :=
match hd? with
| none =>
let t := op nil
match t.destruct with
| none => none
| some (t_hd, t_tl) =>
some (t_hd, ⟨fun _ ↦ t_tl, FriendlyOperation.const⟩)
| some s_hd =>
let s := .cons s_hd nil
let t := op s
match t.destruct with
| none => none
| some (t_hd, _) =>
some (t_hd, ⟨fun s_tl ↦ (op (.cons s_hd s_tl)).tail, FriendlyOperation.cons_tail h⟩)
/-- `unfold` correctly decomposes a friendly operation: the head of `op s` depends only on the
head of `s` (and is given by `unfold`), while the tail of `op s` is obtained by applying the
friendly operation returned by `unfold` to the tail of `s`. This gives a coinductive
characterization of `FriendlyOperation`. For the coinduction principle, see
`FriendlyOperation.coind`. -/
theorem FriendlyOperation.destruct_apply_eq_unfold {op : Seq α → Seq α} (h : FriendlyOperation op)
{s : Seq α} :
destruct (op s) = (h.unfold s.head).map (fun (hd, op') => (hd, op'.val s.tail)) := by
unfold unfold
cases s with
| nil =>
generalize op nil = t
cases t <;> simp
| cons s_hd s_tl =>
simp only [Seq.tail_cons, Seq.head_cons]
generalize ht0 : op (.cons s_hd nil) = t0 at *
generalize ht : op (.cons s_hd s_tl) = t at *
have : t0.head = t.head := by
rw [← ht0, ← ht, FriendlyOperation.op_cons_head_eq h]
cases t0 with
| nil =>
cases t with
| nil => simp
| cons => simp at this
| cons =>
cases t with
| nil => simp at this
| cons => simp_all
/-- If `op` is friendly, then `op s` and `op t` have the same head if `s` and `t`
have the same head. -/
theorem FriendlyOperation.op_head_eq {op : Seq α → Seq α} (h : FriendlyOperation op) {s t : Seq α}
(h_head : s.head = t.head) : (op s).head = (op t).head := by
simp only [head_eq_destruct, Option.map_eq_map, h.destruct_apply_eq_unfold, Option.map_map]
at h_head ⊢
simp [h_head]
rfl
attribute [-simp] inv_pow in
/-- Coinduction principle for proving that an operation is friendly. -/
theorem FriendlyOperation.coind (motive : (Seq α → Seq α) → Prop)
{op : Seq α → Seq α}
(h_base : motive op)
(h_step : ∀ op, motive op → ∃ T : Option α → Option (α × Subtype motive),
∀ s, (op s).destruct = (T s.head).map (fun (hd, op') => (hd, op'.val s.tail))) :
FriendlyOperation op := by
rw [FriendlyOperation, lipschitzWith_iff_dist_le_mul]
intro s t
simp only [NNReal.coe_one, one_mul]
suffices ∀ n, dist s t ≤ (2⁻¹ : ℝ) ^ n → dist (op s) (op t) ≤ (2⁻¹ : ℝ) ^ n by
by_cases h : s = t
· simp [h]
obtain ⟨n, hst⟩ := dist_eq_two_inv_pow h
rw [hst] at this ⊢
apply this
rfl
intro n hn
induction n generalizing op s t with
| zero => simp
| succ n ih =>
specialize h_step _ h_base
obtain ⟨T, hT⟩ := h_step
have hs := hT s
have ht := hT t
by_cases! h_head : s.head ≠ t.head
· contrapose! hn
norm_num [pow_succ, dist_eq_one_of_head h_head]
refine mul_lt_one_of_nonneg_of_lt_one_right (pow_le_one₀ ?_ ?_) ?_ ?_
all_goals norm_num
cases hT_head : T s.head with
| none =>
simp only [hT_head, Option.map_none, ← h_head] at hs ht
apply Stream'.Seq.destruct_eq_none at hs
apply Stream'.Seq.destruct_eq_none at ht
simp [hs,destruct_eq_none, ht]
| some v =>
obtain ⟨hd, op', h_next⟩ := v
simp only [hT_head, Option.map_some, ← h_head] at hs ht
apply Stream'.Seq.destruct_eq_cons at hs
apply Stream'.Seq.destruct_eq_cons at ht
simp only [hs, ht, dist_cons_cons, pow_succ', inv_pos, Nat.ofNat_pos, mul_le_mul_iff_right₀,
ge_iff_le]
apply ih h_next
simpa [dist_eq_half_of_head h_head, pow_succ'] using hn
/-- A generalisation of `FriendlyOperation.coind` which allows using `opf ∘ op'` in the tail
of `op s` where `opf` is friendly and `op'` is a function satisfying `motive`. -/
theorem FriendlyOperation.coind_comp_friend_left {op : Seq α → Seq α}
(motive : (Seq α → Seq α) → Prop)
(h_base : motive op)
(h_step : ∀ op, motive op →
∃ T : Option α → Option (α × Subtype FriendlyOperation × Subtype motive),
∀ s, (op s).destruct = (T s.head).map fun (hd, opf, op') => (hd, opf.val <| op'.val s.tail)) :
FriendlyOperation op := by
let motive' (op : Seq α → Seq α) : Prop :=
∃ opf op', op = opf ∘ op' ∧ FriendlyOperation opf ∧ motive op'
apply FriendlyOperation.coind motive'
· exact ⟨_root_.id, op, rfl, FriendlyOperation.id, h_base⟩
clear h_base op
rintro _ ⟨opf, op, rfl, h_opf, h_op⟩
specialize h_step _ h_op
obtain ⟨T, hT⟩ := h_step
-- obtain ⟨F, hF⟩ := FriendlyOperation.destruct h_opf
use fun hd? ↦
match (T hd?) with
| none => (h_opf.unfold none).map fun (hd, opf') =>
(hd, ⟨_, fun _ ↦ opf'.val nil, op, rfl, FriendlyOperation.const, h_op⟩)
| some (hd, opf', op') => (h_opf.unfold (some hd)).map fun (hd', opf'') =>
(hd', ⟨_, opf''.val ∘ opf'.val, op'.val, rfl,
FriendlyOperation.comp opf''.prop opf'.prop, op'.prop⟩)
intro s
specialize hT s
simp only [Function.comp_apply]
generalize op s = s' at *
cases s' with
| nil =>
symm at hT
simp at hT
simp [hT, destruct_apply_eq_unfold h_opf]
rfl
| cons s_hd s_tl =>
simp only [destruct_cons] at hT
simp only [destruct_apply_eq_unfold h_opf, Seq.tail_cons, Seq.head_cons]
generalize T s.head = t? at *
cases t? with
| none => simp at hT
| some v =>
obtain ⟨hd, opf', op'⟩ := v
simp at hT
simp [hT]
rfl
/-- A generalisation of `FriendlyOperation.coind` that allows using `op' ∘ opf` in the tail
of `op s` where `opf` is friendly and `op'` is a function satisfying `motive`. -/
theorem FriendlyOperation.coind_comp_friend_right {op : Seq α → Seq α}
(motive : (Seq α → Seq α) → Prop)
(h_base : motive op)
(h_step : ∀ op, motive op →
∃ T : Option α → Option (α × Subtype FriendlyOperation × Subtype motive),
∀ s, (op s).destruct = (T s.head).map fun (hd, opf, op') => (hd, op'.val <| opf.val s.tail)) :
FriendlyOperation op := by
let motive' (op : Seq α → Seq α) : Prop :=
∃ opf op', op = op' ∘ opf ∧ FriendlyOperation opf ∧ motive op'
apply FriendlyOperation.coind motive'
· exact ⟨_root_.id, op, rfl, FriendlyOperation.id, h_base⟩
clear h_base op
rintro _ ⟨opf, op, rfl, h_opf, h_op⟩
specialize h_step _ h_op
obtain ⟨T, hT⟩ := h_step
-- obtain ⟨F, hF⟩ := FriendlyOperation.destruct h_opf
use fun hd? ↦
match (h_opf.unfold hd?) with
| none => (T none).map fun (hd, opf', op') =>
(hd, ⟨_, fun _ ↦ opf'.val nil, op', rfl, FriendlyOperation.const, op'.prop⟩)
| some (hd, opf') => (T (some hd)).map fun (hd', opf'', op') =>
(hd', ⟨_, opf''.val ∘ opf'.val, op'.val, rfl,
FriendlyOperation.comp opf''.prop opf'.prop, op'.prop⟩)
intro s
simp only [Function.comp_apply]
have hF := h_opf.destruct_apply_eq_unfold (s := s)
generalize opf s = s' at *
cases s' with
| nil =>
symm at hF
simp only [destruct_nil, Option.map_eq_none_iff] at hF
simp only [hF, Option.map_map]
specialize hT nil
simp only [tail_nil, head_nil] at hT
simp [hT]
rfl
| cons s_hd s_tl =>
simp only [destruct_cons] at hF
generalize h_opf.unfold s.head = t? at *
cases t? with
| none => simp at hF
| some v =>
obtain ⟨hd, opf', op'⟩ := v
simp only [Option.map_some, Option.some.injEq, Prod.mk.injEq] at hF
simp only [hF, Option.map_map]
rw [hT]
rfl
/-- A generalisation of `Seq.eq_of_bisim'` that allows using a friendly operation in the tail
of the sequences. -/
theorem FriendlyOperationClass.eq_of_bisim {s t : Seq α} {op : γ → Seq α → Seq α}
[FriendlyOperationClass op]
(motive : Seq α → Seq α → Prop)
(base : motive s t)
(step : ∀ u v, motive u v → (u = v) ∨
∃ hd u' v' c, u = cons hd (op c u') ∧ v = cons hd (op c v') ∧
motive u' v') :
s = t := by
suffices dist s t = 0 by simpa using this
suffices ∀ n, dist s t ≤ (2⁻¹ : ℝ) ^ n by
apply eq_of_le_of_ge
· apply ge_of_tendsto' (x := Filter.atTop) _ this
rw [tendsto_pow_atTop_nhds_zero_iff]
norm_num
· simp
intro n
induction n generalizing s t with
| zero => simp
| succ n ih =>
specialize step s t base
obtain step | ⟨hd, u, v, c, rfl, rfl, h_next⟩ := step
· simp [step]
simp only [dist_cons_cons]
specialize ih h_next
calc
_ ≤ 2⁻¹ * dist u v := by
gcongr
apply FriendlyOperation.dist_le
apply FriendlyOperationClass.friend
_ ≤ _ := by
grw [ih, pow_succ']
end Tactic.ComputeAsymptotics.Seq