~ubuntu-branches/ubuntu/precise/agda-stdlib/precise

« back to all changes in this revision

Viewing changes to src/Relation/Binary/StrictToNonStrict.agda

  • Committer: Bazaar Package Importer
  • Author(s): Iain Lane
  • Date: 2010-01-08 23:35:09 UTC
  • Revision ID: james.westby@ubuntu.com-20100108233509-l6ejt9xji64xysfb
Import upstream version 0.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
------------------------------------------------------------------------
 
2
-- Conversion of < to ≤, along with a number of properties
 
3
------------------------------------------------------------------------
 
4
 
 
5
{-# OPTIONS --universe-polymorphism #-}
 
6
 
 
7
-- Possible TODO: Prove that a conversion ≤ → < → ≤ returns a
 
8
-- relation equivalent to the original one (and similarly for
 
9
-- < → ≤ → <).
 
10
 
 
11
open import Relation.Binary
 
12
 
 
13
module Relation.Binary.StrictToNonStrict
 
14
         {a ℓ₁ ℓ₂} {A : Set a}
 
15
         (_≈_ : Rel A ℓ₁) (_<_ : Rel A ℓ₂)
 
16
         where
 
17
 
 
18
open import Relation.Nullary
 
19
open import Relation.Binary.Consequences
 
20
open import Data.Function
 
21
open import Data.Product
 
22
open import Data.Sum
 
23
open import Data.Empty
 
24
 
 
25
------------------------------------------------------------------------
 
26
-- Conversion
 
27
 
 
28
-- _<_ can be turned into _≤_ as follows:
 
29
 
 
30
_≤_ : Rel A _
 
31
x ≤ y = (x < y) ⊎ (x ≈ y)
 
32
 
 
33
------------------------------------------------------------------------
 
34
-- The converted relations have certain properties
 
35
-- (if the original relations have certain other properties)
 
36
 
 
37
reflexive : _≈_ ⇒ _≤_
 
38
reflexive = inj₂
 
39
 
 
40
antisym : IsEquivalence _≈_ →
 
41
          Transitive _<_ →
 
42
          Irreflexive _≈_ _<_ →
 
43
          Antisymmetric _≈_ _≤_
 
44
antisym eq trans irrefl = as
 
45
  where
 
46
  module Eq = IsEquivalence eq
 
47
 
 
48
  as : Antisymmetric _≈_ _≤_
 
49
  as (inj₂ x≈y) _          = x≈y
 
50
  as (inj₁ _)   (inj₂ y≈x) = Eq.sym y≈x
 
51
  as (inj₁ x<y) (inj₁ y<x) =
 
52
    ⊥-elim (trans∧irr⟶asym {≈ = _≈_} Eq.refl trans irrefl x<y y<x)
 
53
 
 
54
trans : IsEquivalence _≈_ → _<_ Respects₂ _≈_ →
 
55
        Transitive _<_ → Transitive _≤_
 
56
trans eq <-resp-≈ <-trans = tr
 
57
  where
 
58
  module Eq = IsEquivalence eq
 
59
 
 
60
  tr : Transitive _≤_
 
61
  tr (inj₁ x<y) (inj₁ y<z) = inj₁ $ <-trans x<y y<z
 
62
  tr (inj₁ x<y) (inj₂ y≈z) = inj₁ $ proj₁ <-resp-≈ y≈z x<y
 
63
  tr (inj₂ x≈y) (inj₁ y<z) = inj₁ $ proj₂ <-resp-≈ (Eq.sym x≈y) y<z
 
64
  tr (inj₂ x≈y) (inj₂ y≈z) = inj₂ $ Eq.trans x≈y y≈z
 
65
 
 
66
≤-resp-≈ : IsEquivalence _≈_ → _<_ Respects₂ _≈_ → _≤_ Respects₂ _≈_
 
67
≤-resp-≈ eq <-resp-≈ = ((λ {_ _ _} → resp₁) , (λ {_ _ _} → resp₂))
 
68
  where
 
69
  module Eq = IsEquivalence eq
 
70
 
 
71
  resp₁ : ∀ {x y' y} → y' ≈ y → x  ≤ y' → x ≤ y
 
72
  resp₁ y'≈y (inj₁ x<y') = inj₁ (proj₁ <-resp-≈ y'≈y x<y')
 
73
  resp₁ y'≈y (inj₂ x≈y') = inj₂ (Eq.trans x≈y' y'≈y)
 
74
 
 
75
  resp₂ : ∀ {y x' x} → x' ≈ x → x' ≤ y  → x ≤ y
 
76
  resp₂ x'≈x (inj₁ x'<y) = inj₁ (proj₂ <-resp-≈ x'≈x x'<y)
 
77
  resp₂ x'≈x (inj₂ x'≈y) = inj₂ (Eq.trans (Eq.sym x'≈x) x'≈y)
 
78
 
 
79
total : Trichotomous _≈_ _<_ → Total _≤_
 
80
total <-tri x y with <-tri x y
 
81
... | tri< x<y x≉y x≯y = inj₁ (inj₁ x<y)
 
82
... | tri≈ x≮y x≈y x≯y = inj₁ (inj₂ x≈y)
 
83
... | tri> x≮y x≉y x>y = inj₂ (inj₁ x>y)
 
84
 
 
85
decidable : Decidable _≈_ → Decidable _<_ → Decidable _≤_
 
86
decidable ≈-dec <-dec x y with ≈-dec x y | <-dec x y
 
87
... | yes x≈y | _       = yes (inj₂ x≈y)
 
88
... | no  x≉y | yes x<y = yes (inj₁ x<y)
 
89
... | no  x≉y | no  x≮y = no helper
 
90
  where
 
91
  helper : x ≤ y → ⊥
 
92
  helper (inj₁ x<y) = x≮y x<y
 
93
  helper (inj₂ x≈y) = x≉y x≈y
 
94
 
 
95
decidable' : Trichotomous _≈_ _<_ → Decidable _≤_
 
96
decidable' compare x y with compare x y
 
97
... | tri< x<y _   _ = yes (inj₁ x<y)
 
98
... | tri≈ _   x≈y _ = yes (inj₂ x≈y)
 
99
... | tri> x≮y x≉y _ = no helper
 
100
  where
 
101
  helper : x ≤ y → ⊥
 
102
  helper (inj₁ x<y) = x≮y x<y
 
103
  helper (inj₂ x≈y) = x≉y x≈y