Skip to content

Commit 878b375

Browse files
Added a properties file for Data.Bin (#142)
1 parent 76b99a7 commit 878b375

File tree

3 files changed

+199
-124
lines changed

3 files changed

+199
-124
lines changed

CHANGELOG.md

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,32 @@ Important changes since 0.13:
88
Non-backwards compatible changes
99
--------------------------------
1010

11+
* Added new module `Data.Bin.Properties` and moved `strictTotalOrder` and
12+
`decSetoid` from `Data.Bin` to `<-strictTotalOrder` and `≡-decSetoid`
13+
in `Data.Bin.Properties`.
14+
15+
Reasons:
16+
17+
1. `Data.Bin` was becoming too large.
18+
2. Better conforms to library conventions for other numeric datatypes.
19+
1120
* Moved `decTotalOrder` in `Data.Nat` to `≤-decTotalOrder` in
1221
`Data.Nat.Properties`.
1322

1423
Reasons:
15-
1. Its old location didn't conform to the library's conventions.
16-
2. Its old location was causing dependency cyles when trying to
17-
add new ordering properties to `Data.Nat.Properties`.
24+
25+
1. Its old location was causing dependency cyles when trying to add new ordering
26+
properties to `Data.Nat.Properties`.
27+
2. Better conforms to library conventions.
1828

1929
* Moved module `≤-Reasoning` from `Data.Nat` to `Data.Nat.Properties`
2030

2131
* Moved `¬∀⟶∃¬` from `Relation.Nullary.Negation` to `Data.Fin.Dec`.
2232

2333
Reasons:
24-
1. Its old location was causing dependency cyles to form between
25-
`Data.Fin.Dec`, `Relation.Nullary.Negation` and `Data.Fin`.
34+
35+
1. Its old location was causing dependency cyles to form between `Data.Fin.Dec`,
36+
`Relation.Nullary.Negation` and `Data.Fin`.
2637

2738
* Moved existing contents of `Data.List.Any.Membership` to
2839
`Data.List.Any.Membership.Propositional.Properties` and moved internal modules
@@ -31,21 +42,23 @@ Non-backwards compatible changes
3142
respectively.
3243

3344
Reasons:
34-
1. Improves the ease of importing and opening the membership modules
35-
2. Allows the creation of a new file `Data.List.Any.Membership.Properties`
36-
for setoid membership properties.
45+
46+
1. Improves the ease of importing and opening the membership modules.
47+
2. Allows the creation of a new file `Data.List.Any.Membership.Properties`
48+
for setoid membership properties.
3749

3850
* The well-founded relation proofs for the `_<′_` relation have been renamed
3951
from `<-Rec` and `<-well-founded` to `<′-Rec` and `<′-well-founded`
4052
respectively. The original names `<-Rec` and `<-well-founded` now refer to new
4153
corresponding proofs for `_<_`.
4254

4355
Reasons:
44-
1. The old names were confusing for newcomers to the library as they
45-
would assume `<-wellfounded` referred to the standard `_<_` relation.
46-
2. Without renaming the existing proofs, there was no way of adding
47-
wellfoundedness proofs for the `_<_` relation without increasing the
48-
confusion.
56+
57+
1. The old names were confusing for newcomers to the library as they
58+
would assume `<-wellfounded` referred to the standard `_<_` relation.
59+
2. Without renaming the existing proofs, there was no way of adding
60+
wellfoundedness proofs for the `_<_` relation without increasing the
61+
confusion.
4962

5063
* Changed the implementation of `map` and `zipWith` in `Data.Vec` to use native
5164
(pattern-matching) definitions. Previously they were defined using the
@@ -54,21 +67,24 @@ Non-backwards compatible changes
5467
in `Data.Vec.Properties`.
5568

5669
Reasons:
57-
1. Better printing of goals involving `map` or `zipWith`.
58-
2. It has been argued that `zipWith` is fundamental than `_⊛_`.
70+
71+
1. Better printing of goals involving `map` or `zipWith`.
72+
2. It has been argued that `zipWith` is fundamental than `_⊛_`.
5973

6074
* Changed the implementation of `All₂` in `Data.Vec.All` to a native datatype.
6175

6276
Reasons:
63-
1. Improves pattern matching on terms
64-
2. The new datatype is more generic with respect to types and levels.
77+
78+
1. Improves pattern matching on terms.
79+
2. The new datatype is more generic with respect to types and levels.
6580

6681
* Changed the implementation of `downFrom` in `Data.List` to a native
6782
(pattern-matching) definition. Previously it was defined using a private
6883
internal module.
6984

7085
Reasons:
71-
1. Improves pattern matching.
86+
87+
1. Improves pattern matching on terms.
7288

7389
Deprecated features
7490
-------------------
@@ -184,6 +200,25 @@ Backwards compatible changes
184200
∧-∨-distribʳ : _∧_ DistributesOverʳ _∨_
185201
```
186202

203+
* Added proofs to `Data.Bin.Properties`:
204+
```agda
205+
1#-injective : as 1# ≡ bs 1# → as ≡ bs
206+
_≟_ : Decidable {A = Bin} _≡_
207+
≡-isDecEquivalence : IsDecEquivalence _≡_
208+
≡-decSetoid : DecSetoid _ _
209+
210+
<-trans : Transitive _<_
211+
<-asym : Asymmetric _<_
212+
<-irrefl : Irreflexive _≡_ _<_
213+
<-cmp : Trichotomous _≡_ _<_
214+
<-isStrictTotalOrder : IsStrictTotalOrder _≡_ _<_
215+
216+
<⇒≢ : a < b → a ≢ b
217+
1<[23] : [] 1# < (b ∷ []) 1#
218+
1<2+ : [] 1# < (b ∷ bs) 1#
219+
0<1+ : 0# < bs 1#
220+
```
221+
187222
* Added functions to `Data.Fin`:
188223
```agda
189224
punchIn i j ≈ if j≥i then j+1 else j

src/Data/Bin.agda

Lines changed: 5 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,17 @@ module Data.Bin where
88

99
open import Data.Nat as Nat
1010
using (ℕ; zero; z≤n; s≤s) renaming (suc to 1+_)
11-
import Data.Nat.Properties as NP
12-
open NP.≤-Reasoning
1311
open import Data.Digit
1412
open import Data.Fin as Fin using (Fin; zero) renaming (suc to 1+_)
1513
open import Data.Fin.Properties as FP using (_+′_)
1614
open import Data.List.Base as List hiding (downFrom)
1715
open import Function
18-
open import Data.Product
19-
open import Algebra
16+
open import Data.Product using (uncurry; _,_; _×_)
2017
open import Relation.Binary
21-
open import Relation.Binary.Consequences
22-
open import Relation.Binary.PropositionalEquality as PropEq
18+
open import Relation.Binary.PropositionalEquality
2319
using (_≡_; _≢_; refl; sym)
2420
open import Relation.Nullary
2521
open import Relation.Nullary.Decidable
26-
private
27-
module BitOrd = StrictTotalOrder (FP.strictTotalOrder 2)
2822

2923
------------------------------------------------------------------------
3024
-- The type
@@ -93,110 +87,15 @@ fromℕ : ℕ → Bin
9387
fromℕ n = fromBits $ ntoBits n
9488

9589
------------------------------------------------------------------------
96-
-- (Bin, _≡_, _<_) is a strict total order
90+
-- Order relation.
9791

98-
infix 4 _<_
92+
-- Wrapped so that the parameters can be inferred.
9993

100-
-- Order relation. Wrapped so that the parameters can be inferred.
94+
infix 4 _<_
10195

10296
data _<_ (b₁ b₂ : Bin) : Set where
10397
less : (lt : (Nat._<_ on toℕ) b₁ b₂) b₁ < b₂
10498

105-
private
106-
<-trans : Transitive _<_
107-
<-trans (less lt₁) (less lt₂) = less (NP.<-trans lt₁ lt₂)
108-
109-
asym : {b₁ b₂} b₁ < b₂ ¬ b₂ < b₁
110-
asym {b₁} {b₂} (less lt) (less gt) = tri⟶asym cmp lt gt
111-
where cmp = StrictTotalOrder.compare NP.strictTotalOrder
112-
113-
irr : {b₁ b₂} b₁ < b₂ b₁ ≢ b₂
114-
irr lt eq = asym⟶irr (PropEq.resp₂ _<_) sym asym eq lt
115-
116-
irr′ : {b} ¬ b < b
117-
irr′ lt = irr lt refl
118-
119-
∷∙ : {b₁ b₂ bs₁ bs₂}
120-
bs₁ 1# < bs₂ 1# (b₁ ∷ bs₁) 1# < (b₂ ∷ bs₂) 1#
121-
∷∙ {b₁} {b₂} {bs₁} {bs₂} (less lt) = less (begin
122-
1 + (m₁ + n₁ * 2) ≤⟨ +-mono-≤ (≤-refl {x = 1})
123-
(+-mono-≤ (≤-pred (FP.bounded b₁)) ≤-refl) ⟩
124-
1 + (1 + n₁ * 2) ≡⟨ refl ⟩
125-
suc n₁ * 2 ≤⟨ *-mono-≤ lt ≤-refl ⟩
126-
n₂ * 2 ≤⟨ n≤m+n m₂ (n₂ * 2) ⟩
127-
m₂ + n₂ * 2
128-
)
129-
where
130-
open Nat; open NP
131-
m₁ = Fin.toℕ b₁; m₂ = Fin.toℕ b₂
132-
n₁ = toℕ (bs₁ 1#); n₂ = toℕ (bs₂ 1#)
133-
134-
∙∷ : {b₁ b₂ bs}
135-
Fin._<_ b₁ b₂ (b₁ ∷ bs) 1# < (b₂ ∷ bs) 1#
136-
∙∷ {b₁} {b₂} {bs} lt = less (begin
137-
1 + (m₁ + n * 2) ≡⟨ sym (+-assoc 1 m₁ (n * 2)) ⟩
138-
(1 + m₁) + n * 2 ≤⟨ +-mono-≤ lt ≤-refl ⟩
139-
m₂ + n * 2 ∎)
140-
where
141-
open Nat; open NP
142-
m₁ = Fin.toℕ b₁; m₂ = Fin.toℕ b₂; n = toℕ (bs 1#)
143-
144-
1<[23] : {b} [] 1# < (b ∷ []) 1#
145-
1<[23] {b} = less (NP.n≤m+n (Fin.toℕ b) 2)
146-
147-
1<2+ : {bs b} [] 1# < (b ∷ bs) 1#
148-
1<2+ {[]} = 1<[23]
149-
1<2+ {b ∷ bs} = <-trans 1<[23] (∷∙ {b₁ = b} (1<2+ {bs}))
150-
151-
0<1 : 0# < [] 1#
152-
0<1 = less (s≤s z≤n)
153-
154-
0<+ : {bs} 0# < bs 1#
155-
0<+ {[]} = 0<1
156-
0<+ {b ∷ bs} = <-trans 0<1 1<2+
157-
158-
compare⁺ : Trichotomous (_≡_ on _1#) (_<_ on _1#)
159-
compare⁺ [] [] = tri≈ irr′ refl irr′
160-
compare⁺ [] (b ∷ bs) = tri< 1<2+ (irr 1<2+) (asym 1<2+)
161-
compare⁺ (b ∷ bs) [] = tri> (asym 1<2+) (irr 1<2+ ∘ sym) 1<2+
162-
compare⁺ (b₁ ∷ bs₁) (b₂ ∷ bs₂) with compare⁺ bs₁ bs₂
163-
... | tri< lt ¬eq ¬gt = tri< (∷∙ lt) (irr (∷∙ lt)) (asym (∷∙ lt))
164-
... | tri> ¬lt ¬eq gt = tri> (asym (∷∙ gt)) (irr (∷∙ gt) ∘ sym) (∷∙ gt)
165-
compare⁺ (b₁ ∷ bs) (b₂ ∷ .bs) | tri≈ ¬lt refl ¬gt with BitOrd.compare b₁ b₂
166-
compare⁺ (b ∷ bs) (.b ∷ .bs) | tri≈ ¬lt refl ¬gt | tri≈ ¬lt′ refl ¬gt′ =
167-
tri≈ irr′ refl irr′
168-
... | tri< lt′ ¬eq ¬gt′ = tri< (∙∷ lt′) (irr (∙∷ lt′)) (asym (∙∷ lt′))
169-
... | tri> ¬lt′ ¬eq gt′ = tri> (asym (∙∷ gt′)) (irr (∙∷ gt′) ∘ sym) (∙∷ gt′)
170-
171-
compare : Trichotomous _≡_ _<_
172-
compare 0# 0# = tri≈ irr′ refl irr′
173-
compare 0# (bs₂ 1#) = tri< 0<+ (irr 0<+) (asym 0<+)
174-
compare (bs₁ 1#) 0# = tri> (asym 0<+) (irr 0<+ ∘ sym) 0<+
175-
compare (bs₁ 1#) (bs₂ 1#) = compare⁺ bs₁ bs₂
176-
177-
strictTotalOrder : StrictTotalOrder _ _ _
178-
strictTotalOrder = record
179-
{ Carrier = Bin
180-
; _≈_ = _≡_
181-
; _<_ = _<_
182-
; isStrictTotalOrder = record
183-
{ isEquivalence = PropEq.isEquivalence
184-
; trans = <-trans
185-
; compare = compare
186-
}
187-
}
188-
189-
------------------------------------------------------------------------
190-
-- (Bin, _≡_) is a decidable setoid
191-
192-
decSetoid : DecSetoid _ _
193-
decSetoid = StrictTotalOrder.decSetoid strictTotalOrder
194-
195-
infix 4 _≟_
196-
197-
_≟_ : Decidable {A = Bin} _≡_
198-
_≟_ = DecSetoid._≟_ decSetoid
199-
20099
------------------------------------------------------------------------
201100
-- Arithmetic
202101

0 commit comments

Comments
 (0)