Skip to content

Commit 7ea5b4c

Browse files
committed
chore: cleanup Vec.lookup
Having both Vec.lookup and Vec.lookup-safe is a bit silly, and we can get the best of both worlds by modifying Vec.lookup to only ever compute on the index. Suggested-by: Calvin Lee <[email protected]>
1 parent de2acf1 commit 7ea5b4c

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

src/Data/Vec/Base.lagda.md

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ open import 1Lab.Path
44
open import 1Lab.Type
55
66
open import Data.Product.NAry
7-
open import Data.List.Base hiding (lookup ; tabulate ; _++_)
87
open import Data.Fin.Base
98
open import Data.Nat.Base
109
```
@@ -42,10 +41,16 @@ private variable
4241
A B C : Type ℓ
4342
n k : Nat
4443
44+
head : Vec A (suc n) → A
45+
head (x ∷ xs) = x
46+
47+
tail : Vec A (suc n) → Vec A n
48+
tail (x ∷ xs) = xs
49+
4550
lookup : Vec A n → Fin n → A
46-
lookup (x ∷ xs) n with fin-view n
47-
... | zero = x
48-
... | suc i = lookup xs i
51+
lookup xs n with fin-view n
52+
... | zero = head xs
53+
... | suc i = lookup (tail xs) i
4954
```
5055

5156
<!--
@@ -61,19 +66,6 @@ Vec-cast {A = A} {x = x} {y = y} p xs =
6166
head ∷ cast-tail len (suc-inj 1+n=len)
6267
})
6368
xs y p
64-
65-
-- Will always compute:
66-
lookup-safe : Vec A n → Fin n → A
67-
lookup-safe {A = A} xs n =
68-
Fin-elim (λ {n} _ → Vec A n → A)
69-
(λ {k} xs → Vec-elim (λ {k'} _ → suc k ≡ k' → A)
70-
(λ p → absurd (suc≠zero p))
71-
(λ x _ _ _ → x) xs refl)
72-
(λ {i} j cont vec →
73-
Vec-elim (λ {k'} xs → suc i ≡ k' → A)
74-
(λ p → absurd (suc≠zero p))
75-
(λ {n} head tail _ si=sn → cont (Vec-cast (suc-inj (sym si=sn)) tail)) vec refl)
76-
n xs
7769
```
7870
-->
7971

0 commit comments

Comments
 (0)