| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Tree Zipper-related properties |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.Tree.Binary.Zipper.Properties where |
| 10 |
|
| 11 |
open import Data.List.Base as List using (List ; [] ; _∷_) |
| 12 |
open import Data.Maybe.Base using (Maybe; just; nothing) |
| 13 |
open import Data.Maybe.Relation.Unary.All using (All; just; nothing) |
| 14 |
open import Data.Nat.Base using (ℕ; suc; _+_) |
| 15 |
open import Data.Nat.Properties using (+-identityʳ; +-comm; +-assoc) |
| 16 |
open import Data.Nat.ListAction using (sum) |
| 17 |
open import Data.Tree.Binary as BT using (Tree; node; leaf) |
| 18 |
open import Data.Tree.Binary.Zipper using (Zipper; toTree; up; mkZipper; |
| 19 |
leftBranch; rightBranch; left; right; #nodes; Crumb; getTree; #leaves; |
| 20 |
map; foldr; _⟪_⟫ˡ_; _⟪_⟫ʳ_) |
| 21 |
open import Function.Base using (_on_; _∘_; _$_) |
| 22 |
open import Level using (Level) |
| 23 |
open import Relation.Binary.PropositionalEquality.Core |
| 24 |
using (_≡_; refl; cong) |
| 25 |
open import Relation.Binary.PropositionalEquality.Properties |
| 26 |
using (module ≡-Reasoning) |
| 27 |
open ≡-Reasoning |
| 28 |
|
| 29 |
private |
| 30 |
variable |
| 31 |
a n l n₁ l₁ : Level |
| 32 |
A : Set a |
| 33 |
N : Set n |
| 34 |
L : Set l |
| 35 |
N₁ : Set n₁ |
| 36 |
L₁ : Set l₁ |
| 37 |
|
| 38 |
-- Invariant: Zipper represents a given tree |
| 39 |
|
| 40 |
-- Stability under moving |
| 41 |
|
| 42 |
toTree-up-identity : (zp : Zipper N L) → All ((_≡_ on toTree) zp) (up zp) |
| 43 |
toTree-up-identity (mkZipper [] foc) = nothing |
| 44 |
toTree-up-identity (mkZipper (leftBranch m l ∷ ctx) foc) = just refl |
| 45 |
toTree-up-identity (mkZipper (rightBranch m r ∷ ctx) foc) = just refl |
| 46 |
|
| 47 |
toTree-left-identity : (zp : Zipper N L) → All ((_≡_ on toTree) zp) (left zp) |
| 48 |
toTree-left-identity (mkZipper ctx (leaf x)) = nothing |
| 49 |
toTree-left-identity (mkZipper ctx (node l m r)) = just refl |
| 50 |
|
| 51 |
toTree-right-identity : (zp : Zipper N L) → All ((_≡_ on toTree) zp) (right zp) |
| 52 |
toTree-right-identity (mkZipper ctx (leaf x)) = nothing |
| 53 |
toTree-right-identity (mkZipper ctx (node l m r)) = just refl |
| 54 |
|
| 55 |
------------------------------------------------------------------------ |
| 56 |
-- Tree-like operations indeed correspond to their counterparts |
| 57 |
|
| 58 |
toTree-#nodes : ∀ (zp : Zipper N L) → #nodes zp ≡ BT.#nodes (toTree zp) |
| 59 |
toTree-#nodes (mkZipper c v) = helper c v |
| 60 |
where |
| 61 |
helper : (cs : List (Crumb N L)) → |
| 62 |
(t : Tree N L) → |
| 63 |
#nodes (mkZipper cs t) ≡ BT.#nodes (toTree (mkZipper cs t)) |
| 64 |
helper [] foc = +-identityʳ (BT.#nodes foc) |
| 65 |
helper cs@(leftBranch m l ∷ ctx) foc = let |
| 66 |
#ctx = sum (List.map (suc ∘ BT.#nodes ∘ getTree) ctx) |
| 67 |
#foc = BT.#nodes foc |
| 68 |
#l = BT.#nodes l in begin |
| 69 |
#foc + (1 + (#l + #ctx)) ≡⟨ +-assoc #foc 1 (#l + #ctx) ⟨ |
| 70 |
#foc + 1 + (#l + #ctx) ≡⟨ cong (_+ (#l + #ctx)) (+-comm #foc 1) ⟩ |
| 71 |
1 + #foc + (#l + #ctx) ≡⟨ +-assoc (1 + #foc) #l #ctx ⟨ |
| 72 |
1 + #foc + #l + #ctx ≡⟨ cong (_+ #ctx) (+-comm (1 + #foc) #l) ⟩ |
| 73 |
#nodes (mkZipper ctx (node l m foc)) ≡⟨ helper ctx (node l m foc) ⟩ |
| 74 |
BT.#nodes (toTree (mkZipper cs foc)) ∎ |
| 75 |
helper cs@(rightBranch m r ∷ ctx) foc = let |
| 76 |
#ctx = sum (List.map (suc ∘ BT.#nodes ∘ getTree) ctx) |
| 77 |
#foc = BT.#nodes foc |
| 78 |
#r = BT.#nodes r in begin |
| 79 |
#foc + (1 + (#r + #ctx)) ≡⟨ cong (#foc +_) (+-assoc 1 #r #ctx) ⟨ |
| 80 |
#foc + (1 + #r + #ctx) ≡⟨ +-assoc #foc (suc #r) #ctx ⟨ |
| 81 |
#nodes (mkZipper ctx (node foc m r)) ≡⟨ helper ctx (node foc m r) ⟩ |
| 82 |
BT.#nodes (toTree (mkZipper cs foc)) ∎ |
| 83 |
|
| 84 |
toTree-#leaves : ∀ (zp : Zipper N L) → #leaves zp ≡ BT.#leaves (toTree zp) |
| 85 |
toTree-#leaves (mkZipper c v) = helper c v |
| 86 |
where |
| 87 |
helper : (cs : List (Crumb N L)) → |
| 88 |
(t : Tree N L) → |
| 89 |
#leaves (mkZipper cs t) ≡ BT.#leaves (toTree (mkZipper cs t)) |
| 90 |
helper [] foc = +-identityʳ (BT.#leaves foc) |
| 91 |
helper cs@(leftBranch m l ∷ ctx) foc = let |
| 92 |
#ctx = sum (List.map (BT.#leaves ∘ getTree) ctx) |
| 93 |
#foc = BT.#leaves foc |
| 94 |
#l = BT.#leaves l in begin |
| 95 |
#foc + (#l + #ctx) ≡⟨ +-assoc #foc #l #ctx ⟨ |
| 96 |
#foc + #l + #ctx ≡⟨ cong (_+ #ctx) (+-comm #foc #l) ⟩ |
| 97 |
#leaves (mkZipper ctx (node l m foc)) ≡⟨ helper ctx (node l m foc) ⟩ |
| 98 |
BT.#leaves (toTree (mkZipper cs foc)) ∎ |
| 99 |
helper cs@(rightBranch m r ∷ ctx) foc = let |
| 100 |
#ctx = sum (List.map (BT.#leaves ∘ getTree) ctx) |
| 101 |
#foc = BT.#leaves foc |
| 102 |
#r = BT.#leaves r in begin |
| 103 |
#foc + (#r + #ctx) ≡⟨ +-assoc #foc #r #ctx ⟨ |
| 104 |
#leaves (mkZipper ctx (node foc m r)) ≡⟨ helper ctx (node foc m r) ⟩ |
| 105 |
BT.#leaves (toTree (mkZipper cs foc)) ∎ |
| 106 |
|
| 107 |
toTree-map : ∀ (f : N → N₁) (g : L → L₁) zp → toTree (map f g zp) ≡ BT.map f g (toTree zp) |
| 108 |
toTree-map {N = N} {L = L} f g (mkZipper c v) = helper c v |
| 109 |
where |
| 110 |
helper : (cs : List (Crumb N L)) → |
| 111 |
(t : Tree N L) → |
| 112 |
toTree (map f g (mkZipper cs t)) ≡ BT.map f g (toTree (mkZipper cs t)) |
| 113 |
helper [] foc = refl |
| 114 |
helper (leftBranch m l ∷ ctx) foc = helper ctx (node l m foc) |
| 115 |
helper (rightBranch m r ∷ ctx) foc = helper ctx (node foc m r) |
| 116 |
|
| 117 |
toTree-foldr : ∀ (f : A → N → A → A) (g : L → A) zp → foldr f g zp ≡ BT.foldr f g (toTree zp) |
| 118 |
toTree-foldr {N = N} {L = L} f g (mkZipper c v) = helper c v |
| 119 |
where |
| 120 |
helper : (cs : List (Crumb N L)) → |
| 121 |
(t : Tree N L) → |
| 122 |
foldr f g (mkZipper cs t) ≡ BT.foldr f g (toTree (mkZipper cs t)) |
| 123 |
helper [] foc = refl |
| 124 |
helper (leftBranch m l ∷ ctx) foc = helper ctx (node l m foc) |
| 125 |
helper (rightBranch m r ∷ ctx) foc = helper ctx (node foc m r) |
| 126 |
|
| 127 |
------------------------------------------------------------------------ |
| 128 |
-- Properties of the building functions |
| 129 |
|
| 130 |
-- _⟪_⟫ˡ_ properties |
| 131 |
|
| 132 |
toTree-⟪⟫ˡ : ∀ l m (zp : Zipper N L) → toTree (l ⟪ m ⟫ˡ zp) ≡ node l m (toTree zp) |
| 133 |
toTree-⟪⟫ˡ {N = N} {L = L} l m (mkZipper c v) = helper c v |
| 134 |
where |
| 135 |
helper : (cs : List (Crumb N L)) → |
| 136 |
(t : Tree N L) → |
| 137 |
toTree (l ⟪ m ⟫ˡ mkZipper cs t) ≡ node l m (toTree $ mkZipper cs t) |
| 138 |
helper [] foc = refl |
| 139 |
helper (leftBranch m l ∷ ctx) foc = helper ctx (node l m foc) |
| 140 |
helper (rightBranch m r ∷ ctx) foc = helper ctx (node foc m r) |
| 141 |
|
| 142 |
-- _⟪_⟫ʳ_ properties |
| 143 |
|
| 144 |
toTree-⟪⟫ʳ : ∀ (zp : Zipper N L) m r → toTree (zp ⟪ m ⟫ʳ r) ≡ node (toTree zp) m r |
| 145 |
toTree-⟪⟫ʳ {N = N} {L = L} (mkZipper c v) m r = helper c v |
| 146 |
where |
| 147 |
helper : (cs : List (Crumb N L)) → |
| 148 |
(t : Tree N L) → |
| 149 |
toTree (mkZipper cs t ⟪ m ⟫ʳ r) ≡ node (toTree $ mkZipper cs t) m r |
| 150 |
helper [] foc = refl |
| 151 |
helper (leftBranch m l ∷ ctx) foc = helper ctx (node l m foc) |
| 152 |
helper (rightBranch m r ∷ ctx) foc = helper ctx (node foc m r) |
| 153 |
|
| 154 |
------------------------------------------------------------------------ |
| 155 |
-- DEPRECATED NAMES |
| 156 |
------------------------------------------------------------------------ |
| 157 |
-- Please use the new names as continuing support for the old names is |
| 158 |
-- not guaranteed. |
| 159 |
|
| 160 |
-- Version 2.0 |
| 161 |
|
| 162 |
toTree-#nodes-commute = toTree-#nodes |
| 163 |
{-# WARNING_ON_USAGE toTree-#nodes-commute |
| 164 |
"Warning: toTree-#nodes-commute was deprecated in v2.0. |
| 165 |
Please use toTree-#nodes instead." |
| 166 |
#-} |
| 167 |
|
| 168 |
toTree-#leaves-commute = toTree-#leaves |
| 169 |
{-# WARNING_ON_USAGE toTree-#leaves-commute |
| 170 |
"Warning: toTree-#leaves-commute was deprecated in v2.0. |
| 171 |
Please use toTree-#leaves instead." |
| 172 |
#-} |
| 173 |
|
| 174 |
toTree-map-commute = toTree-map |
| 175 |
{-# WARNING_ON_USAGE toTree-map-commute |
| 176 |
"Warning: toTree-map-commute was deprecated in v2.0. |
| 177 |
Please use toTree-map instead." |
| 178 |
#-} |
| 179 |
|
| 180 |
toTree-foldr-commute = toTree-foldr |
| 181 |
{-# WARNING_ON_USAGE toTree-foldr-commute |
| 182 |
"Warning: toTree-foldr-commute was deprecated in v2.0. |
| 183 |
Please use toTree-foldr instead." |
| 184 |
#-} |
| 185 |
|
| 186 |
toTree-⟪⟫ˡ-commute = toTree-⟪⟫ˡ |
| 187 |
{-# WARNING_ON_USAGE toTree-⟪⟫ˡ-commute |
| 188 |
"Warning: toTree-⟪⟫ˡ-commute was deprecated in v2.0. |
| 189 |
Please use toTree-⟪⟫ˡ instead." |
| 190 |
#-} |
| 191 |
|
| 192 |
toTree-⟪⟫ʳ-commute = toTree-⟪⟫ʳ |
| 193 |
{-# WARNING_ON_USAGE toTree-⟪⟫ʳ-commute |
| 194 |
"Warning: toTree-⟪⟫ʳ-commute was deprecated in v2.0. |
| 195 |
Please use toTree-⟪⟫ʳ instead." |
| 196 |
#-} |