0

我正在开发一个使用向量空间库的程序,但在使用它时遇到了一些麻烦。请参阅下面的代码。

import Data.VectorSpace
-- scale a vector with a float
step :: (VectorSpace a) => a -> Float -> a
step x dt = x ^* dt

编译此代码段时,我收到有关向量类型类的关联标量类型的错误。

Could not deduce (Scalar a ~ Float)
from the context (VectorSpace a)
  bound by the type signature for
        step :: VectorSpace a => a -> Float -> a
  at Test.hs:5:9-42 
In the expression: x ^* dt
In an equation for `step': step x dt = x ^* dt

是否有可以修复此编译器错误的类型签名?或者是否有更好的库可用于描述我在类型中寻找的操作(如加法和缩放)?最后,我希望将代码用于事物。

step (1,1) 0.5
step 1 0.5

本质上,我希望重用向量空间定义的一些实例。

4

1 回答 1

3

编辑:发现 hackage 上的签名不正确

您可以添加 GHC 抱怨的约束:

{-# LANGUAGE GADTs #-}
import Data.VectorSpace

step :: (VectorSpace a, Scalar a ~ Float) => a -> Float -> a
step x dt = x ^* dt
于 2013-11-10T20:20:02.920 回答