Debian Codegraph @

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

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- The basic code for equational reasoning with two relations:
5 -- equality and some other 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 open import Level using (_⊔_)
14 open import Function.Base using (case_of_)
15 open import Relation.Nullary.Decidable.Core using (Dec; yes; no)
16 open import Relation.Binary.Core using (Rel; _⇒_)
17 open import Relation.Binary.Definitions using (Reflexive; Trans)
18 open import Relation.Binary.Structures using (IsPreorder)
19 open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_)
20 open import Relation.Binary.Reasoning.Syntax
21
22
23 module Relation.Binary.Reasoning.Base.Double {a ℓ₁ ℓ₂} {A : Set a}
24 {_≈_ : Rel A ℓ₁} {_≲_ : Rel A ℓ₂} (isPreorder : IsPreorder _≈_ _≲_)
25 where
26
27 open IsPreorder isPreorder
28
29 ------------------------------------------------------------------------
30 -- A datatype to hide the current relation type
31
32 infix 4 _IsRelatedTo_
33
34 data _IsRelatedTo_ (x y : A) : Set (a ⊔ ℓ₁ ⊔ ℓ₂) where
35 nonstrict : (x≲y : x ≲ y) → x IsRelatedTo y
36 equals : (x≈y : x ≈ y) → x IsRelatedTo y
37
38 start : _IsRelatedTo_ ⇒ _≲_
39 start (equals x≈y) = reflexive x≈y
40 start (nonstrict x≲y) = x≲y
41
42 ≡-go : Trans _≡_ _IsRelatedTo_ _IsRelatedTo_
43 ≡-go x≡y (equals y≈z) = equals (case x≡y of λ where ≡.refl → y≈z)
44 ≡-go x≡y (nonstrict y≤z) = nonstrict (case x≡y of λ where ≡.refl → y≤z)
45
46 ≲-go : Trans _≲_ _IsRelatedTo_ _IsRelatedTo_
47 ≲-go x≲y (equals y≈z) = nonstrict (∼-respʳ-≈ y≈z x≲y)
48 ≲-go x≲y (nonstrict y≲z) = nonstrict (trans x≲y y≲z)
49
50 ≈-go : Trans _≈_ _IsRelatedTo_ _IsRelatedTo_
51 ≈-go x≈y (equals y≈z) = equals (Eq.trans x≈y y≈z)
52 ≈-go x≈y (nonstrict y≲z) = nonstrict (∼-respˡ-≈ (Eq.sym x≈y) y≲z)
53
54 stop : Reflexive _IsRelatedTo_
55 stop = equals Eq.refl
56
57 ------------------------------------------------------------------------
58 -- A record that is used to ensure that the final relation proved by the
59 -- chain of reasoning can be converted into the required relation.
60
61 data IsEquality {x y} : x IsRelatedTo y → Set (a ⊔ ℓ₁ ⊔ ℓ₂) where
62 isEquality : ∀ x≈y → IsEquality (equals x≈y)
63
64 IsEquality? : ∀ {x y} (x≲y : x IsRelatedTo y) → Dec (IsEquality x≲y)
65 IsEquality? (nonstrict _) = no λ()
66 IsEquality? (equals x≈y) = yes (isEquality x≈y)
67
68 extractEquality : ∀ {x y} {x≲y : x IsRelatedTo y} → IsEquality x≲y → x ≈ y
69 extractEquality (isEquality x≈y) = x≈y
70
71 equalitySubRelation : SubRelation _IsRelatedTo_ _ _
72 equalitySubRelation = record
73 { IsS = IsEquality
74 ; IsS? = IsEquality?
75 ; extract = extractEquality
76 }
77
78 ------------------------------------------------------------------------
79 -- Reasoning combinators
80
81 open begin-syntax _IsRelatedTo_ start public
82 open begin-equality-syntax _IsRelatedTo_ equalitySubRelation public
83 open ≡-syntax _IsRelatedTo_ ≡-go public
84 open ≈-syntax _IsRelatedTo_ _IsRelatedTo_ ≈-go Eq.sym public
85 open ≲-syntax _IsRelatedTo_ _IsRelatedTo_ ≲-go public
86 open end-syntax _IsRelatedTo_ stop public
87
88
89 ------------------------------------------------------------------------
90 -- DEPRECATED NAMES
91 ------------------------------------------------------------------------
92 -- Please use the new names as continuing support for the old names is
93 -- not guaranteed.
94
95 -- Version 2.0
96
97 open ∼-syntax _IsRelatedTo_ _IsRelatedTo_ ≲-go public
98 {-# WARNING_ON_USAGE step-∼
99 "Warning: step-∼ and _∼⟨_⟩_ syntax was deprecated in v2.0.
100 Please use step-≲ and _≲⟨_⟩_ instead. "
101 #-}