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

Commit f98fc00

Browse files
YaelDillieskim-em
andcommitted
docs(logic/relation): add module docstring (#8773)
Also fix whitespaces Co-authored-by: Scott Morrison <scott.morrison@gmail.com>
1 parent c811dd7 commit f98fc00

File tree

1 file changed

+86
-58
lines changed

1 file changed

+86
-58
lines changed

src/logic/relation.lean

Lines changed: 86 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,41 @@
22
Copyright (c) 2018 Johannes Hölzl. All rights reserved.
33
Released under Apache 2.0 license as described in the file LICENSE.
44
Authors: Johannes Hölzl
5-
6-
Transitive reflexive as well as reflexive closure of relations.
75
-/
86
import tactic.basic
97

10-
variables {α : Type*} {β : Type*} {γ : Type*} {δ : Type*}
8+
/-!
9+
# Relation closures
10+
11+
This file defines the reflexive, transitive, and reflexive transitive closures of relations.
12+
It also proves some basic results on definitions in core, such as `eqv_gen`.
13+
14+
Note that this is about unbundled relations, that is terms of types of the form `α → β → Prop`. For
15+
the bundled version, see `rel`.
16+
17+
## Definitions
18+
19+
* `relation.refl_gen`: Reflexive closure. `refl_gen r` relates everything `r` related, plus for all
20+
`a` it relates `a` with itself. So `refl_gen r a b ↔ r a b ∨ a = b`.
21+
* `relation.trans_gen`: Transitive closure. `trans_gen r` relates everything `r` related
22+
transitively. So `trans_gen r a b ↔ ∃ x₀ ... xₙ, r a x₀ ∧ r x₀ x₁ ∧ ... ∧ r xₙ b`.
23+
* `relation.refl_trans_gen`: Reflexive transitive closure. `refl_trans_gen r` relates everything
24+
`r` related transitively, plus for all `a` it relates `a` with itself. So
25+
`refl_trans_gen r a b ↔ (∃ x₀ ... xₙ, r a x₀ ∧ r x₀ x₁ ∧ ... ∧ r xₙ b) ∨ a = b`. It is the same as
26+
the reflexive closure of the transitive closure, or the transitive closure of the reflexive
27+
closure. In terms of rewriting systems, this means that `a` can be rewritten to `b` in a number of
28+
rewrites.
29+
* `relation.comp`: Relation composition. We provide notation `∘r`. For `r : α → β → Prop` and
30+
`s : β → γ → Prop`, `r ∘r s`relates `a : α` and `c : γ` iff there exists `b : β` that's related to
31+
both.
32+
* `relation.map`: Image of a relation under a pair of maps. For `r : α → β → Prop`, `f : α → γ`,
33+
`g : β → δ`, `map r f g` is the relation `γ → δ → Prop` relating `f a` and `g b` for all `a`, `b`
34+
related by `r`.
35+
* `relation.join`: Join of a relation. For `r : α → α → Prop`, `join r a b ↔ ∃ c, r a c ∧ r b c`. In
36+
terms of rewriting systems, this means that `a` and `b` can be rewritten to the same term.
37+
-/
38+
39+
variables {α β γ δ : Type*}
1140

1241
section ne_imp
1342

@@ -66,19 +95,19 @@ The composition of two relations, yielding a new relation. The result
6695
relates a term of `α` and a term of `γ` if there is an intermediate
6796
term of `β` related to both.
6897
-/
69-
def comp (r : α → β → Prop) (p : β → γ → Prop) (a : α) (c : γ) : Prop := ∃b, r a b ∧ p b c
98+
def comp (r : α → β → Prop) (p : β → γ → Prop) (a : α) (c : γ) : Prop := ∃ b, r a b ∧ p b c
7099

71100
local infixr ` ∘r ` : 80 := relation.comp
72101

73102
lemma comp_eq : r ∘r (=) = r :=
74-
funext $ assume a, funext $ assume b, propext $ iff.intro
75-
(assume ⟨c, h, eq⟩, eq ▸ h)
76-
(assume h, ⟨b, h, rfl⟩)
103+
funext $ λ a, funext $ λ b, propext $ iff.intro
104+
(λ ⟨c, h, eq⟩, eq ▸ h)
105+
(λ h, ⟨b, h, rfl⟩)
77106

78107
lemma eq_comp : (=) ∘r r = r :=
79-
funext $ assume a, funext $ assume b, propext $ iff.intro
80-
(assume ⟨c, eq, h⟩, eq.symm ▸ h)
81-
(assume h, ⟨a, rfl, h⟩)
108+
funext $ λ a, funext $ λ b, propext $ iff.intro
109+
(λ ⟨c, eq, h⟩, eq.symm ▸ h)
110+
(λ h, ⟨a, rfl, h⟩)
82111

83112
lemma iff_comp {r : Prop → α → Prop} : (↔) ∘r r = r :=
84113
have (↔) = (=), by funext a b; exact iff_eq_eq,
@@ -92,16 +121,16 @@ lemma comp_assoc : (r ∘r p) ∘r q = r ∘r p ∘r q :=
92121
begin
93122
funext a d, apply propext,
94123
split,
95-
exact assume ⟨c, ⟨b, hab, hbc⟩, hcd⟩, ⟨b, hab, c, hbc, hcd⟩,
96-
exact assume ⟨b, hab, c, hbc, hcd⟩, ⟨c, ⟨b, hab, hbc⟩, hcd⟩
124+
exact λ ⟨c, ⟨b, hab, hbc⟩, hcd⟩, ⟨b, hab, c, hbc, hcd⟩,
125+
exact λ ⟨b, hab, c, hbc, hcd⟩, ⟨c, ⟨b, hab, hbc⟩, hcd⟩
97126
end
98127

99128
lemma flip_comp : flip (r ∘r p) = (flip p) ∘r (flip r) :=
100129
begin
101130
funext c a, apply propext,
102131
split,
103-
exact assume ⟨b, hab, hbc⟩, ⟨b, hbc, hab⟩,
104-
exact assume ⟨b, hbc, hab⟩, ⟨b, hab, hbc⟩
132+
exact λ ⟨b, hab, hbc⟩, ⟨b, hbc, hab⟩,
133+
exact λ ⟨b, hbc, hab⟩, ⟨b, hab, hbc⟩
105134
end
106135

107136
end comp
@@ -113,7 +142,7 @@ defined by having pairs of terms related if they have preimages
113142
related by `r`.
114143
-/
115144
protected def map (r : α → β → Prop) (f : α → γ) (g : β → δ) : γ → δ → Prop :=
116-
λc d, ∃a b, r a b ∧ f a = c ∧ g b = d
145+
λ c d, ∃ a b, r a b ∧ f a = c ∧ g b = d
117146

118147
variables {r : α → α → Prop} {a b c d : α}
119148

@@ -137,7 +166,7 @@ attribute [refl] refl_trans_gen.refl
137166

138167
attribute [refl] refl_gen.refl
139168

140-
lemma refl_gen.to_refl_trans_gen : ∀{a b}, refl_gen r a b → refl_trans_gen r a b
169+
lemma refl_gen.to_refl_trans_gen : ∀ {a b}, refl_gen r a b → refl_trans_gen r a b
141170
| a _ refl_gen.refl := by refl
142171
| a b (refl_gen.single h) := refl_trans_gen.tail refl_trans_gen.refl h
143172

@@ -169,33 +198,33 @@ begin
169198
{ apply relation.refl_trans_gen.head (h b) c }
170199
end
171200

172-
lemma cases_tail : refl_trans_gen r a b → b = a ∨ (∃c, refl_trans_gen r a c ∧ r c b) :=
201+
lemma cases_tail : refl_trans_gen r a b → b = a ∨ (∃ c, refl_trans_gen r a c ∧ r c b) :=
173202
(cases_tail_iff r a b).1
174203

175204
@[elab_as_eliminator]
176205
lemma head_induction_on
177-
{P : ∀(a:α), refl_trans_gen r a b → Prop}
206+
{P : ∀ (a:α), refl_trans_gen r a b → Prop}
178207
{a : α} (h : refl_trans_gen r a b)
179208
(refl : P b refl)
180-
(head : ∀{a c} (h' : r a c) (h : refl_trans_gen r c b), P c h → P a (h.head h')) :
209+
(head : ∀ {a c} (h' : r a c) (h : refl_trans_gen r c b), P c h → P a (h.head h')) :
181210
P a h :=
182211
begin
183212
induction h generalizing P,
184213
case refl_trans_gen.refl { exact refl },
185214
case refl_trans_gen.tail : b c hab hbc ih {
186215
apply ih,
187216
show P b _, from head hbc _ refl,
188-
show ∀a a', r a a' → refl_trans_gen r a' b → P a' _ → P a _,
189-
from assume a a' hab hbc, head hab _ }
217+
show a a', r a a' → refl_trans_gen r a' b → P a' _ → P a _,
218+
from λ a a' hab hbc, head hab _ }
190219
end
191220

192221
@[elab_as_eliminator]
193222
lemma trans_induction_on
194-
{P : ∀{a b : α}, refl_trans_gen r a b → Prop}
223+
{P : ∀ {a b : α}, refl_trans_gen r a b → Prop}
195224
{a b : α} (h : refl_trans_gen r a b)
196-
(ih₁ : ∀a, @P a a refl)
197-
(ih₂ : ∀{a b} (h : r a b), P (single h))
198-
(ih₃ : ∀{a b c} (h₁ : refl_trans_gen r a b) (h₂ : refl_trans_gen r b c),
225+
(ih₁ : ∀ a, @P a a refl)
226+
(ih₂ : ∀ {a b} (h : r a b), P (single h))
227+
(ih₃ : ∀ {a b c} (h₁ : refl_trans_gen r a b) (h₂ : refl_trans_gen r b c),
199228
P h₁ → P h₂ → P (h₁.trans h₂)) :
200229
P h :=
201230
begin
@@ -204,21 +233,19 @@ begin
204233
case refl_trans_gen.tail : b c hab hbc ih { exact ih₃ hab (single hbc) ih (ih₂ hbc) }
205234
end
206235

207-
lemma cases_head (h : refl_trans_gen r a b) : a = b ∨ (∃c, r a c ∧ refl_trans_gen r c b) :=
236+
lemma cases_head (h : refl_trans_gen r a b) : a = b ∨ (∃ c, r a c ∧ refl_trans_gen r c b) :=
208237
begin
209238
induction h using relation.refl_trans_gen.head_induction_on,
210239
{ left, refl },
211240
{ right, existsi _, split; assumption }
212241
end
213242

214-
lemma cases_head_iff : refl_trans_gen r a b ↔ a = b ∨ (∃c, r a c ∧ refl_trans_gen r c b) :=
243+
lemma cases_head_iff : refl_trans_gen r a b ↔ a = b ∨ (∃ c, r a c ∧ refl_trans_gen r c b) :=
215244
begin
216-
split,
217-
{ exact cases_head },
218-
{ assume h,
219-
rcases h with rfl | ⟨c, hac, hcb⟩,
220-
{ refl },
221-
{ exact head hac hcb } }
245+
use cases_head,
246+
rintro (rfl | ⟨c, hac, hcb⟩),
247+
{ refl },
248+
{ exact head hac hcb }
222249
end
223250

224251
lemma total_of_right_unique (U : relator.right_unique r)
@@ -303,14 +330,14 @@ end,
303330
trans_gen.single⟩
304331

305332
lemma transitive_trans_gen : transitive (trans_gen r) :=
306-
assume a b c, trans
333+
λ a b c, trans
307334

308335
lemma trans_gen_idem :
309336
trans_gen (trans_gen r) = trans_gen r :=
310337
trans_gen_eq_self transitive_trans_gen
311338

312339
lemma trans_gen_lift {p : β → β → Prop} {a b : α} (f : α → β)
313-
(h : ∀a b, r a b → p (f a) (f b)) (hab : trans_gen r a b) : trans_gen p (f a) (f b) :=
340+
(h : ∀ a b, r a b → p (f a) (f b)) (hab : trans_gen r a b) : trans_gen p (f a) (f b) :=
314341
begin
315342
induction hab,
316343
case trans_gen.single : c hac { exact trans_gen.single (h a c hac) },
@@ -331,7 +358,7 @@ end trans_gen
331358
section refl_trans_gen
332359
open refl_trans_gen
333360

334-
lemma refl_trans_gen_iff_eq (h : ∀b, ¬ r a b) : refl_trans_gen r a b ↔ b = a :=
361+
lemma refl_trans_gen_iff_eq (h : ∀ b, ¬ r a b) : refl_trans_gen r a b ↔ b = a :=
335362
by rw [cases_head_iff]; simp [h, eq_comm]
336363

337364
lemma refl_trans_gen_iff_eq_or_trans_gen :
@@ -345,12 +372,12 @@ begin
345372
end
346373

347374
lemma refl_trans_gen_lift {p : β → β → Prop} {a b : α} (f : α → β)
348-
(h : ∀a b, r a b → p (f a) (f b)) (hab : refl_trans_gen r a b) : refl_trans_gen p (f a) (f b) :=
349-
refl_trans_gen.trans_induction_on hab (assume a, refl)
350-
(assume a b, refl_trans_gen.single ∘ h _ _) (assume a b c _ _, trans)
375+
(h : ∀ a b, r a b → p (f a) (f b)) (hab : refl_trans_gen r a b) : refl_trans_gen p (f a) (f b) :=
376+
refl_trans_gen.trans_induction_on hab (λ a, refl)
377+
(λ a b, refl_trans_gen.single ∘ h _ _) (λ a b c _ _, trans)
351378

352379
lemma refl_trans_gen_mono {p : α → α → Prop} :
353-
(∀a b, r a b → p a b) → refl_trans_gen r a b → refl_trans_gen p a b :=
380+
(∀ a b, r a b → p a b) → refl_trans_gen r a b → refl_trans_gen p a b :=
354381
refl_trans_gen_lift id
355382

356383
lemma refl_trans_gen_eq_self (refl : reflexive r) (trans : transitive r) :
@@ -362,17 +389,17 @@ funext $ λ a, funext $ λ b, propext $
362389
end, single⟩
363390

364391
lemma reflexive_refl_trans_gen : reflexive (refl_trans_gen r) :=
365-
assume a, refl
392+
λ a, refl
366393

367394
lemma transitive_refl_trans_gen : transitive (refl_trans_gen r) :=
368-
assume a b c, trans
395+
λ a b c, trans
369396

370397
lemma refl_trans_gen_idem :
371398
refl_trans_gen (refl_trans_gen r) = refl_trans_gen r :=
372399
refl_trans_gen_eq_self reflexive_refl_trans_gen transitive_refl_trans_gen
373400

374401
lemma refl_trans_gen_lift' {p : β → β → Prop} {a b : α} (f : α → β)
375-
(h : ∀a b, r a b → refl_trans_gen p (f a) (f b))
402+
(h : ∀ a b, r a b → refl_trans_gen p (f a) (f b))
376403
(hab : refl_trans_gen r a b) : refl_trans_gen p (f a) (f b) :=
377404
by simpa [refl_trans_gen_idem] using refl_trans_gen_lift f h hab
378405

@@ -390,21 +417,22 @@ in a term rewriting system, then *confluence* is the property that if
390417
`a` rewrites to both `b` and `c`, then `join r` relates `b` and `c`
391418
(see `relation.church_rosser`).
392419
-/
393-
def join (r : α → α → Prop) : α → α → Prop := λa b, ∃c, r a c ∧ r b c
420+
def join (r : α → α → Prop) : α → α → Prop := λ a b, ∃ c, r a c ∧ r b c
394421

395422
section join
396423
open refl_trans_gen refl_gen
397424

425+
/-- A sufficient condition for the Church-Rosser property. -/
398426
lemma church_rosser
399-
(h : ∀a b c, r a b → r a c → ∃d, refl_gen r b d ∧ refl_trans_gen r c d)
427+
(h : ∀ a b c, r a b → r a c → ∃ d, refl_gen r b d ∧ refl_trans_gen r c d)
400428
(hab : refl_trans_gen r a b) (hac : refl_trans_gen r a c) : join (refl_trans_gen r) b c :=
401429
begin
402430
induction hab,
403431
case refl_trans_gen.refl { exact ⟨c, hac, refl⟩ },
404432
case refl_trans_gen.tail : d e had hde ih {
405433
clear hac had a,
406434
rcases ih with ⟨b, hdb, hcb⟩,
407-
have : ∃a, refl_trans_gen r e a ∧ refl_gen r b a,
435+
have : ∃ a, refl_trans_gen r e a ∧ refl_gen r b a,
408436
{ clear hcb, induction hdb,
409437
case refl_trans_gen.refl { exact ⟨e, refl, refl_gen.single hde⟩ },
410438
case refl_trans_gen.tail : f b hdf hfb ih {
@@ -422,33 +450,33 @@ lemma join_of_single (h : reflexive r) (hab : r a b) : join r a b :=
422450
⟨b, hab, h b⟩
423451

424452
lemma symmetric_join : symmetric (join r) :=
425-
assume a b ⟨c, hac, hcb⟩, ⟨c, hcb, hac⟩
453+
λ a b ⟨c, hac, hcb⟩, ⟨c, hcb, hac⟩
426454

427455
lemma reflexive_join (h : reflexive r) : reflexive (join r) :=
428-
assume a, ⟨a, h a, h a⟩
456+
λ a, ⟨a, h a, h a⟩
429457

430-
lemma transitive_join (ht : transitive r) (h : ∀a b c, r a b → r a c → join r b c) :
458+
lemma transitive_join (ht : transitive r) (h : ∀ a b c, r a b → r a c → join r b c) :
431459
transitive (join r) :=
432-
assume a b c ⟨x, hax, hbx⟩ ⟨y, hby, hcy⟩,
460+
λ a b c ⟨x, hax, hbx⟩ ⟨y, hby, hcy⟩,
433461
let ⟨z, hxz, hyz⟩ := h b x y hbx hby in
434462
⟨z, ht hax hxz, ht hcy hyz⟩
435463

436464
lemma equivalence_join (hr : reflexive r) (ht : transitive r)
437-
(h : ∀a b c, r a b → r a c → join r b c) :
465+
(h : ∀ a b c, r a b → r a c → join r b c) :
438466
equivalence (join r) :=
439467
⟨reflexive_join hr, symmetric_join, transitive_join ht h⟩
440468

441469
lemma equivalence_join_refl_trans_gen
442-
(h : ∀a b c, r a b → r a c → ∃d, refl_gen r b d ∧ refl_trans_gen r c d) :
470+
(h : ∀ a b c, r a b → r a c → ∃ d, refl_gen r b d ∧ refl_trans_gen r c d) :
443471
equivalence (join (refl_trans_gen r)) :=
444-
equivalence_join reflexive_refl_trans_gen transitive_refl_trans_gen (assume a b c, church_rosser h)
472+
equivalence_join reflexive_refl_trans_gen transitive_refl_trans_gen (λ a b c, church_rosser h)
445473

446474
lemma join_of_equivalence {r' : α → α → Prop} (hr : equivalence r)
447-
(h : ∀a b, r' a b → r a b) : join r' a b → r a b
475+
(h : ∀ a b, r' a b → r a b) : join r' a b → r a b
448476
| ⟨c, hac, hbc⟩ := hr.2.2 (h _ _ hac) (hr.2.1 $ h _ _ hbc)
449477

450478
lemma refl_trans_gen_of_transitive_reflexive {r' : α → α → Prop} (hr : reflexive r)
451-
(ht : transitive r) (h : ∀a b, r' a b → r a b) (h' : refl_trans_gen r' a b) :
479+
(ht : transitive r) (h : ∀ a b, r' a b → r a b) (h' : refl_trans_gen r' a b) :
452480
r a b :=
453481
begin
454482
induction h' with b c hab hbc ih,
@@ -457,7 +485,7 @@ begin
457485
end
458486

459487
lemma refl_trans_gen_of_equivalence {r' : α → α → Prop} (hr : equivalence r) :
460-
(∀a b, r' a b → r a b) → refl_trans_gen r' a b → r a b :=
488+
(∀ a b, r' a b → r a b) → refl_trans_gen r' a b → r a b :=
461489
refl_trans_gen_of_transitive_reflexive hr.1 hr.2.2
462490

463491
end join
@@ -467,7 +495,7 @@ section eqv_gen
467495
lemma eqv_gen_iff_of_equivalence (h : equivalence r) : eqv_gen r a b ↔ r a b :=
468496
iff.intro
469497
begin
470-
assume h,
498+
intro h,
471499
induction h,
472500
case eqv_gen.rel { assumption },
473501
case eqv_gen.refl { exact h.1 _ },
@@ -477,7 +505,7 @@ iff.intro
477505
(eqv_gen.rel a b)
478506

479507
lemma eqv_gen_mono {r p : α → α → Prop}
480-
(hrp : ∀a b, r a b → p a b) (h : eqv_gen r a b) : eqv_gen p a b :=
508+
(hrp : ∀ a b, r a b → p a b) (h : eqv_gen r a b) : eqv_gen p a b :=
481509
begin
482510
induction h,
483511
case eqv_gen.rel : a b h { exact eqv_gen.rel _ _ (hrp _ _ h) },

0 commit comments

Comments
 (0)