@@ -1,56 +1,141 @@
1module prelude.vect
1module prelude.vect
4import prelude.fin
3import prelude.fin
6%access public
7%access public
8infixr 10 ::
9infixr 10 ::
10data Vect : Set -> Nat -> Set where
11data Vect : Set -> Nat -> Set where
11− Nil : Vect a 0
12+ Nil : Vect a O
12− (::) : a -> Vect a k -> Vect a (1 + k)
13+ (::) : a -> Vect a n -> Vect a (S n)
15+--------------------------------------------------------------------------------
16+-- Indexing into vectors
17+--------------------------------------------------------------------------------
14tail : Vect a (S n) -> Vect a n
19tail : Vect a (S n) -> Vect a n
15−tail (x :: xs) = xs
20+tail (x::xs) = xs
23+head : Vect a (S n) -> a
27+last : Vect a (S n) -> a
30+last (x::y::ys) = last $ y::ys
32+init : Vect a (S n) -> Vect a n
35+init (x::y::ys) = x :: init (y::ys)
17−lookup : Fin n -> Vect a n -> a
37+index : Fin n -> Vect a n -> a
18−lookup fO (x :: xs) = x
38+index fO (x::xs) = x
19−lookup (fS k) (x :: xs) = lookup k xs
39+index (fS k) (x::xs) = index k xs
20−lookup fO [] impossible
40+index fO [] impossible
21−lookup (fS _) [] impossible
41+index (fS _) [] impossible
43+--------------------------------------------------------------------------------
44+-- Conversions to and from list
45+--------------------------------------------------------------------------------
47+toList : Vect a n -> List a
49+toList (x::xs) = x :: toList xs
51+fromList : (l : List a) -> Vect a (length l)
53+fromList (x::xs) = x :: fromList xs
55+--------------------------------------------------------------------------------
56+-- Building bigger vectors
57+--------------------------------------------------------------------------------
59+(++) : Vect a m -> Vect a n -> Vect a (m + n)
61+(++) (x::xs) ys = x :: xs ++ ys
63+--------------------------------------------------------------------------------
65+--------------------------------------------------------------------------------
67+reverse : Vect a n -> Vect a n
23−(++) : Vect a n -> Vect a m -> Vect a (n + m)
70+ reverse' : Vect a m -> Vect a n -> Vect a (m + n)
24−(++) [] ys = ys
71+ reverse' acc [] ?= acc
72+ reverse' acc (x::xs) ?= reverse' (x::acc) xs
74+--------------------------------------------------------------------------------
76+--------------------------------------------------------------------------------
78+map : (a -> b) -> Vect a n -> Vect b n
25−(++) (x :: xs) ys = x :: xs ++ ys
80+map f (x::xs) = f x :: map f xs
82+--------------------------------------------------------------------------------
84+--------------------------------------------------------------------------------
27filter : (a -> Bool) -> Vect a n -> (p ** Vect a p)
86filter : (a -> Bool) -> Vect a n -> (p ** Vect a p)
28filter p [] = ( _ ** [] )
87filter p [] = ( _ ** [] )
29−filter p (x :: xs)
88+filter p (x::xs) with (filter p xs)
30− = let (_ ** xs') = filter p xs in
89+ | (_ ** tail) =
31− if (p x) then ( _ ** x :: xs' ) else ( _ ** xs' )
90+ if p x then
33−map : (a -> b) -> Vect a n -> Vect b n
95+--------------------------------------------------------------------------------
34−map f [] = []
96+-- Membership tests
35−map f (x :: xs) = f x :: map f xs
97+--------------------------------------------------------------------------------
37−reverse : Vect a n -> Vect a n
99+elemBy : (a -> a -> Bool) -> a -> Vect a n -> Bool
38−reverse xs = revAcc [] xs where
100+elemBy p e [] = False
101+elemBy p e (x::xs) with (p e x)
103+ | False = elemBy p e xs
105+elem : Eq a => a -> Vect a n -> Bool
108+lookupBy : (a -> a -> Bool) -> a -> Vect (a, b) n -> Maybe b
109+lookupBy p e [] = Nothing
110+lookupBy p e ((l, r)::xs) with (p e l)
112+ | False = lookupBy p e xs
114+lookup : Eq a => a -> Vect (a, b) n -> Maybe b
115+lookup = lookupBy (==)
39− revAcc : Vect a n -> Vect a m -> Vect a (n + m)
117+hasAnyBy : (a -> a -> Bool) -> Vect a m -> Vect a n -> Bool
40− revAcc acc [] ?= acc
118+hasAnyBy p elems [] = False
41− revAcc acc (x :: xs) ?= revAcc (x :: acc) xs
119+hasAnyBy p elems (x::xs) with (elemBy p x elems)
121+ | False = hasAnyBy p elems xs
123+hasAny : Eq a => Vect a m -> Vect a n -> Bool
124+hasAny = hasAnyBy (==)
126+--------------------------------------------------------------------------------
43−---------- Proofs ----------
127+-- Proofs
128+--------------------------------------------------------------------------------
45−revAcc_lemma_2 = proof {
130+prelude.vect.reverse'_lemma_2 = proof {
47− rewrite plusSuccRightSucc n k;
132+ rewrite (plusSuccRightSucc m n1);
48 exact value;
133 exact value;
51−revAcc_lemma_1 = proof {
136+prelude.vect.reverse'_lemma_1 = proof {
53− rewrite sym (plusZeroRightNeutral n);
138+ rewrite sym (plusZeroRightNeutral m);
54 exact value;
139 exact value;