Debian Codegraph @

src/Data/List/Zipper.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- List Zippers, basic types and operations
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.List.Zipper where
10
11 open import Data.Nat.Base using (ℕ; suc; zero; _+_)
12 open import Data.Maybe.Base as Maybe using (Maybe ; just ; nothing)
13 open import Data.List.Base as List using (List ; [] ; _∷_)
14 open import Function.Base using (_on_; flip)
15
16
17 -- Definition
18 ------------------------------------------------------------------------
19
20 -- A List Zipper represents a List together with a particular sub-List
21 -- in focus. The user can attempt to move the focus left or right, with
22 -- a risk of failure if one has already reached the corresponding end.
23
24 -- To make these operations efficient, the `context` the sub List in
25 -- focus lives in is stored *backwards*. This is made formal by `toList`
26 -- which returns the List a Zipper represents.
27
28 record Zipper {a} (A : Set a) : Set a where
29 constructor mkZipper
30 field context : List A
31 value : List A
32
33 toList : List A
34 toList = List.reverse context List.++ value
35 open Zipper public
36
37 -- Embedding Lists as Zippers without any context
38 fromList : ∀ {a} {A : Set a} → List A → Zipper A
39 fromList = mkZipper []
40
41 -- Fundamental operations of a Zipper: Moving around
42 ------------------------------------------------------------------------
43
44 module _ {a} {A : Set a} where
45
46 left : Zipper A → Maybe (Zipper A)
47 left (mkZipper [] val) = nothing
48 left (mkZipper (x ∷ ctx) val) = just (mkZipper ctx (x ∷ val))
49
50 right : Zipper A → Maybe (Zipper A)
51 right (mkZipper ctx []) = nothing
52 right (mkZipper ctx (x ∷ val)) = just (mkZipper (x ∷ ctx) val)
53
54
55 -- Focus-respecting operations
56 ------------------------------------------------------------------------
57
58 module _ {a} {A : Set a} where
59
60 reverse : Zipper A → Zipper A
61 reverse (mkZipper ctx val) = mkZipper val ctx
62
63 -- If we think of a List [x₁⋯xₘ] split into a List [xₙ₊₁⋯xₘ] in focus
64 -- of another list [x₁⋯xₙ] then there are 4 places (marked {k} here) in
65 -- which we can insert new values: [{1}x₁⋯xₙ{2}][{3}xₙ₊₁⋯xₘ{4}]
66
67 -- The following 4 functions implement these 4 insertions.
68
69 -- `xs ˢ++ zp` inserts `xs` on the `s` side of the context of the Zipper `zp`
70 -- `zp ++ˢ xs` insert `xs` on the `s` side of the value in focus of the Zipper `zp`
71
72 infixr 5 _ˡ++_ _ʳ++_
73 infixl 5 _++ˡ_ _++ʳ_
74 -- {1}
75 _ˡ++_ : List A → Zipper A → Zipper A
76 xs ˡ++ mkZipper ctx val = mkZipper (ctx List.++ List.reverse xs) val
77
78 -- {2}
79 _ʳ++_ : List A → Zipper A → Zipper A
80 xs ʳ++ mkZipper ctx val = mkZipper (List.reverse xs List.++ ctx) val
81
82 -- {3}
83 _++ˡ_ : Zipper A → List A → Zipper A
84 mkZipper ctx val ++ˡ xs = mkZipper ctx (xs List.++ val)
85
86 -- {4}
87 _++ʳ_ : Zipper A → List A → Zipper A
88 mkZipper ctx val ++ʳ xs = mkZipper ctx (val List.++ xs)
89
90
91 -- List-like operations
92 ------------------------------------------------------------------------
93
94 module _ {a} {A : Set a} where
95
96 length : Zipper A → ℕ
97 length (mkZipper ctx val) = List.length ctx + List.length val
98
99 module _ {a b} {A : Set a} {B : Set b} where
100
101 map : (A → B) → Zipper A → Zipper B
102 map f (mkZipper ctx val) = (mkZipper on List.map f) ctx val
103
104 foldr : (A → B → B) → B → Zipper A → B
105 foldr c n (mkZipper ctx val) = List.foldl (flip c) (List.foldr c n val) ctx
106
107
108 -- Generating all the possible foci of a list
109 ------------------------------------------------------------------------
110
111 module _ {a} {A : Set a} where
112
113 allFociIn : List A → List A → List (Zipper A)
114 allFociIn ctx [] = List.[ mkZipper ctx [] ]
115 allFociIn ctx xxs@(x ∷ xs) = mkZipper ctx xxs ∷ allFociIn (x ∷ ctx) xs
116
117 allFoci : List A → List (Zipper A)
118 allFoci = allFociIn []