简体   繁体   中英

Haskell data type not recognised

I got this code:

data Station a b = Machine a b
             | Line [Station a b]
        deriving(Show)

data Machine a b = Req [(a,Int)] b
    deriving(Show)


machine :: [(a, Int)] -> b -> Station a b
machine l b = Req l b

and when i try to compile, it says that the signature of machine is wrong. It says it is [(a, Int)] -> b -> Machine ab, instead of [(a, Int)] -> b -> Station a b. But in my data type i say that Station ab = Machine a b. I don't get why this won't work?

You have two things called Machine . One is a type constructor that returns a Station type, and the other is a type. The two are unrelated, even though they share the same name.

It may be helpful to disambiguate the two terms by adding a ' (prime) to the data constructor. In doing so, we can tie the Machine' data constructor to hold a Machine as its only argument:

data Station a b = Machine' (Machine a b)
             | Line [Station a b]
        deriving(Show)

And now we can update the machine function to use the newly defined constructor:

machine :: [(a, Int)] -> b -> Station a b
machine l b = Machine' (Req l b)

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