简体   繁体   中英

How do I declare new type with type constraints in Haskell?

I'm tring to declare a new type Value as below:

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

And GHCI complains:

<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: I'm trying to implement code of The essence of functional programming

Since Value takes a type parameter, you need to supply that parameter any time you use Value . That is, you should refer to it as Value m , not just Value . So your type definition should be:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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