Debian Codegraph @

doc/README/Data/Vec/Relation/Binary/Equality/Cast.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- An equational reasoning library for propositional equality over
5 -- vectors of different indices using cast.
6 --
7 -- To see example usages of this library, scroll to the `Combinators`
8 -- section.
9 ------------------------------------------------------------------------
10
11 {-# OPTIONS --cubical-compatible --safe #-}
12
13 module README.Data.Vec.Relation.Binary.Equality.Cast where
14
15 open import Agda.Primitive
16 open import Data.List.Base as List using (List)
17 import Data.List.Properties as List
18 open import Data.Nat.Base
19 open import Data.Nat.Properties
20 open import Data.Vec.Base
21 open import Data.Vec.Properties
22 open import Data.Vec.Relation.Binary.Equality.Cast
23 open import Function using (_∘_)
24 open import Relation.Binary.PropositionalEquality
25 using (_≡_; refl; sym; cong; module ≡-Reasoning)
26
27 private variable
28 a : Level
29 A : Set a
30 l m n o : ℕ
31 xs ys zs ws : Vec A n
32
33
34 ------------------------------------------------------------------------
35 -- Motivation
36 --
37 -- The `cast` function is the computational variant of `subst` for
38 -- vectors. Since `cast` computes under vector constructors, it
39 -- enables reasoning about vectors with non-definitionally equal indices
40 -- by induction. See, e.g., Jacques Carette's comment in issue #1668.
41 -- <https://github.com/agda/agda-stdlib/pull/1668#issuecomment-1003449509>
42 --
43 -- Suppose we want to prove that ‘xs ++ [] ≡ xs’. Because `xs ++ []`
44 -- has type `Vec A (n + 0)` while `xs` has type `Vec A n`, they cannot
45 -- be directly related by homogeneous equality.
46 -- To resolve the issue, `++-right-identity` uses `cast` to recast
47 -- `xs ++ []` as a vector in `Vec A n`.
48 --
49 ++-right-identity : ∀ .(eq : n + 0 ≡ n) (xs : Vec A n) → cast eq (xs ++ []) ≡ xs
50 ++-right-identity eq [] = refl
51 ++-right-identity eq (x ∷ xs) = cong (x ∷_) (++-right-identity (cong pred eq) xs)
52 --
53 -- When the input is `x ∷ xs`, because `cast eq (x ∷ _)` equals
54 -- `x ∷ cast (cong pred eq) _`, the proof obligation
55 -- cast eq (x ∷ xs ++ []) ≡ x ∷ xs
56 -- simplifies to
57 -- x :: cast (cong pred eq) (xs ++ []) ≡ x ∷ xs
58
59
60 -- Although `cast` makes it possible to prove vector identities by ind-
61 -- uction, the explicit type-casting nature poses a significant barrier
62 -- to code reuse in larger proofs. For example, consider the identity
63 -- ‘fromList (xs List.∷ʳ x) ≡ (fromList xs) ∷ʳ x’ where `List._∷ʳ_` is the
64 -- snoc function of lists. We have
65 --
66 -- fromList (xs List.∷ʳ x) : Vec A (List.length (xs List.∷ʳ x))
67 -- = {- by definition -}
68 -- fromList (xs List.++ List.[ x ]) : Vec A (List.length (xs List.++ List.[ x ]))
69 -- = {- by fromList-++ -}
70 -- fromList xs ++ fromList List.[ x ] : Vec A (List.length xs + List.length [ x ])
71 -- = {- by definition -}
72 -- fromList xs ++ [ x ] : Vec A (List.length xs + 1)
73 -- = {- by unfold-∷ʳ -}
74 -- fromList xs ∷ʳ x : Vec A (suc (List.length xs))
75 -- where
76 -- fromList-++ : cast _ (fromList (xs List.++ ys)) ≡ fromList xs ++ fromList ys
77 -- unfold-∷ʳ : cast _ (xs ∷ʳ x) ≡ xs ++ [ x ]
78 --
79 -- Although the identity itself is simple, the reasoning process changes
80 -- the index in the type twice. Consequently, its Agda translation must
81 -- insert two `cast`s in the proof. Moreover, the proof first has to
82 -- rearrange (the Agda version of) the identity into one with two
83 -- `cast`s, resulting in lots of boilerplate code as demonstrated by
84 -- `example1a-fromList-∷ʳ`.
85 example1a-fromList-∷ʳ : ∀ (x : A) xs →
86 .(eq : List.length (xs List.∷ʳ x) ≡ suc (List.length xs)) →
87 cast eq (fromList (xs List.∷ʳ x)) ≡ fromList xs ∷ʳ x
88 example1a-fromList-∷ʳ x xs eq = begin
89 cast eq (fromList (xs List.∷ʳ x))
90 ≡⟨⟩
91 cast eq (fromList (xs List.++ List.[ x ]))
92 ≡⟨ cast-trans eq₁ eq₂ (fromList (xs List.++ List.[ x ])) ⟨
93 cast eq₂ (cast eq₁ (fromList (xs List.++ List.[ x ])))
94 ≡⟨ cong (cast eq₂) (fromList-++ xs) ⟩
95 cast eq₂ (fromList xs ++ [ x ])
96 ≡⟨ ≈-sym (unfold-∷ʳ-eqFree x (fromList xs)) ⟩
97 fromList xs ∷ʳ x
98 ∎
99 where
100 open ≡-Reasoning
101 eq₁ = List.length-++ xs {List.[ x ]}
102 eq₂ = +-comm (List.length xs) 1
103
104 -- The `cast`s are irrelevant to core of the proof. At the same time,
105 -- they can be inferred from the lemmas used during the reasoning steps
106 -- (e.g. `fromList-++` and `unfold-∷ʳ`). To eliminate the boilerplate,
107 -- this library provides a set of equational reasoning combinators for
108 -- equality of the form `cast eq xs ≡ ys`.
109 example1b-fromList-∷ʳ : ∀ (x : A) xs →
110 .(eq : List.length (xs List.∷ʳ x) ≡ suc (List.length xs)) →
111 cast eq (fromList (xs List.∷ʳ x)) ≡ fromList xs ∷ʳ x
112 example1b-fromList-∷ʳ x xs eq = begin
113 fromList (xs List.∷ʳ x)
114 ≈⟨⟩
115 fromList (xs List.++ List.[ x ])
116 ≈⟨ fromList-++ xs ⟩
117 fromList xs ++ [ x ]
118 ≈⟨ unfold-∷ʳ-eqFree x (fromList xs) ⟨
119 fromList xs ∷ʳ x
120 ∎
121 where open CastReasoning
122
123
124 ------------------------------------------------------------------------
125 -- Combinators
126 --
127 -- Let `xs ≈[ m≡n ] ys` denote `cast m≡n xs ≡ ys`. We have reflexivity,
128 -- symmetry and transitivity:
129 -- ≈-reflexive : xs ≈[ refl ] xs
130 -- ≈-sym : xs ≈[ m≡n ] ys → ys ≈[ sym m≡n ] xs
131 -- ≈-trans : xs ≈[ m≡n ] ys → ys ≈[ n≡o ] zs → xs ≈[ trans m≡n n≡o ] zs
132 -- Accordingly, `_≈[_]_` admits the standard set of equational reasoning
133 -- combinators. Suppose `≈-eqn : xs ≈[ m≡n ] ys`,
134 -- xs ≈⟨ ≈-eqn ⟩ -- `_≈⟨_⟩_` takes a `_≈[_]_` step, adjusting
135 -- ys -- the index at the same time
136 --
137 -- ys ≈⟨ ≈-eqn ⟨ -- `_≈⟨_⟨_` takes a symmetric `_≈[_]_` step
138 -- xs
139 example2a : ∀ .(eq : suc m + n ≡ m + suc n) (xs : Vec A m) a ys →
140 cast eq ((reverse xs ∷ʳ a) ++ ys) ≡ reverse xs ++ (a ∷ ys)
141 example2a eq xs a ys = begin
142 (reverse xs ∷ʳ a) ++ ys ≈⟨ ∷ʳ-++-eqFree a (reverse xs) ⟩ -- index: suc m + n
143 reverse xs ++ (a ∷ ys) ∎ -- index: m + suc n
144 where open CastReasoning
145
146 -- To interoperate with `_≡_`, this library provides `_≂⟨_⟩_` (\-~) for
147 -- taking a `_≡_` step during equational reasoning.
148 -- Let `≡-eqn : xs ≡ ys`, then
149 -- xs ≂⟨ ≡-eqn ⟩ -- Takes a `_≡_` step; no change to the index
150 -- ys
151 --
152 -- ys ≂⟨ ≡-eqn ⟨ -- Takes a symmetric `_≡_` step
153 -- xs
154 -- Equivalently, `≈-reflexive` injects `_≡_` into `_≈[_]_`. That is,
155 -- `xs ≂⟨ ≡-eqn ⟩ ys` is the same as `xs ≈⟨ ≈-reflexive ≡-eqn ⟩ ys`.
156 -- Extending `example2a`, we have:
157 example2b : ∀ .(eq : suc m + n ≡ m + suc n) (xs : Vec A m) a ys →
158 cast eq ((a ∷ xs) ʳ++ ys) ≡ xs ʳ++ (a ∷ ys)
159 example2b eq xs a ys = begin
160 (a ∷ xs) ʳ++ ys ≂⟨ unfold-ʳ++ (a ∷ xs) ys ⟩ -- index: suc m + n
161 reverse (a ∷ xs) ++ ys ≂⟨ cong (_++ ys) (reverse-∷ a xs) ⟩ -- index: suc m + n
162 (reverse xs ∷ʳ a) ++ ys ≈⟨ ∷ʳ-++-eqFree a (reverse xs) ⟩ -- index: suc m + n
163 reverse xs ++ (a ∷ ys) ≂⟨ unfold-ʳ++ xs (a ∷ ys) ⟨ -- index: m + suc n
164 xs ʳ++ (a ∷ ys) ∎ -- index: m + suc n
165 where open CastReasoning
166
167 -- Oftentimes index-changing identities apply to only part of the proof
168 -- term. When reasoning about `_≡_`, `cong` shifts the focus to the
169 -- subterm of interest. In this library, `≈-cong′` does a similar job.
170 -- For the typechecker to infer the congruence function `f`, it must be
171 -- polymorphic in the length of the given vector,
172 example3a-fromList-++-++ : {xs ys zs : List A} →
173 .(eq : List.length (xs List.++ ys List.++ zs) ≡
174 List.length xs + (List.length ys + List.length zs)) →
175 cast eq (fromList (xs List.++ ys List.++ zs)) ≡
176 fromList xs ++ fromList ys ++ fromList zs
177 example3a-fromList-++-++ {xs = xs} {ys} {zs} eq = begin
178 fromList (xs List.++ ys List.++ zs)
179 ≈⟨ fromList-++ xs ⟩
180 fromList xs ++ fromList (ys List.++ zs)
181 ≈⟨ ≈-cong′ (fromList xs ++_) (fromList-++ ys) ⟩
182 fromList xs ++ fromList ys ++ fromList zs
183 ∎
184 where open CastReasoning
185
186 -- As an alternative, one can manually apply `cast-++ʳ` to expose `cast`
187 -- in the subterm. However, this unavoidably duplicates the proof term.
188 example3b-fromList-++-++′ : {xs ys zs : List A} →
189 .(eq : List.length (xs List.++ ys List.++ zs) ≡
190 List.length xs + (List.length ys + List.length zs)) →
191 cast eq (fromList (xs List.++ ys List.++ zs)) ≡
192 fromList xs ++ fromList ys ++ fromList zs
193 example3b-fromList-++-++′ {xs = xs} {ys} {zs} eq = begin
194 fromList (xs List.++ ys List.++ zs)
195 ≈⟨ fromList-++ xs ⟩
196 fromList xs ++ fromList (ys List.++ zs)
197 ≈⟨ cast-++ʳ (List.length-++ ys) (fromList xs) ⟩
198 fromList xs ++ cast _ (fromList (ys List.++ zs))
199 ≂⟨ cong (fromList xs ++_) (fromList-++ ys) ⟩
200 fromList xs ++ fromList ys ++ fromList zs
201 ∎
202 where open CastReasoning
203
204 -- `≈-cong′` can be chained together much like how `cong` can be nested.
205 -- In this example, `unfold-∷ʳ` is applied to the term `xs ++ [ a ]`
206 -- in `(_++ ys)` inside of `reverse`. Thus the proof employs two
207 -- `≈-cong′` usages, which is equivalent to using one `≈-cong′` with
208 -- a single composed function.
209 example4-cong² : ∀ .(eq : (m + 1) + n ≡ n + suc m) a (xs : Vec A m) ys →
210 cast eq (reverse ((xs ++ [ a ]) ++ ys)) ≡ ys ʳ++ reverse (xs ∷ʳ a)
211 example4-cong² {m = m} {n} eq a xs ys = begin
212 reverse ((xs ++ [ a ]) ++ ys)
213 ≈⟨ ≈-cong′ reverse (≈-cong′ (_++ ys) (unfold-∷ʳ-eqFree a xs)) ⟨
214 -- the same as ≈-cong′ (reverse ∘ (_++ ys)) ...
215 reverse ((xs ∷ʳ a) ++ ys)
216 ≈⟨ reverse-++-eqFree (xs ∷ʳ a) ys ⟩
217 reverse ys ++ reverse (xs ∷ʳ a)
218 ≂⟨ unfold-ʳ++ ys (reverse (xs ∷ʳ a)) ⟨
219 ys ʳ++ reverse (xs ∷ʳ a)
220 ∎
221 where open CastReasoning
222
223 ------------------------------------------------------------------------
224 -- Interoperation between `_≈[_]_` and `_≡_`
225 --
226 -- This library is designed to interoperate with `_≡_`. Examples in the
227 -- combinators section showed how to apply `_≂⟨_⟩_` to take an `_≡_`
228 -- step during equational reasoning about `_≈[_]_`. Recall that
229 -- `xs ≈[ m≡n ] ys` is a shorthand for `cast m≡n xs ≡ ys`, the
230 -- combinator is essentially the composition of `_≡_` on the left-hand
231 -- side of `_≈[_]_`. Dually, the combinator `_≃⟨_⟩_` composes `_≡_` on
232 -- the right-hand side of `_≈[_]_`. Thus `_≃⟨_⟩_` intuitively ends the
233 -- reasoning system of `_≈[_]_` and switches back to the reasoning
234 -- system of `_≡_`.
235 example5-fromList-++-++′ : {xs ys zs : List A} →
236 .(eq : List.length (xs List.++ ys List.++ zs) ≡
237 List.length xs + (List.length ys + List.length zs)) →
238 cast eq (fromList (xs List.++ ys List.++ zs)) ≡
239 fromList xs ++ fromList ys ++ fromList zs
240 example5-fromList-++-++′ {xs = xs} {ys} {zs} eq = begin
241 fromList (xs List.++ ys List.++ zs)
242 ≈⟨ fromList-++ xs ⟩
243 fromList xs ++ fromList (ys List.++ zs)
244 ≃⟨ cast-++ʳ (List.length-++ ys) (fromList xs) ⟩
245 fromList xs ++ cast _ (fromList (ys List.++ zs))
246 ≡⟨ cong (fromList xs ++_) (fromList-++ ys) ⟩
247 fromList xs ++ fromList ys ++ fromList zs
248 ≡-∎
249 where open CastReasoning
250
251 -- Of course, it is possible to start with the reasoning system of `_≡_`
252 -- and then switch to the reasoning system of `_≈[_]_`.
253 example6a-reverse-∷ʳ : ∀ x (xs : Vec A n) → reverse (xs ∷ʳ x) ≡ x ∷ reverse xs
254 example6a-reverse-∷ʳ {n = n} x xs = begin-≡
255 reverse (xs ∷ʳ x)
256 ≡⟨ ≈-reflexive refl ⟨
257 reverse (xs ∷ʳ x)
258 ≈⟨ ≈-cong′ reverse (unfold-∷ʳ-eqFree x xs) ⟩
259 reverse (xs ++ [ x ])
260 ≈⟨ reverse-++-eqFree xs [ x ] ⟩
261 x ∷ reverse xs
262 ∎
263 where open CastReasoning
264
265 example6b-reverse-∷ʳ-by-induction : ∀ x (xs : Vec A n) → reverse (xs ∷ʳ x) ≡ x ∷ reverse xs
266 example6b-reverse-∷ʳ-by-induction x [] = refl
267 example6b-reverse-∷ʳ-by-induction x (y ∷ xs) = begin
268 reverse (y ∷ (xs ∷ʳ x)) ≡⟨ reverse-∷ y (xs ∷ʳ x) ⟩
269 reverse (xs ∷ʳ x) ∷ʳ y ≡⟨ cong (_∷ʳ y) (example6b-reverse-∷ʳ-by-induction x xs) ⟩
270 (x ∷ reverse xs) ∷ʳ y ≡⟨⟩
271 x ∷ (reverse xs ∷ʳ y) ≡⟨ cong (x ∷_) (reverse-∷ y xs) ⟨
272 x ∷ reverse (y ∷ xs) ∎
273 where open ≡-Reasoning