| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- List Zipper-related properties |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.List.Zipper.Properties where |
| 10 |
|
| 11 |
open import Data.List.Base as List using (List ; [] ; _∷_) |
| 12 |
open import Data.List.Properties as List |
| 13 |
using (reverse-++; unfold-reverse; ++-assoc; reverse-involutive; map-++ |
| 14 |
; reverse-map; foldr-++; reverse-foldr) |
| 15 |
open import Data.List.Zipper |
| 16 |
using (Zipper; toList; left; right; mkZipper; reverse; _ˡ++_; _ʳ++_ |
| 17 |
; _++ˡ_; _++ʳ_; map; foldr) |
| 18 |
open import Data.Maybe.Base using (Maybe; just; nothing) |
| 19 |
open import Data.Maybe.Relation.Unary.All using (All; just; nothing) |
| 20 |
open import Relation.Binary.PropositionalEquality.Core |
| 21 |
using (_≡_; refl; cong; sym) |
| 22 |
open import Relation.Binary.PropositionalEquality.Properties |
| 23 |
using (module ≡-Reasoning) |
| 24 |
open ≡-Reasoning |
| 25 |
open import Function.Base using (_on_; _$′_; _$_; flip) |
| 26 |
|
| 27 |
-- Invariant: Zipper represents a given list |
| 28 |
------------------------------------------------------------------------ |
| 29 |
|
| 30 |
module _ {a} {A : Set a} where |
| 31 |
|
| 32 |
-- Stability under moving left or right |
| 33 |
|
| 34 |
toList-left-identity : (zp : Zipper A) → All ((_≡_ on toList) zp) (left zp) |
| 35 |
toList-left-identity (mkZipper [] val) = nothing |
| 36 |
toList-left-identity (mkZipper (x ∷ ctx) val) = just $′ begin |
| 37 |
List.reverse (x ∷ ctx) List.++ val |
| 38 |
≡⟨ cong (List._++ val) (unfold-reverse x ctx) ⟩ |
| 39 |
(List.reverse ctx List.++ List.[ x ]) List.++ val |
| 40 |
≡⟨ ++-assoc (List.reverse ctx) List.[ x ] val ⟩ |
| 41 |
toList (mkZipper ctx (x ∷ val)) |
| 42 |
∎ |
| 43 |
|
| 44 |
toList-right-identity : (zp : Zipper A) → All ((_≡_ on toList) zp) (right zp) |
| 45 |
toList-right-identity (mkZipper ctx []) = nothing |
| 46 |
toList-right-identity (mkZipper ctx (x ∷ val)) = just $′ begin |
| 47 |
List.reverse ctx List.++ x ∷ val |
| 48 |
≡⟨ sym (++-assoc (List.reverse ctx) List.[ x ] val) ⟩ |
| 49 |
(List.reverse ctx List.++ List.[ x ]) List.++ val |
| 50 |
≡⟨ cong (List._++ val) (sym (unfold-reverse x ctx)) ⟩ |
| 51 |
List.reverse (x ∷ ctx) List.++ val |
| 52 |
∎ |
| 53 |
|
| 54 |
-- Applying reverse does correspond to reversing the represented list |
| 55 |
|
| 56 |
toList-reverse : (zp : Zipper A) → toList (reverse zp) ≡ List.reverse (toList zp) |
| 57 |
toList-reverse (mkZipper ctx val) = begin |
| 58 |
List.reverse val List.++ ctx |
| 59 |
≡⟨ cong (List.reverse val List.++_) (sym (reverse-involutive ctx)) ⟩ |
| 60 |
List.reverse val List.++ List.reverse (List.reverse ctx) |
| 61 |
≡⟨ sym (reverse-++ (List.reverse ctx) val) ⟩ |
| 62 |
List.reverse (List.reverse ctx List.++ val) |
| 63 |
∎ |
| 64 |
|
| 65 |
|
| 66 |
-- Properties of the insertion functions |
| 67 |
------------------------------------------------------------------------ |
| 68 |
|
| 69 |
-- _ˡ++_ properties |
| 70 |
|
| 71 |
toList-ˡ++ : ∀ xs (zp : Zipper A) → toList (xs ˡ++ zp) ≡ xs List.++ toList zp |
| 72 |
toList-ˡ++ xs (mkZipper ctx val) = begin |
| 73 |
List.reverse (ctx List.++ List.reverse xs) List.++ val |
| 74 |
≡⟨ cong (List._++ _) (reverse-++ ctx (List.reverse xs)) ⟩ |
| 75 |
(List.reverse (List.reverse xs) List.++ List.reverse ctx) List.++ val |
| 76 |
≡⟨ ++-assoc (List.reverse (List.reverse xs)) (List.reverse ctx) val ⟩ |
| 77 |
List.reverse (List.reverse xs) List.++ List.reverse ctx List.++ val |
| 78 |
≡⟨ cong (List._++ _) (reverse-involutive xs) ⟩ |
| 79 |
xs List.++ List.reverse ctx List.++ val |
| 80 |
∎ |
| 81 |
|
| 82 |
ˡ++-assoc : ∀ xs ys (zp : Zipper A) → xs ˡ++ (ys ˡ++ zp) ≡ (xs List.++ ys) ˡ++ zp |
| 83 |
ˡ++-assoc xs ys (mkZipper ctx val) = cong (λ ctx → mkZipper ctx val) $ begin |
| 84 |
(ctx List.++ List.reverse ys) List.++ List.reverse xs |
| 85 |
≡⟨ ++-assoc ctx _ _ ⟩ |
| 86 |
ctx List.++ List.reverse ys List.++ List.reverse xs |
| 87 |
≡⟨ cong (ctx List.++_) (sym (reverse-++ xs ys)) ⟩ |
| 88 |
ctx List.++ List.reverse (xs List.++ ys) |
| 89 |
∎ |
| 90 |
|
| 91 |
-- _ʳ++_ properties |
| 92 |
|
| 93 |
ʳ++-assoc : ∀ xs ys (zp : Zipper A) → xs ʳ++ (ys ʳ++ zp) ≡ (ys List.++ xs) ʳ++ zp |
| 94 |
ʳ++-assoc xs ys (mkZipper ctx val) = cong (λ ctx → mkZipper ctx val) $ begin |
| 95 |
List.reverse xs List.++ List.reverse ys List.++ ctx |
| 96 |
≡⟨ sym (++-assoc (List.reverse xs) (List.reverse ys) ctx) ⟩ |
| 97 |
(List.reverse xs List.++ List.reverse ys) List.++ ctx |
| 98 |
≡⟨ cong (List._++ ctx) (sym (reverse-++ ys xs)) ⟩ |
| 99 |
List.reverse (ys List.++ xs) List.++ ctx |
| 100 |
∎ |
| 101 |
|
| 102 |
-- _++ˡ_ properties |
| 103 |
|
| 104 |
++ˡ-assoc : ∀ xs ys (zp : Zipper A) → zp ++ˡ xs ++ˡ ys ≡ zp ++ˡ (ys List.++ xs) |
| 105 |
++ˡ-assoc xs ys (mkZipper ctx val) = cong (mkZipper ctx) $ sym $ ++-assoc ys xs val |
| 106 |
|
| 107 |
-- _++ʳ_ properties |
| 108 |
|
| 109 |
toList-++ʳ : ∀ (zp : Zipper A) xs → toList (zp ++ʳ xs) ≡ toList zp List.++ xs |
| 110 |
toList-++ʳ (mkZipper ctx val) xs = begin |
| 111 |
List.reverse ctx List.++ val List.++ xs |
| 112 |
≡⟨ sym (++-assoc (List.reverse ctx) val xs) ⟩ |
| 113 |
(List.reverse ctx List.++ val) List.++ xs |
| 114 |
∎ |
| 115 |
|
| 116 |
++ʳ-assoc : ∀ xs ys (zp : Zipper A) → zp ++ʳ xs ++ʳ ys ≡ zp ++ʳ (xs List.++ ys) |
| 117 |
++ʳ-assoc xs ys (mkZipper ctx val) = cong (mkZipper ctx) $ ++-assoc val xs ys |
| 118 |
|
| 119 |
|
| 120 |
-- List-like operations indeed correspond to their counterparts |
| 121 |
------------------------------------------------------------------------ |
| 122 |
|
| 123 |
module _ {a b} {A : Set a} {B : Set b} where |
| 124 |
|
| 125 |
toList-map : ∀ (f : A → B) zp → toList (map f zp) ≡ List.map f (toList zp) |
| 126 |
toList-map f zp@(mkZipper ctx val) = begin |
| 127 |
List.reverse (List.map f ctx) List.++ List.map f val |
| 128 |
≡⟨ cong (List._++ _) (sym (reverse-map f ctx)) ⟩ |
| 129 |
List.map f (List.reverse ctx) List.++ List.map f val |
| 130 |
≡⟨ sym (map-++ f (List.reverse ctx) val) ⟩ |
| 131 |
List.map f (List.reverse ctx List.++ val) |
| 132 |
∎ |
| 133 |
|
| 134 |
toList-foldr : ∀ (c : A → B → B) n zp → foldr c n zp ≡ List.foldr c n (toList zp) |
| 135 |
toList-foldr c n (mkZipper ctx val) = begin |
| 136 |
List.foldl (flip c) (List.foldr c n val) ctx |
| 137 |
≡⟨ sym (reverse-foldr c (List.foldr c n val) ctx) ⟩ |
| 138 |
List.foldr c (List.foldr c n val) (List.reverse ctx) |
| 139 |
≡⟨ sym (foldr-++ c n (List.reverse ctx) val) ⟩ |
| 140 |
List.foldr c n (List.reverse ctx List.++ val) |
| 141 |
∎ |
| 142 |
|
| 143 |
------------------------------------------------------------------------ |
| 144 |
-- DEPRECATED |
| 145 |
------------------------------------------------------------------------ |
| 146 |
-- Please use the new names as continuing support for the old names is |
| 147 |
-- not guaranteed. |
| 148 |
|
| 149 |
-- Version 2.0 |
| 150 |
|
| 151 |
toList-reverse-commute = toList-reverse |
| 152 |
{-# WARNING_ON_USAGE toList-reverse-commute |
| 153 |
"Warning: toList-reverse-commute was deprecated in v2.0. |
| 154 |
Please use toList-reverse instead." |
| 155 |
#-} |
| 156 |
|
| 157 |
toList-ˡ++-commute = toList-ˡ++ |
| 158 |
{-# WARNING_ON_USAGE toList-ˡ++-commute |
| 159 |
"Warning: toList-ˡ++-commute was deprecated in v2.0. |
| 160 |
Please use toList-ˡ++ instead." |
| 161 |
#-} |
| 162 |
|
| 163 |
toList-++ʳ-commute = toList-++ʳ |
| 164 |
{-# WARNING_ON_USAGE toList-++ʳ-commute |
| 165 |
"Warning: toList-++ʳ-commute was deprecated in v2.0. |
| 166 |
Please use toList-++ʳ instead." |
| 167 |
#-} |
| 168 |
|
| 169 |
toList-map-commute = toList-map |
| 170 |
{-# WARNING_ON_USAGE toList-map-commute |
| 171 |
"Warning: toList-map-commute was deprecated in v2.0. |
| 172 |
Please use toList-map instead." |
| 173 |
#-} |
| 174 |
|
| 175 |
toList-foldr-commute = toList-foldr |
| 176 |
{-# WARNING_ON_USAGE toList-foldr-commute |
| 177 |
"Warning: toList-foldr-commute was deprecated in v2.0. |
| 178 |
Please use toList-foldr instead." |
| 179 |
#-} |