Skip to content

Commit 9178be0

Browse files
authored
Merge pull request #3 from davidsd/master
Different Applicative and Monoid instance for Matrix
2 parents 2588136 + f17185a commit 9178be0

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/Data/Matrix/Static.hs

+13-5
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,11 @@ module Data.Matrix.Static (
106106

107107
) where
108108

109+
import Control.Applicative (Applicative(..), liftA2)
109110
import Control.DeepSeq (NFData)
110111
import Data.Kind (Type)
111112
import Data.Maybe (fromMaybe)
113+
import Data.Monoid (Monoid(..))
112114
import Data.Proxy (Proxy(..))
113115
import GHC.TypeLits
114116
( Nat, KnownNat, natVal
@@ -123,14 +125,20 @@ import qualified Data.Vector as V
123125
-- the 'Data.Matrix.Static.Matrix' constructor and adds size information to
124126
-- the type
125127
newtype Matrix (m :: Nat) (n :: Nat) (a :: Type) = Matrix (M.Matrix a)
126-
deriving ( Eq, Functor, Applicative, Foldable, Traversable
127-
, Monoid, NFData
128-
)
128+
deriving ( Eq, Functor, Foldable, Traversable , NFData )
129+
129130
#if MIN_VERSION_base(4,10,0)
130-
instance Monoid a => S.Semigroup (Matrix m n a) where
131-
(<>) = applyBinary mappend
131+
instance (KnownNat m, KnownNat n, Monoid a) => S.Semigroup (Matrix m n a) where
132+
(<>) = mappend
132133
#endif
133134

135+
instance (KnownNat m, KnownNat n) => Applicative (Matrix m n) where
136+
pure a = matrix (const a)
137+
m <*> n = matrix (\ij -> (m!ij) (n!ij))
138+
139+
instance (KnownNat m, KnownNat n, Monoid a) => Monoid (Matrix m n a) where
140+
mempty = pure mempty
141+
mappend = liftA2 mappend
134142

135143
nrows :: forall m n a. KnownNat m => Matrix m n a -> Int
136144
nrows = const m

0 commit comments

Comments
 (0)