on master help sign in
Add Functor, Applicative and Monad instances for (Vect n)
commit8a53e68faf96a682182d155d7e7790c61085f889 authorMelvar Chen <Melvar.Chen@googlemail.com> dateSat 27 Jul 2013, 19:54 (13 years ago) parent61d39da
2 files changed+17−5
lib/Prelude.idr+8 lib/Prelude/Vect.idr+9−5
lib/Prelude.idr+8
@@ -216,6 +216,11 @@ instance Applicative List where
216216
217217 fs <$> vs = concatMap (\f => map f vs) fs
218218
219+instance Applicative (Vect k) where
220+ pure = replicate _
221+
222+ fs <$> vs = zipWith ($) fs vs
223+
219224---- Alternative instances
220225
221226instance Alternative Maybe where
@@ -245,6 +250,9 @@ instance Monad (Either e) where
245250instance Monad List where
246251 m >>= f = concatMap f m
247252
253+instance Monad (Vect n) where
254+ m >>= f = diag (map f m)
255+
248256---- some mathematical operations
249257
250258%include C "math.h"
lib/Prelude/Vect.idr+9−5
@@ -104,9 +104,9 @@ unzip ((l, r)::xs) with (unzip xs)
104104-- Maps
105105--------------------------------------------------------------------------------
106106
107−map : (a -> b) -> Vect n a -> Vect n b
108−map f [] = []
109−map f (x::xs) = f x :: map f xs
107+instance Functor (Vect n) where
108+ map f [] = []
109+ map f (x::xs) = f x :: map f xs
110110
111111-- XXX: causes Idris to enter an infinite loop when type checking in the REPL
112112--mapMaybe : (a -> Maybe b) -> Vect n a -> (p ** Vect b p)
@@ -145,10 +145,10 @@ total or : Vect m Bool -> Bool
145145or = foldr (||) False
146146
147147total any : (a -> Bool) -> Vect m a -> Bool
148−any p = Vect.or . Vect.map p
148+any p = Vect.or . map p
149149
150150total all : (a -> Bool) -> Vect m a -> Bool
151−all p = Vect.and . Vect.map p
151+all p = Vect.and . map p
152152
153153--------------------------------------------------------------------------------
154154-- Transformations
@@ -317,6 +317,10 @@ catMaybes (Nothing::xs) = catMaybes xs
317317catMaybes ((Just j)::xs) with (catMaybes xs)
318318 | (_ ** tail) = (_ ** j::tail)
319319
320+diag : Vect n (Vect n a) -> Vect n a
321+diag [] = []
322+diag ((x::xs)::xss) = x :: diag (map tail xss)
323+
320324range : Vect n (Fin n)
321325range =
322326 reverse range_