Skip to content

Commit d0841d1

Browse files
authored
[ new ] Basic unary predicates for Data.Tree.AVL (#1385)
1 parent d0d71c1 commit d0841d1

File tree

20 files changed

+813
-89
lines changed

20 files changed

+813
-89
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ matrix:
4848
cd ../ &&
4949
git clone https://github.com/agda/agda &&
5050
cd agda &&
51-
git checkout tags/v2.6.1.1 &&
51+
git checkout tags/v2.6.1.3 &&
5252
cabal install --only-dependencies --dry -v > $HOME/installplan.txt ;
5353
fi
5454

CHANGELOG.md

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Version 1.6-dev
22
===============
33

4-
The library has been tested using Agda 2.6.1 and 2.6.1.1.
4+
The library has been tested using Agda 2.6.1 and 2.6.1.3.
55

66
Highlights
77
----------
@@ -15,23 +15,23 @@ Highlights
1515
Bug-fixes
1616
---------
1717

18-
* Despite being designed for use with non-reflexive relations, the combinators
18+
* Despite being designed for use with non-reflexive relations, the combinators
1919
in `Relation.Binary.Reasoning.Base.Partial` required users to provide a proof
2020
of reflexivity of the relation over the last element in the chain:
2121
```agda
22-
begin
22+
begin
2323
x ⟨ x∼y ⟩
2424
y ∎⟨ y∼y ⟩
2525
```
2626
These have now been redefined so that this proof is no longer needed:
2727
```agda
28-
begin
28+
begin
2929
x ⟨ x∼y ⟩
3030
y ∎
3131
```
32-
For direct users of `Relation.Binary.Reasoning.PartialSetoid` API this is a
32+
For direct users of `Relation.Binary.Reasoning.PartialSetoid` API this is a
3333
backwards compatible change as the `_∎⟨_⟩` combinator has simply been deprecated. For
34-
users who were building their own reasoning combinators on top of
34+
users who were building their own reasoning combinators on top of
3535
`Relation.Binary.Reasoning.Base.Partial`, they will need to adjust their additional
3636
combinators to use the new `singleStep`/`multiStep` constructors of `_IsRelatedTo_`.
3737

@@ -48,6 +48,9 @@ Bug-fixes
4848
Their definitions have now been moved out of the anonymous modules so that they no
4949
longer require these unnecessary arguments.
5050

51+
* In `Relation.Binary.Reasoning.StrictPartialOrder` filled a missing argument to the
52+
re-exported `Relation.Binary.Reasoning.Base.Triple`.
53+
5154
Non-backwards compatible changes
5255
--------------------------------
5356

@@ -67,6 +70,12 @@ Non-backwards compatible changes
6770
only a breaking change if you were supplying the module arguments upon import, in which
6871
case you will have to change to supplying them upon application of the proofs.
6972

73+
* In `Data.Tree.AVL.Indexed` the type alias `K&_` defined in terms of `Σ` has been changed
74+
into a standalone record to help with parameter inference. The record constructor remains
75+
the same so you will only observe the change if you are using functions explicitly expecting
76+
a pair (e.g. `(un)curry`). In this case you can use `Data.Tree.AVL.Value`'s `(to/from)Pair`
77+
to convert back and forth.
78+
7079
Deprecated modules
7180
------------------
7281

@@ -221,6 +230,18 @@ New modules
221230
Data.Tree.Binary.Show
222231
```
223232

233+
* Added new modules in `Data.Tree.AVL`:
234+
```
235+
Data.Tree.AVL.Indexed.Relation.Unary.All
236+
237+
Data.Tree.AVL.Relation.Unary.Any
238+
239+
Data.Tree.AVL.Indexed.Relation.Unary.Any
240+
Data.Tree.AVL.Indexed.Relation.Unary.Any.Properties
241+
242+
Data.Tree.AVL.Map.Relation.Unary.Any
243+
```
244+
224245
* Bundles for binary relation morphisms
225246
```
226247
Relation.Binary.Morphism.Bundles
@@ -979,6 +1000,17 @@ Other minor additions
9791000
Main : Set
9801001
```
9811002

1003+
* Added new functions and pattern synonyms to `Data.Tree.AVL.Indexed`:
1004+
```agda
1005+
foldr : (∀ {k} → Val k → A → A) → A → Tree V l u h → A
1006+
size : Tree V → ℕ
1007+
1008+
pattern node⁺ k₁ t₁ k₂ t₂ t₃ bal = node k₁ t₁ (node k₂ t₂ t₃ bal) ∼+
1009+
pattern node⁻ k₁ k₂ t₁ t₂ bal t₃ = node k₁ (node k₂ t₁ t₂ bal) t₃ ∼-
1010+
1011+
ordered : Tree V l u n → l <⁺ u
1012+
```
1013+
9821014
* Added new functions to `Codata.Stream`:
9831015
```agda
9841016
nats : Stream ℕ ∞
@@ -998,6 +1030,18 @@ Other minor additions
9981030
record IsTotalPreorder (_≲_ : Rel A ℓ₂) : Set (a ⊔ ℓ ⊔ ℓ₂)
9991031
```
10001032

1033+
* Re-exported and defined new functions in `Data.Tree.AVL.Key`:
1034+
```agda
1035+
_≈⁺_ : Rel Key _
1036+
[_]ᴱ : x ≈ y → [ x ] ≈⁺ [ y ]
1037+
refl⁺ : Reflexive _≈⁺_
1038+
sym⁺ : l ≈⁺ u → u ≈⁺ l
1039+
irrefl⁺ : ∀ k → ¬ (k <⁺ k)
1040+
1041+
strictPartialOrder : StrictPartialOrder _ _ _
1042+
strictTotalOrder : StrictTotalOrder _ _ _
1043+
```
1044+
10011045
* Added new proofs to `Relation.Binary.Properties.Poset`:
10021046
```agda
10031047
mono⇒cong : f Preserves _≤_ ⟶ _≤_ → f Preserves _≈_ ⟶ _≈_
@@ -1028,7 +1072,7 @@ Other minor additions
10281072
setoidHomomorphism : SetoidHomomorphism S T → SetoidHomomorphism T U → SetoidHomomorphism S U
10291073
setoidMonomorphism : SetoidMonomorphism S T → SetoidMonomorphism T U → SetoidMonomorphism S U
10301074
setoidIsomorphism : SetoidIsomorphism S T → SetoidIsomorphism T U → SetoidIsomorphism S U
1031-
1075+
10321076
preorderHomomorphism : PreorderHomomorphism P Q → PreorderHomomorphism Q R → PreorderHomomorphism P R
10331077
posetHomomorphism : PosetHomomorphism P Q → PosetHomomorphism Q R → PosetHomomorphism P R
10341078
```
@@ -1038,7 +1082,7 @@ Other minor additions
10381082
setoidHomomorphism : (S : Setoid a ℓ₁) → SetoidHomomorphism S S
10391083
setoidMonomorphism : (S : Setoid a ℓ₁) → SetoidMonomorphism S S
10401084
setoidIsomorphism : (S : Setoid a ℓ₁) → SetoidIsomorphism S S
1041-
1085+
10421086
preorderHomomorphism : (P : Preorder a ℓ₁ ℓ₂) → PreorderHomomorphism P P
10431087
posetHomomorphism : (P : Poset a ℓ₁ ℓ₂) → PosetHomomorphism P P
10441088
```

README/Data/Tree/AVL.agda

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import Data.Tree.AVL
2020
-- natural numbers as keys and vectors of strings as values.
2121

2222
open import Data.Nat.Properties using (<-strictTotalOrder)
23+
open import Data.Product as Prod using (_,_; _,′_)
2324
open import Data.String using (String)
2425
open import Data.Vec using (Vec; _∷_; [])
2526
open import Relation.Binary.PropositionalEquality
@@ -60,7 +61,6 @@ t₃ = delete 2 t₂
6061
-- Conversion of a list of key-value mappings to a tree.
6162

6263
open import Data.List using (_∷_; [])
63-
open import Data.Product as Prod using (_,_; _,′_)
6464

6565
t₄ : Tree
6666
t₄ = fromList ((2 , v₂) ∷ (1 , v₁) ∷ [])
@@ -111,14 +111,14 @@ open import Function.Base using (id)
111111
v₆ : headTail t₀ ≡ nothing
112112
v₆ = refl
113113

114-
v₇ : Maybe.map (Prod.map id toList) (headTail t₂) ≡
114+
v₇ : Maybe.map (Prod.map toList) (headTail t₂) ≡
115115
just ((1 , v₁) , ((2 , v₂) ∷ []))
116116
v₇ = refl
117117

118118
v₈ : initLast t₀ ≡ nothing
119119
v₈ = refl
120120

121-
v₉ : Maybe.map (Prod.map toList id) (initLast t₄) ≡
121+
v₉ : Maybe.map (Prod.map toList) (initLast t₄) ≡
122122
just (((1 , v₁) ∷ []) ,′ (2 , v₂))
123123
v₉ = refl
124124

src/Data/Integer/Properties.agda

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2437,8 +2437,6 @@ Please use *-cancelˡ-<-nonNeg instead."
24372437
Please use *-cancelʳ-<-nonNeg instead."
24382438
#-}
24392439

2440-
2441-
24422440
-- Version 1.6
24432441

24442442
m≤n⇒m⊓n≡m = i≤j⇒i⊓j≡i

src/Data/Tree/AVL.agda

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ private
3636

3737
open StrictTotalOrder strictTotalOrder renaming (Carrier to Key)
3838
import Data.Tree.AVL.Indexed strictTotalOrder as Indexed
39-
open Indexed using (K&_ ; ⊥⁺ ; ⊤⁺; ⊥⁺<⊤⁺; ⊥⁺<[_]<⊤⁺; ⊥⁺<[_]; [_]<⊤⁺)
39+
open Indexed using (⊥⁺; ⊤⁺; ⊥⁺<⊤⁺; ⊥⁺<[_]<⊤⁺; ⊥⁺<[_]; [_]<⊤⁺)
4040

4141
------------------------------------------------------------------------
4242
-- Re-export some core definitions publically
4343

44-
open Indexed using (Value; MkValue; const) public
44+
open Indexed using (K&_;_,_; toPair; fromPair; Value; MkValue; const) public
4545

4646
------------------------------------------------------------------------
4747
-- Types and functions with hidden indices
@@ -92,12 +92,12 @@ module _ {v} {V : Value v} where
9292
_∈?_ : Key Tree V Bool
9393
k ∈? t = is-just (lookup k t)
9494

95-
headTail : Tree V Maybe ((K& V) × Tree V)
95+
headTail : Tree V Maybe (K& V × Tree V)
9696
headTail (tree (Indexed.leaf _)) = nothing
9797
headTail (tree {h = suc _} t) with Indexed.headTail t
9898
... | (k , _ , _ , t′) = just (k , tree (Indexed.castˡ ⊥⁺<[ _ ] t′))
9999

100-
initLast : Tree V Maybe (Tree V × (K& V))
100+
initLast : Tree V Maybe (Tree V × K& V)
101101
initLast (tree (Indexed.leaf _)) = nothing
102102
initLast (tree {h = suc _} t) with Indexed.initLast t
103103
... | (k , _ , _ , t′) = just (tree (Indexed.castʳ t′ [ _ ]<⊤⁺) , k)
@@ -108,7 +108,7 @@ module _ {v} {V : Value v} where
108108
-- The input does not need to be ordered.
109109

110110
fromList : List (K& V) Tree V
111-
fromList = List.foldr (uncurry insert) empty
111+
fromList = List.foldr (uncurry insert ∘′ toPair) empty
112112

113113
-- Returns an ordered list.
114114

src/Data/Tree/AVL/Indexed.agda

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ open import Data.Product using (Σ; ∃; _×_; _,_; proj₁)
1717
open import Data.Maybe.Base using (Maybe; just; nothing)
1818
open import Data.List.Base as List using (List)
1919
open import Data.DifferenceList using (DiffList; []; _∷_; _++_)
20-
open import Function as F hiding (const)
20+
open import Function.Base as F hiding (const)
2121
open import Relation.Unary
2222
open import Relation.Binary using (_Respects_; Tri; tri<; tri≈; tri>)
2323
open import Relation.Binary.PropositionalEquality using (_≡_; refl)
2424

2525
private
2626
variable
27-
l : Level
27+
l v : Level
2828
A : Set l
2929

3030
open StrictTotalOrder strictTotalOrder renaming (Carrier to Key)
@@ -39,9 +39,6 @@ open import Data.Tree.AVL.Height public
3939
------------------------------------------------------------------------
4040
-- Definitions of the tree
4141

42-
K&_ : {v} (V : Value v) Set (a ⊔ v)
43-
K& V = Σ Key (Value.family V)
44-
4542
-- The trees have three parameters/indices: a lower bound on the
4643
-- keys, an upper bound, and a height.
4744
--
@@ -50,14 +47,18 @@ K& V = Σ Key (Value.family V)
5047
data Tree {v} (V : Value v) (l u : Key⁺) : Set (a ⊔ v ⊔ ℓ₂) where
5148
leaf : (l<u : l <⁺ u) Tree V l u 0
5249
node : {hˡ hʳ h}
53-
(k : K& V)
54-
(lk : Tree V l [ proj₁ k ] hˡ)
55-
(ku : Tree V [ proj₁ k ] u hʳ)
50+
(kv : K& V)
51+
(lk : Tree V l [ kv .key ] hˡ)
52+
(ku : Tree V [ kv .key ] u hʳ)
5653
(bal : hˡ ∼ hʳ ⊔ h)
5754
Tree V l u (suc h)
5855

5956
module _ {v} {V : Value v} where
6057

58+
ordered : {l u n} Tree V l u n l <⁺ u
59+
ordered (leaf l<u) = l<u
60+
ordered (node kv lk ku bal) = trans⁺ _ (ordered lk) (ordered ku)
61+
6162
private
6263
Val = Value.family V
6364
V≈ = Value.respects V
@@ -67,8 +68,8 @@ module _ {v} {V : Value v} where
6768

6869
node-injective-key :
6970
{hˡ₁ hˡ₂ hʳ₁ hʳ₂ h l u k₁ k₂}
70-
{lk₁ : Tree V l [ proj₁ k₁ ] hˡ₁} {lk₂ : Tree V l [ proj₁ k₂ ] hˡ₂}
71-
{ku₁ : Tree V [ proj₁ k₁ ] u hʳ₁} {ku₂ : Tree V [ proj₁ k₂ ] u hʳ₂}
71+
{lk₁ : Tree V l [ k₁ .key ] hˡ₁} {lk₂ : Tree V l [ k₂ .key ] hˡ₂}
72+
{ku₁ : Tree V [ k₁ .key ] u hʳ₁} {ku₂ : Tree V [ k₂ .key ] u hʳ₂}
7273
{bal₁ : hˡ₁ ∼ hʳ₁ ⊔ h} {bal₂ : hˡ₂ ∼ hʳ₂ ⊔ h}
7374
node k₁ lk₁ ku₁ bal₁ ≡ node k₂ lk₂ ku₂ bal₂ k₁ ≡ k₂
7475
node-injective-key refl = refl
@@ -97,46 +98,46 @@ module _ {v} {V : Value v} where
9798
-- Various constant-time functions which construct trees out of
9899
-- smaller pieces, sometimes using rotation.
99100

101+
pattern node⁺ k₁ t₁ k₂ t₂ t₃ bal = node k₁ t₁ (node k₂ t₂ t₃ bal) ∼+
102+
100103
joinˡ⁺ : {l u hˡ hʳ h}
101104
(k : K& V)
102-
(∃ λ i Tree V l [ proj₁ k ] (i ⊕ hˡ))
103-
Tree V [ proj₁ k ] u hʳ
105+
(∃ λ i Tree V l [ k .key ] (i ⊕ hˡ))
106+
Tree V [ k .key ] u hʳ
104107
(bal : hˡ ∼ hʳ ⊔ h)
105108
λ i Tree V l u (i ⊕ (1 + h))
106-
joinˡ⁺ k₆ (1# , node k₂ t₁
107-
(node k₄ t₃ t₅ bal)
108-
∼+) t₇ ∼- = (0# , node k
109-
(node k₂ t₁ t₃ (max∼ bal))
110-
(node k₆ t₅ t₇ (∼max bal))
111-
∼0)
112-
joinˡ⁺ k₄ (1# , node k₂ t₁ t₃ ∼-) t₅ ∼- = (0# , node k₂ t₁ (node k₄ t₃ t₅ ∼0) ∼0)
113-
joinˡ⁺ k₄ (1# , node k₂ t₁ t₃ ∼0) t₅ ∼- = (1# , node k₂ t₁ (node k₄ t₃ t₅ ∼-) ∼+)
114-
joinˡ⁺ k₂ (1# , t₁) t₃ ∼0 = (1# , node k₂ t₁ t₃ ∼-)
115-
joinˡ⁺ k₂ (1# , t₁) t₃ ∼+ = (0# , node k₂ t₁ t₃ ∼0)
116-
joinˡ⁺ k₂ (0# , t₁) t₃ bal = (0# , node k₂ t₁ t bal)
109+
joinˡ⁺ k₂ (0# , t₁) t₃ bal = (0# , node k₂ t₁ t₃ bal)
110+
joinˡ⁺ k₂ (1# , t₁) t₃ ∼0 = (1# , node k₂ t₁ t₃ ∼-)
111+
joinˡ⁺ k₂ (1# , t₁) t₃ ∼+ = (0# , node k₂ t₁ t₃ ∼0)
112+
joinˡ⁺ k₄ (1# , node k₂ t₁ t₃ ∼-) t₅ ∼- = (0# , node k₂ t₁ (node k₄ t₃ t₅ ∼0) ∼0)
113+
joinˡ⁺ k₄ (1# , node k₂ t₁ t₃ ∼0) t₅ ∼- = (1# , node k₂ t₁ (node k₄ t₃ t₅ ∼-) ∼+)
114+
joinˡ⁺ k₆ (1# , node⁺ k₂ t₁ k₄ t₃ t₅ bal) t₇ ∼-
115+
= (0# , node k₄ (node k₂ t₁ t₃ (max∼ bal))
116+
(node k₆ t₅ t₇ (∼max bal))
117+
∼0)
118+
119+
pattern node⁻ k₁ k₂ t₁ t₂ bal t₃ = node k₁ (node k₂ t₁ t bal) t₃ ∼-
117120

118121
joinʳ⁺ : {l u hˡ hʳ h}
119122
(k : K& V)
120-
Tree V l [ proj₁ k ] hˡ
121-
(∃ λ i Tree V [ proj₁ k ] u (i ⊕ hʳ))
123+
Tree V l [ k .key ] hˡ
124+
(∃ λ i Tree V [ k .key ] u (i ⊕ hʳ))
122125
(bal : hˡ ∼ hʳ ⊔ h)
123126
λ i Tree V l u (i ⊕ (1 + h))
124-
joinʳ⁺ k₂ t₁ (1# , node k₆
125-
(node k₄ t₃ t₅ bal)
126-
t₇ ∼-) ∼+ = (0# , node k₄
127-
(node k₂ t₁ t₃ (max∼ bal))
128-
(node k₆ t₅ t₇ (∼max bal))
129-
∼0)
130-
joinʳ⁺ k₂ t₁ (1# , node k₄ t₃ t₅ ∼+) ∼+ = (0# , node k₄ (node k₂ t₁ t₃ ∼0) t₅ ∼0)
131-
joinʳ⁺ k₂ t₁ (1# , node k₄ t₃ t₅ ∼0) ∼+ = (1# , node k₄ (node k₂ t₁ t₃ ∼+) t₅ ∼-)
127+
joinʳ⁺ k₂ t₁ (0# , t₃) bal = (0# , node k₂ t₁ t₃ bal)
132128
joinʳ⁺ k₂ t₁ (1# , t₃) ∼0 = (1# , node k₂ t₁ t₃ ∼+)
133129
joinʳ⁺ k₂ t₁ (1# , t₃) ∼- = (0# , node k₂ t₁ t₃ ∼0)
134-
joinʳ⁺ k₂ t₁ (0# , t₃) bal = (0# , node k₂ t₁ t₃ bal)
130+
joinʳ⁺ k₂ t₁ (1# , node k₄ t₃ t₅ ∼+) ∼+ = (0# , node k₄ (node k₂ t₁ t₃ ∼0) t₅ ∼0)
131+
joinʳ⁺ k₂ t₁ (1# , node k₄ t₃ t₅ ∼0) ∼+ = (1# , node k₄ (node k₂ t₁ t₃ ∼+) t₅ ∼-)
132+
joinʳ⁺ k₂ t₁ (1# , node⁻ k₆ k₄ t₃ t₅ bal t₇) ∼+
133+
= (0# , node k₄ (node k₂ t₁ t₃ (max∼ bal))
134+
(node k₆ t₅ t₇ (∼max bal))
135+
∼0)
135136

136137
joinˡ⁻ : {l u} hˡ {hʳ h}
137138
(k : K& V)
138-
(∃ λ i Tree V l [ proj₁ k ] pred[ i ⊕ hˡ ])
139-
Tree V [ proj₁ k ] u hʳ
139+
(∃ λ i Tree V l [ k .key ] pred[ i ⊕ hˡ ])
140+
Tree V [ k .key ] u hʳ
140141
(bal : hˡ ∼ hʳ ⊔ h)
141142
λ i Tree V l u (i ⊕ h)
142143
joinˡ⁻ zero k₂ (0# , t₁) t₃ bal = (1# , node k₂ t₁ t₃ bal)
@@ -148,8 +149,8 @@ module _ {v} {V : Value v} where
148149

149150
joinʳ⁻ : {l u hˡ} hʳ {h}
150151
(k : K& V)
151-
Tree V l [ proj₁ k ] hˡ
152-
(∃ λ i Tree V [ proj₁ k ] u pred[ i ⊕ hʳ ])
152+
Tree V l [ k .key ] hˡ
153+
(∃ λ i Tree V [ k .key ] u pred[ i ⊕ hʳ ])
153154
(bal : hˡ ∼ hʳ ⊔ h)
154155
λ i Tree V l u (i ⊕ h)
155156
joinʳ⁻ zero k₂ t₁ (0# , t₃) bal = (1# , node k₂ t₁ t₃ bal)
@@ -163,8 +164,8 @@ module _ {v} {V : Value v} where
163164
-- Logarithmic in the size of the tree.
164165

165166
headTail : {l u h} Tree V l u (1 + h)
166-
λ (k : K& V) l <⁺ [ proj₁ k ] ×
167-
λ i Tree V [ proj₁ k ] u (i ⊕ h)
167+
λ (k : K& V) l <⁺ [ k .key ] ×
168+
λ i Tree V [ k .key ] u (i ⊕ h)
168169
headTail (node k₁ (leaf l<k₁) t₂ ∼+) = (k₁ , l<k₁ , 0# , t₂)
169170
headTail (node k₁ (leaf l<k₁) t₂ ∼0) = (k₁ , l<k₁ , 0# , t₂)
170171
headTail (node {hˡ = suc _} k₃ t₁₂ t₄ bal) with headTail t₁₂
@@ -174,8 +175,8 @@ module _ {v} {V : Value v} where
174175
-- Logarithmic in the size of the tree.
175176

176177
initLast : {l u h} Tree V l u (1 + h)
177-
λ (k : K& V) [ proj₁ k ] <⁺ u ×
178-
λ i Tree V l [ proj₁ k ] (i ⊕ h)
178+
λ (k : K& V) [ k .key ] <⁺ u ×
179+
λ i Tree V l [ k .key ] (i ⊕ h)
179180
initLast (node k₂ t₁ (leaf k₂<u) ∼-) = (k₂ , k₂<u , (0# , t₁))
180181
initLast (node k₂ t₁ (leaf k₂<u) ∼0) = (k₂ , k₂<u , (0# , t₁))
181182
initLast (node {hʳ = suc _} k₂ t₁ t₃₄ bal) with initLast t₃₄

0 commit comments

Comments
 (0)