| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- AVL trees where at least one element satisfies a given property |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
open import Relation.Binary.Bundles using (StrictTotalOrder) |
| 10 |
|
| 11 |
module Data.Tree.AVL.Relation.Unary.Any |
| 12 |
{a ℓ₁ ℓ₂} (strictTotalOrder : StrictTotalOrder a ℓ₁ ℓ₂) |
| 13 |
where |
| 14 |
|
| 15 |
open import Data.Product.Base as Product using (∃) |
| 16 |
open import Function.Base using (_∘_; _$_) |
| 17 |
open import Level using (Level; _⊔_) |
| 18 |
|
| 19 |
open import Relation.Nullary.Decidable using (map′) |
| 20 |
open import Relation.Unary |
| 21 |
|
| 22 |
open StrictTotalOrder strictTotalOrder renaming (Carrier to Key) |
| 23 |
open import Data.Tree.AVL.Indexed strictTotalOrder as Indexed using (K&_; _,_) |
| 24 |
open import Data.Tree.AVL strictTotalOrder using (Tree; tree; Value) |
| 25 |
import Data.Tree.AVL.Indexed.Relation.Unary.Any strictTotalOrder as AVLₚ |
| 26 |
|
| 27 |
|
| 28 |
private |
| 29 |
variable |
| 30 |
v p q : Level |
| 31 |
V : Value v |
| 32 |
t : Tree V |
| 33 |
P : Pred (K& V) p |
| 34 |
Q : Pred (K& V) q |
| 35 |
|
| 36 |
------------------------------------------------------------------------ |
| 37 |
-- Definition |
| 38 |
|
| 39 |
-- Given a predicate P, Any P t describes a path in t to an element that |
| 40 |
-- satisfies P. There may be others. |
| 41 |
-- See `Relation.Unary` for an explanation of predicates. |
| 42 |
|
| 43 |
data Any {V : Value v} (P : Pred (K& V) p) : |
| 44 |
Tree V → Set (p ⊔ a ⊔ v ⊔ ℓ₂) where |
| 45 |
tree : ∀ {h t} → AVLₚ.Any P t → Any P (tree {h = h} t) |
| 46 |
|
| 47 |
------------------------------------------------------------------------ |
| 48 |
-- Operations on Any |
| 49 |
|
| 50 |
map : P ⊆ Q → Any P t → Any Q t |
| 51 |
map f (tree p) = tree (AVLₚ.map f p) |
| 52 |
|
| 53 |
lookup : Any {V = V} P t → K& V |
| 54 |
lookup (tree p) = AVLₚ.lookup p |
| 55 |
|
| 56 |
lookupKey : Any P t → Key |
| 57 |
lookupKey (tree p) = AVLₚ.lookupKey p |
| 58 |
|
| 59 |
-- If any element satisfies P, then P is satisfied. |
| 60 |
|
| 61 |
satisfied : Any P t → ∃ P |
| 62 |
satisfied (tree p) = AVLₚ.satisfied p |
| 63 |
|
| 64 |
------------------------------------------------------------------------ |
| 65 |
-- Properties of predicates preserved by Any |
| 66 |
|
| 67 |
any? : Decidable P → Decidable (Any {V = V} P) |
| 68 |
any? P? (tree p) = map′ tree (λ where (tree p) → p) (AVLₚ.any? P? p) |
| 69 |
|
| 70 |
satisfiable : (k : Key) → Satisfiable (P ∘ (k ,_)) → Satisfiable (Any {V = V} P) |
| 71 |
satisfiable k sat = Product.map tree tree |
| 72 |
$ AVLₚ.satisfiable Indexed.⊥⁺<[ k ] Indexed.[ k ]<⊤⁺ sat |