@@ -106,9 +106,11 @@ module Data.Matrix.Static (
106
106
107
107
) where
108
108
109
+ import Control.Applicative (Applicative (.. ), liftA2 )
109
110
import Control.DeepSeq (NFData )
110
111
import Data.Kind (Type )
111
112
import Data.Maybe (fromMaybe )
113
+ import Data.Monoid (Monoid (.. ))
112
114
import Data.Proxy (Proxy (.. ))
113
115
import GHC.TypeLits
114
116
( Nat , KnownNat , natVal
@@ -123,14 +125,20 @@ import qualified Data.Vector as V
123
125
-- the 'Data.Matrix.Static.Matrix' constructor and adds size information to
124
126
-- the type
125
127
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
+
129
130
#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
132
133
#endif
133
134
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
134
142
135
143
nrows :: forall m n a . KnownNat m => Matrix m n a -> Int
136
144
nrows = const m
0 commit comments