Debian Codegraph @

src/Data/Tree/AVL/Indexed/Relation/Unary/All.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- AVL trees whose elements satisfy 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.Indexed.Relation.Unary.All
12 {a ℓ₁ ℓ₂} (strictTotalOrder : StrictTotalOrder a ℓ₁ ℓ₂)
13 where
14
15 open import Data.Nat.Base using (ℕ)
16 open import Data.Product.Base using (_,_; _×_)
17 open import Data.Product.Nary.NonDependent using (uncurryₙ)
18 open import Data.Sum.Base using (_⊎_; inj₁; inj₂)
19 open import Function.Base using (flip; _∘_; const)
20 open import Function.Nary.NonDependent using (congₙ)
21 open import Level using (Level; _⊔_)
22 open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl)
23 open import Relation.Nullary.Decidable.Core using (Dec; yes; map′; _×-dec_)
24 open import Relation.Unary using (Pred; Decidable; Universal; Irrelevant; _⊆_; _∪_)
25
26 open StrictTotalOrder strictTotalOrder renaming (Carrier to Key)
27 open import Data.Tree.AVL.Indexed strictTotalOrder
28 using (Tree; Value; Key⁺; [_]; _<⁺_; K&_; key; _∼_⊔_; ∼0; leaf; node)
29 open import Data.Tree.AVL.Indexed.Relation.Unary.Any strictTotalOrder
30 using (Any; here; left; right)
31
32 private
33 variable
34 v p q : Level
35 V : Value v
36 P : Pred (K& V) p
37 Q : Pred (K& V) q
38 l u : Key⁺
39 n : ℕ
40 t : Tree V l u n
41
42 ------------------------------------------------------------------------
43 -- Definition
44
45 -- Given a predicate P, All P t is a proof that all of the elements in t
46 -- satisfy P.
47 -- See `Relation.Unary` for an explanation of predicates.
48
49 data All {V : Value v} (P : Pred (K& V) p) {l u}
50 : ∀ {n} → Tree V l u n → Set (p ⊔ a ⊔ v ⊔ ℓ₂) where
51 leaf : {p : l <⁺ u} → All P (leaf p)
52 node : ∀ {hˡ hʳ h} {kv : K& V}
53 {lk : Tree V l [ kv .key ] hˡ}
54 {ku : Tree V [ kv .key ] u hʳ} →
55 {bal : hˡ ∼ hʳ ⊔ h} →
56 P kv → All P lk → All P ku → All P (node kv lk ku bal)
57
58 ------------------------------------------------------------------------
59 -- Operations on All
60
61 map : P ⊆ Q → All P t → All Q t
62 map f leaf = leaf
63 map f (node p l r) = node (f p) (map f l) (map f r)
64
65 decide : Π[ P ∪ Q ] → (t : Tree V l u n) → All P t ⊎ Any Q t
66 decide p⊎q (leaf l<u) = inj₁ leaf
67 decide p⊎q (node kv l r bal) with p⊎q kv | decide p⊎q l | decide p⊎q r
68 ... | inj₂ q | _ | _ = inj₂ (here q)
69 ... | _ | inj₂ q | _ = inj₂ (left q)
70 ... | _ | _ | inj₂ q = inj₂ (right q)
71 ... | inj₁ pv | inj₁ pl | inj₁ pr = inj₁ (node pv pl pr)
72
73 unnode : ∀ {hˡ hʳ h} {kv : K& V}
74 {lk : Tree V l [ kv .key ] hˡ}
75 {ku : Tree V [ kv .key ] u hʳ}
76 {bal : hˡ ∼ hʳ ⊔ h} →
77 All P (node kv lk ku bal) → P kv × All P lk × All P ku
78 unnode (node p l r) = p , l , r
79
80 all? : Decidable P → ∀ (t : Tree V l u n) → Dec (All P t)
81 all? p? (leaf l<u) = yes leaf
82 all? p? (node kv l r bal) = map′ (uncurryₙ 3 node) unnode
83 (p? kv ×-dec all? p? l ×-dec all? p? r)
84
85 universal : Universal P → (t : Tree V l u n) → All P t
86 universal u (leaf l<u) = leaf
87 universal u (node kv l r bal) = node (u kv) (universal u l) (universal u r)
88
89 irrelevant : Irrelevant P → (p q : All P t) → p ≡ q
90 irrelevant irr leaf leaf = refl
91 irrelevant irr (node p l₁ r₁) (node q l₂ r₂) =
92 congₙ 3 node (irr p q) (irrelevant irr l₁ l₂) (irrelevant irr r₁ r₂)