Debian Codegraph @

src/Relation/Binary/Reasoning/Base/Triple.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- The basic code for equational reasoning with three relations:
5 -- equality, strict ordering and non-strict ordering.
6 ------------------------------------------------------------------------
7 --
8 -- See `Data.Nat.Properties` or `Relation.Binary.Reasoning.PartialOrder`
9 -- for examples of how to instantiate this module.
10
11 {-# OPTIONS --cubical-compatible --safe #-}
12
13
14 open import Relation.Binary.Core using (Rel; _⇒_)
15 open import Relation.Binary.Structures using (IsPreorder)
16 open import Relation.Binary.Definitions
17 using (Transitive; _Respects₂_; Reflexive; Trans; Irreflexive; Asymmetric)
18
19 module Relation.Binary.Reasoning.Base.Triple {a ℓ₁ ℓ₂ ℓ₃} {A : Set a}
20 {_≈_ : Rel A ℓ₁} {_≤_ : Rel A ℓ₂} {_<_ : Rel A ℓ₃}
21 (isPreorder : IsPreorder _≈_ _≤_)
22 (<-asym : Asymmetric _<_) (<-trans : Transitive _<_) (<-resp-≈ : _<_ Respects₂ _≈_)
23 (<⇒≤ : _<_ ⇒ _≤_)
24 (<-≤-trans : Trans _<_ _≤_ _<_) (≤-<-trans : Trans _≤_ _<_ _<_)
25 where
26
27 open import Data.Product.Base using (proj₁; proj₂)
28 open import Level using (_⊔_)
29 open import Function.Base using (case_of_)
30 open import Relation.Nullary.Decidable.Core
31 using (Dec; yes; no)
32 open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_)
33 open import Relation.Binary.Reasoning.Syntax
34
35 open IsPreorder isPreorder
36
37 ------------------------------------------------------------------------
38 -- A datatype to abstract over the current relation
39
40 infix 4 _IsRelatedTo_
41
42 data _IsRelatedTo_ (x y : A) : Set (a ⊔ ℓ₁ ⊔ ℓ₂ ⊔ ℓ₃) where
43 strict : (x<y : x < y) → x IsRelatedTo y
44 nonstrict : (x≤y : x ≤ y) → x IsRelatedTo y
45 equals : (x≈y : x ≈ y) → x IsRelatedTo y
46
47 start : _IsRelatedTo_ ⇒ _≤_
48 start (equals x≈y) = reflexive x≈y
49 start (nonstrict x≤y) = x≤y
50 start (strict x<y) = <⇒≤ x<y
51
52 ≡-go : Trans _≡_ _IsRelatedTo_ _IsRelatedTo_
53 ≡-go x≡y (equals y≈z) = equals (case x≡y of λ where ≡.refl → y≈z)
54 ≡-go x≡y (nonstrict y≤z) = nonstrict (case x≡y of λ where ≡.refl → y≤z)
55 ≡-go x≡y (strict y<z) = strict (case x≡y of λ where ≡.refl → y<z)
56
57 ≈-go : Trans _≈_ _IsRelatedTo_ _IsRelatedTo_
58 ≈-go x≈y (equals y≈z) = equals (Eq.trans x≈y y≈z)
59 ≈-go x≈y (nonstrict y≤z) = nonstrict (∼-respˡ-≈ (Eq.sym x≈y) y≤z)
60 ≈-go x≈y (strict y<z) = strict (proj₂ <-resp-≈ (Eq.sym x≈y) y<z)
61
62 ≤-go : Trans _≤_ _IsRelatedTo_ _IsRelatedTo_
63 ≤-go x≤y (equals y≈z) = nonstrict (∼-respʳ-≈ y≈z x≤y)
64 ≤-go x≤y (nonstrict y≤z) = nonstrict (trans x≤y y≤z)
65 ≤-go x≤y (strict y<z) = strict (≤-<-trans x≤y y<z)
66
67 <-go : Trans _<_ _IsRelatedTo_ _IsRelatedTo_
68 <-go x<y (equals y≈z) = strict (proj₁ <-resp-≈ y≈z x<y)
69 <-go x<y (nonstrict y≤z) = strict (<-≤-trans x<y y≤z)
70 <-go x<y (strict y<z) = strict (<-trans x<y y<z)
71
72 stop : Reflexive _IsRelatedTo_
73 stop = equals Eq.refl
74
75
76 ------------------------------------------------------------------------
77 -- Types that are used to ensure that the final relation proved by the
78 -- chain of reasoning can be converted into the required relation.
79
80 data IsStrict {x y} : x IsRelatedTo y → Set (a ⊔ ℓ₁ ⊔ ℓ₂ ⊔ ℓ₃) where
81 isStrict : ∀ x<y → IsStrict (strict x<y)
82
83 IsStrict? : ∀ {x y} (x≲y : x IsRelatedTo y) → Dec (IsStrict x≲y)
84 IsStrict? (strict x<y) = yes (isStrict x<y)
85 IsStrict? (nonstrict _) = no λ()
86 IsStrict? (equals _) = no λ()
87
88 extractStrict : ∀ {x y} {x≲y : x IsRelatedTo y} → IsStrict x≲y → x < y
89 extractStrict (isStrict x<y) = x<y
90
91 strictRelation : SubRelation _IsRelatedTo_ _ _
92 strictRelation = record
93 { IsS = IsStrict
94 ; IsS? = IsStrict?
95 ; extract = extractStrict
96 }
97
98 ------------------------------------------------------------------------
99 -- Equality sub-relation
100
101 data IsEquality {x y} : x IsRelatedTo y → Set (a ⊔ ℓ₁ ⊔ ℓ₂ ⊔ ℓ₃) where
102 isEquality : ∀ x≈y → IsEquality (equals x≈y)
103
104 IsEquality? : ∀ {x y} (x≲y : x IsRelatedTo y) → Dec (IsEquality x≲y)
105 IsEquality? (strict _) = no λ()
106 IsEquality? (nonstrict _) = no λ()
107 IsEquality? (equals x≈y) = yes (isEquality x≈y)
108
109 extractEquality : ∀ {x y} {x≲y : x IsRelatedTo y} → IsEquality x≲y → x ≈ y
110 extractEquality (isEquality x≈y) = x≈y
111
112 eqRelation : SubRelation _IsRelatedTo_ _ _
113 eqRelation = record
114 { IsS = IsEquality
115 ; IsS? = IsEquality?
116 ; extract = extractEquality
117 }
118
119 ------------------------------------------------------------------------
120 -- Reasoning combinators
121
122 open begin-syntax _IsRelatedTo_ start public
123 open begin-equality-syntax _IsRelatedTo_ eqRelation public
124 open begin-strict-syntax _IsRelatedTo_ strictRelation public
125 open begin-contradiction-syntax _IsRelatedTo_ strictRelation <-asym public
126 open ≡-syntax _IsRelatedTo_ ≡-go public
127 open ≈-syntax _IsRelatedTo_ _IsRelatedTo_ ≈-go Eq.sym public
128 open ≤-syntax _IsRelatedTo_ _IsRelatedTo_ ≤-go public
129 open <-syntax _IsRelatedTo_ _IsRelatedTo_ <-go public
130 open end-syntax _IsRelatedTo_ stop public