Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {-# OPTIONS_GHC -fno-warn-tabs -fno-warn-orphans #-}
- {-# LANGUAGE FlexibleInstances #-}
- {-# LANGUAGE IncoherentInstances #-}
- module Math.Vector where
- data Vec2 a = Vec2 a a deriving (Read, Show, Eq, Ord)
- instance Functor Vec2 where
- fmap f (Vec2 x y) = Vec2 (f x) (f y)
- instance Applicative Vec2 where
- pure a = Vec2 a a
- (Vec2 f g) <*> (Vec2 x y) = Vec2 (f x) (g y)
- instance Foldable Vec2 where
- foldr f a (Vec2 x y) = f x $ f y $ a
- class (Foldable v, Applicative v) => Vector v
- instance Vector Vec2
- instance (Vector v, Num a) => Num (v a) where
- v + v2 = pure (+) <*> v <*> v2
- v - v2 = pure (-) <*> v <*> v2
- v * v2 = pure (*) <*> v <*> v2
- signum v = pure signum <*> v
- abs v = pure abs <*> v
- fromInteger i = pure (fromInteger i)
- --dot :: (Vector v, Num a, Num (v a)) => v a -> v a -> a -- works without IncoherentInstances
- dot :: (Vector v, Num a) => v a -> v a -> a
- dot v v2 = sum $ v*v2
Add Comment
Please, Sign In to add comment