haskell - Could not deduce error when using type constraints -


i using haskell implement linear algebra example. however, run problem when declaring magnitude function.

my implementation follows:

magnitude :: (foldable t, functor t, floating a) => t -> magnitude = sqrt $ data.foldable.foldr1 (+) $ fmap (^2) 

the idea magnitude accept vec2d, vec3d, or vec4d, , return square root of sum of squares of components.

each of 3 vector types implements functor , foldable. example,

newtype vec2d = vec2d (a, a) deriving (eq, show) instance functor vec2d     fmap f (vec2d (x, y)) = vec2d (f x, f y) instance foldable vec2d     foldr f b (vec2d (x, y)) = f x $ f y b 

however, receive multitude of errors:

linearalgebra.hs:9:13:     not deduce (floating (t -> a)) arising use of `sqrt'     context (foldable t, functor t, floating a)       bound type signature                  magnitude :: (foldable t, functor t, floating a) => t ->       @ linearalgebra.hs:8:14-60     possible fix: add instance declaration (floating (t -> a))     in expression: sqrt     in expression: sqrt $ data.foldable.foldr1 (+) $ fmap (^ 2)     in equation `magnitude':         magnitude = sqrt $ data.foldable.foldr1 (+) $ fmap (^ 2)  linearalgebra.hs:9:20:     not deduce (foldable ((->) (t -> a)))       arising use of `data.foldable.foldr1'     context (foldable t, functor t, floating a)       bound type signature                  magnitude :: (foldable t, functor t, floating a) => t ->       @ linearalgebra.hs:8:14-60     possible fix:       add instance declaration (foldable ((->) (t -> a)))     in expression: data.foldable.foldr1 (+)     in second argument of `($)', namely       `data.foldable.foldr1 (+) $ fmap (^ 2)'     in expression: sqrt $ data.foldable.foldr1 (+) $ fmap (^ 2)  linearalgebra.hs:9:41:     not deduce (num (t -> a)) arising use of `+'     context (foldable t, functor t, floating a)       bound type signature                  magnitude :: (foldable t, functor t, floating a) => t ->       @ linearalgebra.hs:8:14-60     possible fix: add instance declaration (num (t -> a))     in first argument of `data.foldable.foldr1', namely `(+)'     in expression: data.foldable.foldr1 (+)     in second argument of `($)', namely       `data.foldable.foldr1 (+) $ fmap (^ 2)' failed, modules loaded: none. 

i'm not entirely comfortable functor or foldable yet - , believe indirect reason errors.

can explain me error messages pointing at?

you ought combine functions pipeline (.) not ($). error occurring because, instance, data.foldable.foldr1 (+) expects applied foldable type [a] you're applying directly fmap (^2) function.

magnitude :: (foldable t, functor t, floating a) => t -> magnitude = sqrt . data.foldable.foldr1 (+) . fmap (^2) 

or

magnitude :: (foldable t, functor t, floating a) => t -> magnitude ta = sqrt $ data.foldable.foldr1 (+) $ fmap (^2) $ ta 

both better.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -