[英]Vector-space library and constraining the scalar type
I'm working on a program that uses the vector-space library, and I'm having some troubles with using it. 我正在开发一个使用向量空间库的程序,但在使用时遇到了一些麻烦。 See the code below.
请参见下面的代码。
import Data.VectorSpace
-- scale a vector with a float
step :: (VectorSpace a) => a -> Float -> a
step x dt = x ^* dt
When compiling this code segment I get errors regarding the associated scalar type for the vector typeclass. 编译此代码段时,出现关于向量类型类的相关标量类型的错误。
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
Is there a type signature that will fix this compiler error? 是否有类型签名可以解决此编译器错误? Or is there a better library to use for descibing the operations that I'm looking for in a type (like addition and scaling)?
还是有更好的库可用来描述我正在寻找的类型的操作(如加法和缩放)? In the end I'm hoping to use the code like for things.
最后,我希望使用类似的代码。
step (1,1) 0.5
step 1 0.5
Essentially I'm hoping to reuse some of the instances that vector-space defines. 本质上,我希望重用向量空间定义的某些实例。
EDIT: found signature on hackage to be incorrect 编辑:发现黑客的签名不正确
You can just add the constraint about which GHC complained: 您可以添加GHC抱怨的约束:
{-# LANGUAGE GADTs #-}
import Data.VectorSpace
step :: (VectorSpace a, Scalar a ~ Float) => a -> Float -> a
step x dt = x ^* dt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.