Midasso
Midasso

Reputation: 15

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 a b, instead of [(a, Int)] -> b -> Station a b. But in my data type i say that Station a b = Machine a b. I don't get why this won't work?

Upvotes: 0

Views: 154

Answers (1)

Chad Gilbert
Chad Gilbert

Reputation: 36385

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)

Upvotes: 4

Related Questions