@@ -1,5 +1,6 @@
1module Data.BoundedList
1module Data.BoundedList
3%default total
4%default total
5data BoundedList : Type -> Nat -> Type where
6data BoundedList : Type -> Nat -> Type where
@@ -10,15 +11,27 @@ length : BoundedList a n -> Fin (S n)
10length [] = fO
11length [] = fO
11length (x :: xs) = fS (length xs)
12length (x :: xs) = fS (length xs)
14+--------------------------------------------------------------------------------
15+-- Indexing into bounded lists
16+--------------------------------------------------------------------------------
13index : Fin (S n) -> BoundedList a n -> Maybe a
18index : Fin (S n) -> BoundedList a n -> Maybe a
14index _ [] = Nothing
19index _ [] = Nothing
15index fO (x :: _) = Just x
20index fO (x :: _) = Just x
16index (fS f) (_ :: xs) = index f xs
21index (fS f) (_ :: xs) = index f xs
23+--------------------------------------------------------------------------------
25+--------------------------------------------------------------------------------
18weaken : BoundedList a n -> BoundedList a (n + m)
27weaken : BoundedList a n -> BoundedList a (n + m)
19weaken [] = []
28weaken [] = []
20weaken (x :: xs) = x :: weaken xs
29weaken (x :: xs) = x :: weaken xs
31+--------------------------------------------------------------------------------
32+-- Conversions to and from list
33+--------------------------------------------------------------------------------
22take : (n : Nat) -> List a -> BoundedList a n
35take : (n : Nat) -> List a -> BoundedList a n
23take _ [] = []
36take _ [] = []
24take O _ = []
37take O _ = []
@@ -32,11 +45,49 @@ fromList : (xs : List a) -> BoundedList a (length xs)
32fromList [] = []
45fromList [] = []
33fromList (x :: xs) = x :: fromList xs
46fromList (x :: xs) = x :: fromList xs
48+--------------------------------------------------------------------------------
49+-- Building (bigger) bounded lists
50+--------------------------------------------------------------------------------
52+replicate : (n : Nat) -> a -> BoundedList a n
54+replicate (S n) x = x :: replicate n x
56+--------------------------------------------------------------------------------
58+--------------------------------------------------------------------------------
60+foldl : (a -> b -> a) -> a -> BoundedList b n -> a
62+foldl f e (x::xs) = foldl f (f e x) xs
64+foldr : (a -> b -> b) -> b -> BoundedList a n -> b
66+foldr f e (x::xs) = f x (foldr f e xs)
68+--------------------------------------------------------------------------------
70+--------------------------------------------------------------------------------
35map : (a -> b) -> BoundedList a n -> BoundedList b n
72map : (a -> b) -> BoundedList a n -> BoundedList b n
36map f [] = []
73map f [] = []
37map f (x :: xs) = f x :: map f xs
74map f (x :: xs) = f x :: map f xs
76+--------------------------------------------------------------------------------
78+--------------------------------------------------------------------------------
80+%assert_total -- not sure why this isn't accepted - clearly decreasing on n
81+pad : (xs : BoundedList a n) -> (padding : a) -> BoundedList a n
83+pad {n=S n'} [] padding = padding :: (pad {n=n'} [] padding)
84+pad {n=S n'} (x :: xs) padding = x :: pad {n=n'} xs padding
87+--------------------------------------------------------------------------------
89+--------------------------------------------------------------------------------
39zeroBoundIsEmpty : (xs : BoundedList a 0) -> xs = the (BoundedList a 0) []
91zeroBoundIsEmpty : (xs : BoundedList a 0) -> xs = the (BoundedList a 0) []
40zeroBoundIsEmpty [] = refl
92zeroBoundIsEmpty [] = refl
41−zeroBoundIsEmpty (_ :: _) impossible
93+zeroBoundIsEmpty (_ :: _) impossible