如何在Haskell中声明具有类型约束的新类型?

时间:2017-03-04 13:38:10

标签: haskell

我想要声明一个新类型的值如下:

data Value m = Wrong | Num Int | Fun (Value -> m Value)

GHCI抱怨道:

<interactive>:160:39:
    Expecting one more argument to ‘Value’
    Expected a type, but ‘Value’ has kind ‘k0 -> *’
    In the type ‘Value -> m Value’
    In the definition of data constructor ‘Fun’
    In the data declaration for ‘Value’

PS:我试图实现The essence of functional programming

的代码

1 个答案:

答案 0 :(得分:4)

由于Value采用了类型参数,因此您需要在使用Value时随时提供该参数。也就是说,您应该将其称为Value m,而不仅仅是Value。所以你的类型定义应该是:

data Value m = Wrong | Num Int | Fun ((Value m) -> m (Value m))