How do you create a functor instance from Matrix and Vector from the hmatrix library?

The constructor Matrixalso Vectorhas a view *->*, so they look like value constructors. But when I try something like

instance Functor Vector a where
    fmap g ( Vector a ) = Vector ( g a )

I get this error:

 Not in scope: data constructor `Vector'

which makes sense since I cannot make a vector using let v = Vector [1..3]anyway. But, checking the source, I see that both the Matrix and Vector constructors are exported from the corresponding modules:

Vector.hs
module Data.Packed.Vector (
    Vector,
    fromList, (|>), toList, buildVecto.. 
) where

Matrix.hs

module Data.Packed.Matrix (
    Element,
    Matrix,rows,cols...
) where

Dido for applied functor, monad, etc.

+3
source share
2 answers
module Data.Packed.Vector (
    Vector,
    fromList, (|>), toList, buildVecto.. 
) where

This provides the Vector type, but not any of its constructors.

Your instance declaration has been fixed:

instance Functor Vector where
    fmap  = V.map

(, import Vector as V, , Vector ).


: , , . hmatrix mapVector V.map.

EDIT_ 2. , hmatrix , Matrix Vector Storeable.

+2

, Storable .

ghc, Functor ':

{-# LANGUAGE ConstraintKinds, TypeFamilies #-}

import Numeric.LinearAlgebra
import Foreign.Storable(Storable)
import GHC.Exts (Constraint)

class Functor' c where
  type Ok c u v :: Constraint
  type Ok c u v = ()

  fmap' :: Ok c u v => (u -> v) -> c u -> c v

instance Functor' Vector where
  type Ok Vector u v = (Storable u, Storable v)
  fmap' = mapVector
+6

All Articles