简体   繁体   English

Haskell Num实例

[英]Haskell Num Instance

I am new to Haskell so I apologize if I am overlooking something basic, but I am creating a Coord type that has three parameters: Position, Vector, and Scalar. 我是Haskell的新手,如果我忽略了一些基本知识,我深表歉意,但是我正在创建一个Coord类型,它具有三个参数:Position,Vector和Scalar。 Coord must be an instance of Num and must have specific implemented methods in it. Coord必须是Num的实例,并且必须具有特定的实现方法。 I believe I have what I need already, but I'm assuming I'm missing something basic because I get an error based on the Num instance. 我相信已经有了所需的东西,但是我假设我缺少一些基本的东西,因为我收到了一个基于Num实例的错误。

My code: 我的代码:

data Position x y = Position (x, y)
data Vector x y = Vector (x, y)
data Scalar n = Scalar n
data Coord x y n = Coord (Position x y, Vector x y, Scalar n)

instance Num Coord where
    negate (Position (x, y)) = Position (-x, -y)
    negate (Vector (x, y)) = Vector (-x, -y)
    (+) (Vector (a, b) Position (x, y)) = Position (x+a, y+b)
    (+) (Vector (a, b) Vector (c, d)) = Vector (c+a, d+b)
    (+) (Scalar x Scalar y) = Scalar (x+y)
    (*) (Vector (a, b) Scalar c) = Vector (a*c, b*c)
    (*) (Position (x1, y1) Position (x2, y2)) = (x1*x2) + (y1*y2)
    (*) (Scalar x Scalar y) = Scalar (x*y)
    (-) (Vector (a, b) Position (x, y)) = Position (x-a, y-b)
    (-) (Position (x1, y1) Position (x2, y2)) = Vector (x2-x1, y2-y1)
    (-) (Scalar x Scalar y) = Scalar (x-y)
    abs (Position (x, y)) = Scalar sqrt((x*x)+(y*y))
    abs (Vector (x, y)) = Scalar sqrt((x*x)+(y*y))
    signum _ = error "undefined"
    fromInteger _ = error "undefined"

The error I get: 我得到的错误:

Expecting three more arguments to ‘Coord’
The first argument of ‘Num’ should have kind ‘*’,
  but ‘Coord’ has kind ‘* -> * -> * -> *’
In the instance declaration for ‘Num Coord’

Any clarification on how to use Num would be much appreciated (I'm assuming that is what's responsible for the error) 任何有关如何使用Num的澄清都将不胜感激(我假设这是导致错误的原因)

Thank you. 谢谢。

After looking into the suggestions I finally cam up with a solution that works. 在研究了建议之后,我终于想到了一个可行的解决方案。

data Coord = Position (Double, Double) | Vector (Double, Double) | Scalar Double deriving (Show)

instance Num Coord where
    negate (Position (x, y)) = Position (-x, -y)
    negate (Vector (x, y)) = Vector (-x, -y)
    (+) (Vector (a, b)) (Position (x, y)) = Position (x+a, y+b)
    (+) (Position (x, y)) (Vector (a, b)) = Position (x+a, y+b)
    (+) (Vector (a, b)) (Vector (c, d)) = Vector (c+a, d+b)
    (+) (Scalar x) (Scalar y) = Scalar (x+y)
    (*) (Vector (a, b)) (Scalar c) = Vector (a*c, b*c)
    (*) (Scalar c) (Vector (a, b)) = Vector (a*c, b*c)
    (*) (Position (x1, y1)) (Position (x2, y2)) = Scalar((x1*x2) + (y1*y2))
    (*) (Scalar x) (Scalar y) = Scalar (x*y)
    (-) (Vector (a, b)) (Position (x, y)) = Position (x-a, y-b)
    (-) (Position (x, y)) (Vector (a, b)) = Position (a-x, b-y)
    (-) (Position (x1, y1)) (Position (x2, y2)) = Vector (x2-x1, y2-y1)
    (-) (Scalar x) (Scalar y) = Scalar (x-y)
    abs (Position (x, y)) = Scalar (sqrt((x*x)+(y*y)))
    abs (Vector (x, y)) = Scalar (sqrt((x*x)+(y*y)))
    signum _ = error "undefined"
    fromInteger _ = error "undefined"

Hopefully this helps anyone in the future that is making the same mistakes as me. 希望这对将来犯同样错误的人有所帮助。

Your problem in a word: parentheses. 一句话,您的问题是:括号。

instead of 代替

negate Position (x, y) = Position (-x, -y)

do

negate (Position (x, y)) = Position (-x, -y)

The former implicitly implies that negate is a function which takes two arguments - the Position value constructor and (x,y) , rather than a single argument of type Position , since it parses it as 前者隐含暗示negate是一个带有两个参数的函数Position值构造函数和(x,y) ,而不是Position类型的单个参数,因为它将其解析为

((negate Position) (x,y)) = ...

Your other function definitions have the same problem. 您的其他函数定义也有相同的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM