forked from jonsterling/agda-calf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Comparable.agda
60 lines (49 loc) · 2.1 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
{-# OPTIONS --prop --rewriting #-}
open import Calf.CostMonoid
open import Data.Nat using (ℕ)
module Examples.Sorting.Comparable
(costMonoid : CostMonoid) (fromℕ : ℕ → CostMonoid.ℂ costMonoid) where
open CostMonoid costMonoid using (ℂ)
open import Calf costMonoid
open import Calf.Types.Bool
open import Calf.Types.Bounded costMonoid
open import Relation.Nullary
open import Relation.Binary
open import Relation.Binary.PropositionalEquality as Eq using (_≡_; refl; module ≡-Reasoning)
open import Data.Product using (_×_; _,_; proj₁; proj₂; ∃)
open import Function
record Comparable : Set₁ where
field
A : tp pos
_≤_ : val A → val A → Set
_≤ᵇ_ : val A → val A → cmp (F bool)
≤ᵇ-reflects-≤ : ∀ {x y b} → ◯ ((x ≤ᵇ y) ≡ ret b → Reflects (x ≤ y) b)
≤-refl : Reflexive _≤_
≤-trans : Transitive _≤_
≤-total : Total _≤_
≤-antisym : Antisymmetric _≡_ _≤_
h-cost : (x y : val A) → IsBounded bool (x ≤ᵇ y) (fromℕ 1)
NatComparable : Comparable
NatComparable = record
{ A = nat
; _≤_ = _≤_
; _≤ᵇ_ = λ x y → step (F bool) (fromℕ 1) (ret (x ≤ᵇ y))
; ≤ᵇ-reflects-≤ = reflects
; ≤-refl = ≤-refl
; ≤-trans = ≤-trans
; ≤-total = ≤-total
; ≤-antisym = ≤-antisym
; h-cost = λ _ _ →
bound/relax
(λ u → CostMonoid.≤-reflexive costMonoid (CostMonoid.+-identityʳ costMonoid (fromℕ 1)))
(bound/step (fromℕ 1) (CostMonoid.zero costMonoid) bound/ret)
}
where
open import Calf.Types.Nat
open import Data.Nat
open import Data.Nat.Properties
ret-injective : ∀ {𝕊 v₁ v₂} → ret {U (meta 𝕊)} v₁ ≡ ret {U (meta 𝕊)} v₂ → v₁ ≡ v₂
ret-injective {𝕊} = Eq.cong (λ e → bind {U (meta 𝕊)} (meta 𝕊) e id)
reflects : ∀ {m n b} → ◯ (step (F bool) (fromℕ 1) (ret (m ≤ᵇ n)) ≡ ret {bool} b → Reflects (m ≤ n) b)
reflects {m} {n} {b} u h with ret-injective (Eq.subst (_≡ ret b) (step/ext (F bool) (ret (m ≤ᵇ n)) (fromℕ 1) u) h)
... | refl = ≤ᵇ-reflects-≤ m n