on master help sign in
Add more folds and a Foldable instance for (Vect n)
commitd4f173dec74dfdfe83ab39941b63e7e4e7088d11 authorMelvar Chen <Melvar.Chen@googlemail.com> dateFri 2 Aug 2013, 23:01 (13 years ago) parent8c12b8c
4 files changed+19−33
lib/Prelude/Foldable.idr+13−4 lib/Prelude/List.idr−4 lib/Prelude/Vect.idr+5−24 lib/base.ipkg+1−1
lib/Prelude/Foldable.idr+13−4
@@ -15,15 +15,24 @@ foldl f z t = foldr (flip (.) . flip f) id t z
1515concat : (Foldable t, Monoid a) => t a -> a
1616concat = foldr (<+>) neutral
1717
18+concatMap : (Foldable t, Monoid m) => (a -> m) -> t a -> m
19+concatMap f = foldr ((<+>) . f) neutral
20+
1821and : Foldable t => t Bool -> Bool
1922and = foldr (&&) True
2023
2124or : Foldable t => t Bool -> Bool
2225or = foldr (||) False
2326
24−any : (Foldable t, Functor t) => (a -> Bool) -> t a -> Bool
25−any p = Foldable.or . fmap p
27+any : Foldable t => (a -> Bool) -> t a -> Bool
28+any p = foldr ((||) . p) False
29+
30+all : Foldable t => (a -> Bool) -> t a -> Bool
31+all p = foldr ((&&) . p) True
32+
33+sum : (Foldable t, Num a) => t a -> a
34+sum = foldr (+) 0
2635
27−all : (Foldable t, Functor t) => (a -> Bool) -> t a -> Bool
28−all p = Foldable.and . fmap p
36+product : (Foldable t, Num a) => t a -> a
37+product = foldr (*) 1
2938
lib/Prelude/List.idr−4
@@ -226,10 +226,6 @@ instance Foldable List where
226226-- Special folds
227227--------------------------------------------------------------------------------
228228
229−concatMap : (a -> List b) -> List a -> List b
230−concatMap f [] = []
231−concatMap f (x::xs) = f x ++ concatMap f xs
232−
233229--------------------------------------------------------------------------------
234230-- Transformations
235231--------------------------------------------------------------------------------
lib/Prelude/Vect.idr+5−24
@@ -1,6 +1,7 @@
11module Prelude.Vect
22
33import Prelude.Fin
4+import Prelude.Foldable
45import Prelude.Functor
56import Prelude.List
67import Prelude.Nat
@@ -62,13 +63,9 @@ drop (fS k) [] impossible
6263drop (fS k) (x::xs) = drop k xs
6364
6465--------------------------------------------------------------------------------
65−-- Conversions to and from list
66+-- Conversion from list (toList is provided by Foldable)
6667--------------------------------------------------------------------------------
6768
68−toList : Vect n a -> List a
69−toList [] = []
70−toList (x::xs) = x :: toList xs
71−
7269fromList : (l : List a) -> Vect (length l) a
7370fromList [] = []
7471fromList (x::xs) = x :: fromList xs
@@ -123,13 +120,9 @@ instance Functor (Vect n) where
123120-- Folds
124121--------------------------------------------------------------------------------
125122
126−total foldl : (a -> b -> a) -> a -> Vect m b -> a
127−foldl f e [] = e
128−foldl f e (x::xs) = foldl f (f e x) xs
129−
130−total foldr : (a -> b -> b) -> b -> Vect m a -> b
131−foldr f e [] = e
132−foldr f e (x::xs) = f x (foldr f e xs)
123+instance Foldable (Vect n) where
124+ foldr f e [] = e
125+ foldr f e (x::xs) = f x (foldr f e xs)
133126
134127--------------------------------------------------------------------------------
135128-- Special folds
@@ -139,18 +132,6 @@ concat : Vect m (Vect n a) -> Vect (m * n) a
139132concat [] = []
140133concat (v::vs) = v ++ concat vs
141134
142−total and : Vect m Bool -> Bool
143−and = foldr (&&) True
144−
145−total or : Vect m Bool -> Bool
146−or = foldr (||) False
147−
148−total any : (a -> Bool) -> Vect m a -> Bool
149−any p = Vect.or . map p
150−
151−total all : (a -> Bool) -> Vect m a -> Bool
152−all p = Vect.and . map p
153−
154135--------------------------------------------------------------------------------
155136-- Transformations
156137--------------------------------------------------------------------------------
lib/base.ipkg+1−1
@@ -6,7 +6,7 @@ modules = Builtins, Prelude, IO, System,
66 Prelude.Algebra, Prelude.Cast, Prelude.Nat, Prelude.Fin,
77 Prelude.List, Prelude.Maybe, Prelude.Monad, Prelude.Applicative,
88 Prelude.Either, Prelude.Vect, Prelude.Strings, Prelude.Chars,
9− Prelude.Heap, Prelude.Complex, Prelude.Functor,
9+ Prelude.Heap, Prelude.Complex, Prelude.Functor, Prelude.Foldable,
1010
1111 Network.Cgi,
1212 Debug.Trace,