Skip to content

Commit 11a3895

Browse files
4e554c4cTOTBWF
andcommitted
feat: Use partial indexing operation for Lists
Co-authored-by: Reed Mullanix <reedmullanix@gmail.com>
1 parent 928d1cf commit 11a3895

File tree

5 files changed

+54
-22
lines changed

5 files changed

+54
-22
lines changed

1lab.agda-lib

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ include:
33
src
44
wip
55
_build
6-
.
76
flags:
87
--cubical
98
--no-load-primitives

src/Data/List/Base.lagda.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ open import Data.Maybe.Base
99
open import Data.Bool.Base
1010
open import Data.Dec.Base
1111
open import Data.Fin.Base
12+
open import Data.Irr
1213
1314
open import Meta.Traversable
1415
open import Meta.Foldable
1516
open import Meta.Append
1617
open import Meta.Idiom
1718
open import Meta.Bind
1819
open import Meta.Alt
20+
21+
import Data.Nat.Base as Nat
1922
```
2023
-->
2124

@@ -338,10 +341,22 @@ lookup x ((k , v) ∷ xs) with x ≡? k
338341
... | yes _ = just v
339342
... | no _ = lookup x xs
340343
344+
_!?_ : List A → Nat → Maybe A
345+
[] !? n = nothing
346+
(x ∷ xs) !? zero = just x
347+
(x ∷ xs) !? suc n = xs !? n
348+
349+
-- Make sure that all of the actual computation is guarded behind irrelevance to make this zero cost
350+
!?-just : ∀ (xs : List A) (n : Nat) → .(n Nat.< length xs) → is-just (xs !? n)
351+
!?-just {A = a} xs n n<xs = erase (worker xs n n<xs)
352+
where
353+
worker : ∀ (xs : List A) (n : Nat) → n Nat.< length xs → is-true (is-just? (xs !? n))
354+
worker (x ∷ xs) zero n<xs = tt
355+
worker (x ∷ xs) (suc n) n<xs = worker xs n (Nat.≤-peel n<xs)
356+
341357
_!_ : (l : List A) → Fin (length l) → A
342-
(x ∷ xs) ! n with fin-view n
343-
... | zero = x
344-
... | suc i = xs ! i
358+
xs ! (fin n ⦃ forget pf ⦄) = from-just! $ !?-just xs n pf
359+
345360
346361
tabulate : ∀ {n} (f : Fin n → A) → List A
347362
tabulate {n = zero} f = []

src/Data/List/Sigma.agda

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,19 @@ module _ {ℓ ℓ'} {A : Type ℓ} {B : A → Type ℓ'} where
4242
member-pair-inv x (y ∷ ys) a b (there it) = ap there (member-pair-inv x ys a b it)
4343

4444
rem₀
45-
: {x a} (ys : a List (B a)) (b : B a) (p : a ≡ᵢ x) ix .{q} .{q'}
46-
(Id-over B (symᵢ p) (ys x ! fin ix ⦃ forget q ⦄) b) ≃ (ys a ! fin ix ⦃ forget q' ⦄ ≡ᵢ b)
45+
: {x a} (ys : a List (B a)) (b : B a) (p : a ≡ᵢ x) ix {q : Irr (ix Nat.< length (ys x))} {q' : Irr (ix Nat.< length (ys a))}
46+
(Id-over B (symᵢ p) (ys x ! fin ix ⦃ q ⦄) b) ≃ (ys a ! fin ix ⦃ q' ⦄ ≡ᵢ b)
4747
rem₀ {x = x} {a} ys b p ix {q} {q'} = Jᵢ'
48-
(λ a x p b .q .q' Id-over B (symᵢ p) (ys x ! fin ix ⦃ forget q ⦄) b ≃ (ys a ! fin ix ⦃ forget q' ⦄ ≡ᵢ b))
48+
(λ a x p b q q' Id-over B (symᵢ p) (ys x ! fin ix ⦃ q ⦄) b ≃ (ys a ! fin ix ⦃ q' ⦄ ≡ᵢ b))
4949
(λ b q q' id , id-equiv)
5050
p b q q'
5151

5252
rem₁ : {x a} (ys : a List (B a)) (b : B _) (p : a ≡ᵢ x) fibre' (ys x) p b fibreᵢ (ys a !_) b
53-
rem₁ {x = x} {a} ys b p (fin ix ⦃ forget q ⦄ , r) = fin ix ⦃ q' ⦄ , Equiv.to (rem₀ ys b p ix) r where
53+
rem₁ {x = x} {a} ys b p (fin ix ⦃ forget q ⦄ , r) = fin ix ⦃ q' ⦄ , Equiv.to (rem₀ ys b p ix {forget q} {q'}) r where
5454
q' = forget (transport (λ i suc ix Nat.≤ length (ys (Id≃path.to p (~ i)))) q)
5555

5656
rem₂ : {x a} (ys : a List (B a)) (b : B _) (p : a ≡ᵢ x) fibreᵢ (ys a !_) b fibre' (ys x) p b
57-
rem₂ {x = x} {a} ys b p (fin ix ⦃ forget q ⦄ , r) = fin ix ⦃ q' ⦄ , Equiv.from (rem₀ ys b p ix) r where
57+
rem₂ {x = x} {a} ys b p (fin ix ⦃ forget q ⦄ , r) = fin ix ⦃ q' ⦄ , Equiv.from (rem₀ ys b p ix {q'} {forget q}) r where
5858
q' = forget (transport (λ i suc ix Nat.≤ length (ys (Id≃path.to p i))) q)
5959

6060
sigma-member : {a b xs ys} a ∈ₗ xs b ∈ₗ ys a (a , b) ∈ₗ sigma xs ys

src/Data/Maybe/Base.lagda.md

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ open import 1Lab.Equiv
44
open import 1Lab.Path
55
open import 1Lab.Type
66
7+
open import Data.Bool.Base
78
open import Data.Dec.Base
89
910
open import Meta.Traversable
@@ -83,19 +84,27 @@ from-just def nothing = def
8384

8485
<!--
8586
```agda
86-
is-just : Maybe A → Type
87-
is-just (just _) = ⊤
88-
is-just nothing = ⊥
87+
is-just? : Maybe A → Bool
88+
is-just? nothing = false
89+
is-just? (just _) = true
8990
90-
is-nothing : Maybe A → Type
91-
is-nothing (just _) = ⊥
92-
is-nothing nothing = ⊤
91+
record is-just (m : Maybe A) : Type where
92+
constructor erase
93+
field
94+
.witness : is-true (is-just? m)
9395
94-
nothing≠just : {x : A} → ¬ (nothing ≡ just x)
95-
nothing≠just p = subst is-nothing p tt
96+
97+
from-just! : ∀ {x} → is-just x → A
98+
from-just! {x = just x} _ = x
9699
97100
just≠nothing : {x : A} → ¬ (just x ≡ nothing)
98-
just≠nothing p = subst is-just p tt
101+
just≠nothing p = subst is-just' p tt where
102+
is-just' : Maybe A → Type
103+
is-just' (just _) = ⊤
104+
is-just' nothing = ⊥
105+
106+
nothing≠just : {x : A} → ¬ (nothing ≡ just x)
107+
nothing≠just p = just≠nothing (sym p)
99108
100109
just-inj : ∀ {x y : A} → just x ≡ just y → x ≡ y
101110
just-inj {x = x} = ap (from-just x)
@@ -106,6 +115,7 @@ instance
106115
Discrete-Maybe .decide (just x) nothing = no just≠nothing
107116
Discrete-Maybe .decide nothing (just x) = no nothing≠just
108117
Discrete-Maybe .decide nothing nothing = yes refl
118+
109119
```
110120
-->
111121

src/Data/Vec/Base.lagda.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ open import 1Lab.Path
44
open import 1Lab.Type
55
66
open import Data.Product.NAry
7+
open import Data.Maybe.Base
78
open import Data.List.Base hiding (head ; tail ; lookup) renaming (tabulate to tabulateℓ ; _++_ to _++ℓ_)
89
open import Data.Fin.Base
9-
open import Data.Nat.Base
10+
open import Data.Nat.Base as Nat
11+
open import Data.Id.Base
1012
1113
open import Meta.Idiom
1214
@@ -59,6 +61,13 @@ instance
5961
length-uncons : ∀ {xs n x} → Length {A = A} (x ∷ xs) (suc n) → Length xs n
6062
length-uncons (suc l) = l
6163
64+
has-lengthᵢ : ∀ {xs : List A} {n : Nat} → Irr (Length xs n) → length xs ≡ᵢ n
65+
has-lengthᵢ {xs = []} {n = zero} len = reflᵢ
66+
has-lengthᵢ {xs = x ∷ xs} {n = suc n} len = apᵢ suc $ has-lengthᵢ $ length-uncons <$> len
67+
68+
has-length : ∀ {xs : List A} {n : Nat} → Irr (Length xs n) → length xs ≡ n
69+
has-length l = Id≃path.to $ has-lengthᵢ l
70+
6271
record Vec {ℓ} (A : Type ℓ) (n : Nat) : Type ℓ where
6372
constructor vec
6473
field
@@ -89,9 +98,8 @@ tail : Vec A (suc n) → Vec A n
8998
tail v with (x ∷ xs) ← vec-view v = xs
9099
91100
lookup : Vec A n → Fin n → A
92-
lookup xs n with fin-view n
93-
... | zero = head xs
94-
... | suc i = lookup (tail xs) i
101+
lookup (vec xs ⦃ forget len ⦄) (fin n ⦃ forget n<xs ⦄) =
102+
from-just! $ !?-just xs n $ᵢ ≤-trans n<xs $ subst (Nat._≤ length xs) (has-length $ forget len) Nat.x≤x
95103
96104
instance
97105
From-prod-Vec : From-product A (Vec A)

0 commit comments

Comments
 (0)