-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathComparable.agda
76 lines (62 loc) · 2.71 KB
/
Comparable.agda
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
{-# OPTIONS --rewriting #-}
open import Algebra.Cost
open import Data.Nat using (ℕ)
module Examples.Sorting.Comparable
(costMonoid : CostMonoid) (fromℕ : ℕ → CostMonoid.ℂ costMonoid) where
open CostMonoid costMonoid using (ℂ)
open import Calf costMonoid hiding (A)
open import Calf.Data.Bool using (bool)
open import Calf.Data.IsBounded costMonoid
open import Calf.Data.Product using (∃)
open import Relation.Nullary
open import Relation.Nullary.Negation
open import Relation.Nullary.Reflects
open import Relation.Binary
open import Relation.Binary.PropositionalEquality as Eq using (_≡_; refl; module ≡-Reasoning)
open import Data.Sum
open import Function
record Comparable : Set₁ where
field
A : tp⁺
_≤_ : val A → val A → Set
≤-refl : Reflexive _≤_
≤-trans : Transitive _≤_
≤-total : Total _≤_
≤-antisym : Antisymmetric _≡_ _≤_
_≤?_ : cmp (Π A λ x → Π A λ y → F (meta⁺ (Dec (x ≤ y))))
≤?-total : (x y : val A) → ◯ (∃ λ p → x ≤? y ≡ ret p)
h-cost : (x y : val A) → IsBounded (meta⁺ (Dec (x ≤ y))) (x ≤? y) (fromℕ 1)
_≥_ : val A → val A → Set
x ≥ y = y ≤ x
_≰_ : val A → val A → Set
x ≰ y = ¬ x ≤ y
≰⇒≥ : _≰_ ⇒ _≥_
≰⇒≥ ¬x≤y with ≤-total _ _
... | inj₁ x≤y = contradiction x≤y ¬x≤y
... | inj₂ y≤x = y≤x
case-≤ : {S : Set} {x y : val A} → (x ≤ y → S) → (x ≰ y → S) → Dec (x ≤ y) → S
case-≤ {S} {x} {y} yes-branch no-branch (yes x≤y) = yes-branch x≤y
case-≤ {S} {x} {y} yes-branch no-branch (no ¬x≤y) = no-branch ¬x≤y
bind/case-≤ : {x y : val A} {f : val B → cmp X} (yes-branch : x ≤ y → cmp (F B)) (no-branch : x ≰ y → cmp (F B)) (d : Dec (x ≤ y)) →
bind X (case-≤ yes-branch no-branch d) f ≡ case-≤ (λ h → bind X (yes-branch h) f) (λ h → bind X (no-branch h) f) d
bind/case-≤ yes-branch no-branch (yes x≤y) = refl
bind/case-≤ yes-branch no-branch (no ¬x≤y) = refl
case-≤/idem : {S : Set} {x y : val A} (branch : S) (d : Dec (x ≤ y)) →
case-≤ {S} {x} {y} (λ _ → branch) (λ _ → branch) d ≡ branch
case-≤/idem branch (yes x≤y) = refl
case-≤/idem branch (no ¬x≤y) = refl
NatComparable : Comparable
NatComparable = record
{ A = nat
; _≤_ = _≤_
; ≤-refl = ≤-refl
; ≤-trans = ≤-trans
; ≤-total = ≤-total
; ≤-antisym = ≤-antisym
; _≤?_ = λ x y → step (F (meta⁺ (Dec (x ≤ y)))) (fromℕ 1) (ret (x ≤? y))
; ≤?-total = λ x y u → (x ≤? y) , (step/ext (F _) (ret _) (fromℕ 1) u)
; h-cost = λ _ _ → ≤⁻-refl
}
where
open import Calf.Data.Nat
open import Data.Nat.Properties