Haskell中的类问题

huangapple go评论87阅读模式
英文:

Class problems with Haskell

问题

这次,我有以下这些定义:

  1. data Color = Red | Green | Blue
  2. deriving (Show, Eq)
  3. data Suit = Club | Spade | Diamond | Heart
  4. deriving (Show, Eq)
  5. class Eq a => Eq (Cycle a) where
  6. step :: a -> a
  7. stepMany :: Integer -> a -> a
  8. stepMany 0 x = x
  9. stepMany steps x = stepMany (steps - 1) (step x)
  10. instance Eq Color => Cycle Color where
  11. step color
  12. | color == Red = Green
  13. | color == Green = Blue
  14. | color == Blue = Red
  15. instance Eq Suit => Cycle Suit where
  16. step suit
  17. | suit == Club = Spade
  18. | suit == Spade = Diamond
  19. | suit == Diamond = Heart
  20. | suit == Heart = Club

我的问题是这行代码:

  1. class Eq a => Eq (Cycle a) where

产生了错误:

  1. Unexpected type `Cycle a'
  2. In the class declaration for `Eq'
  3. A class declaration should have form
  4. class Eq a where ...

问:我在这里做错了什么?

英文:

This time, I have these definitions:

  1. data Color = Red | Green | Blue
  2. deriving (Show, Eq)
  3. data Suit = Club | Spade | Diamond | Heart
  4. deriving (Show, Eq)
  5. class Eq a => Eq (Cycle a) where
  6. step :: a -> a
  7. stepMany :: Integer -> a -> a
  8. stepMany 0 x = x
  9. stepMany steps x = stepMany (steps - 1) (step x)
  10. instance Eq Color => Cycle Color where
  11. step color
  12. | color == Red = Green
  13. | color == Green = Blue
  14. | color == Blue = Red
  15. instance Eq Suit => Cycle Suit where
  16. step suit
  17. | suit == Club = Spade
  18. | suit == Spade = Diamond
  19. | suit == Diamond = Heart
  20. | suit == Heart = Club

My problem is that the line

  1. class Eq a => Eq (Cycle a) where'='"

produces the error

  1. Unexpected type `Cycle a'
  2. In the class declaration for `Eq'
  3. A class declaration should have form
  4. class Eq a where ...
  5. |
  6. 7 | class Eq a => Eq (Cycle a) where
  7. |

Q: What am I doing wrong here?

答案1

得分: 5

你不需要在CycleColorSuit上使用Eq约束。你可以像这样编写模块:

  1. data Color = Red | Green | Blue
  2. deriving (Show, Eq)
  3. data Suit = Club | Spade | Diamond | Heart
  4. deriving (Show, Eq)
  5. class Cycle a where
  6. step :: a -> a
  7. stepMany :: Integer -> a -> a
  8. stepMany 0 x = x
  9. stepMany steps x = stepMany (steps - 1) (step x)
  10. instance Cycle Color where
  11. step color
  12. | color == Red = Green
  13. | color == Green = Blue
  14. | color == Blue = Red
  15. instance Cycle Suit where
  16. step suit
  17. | suit == Club = Spade
  18. | suit == Spade = Diamond
  19. | suit == Diamond = Heart
  20. | suit == Heart = Club
英文:

You don't need the Eq constraint on Cycle, nor on Color and Suit. You can just write the module like this:

  1. data Color = Red | Green | Blue
  2. deriving (Show, Eq)
  3. data Suit = Club | Spade | Diamond | Heart
  4. deriving (Show, Eq)
  5. class Cycle a where
  6. step :: a -> a
  7. stepMany :: Integer -> a -> a
  8. stepMany 0 x = x
  9. stepMany steps x = stepMany (steps - 1) (step x)
  10. instance Cycle Color where
  11. step color
  12. | color == Red = Green
  13. | color == Green = Blue
  14. | color == Blue = Red
  15. instance Cycle Suit where
  16. step suit
  17. | suit == Club = Spade
  18. | suit == Spade = Diamond
  19. | suit == Diamond = Heart
  20. | suit == Heart = Club

答案2

得分: 3

首先,使用以下代码声明Cycle。这将声明带有Eq约束的Cycle类型类。class Eq a => Eq (Cycle a) where 不是声明CycleEq的有效语法。

  1. class Eq a => Cycle a where
  2. ...

然后,使用以下代码声明其实例。你不能写成Eq Color =>,因为Color是一个刚性类型。如果你尝试将其声明为Cycle的实例而Color不是Eq的实例,编译器会报错。

  1. instance Cycle Color where
  2. ...

最终的代码将如下所示。

  1. data Color = Red | Green | Blue
  2. deriving (Show, Eq)
  3. data Suit = Club | Spade | Diamond | Heart
  4. deriving (Show, Eq)
  5. class Eq a => Cycle a where
  6. step :: a -> a
  7. stepMany :: Integer -> a -> a
  8. stepMany 0 x = x
  9. stepMany steps x = stepMany (steps - 1) (step x)
  10. instance Cycle Color where
  11. step color
  12. | color == Red = Green
  13. | color == Green = Blue
  14. | color == Blue = Red
  15. instance Cycle Suit where
  16. step suit
  17. | suit == Club = Spade
  18. | suit == Spade = Diamond
  19. | suit == Diamond = Heart
  20. | suit == Heart = Club
英文:

First, use this to declare Cycle. This declares Cycle type class with Eq constraint. class Eq a => Eq (Cycle a) where isn't a valid syntax to declare neither Cycle nor Eq.

  1. class Eq a => Cycle a where
  2. ...

Then, use this to declare its instance. You cannot write Eq Color => because Color is a rigid type. The compiler will make it an error if Color isn't an instance of Eq while you try to make it an instance of Cycle.

  1. instance Cycle Color where
  2. ...

The final code will be like this.

  1. data Color = Red | Green | Blue
  2. deriving (Show, Eq)
  3. data Suit = Club | Spade | Diamond | Heart
  4. deriving (Show, Eq)
  5. class Eq a => Cycle a where
  6. step :: a -> a
  7. stepMany :: Integer -> a -> a
  8. stepMany 0 x = x
  9. stepMany steps x = stepMany (steps - 1) (step x)
  10. instance Cycle Color where
  11. step color
  12. | color == Red = Green
  13. | color == Green = Blue
  14. | color == Blue = Red
  15. instance Cycle Suit where
  16. step suit
  17. | suit == Club = Spade
  18. | suit == Spade = Diamond
  19. | suit == Diamond = Heart
  20. | suit == Heart = Club

huangapple
  • 本文由 发表于 2023年6月12日 14:19:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76454029.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定