| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Zippers for Binary Trees |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.Tree.Binary.Zipper where |
| 10 |
|
| 11 |
open import Level using (Level; _⊔_) |
| 12 |
open import Data.Tree.Binary as BT using (Tree; node; leaf) |
| 13 |
open import Data.List.Base as List using (List; []; _∷_; _++_; [_]) |
| 14 |
open import Data.Maybe.Base using (Maybe; nothing; just) |
| 15 |
open import Data.Nat.Base using (ℕ; suc; _+_) |
| 16 |
open import Data.Nat.ListAction using (sum) |
| 17 |
open import Function.Base using (_$_; _∘_) |
| 18 |
|
| 19 |
private |
| 20 |
variable |
| 21 |
a n n₁ l l₁ : Level |
| 22 |
A : Set a |
| 23 |
N : Set n |
| 24 |
N₁ : Set n₁ |
| 25 |
L : Set l |
| 26 |
L₁ : Set l₁ |
| 27 |
|
| 28 |
data Crumb (N : Set n) (L : Set l) : Set (n ⊔ l) where |
| 29 |
leftBranch : N → Tree N L → Crumb N L |
| 30 |
rightBranch : N → Tree N L → Crumb N L |
| 31 |
|
| 32 |
record Zipper (N : Set n) (L : Set l) : Set (n ⊔ l) where |
| 33 |
constructor mkZipper |
| 34 |
field |
| 35 |
context : List (Crumb N L) |
| 36 |
focus : Tree N L |
| 37 |
|
| 38 |
open Zipper public |
| 39 |
|
| 40 |
------------------------------------------------------------------------ |
| 41 |
-- Fundamental operations of a Zipper: Moving around |
| 42 |
|
| 43 |
up : Zipper N L → Maybe (Zipper N L) |
| 44 |
up (mkZipper [] foc) = nothing |
| 45 |
up (mkZipper (leftBranch m l ∷ ctx) foc) = just $ mkZipper ctx (node l m foc) |
| 46 |
up (mkZipper (rightBranch m r ∷ ctx) foc) = just $ mkZipper ctx (node foc m r) |
| 47 |
|
| 48 |
left : Zipper N L → Maybe (Zipper N L) |
| 49 |
left (mkZipper ctx (leaf x)) = nothing |
| 50 |
left (mkZipper ctx (node l m r)) = just $ mkZipper (rightBranch m r ∷ ctx) l |
| 51 |
|
| 52 |
right : Zipper N L → Maybe (Zipper N L) |
| 53 |
right (mkZipper ctx (leaf x)) = nothing |
| 54 |
right (mkZipper ctx (node l m r)) = just $ mkZipper (leftBranch m l ∷ ctx) r |
| 55 |
|
| 56 |
------------------------------------------------------------------------ |
| 57 |
-- To and from trees |
| 58 |
|
| 59 |
plug : List (Crumb N L) → Tree N L → Tree N L |
| 60 |
plug [] t = t |
| 61 |
plug (leftBranch m l ∷ xs) t = plug xs (node l m t) |
| 62 |
plug (rightBranch m r ∷ xs) t = plug xs (node t m r) |
| 63 |
|
| 64 |
toTree : Zipper N L → Tree N L |
| 65 |
toTree (mkZipper ctx foc) = plug ctx foc |
| 66 |
|
| 67 |
fromTree : Tree N L → Zipper N L |
| 68 |
fromTree = mkZipper [] |
| 69 |
|
| 70 |
------------------------------------------------------------------------ |
| 71 |
-- Tree-like operations |
| 72 |
|
| 73 |
getTree : Crumb N L → Tree N L |
| 74 |
getTree (leftBranch m x) = x |
| 75 |
getTree (rightBranch m x) = x |
| 76 |
|
| 77 |
mapCrumb : (N → N₁) → (L → L₁) → Crumb N L → Crumb N₁ L₁ |
| 78 |
mapCrumb f g (leftBranch m x) = leftBranch (f m) (BT.map f g x) |
| 79 |
mapCrumb f g (rightBranch m x) = rightBranch (f m) (BT.map f g x) |
| 80 |
|
| 81 |
#nodes : Zipper N L → ℕ |
| 82 |
#nodes (mkZipper ctx foc) = BT.#nodes foc + sum (List.map (suc ∘ BT.#nodes ∘ getTree) ctx) |
| 83 |
|
| 84 |
#leaves : Zipper N L → ℕ |
| 85 |
#leaves (mkZipper ctx foc) = BT.#leaves foc + sum (List.map (BT.#leaves ∘ getTree) ctx) |
| 86 |
|
| 87 |
map : (N → N₁) → (L → L₁) → Zipper N L → Zipper N₁ L₁ |
| 88 |
map f g (mkZipper ctx foc) = mkZipper (List.map (mapCrumb f g) ctx) (BT.map f g foc) |
| 89 |
|
| 90 |
foldr : (A → N → A → A) → (L → A) → Zipper N L → A |
| 91 |
foldr {A = A} {N = N} {L = L} f g (mkZipper ctx foc) = List.foldl step (BT.foldr f g foc) ctx |
| 92 |
where |
| 93 |
step : A → Crumb N L → A |
| 94 |
step val (leftBranch m x) = f (BT.foldr f g x) m val |
| 95 |
step val (rightBranch m x) = f val m (BT.foldr f g x) |
| 96 |
|
| 97 |
------------------------------------------------------------------------ |
| 98 |
-- Attach nodes to the top most part of the zipper |
| 99 |
|
| 100 |
attach : Zipper N L → List (Crumb N L) → Zipper N L |
| 101 |
attach (mkZipper ctx foc) xs = mkZipper (ctx ++ xs) foc |
| 102 |
|
| 103 |
infixr 5 _⟪_⟫ˡ_ |
| 104 |
infixl 5 _⟪_⟫ʳ_ |
| 105 |
|
| 106 |
_⟪_⟫ˡ_ : Tree N L → N → Zipper N L → Zipper N L |
| 107 |
l ⟪ m ⟫ˡ zp = attach zp [ leftBranch m l ] |
| 108 |
|
| 109 |
_⟪_⟫ʳ_ : Zipper N L → N → Tree N L → Zipper N L |
| 110 |
zp ⟪ m ⟫ʳ r = attach zp [ rightBranch m r ] |