| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- The basic code for equational reasoning with three relations: |
| 5 |
-- equality and apartness |
| 6 |
------------------------------------------------------------------------ |
| 7 |
|
| 8 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 9 |
|
| 10 |
open import Level using (Level; _⊔_) |
| 11 |
open import Function.Base using (case_of_) |
| 12 |
open import Relation.Nullary.Decidable using (Dec; yes; no) |
| 13 |
open import Relation.Binary.Core using (Rel) |
| 14 |
open import Relation.Binary.Structures using (IsEquivalence) |
| 15 |
open import Relation.Binary.Definitions using (Reflexive; Transitive; Symmetric; Trans) |
| 16 |
open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_) |
| 17 |
open import Relation.Binary.Reasoning.Syntax |
| 18 |
|
| 19 |
|
| 20 |
module Relation.Binary.Reasoning.Base.Apartness {a ℓ₁ ℓ₂} {A : Set a} |
| 21 |
{_≈_ : Rel A ℓ₁} {_#_ : Rel A ℓ₂} |
| 22 |
(≈-equiv : IsEquivalence _≈_) |
| 23 |
(#-sym : Symmetric _#_) |
| 24 |
(#-≈-trans : Trans _#_ _≈_ _#_) (≈-#-trans : Trans _≈_ _#_ _#_) |
| 25 |
where |
| 26 |
|
| 27 |
module Eq = IsEquivalence ≈-equiv |
| 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 |
nothing : x IsRelatedTo y |
| 36 |
apartness : (x#y : x # y) → x IsRelatedTo y |
| 37 |
equals : (x≈y : x ≈ y) → x IsRelatedTo y |
| 38 |
|
| 39 |
≡-go : Trans _≡_ _IsRelatedTo_ _IsRelatedTo_ |
| 40 |
≡-go x≡y nothing = nothing |
| 41 |
≡-go x≡y (apartness y#z) = apartness (case x≡y of λ where ≡.refl → y#z) |
| 42 |
≡-go x≡y (equals y≈z) = equals (case x≡y of λ where ≡.refl → y≈z) |
| 43 |
|
| 44 |
≈-go : Trans _≈_ _IsRelatedTo_ _IsRelatedTo_ |
| 45 |
≈-go x≈y nothing = nothing |
| 46 |
≈-go x≈y (apartness y#z) = apartness (≈-#-trans x≈y y#z) |
| 47 |
≈-go x≈y (equals y≈z) = equals (Eq.trans x≈y y≈z) |
| 48 |
|
| 49 |
#-go : Trans _#_ _IsRelatedTo_ _IsRelatedTo_ |
| 50 |
#-go x#y nothing = nothing |
| 51 |
#-go x#y (apartness y#z) = nothing |
| 52 |
#-go x#y (equals y≈z) = apartness (#-≈-trans x#y y≈z) |
| 53 |
|
| 54 |
stop : Reflexive _IsRelatedTo_ |
| 55 |
stop = equals Eq.refl |
| 56 |
|
| 57 |
------------------------------------------------------------------------ |
| 58 |
-- Apartness subrelation |
| 59 |
|
| 60 |
data IsApartness {x y} : x IsRelatedTo y → Set (a ⊔ ℓ₁ ⊔ ℓ₂) where |
| 61 |
isApartness : ∀ x#y → IsApartness (apartness x#y) |
| 62 |
|
| 63 |
IsApartness? : ∀ {x y} (x#y : x IsRelatedTo y) → Dec (IsApartness x#y) |
| 64 |
IsApartness? nothing = no λ() |
| 65 |
IsApartness? (apartness x#y) = yes (isApartness x#y) |
| 66 |
IsApartness? (equals x≈y) = no (λ ()) |
| 67 |
|
| 68 |
extractApartness : ∀ {x y} {x#y : x IsRelatedTo y} → IsApartness x#y → x # y |
| 69 |
extractApartness (isApartness x#y) = x#y |
| 70 |
|
| 71 |
apartnessRelation : SubRelation _IsRelatedTo_ _ _ |
| 72 |
apartnessRelation = record |
| 73 |
{ IsS = IsApartness |
| 74 |
; IsS? = IsApartness? |
| 75 |
; extract = extractApartness |
| 76 |
} |
| 77 |
|
| 78 |
------------------------------------------------------------------------ |
| 79 |
-- Equality subrelation |
| 80 |
|
| 81 |
data IsEquality {x y} : x IsRelatedTo y → Set (a ⊔ ℓ₁ ⊔ ℓ₂) where |
| 82 |
isEquality : ∀ x≈y → IsEquality (equals x≈y) |
| 83 |
|
| 84 |
IsEquality? : ∀ {x y} (x≲y : x IsRelatedTo y) → Dec (IsEquality x≲y) |
| 85 |
IsEquality? nothing = no λ() |
| 86 |
IsEquality? (apartness _) = no λ() |
| 87 |
IsEquality? (equals x≈y) = yes (isEquality x≈y) |
| 88 |
|
| 89 |
extractEquality : ∀ {x y} {x≲y : x IsRelatedTo y} → IsEquality x≲y → x ≈ y |
| 90 |
extractEquality (isEquality x≈y) = x≈y |
| 91 |
|
| 92 |
eqRelation : SubRelation _IsRelatedTo_ _ _ |
| 93 |
eqRelation = record |
| 94 |
{ IsS = IsEquality |
| 95 |
; IsS? = IsEquality? |
| 96 |
; extract = extractEquality |
| 97 |
} |
| 98 |
|
| 99 |
------------------------------------------------------------------------ |
| 100 |
-- Reasoning combinators |
| 101 |
|
| 102 |
open begin-apartness-syntax _IsRelatedTo_ apartnessRelation public |
| 103 |
open begin-equality-syntax _IsRelatedTo_ eqRelation public |
| 104 |
open ≡-syntax _IsRelatedTo_ ≡-go public |
| 105 |
open #-syntax _IsRelatedTo_ _IsRelatedTo_ #-go #-sym public |
| 106 |
open ≈-syntax _IsRelatedTo_ _IsRelatedTo_ ≈-go Eq.sym public |
| 107 |
open end-syntax _IsRelatedTo_ stop public |