Haskell lists of datatypes
haskell, types
Solution
Here's a little more output that led me to figure out the problem:
*Main> [State, State]
[State,State]
*Main> :t State
State :: Place
*Main> :t NewYork
NewYork :: State
This seems to imply that the word "State" is a valid constructor for Place, and that the data State refers to a value of only California or NewYork.
If you change the program slightly, to:
data Place = Country Country
| State State
| City String
deriving Show
data State = California
| NewYork
deriving Show
data Country = USA
| Canada
deriving Show
then you can make a list such as [Country USA, State NewYork] which is properly of type [Place]. Using the same word twice as in the first example does not "bind" the State type together in the way I had thought it would.
Of course, using the constructor State State is just a matter of preference, I could just as easily do `AmericanState State` within the Place type if I were so inclined.
Problem
This is probably another easy Haskell question. If I have some "nested" data types, such as in this example code: ``` data Place = Country | State | City String deriving Show data State = California | NewYork deriving Show data Country = USA | Canada deriving Show ``` I can legally make a list such as [USA, Canada] of type [Country], or [California, NewYork] of type [State], or [City "a", City "b"] of type [Place]. What do I have to do to make a list such as [USA, NewYork]? NewYork is a State which is a Place, and USA is a Country which is a Place, but ghci sees USA so it assumes I am making a list of Countrys (and NewYork is a State, so the list fails). I think I need some way to cast a Country or State to a Place, but I'm at a loss on how to accomplish this. I'm trying to avoid throwing the data contained within State and Country into the Place type, which I know would make it work, but I've got a decent amount of real data that I'd rather not jumble up like that.