[Haskell-cafe] How to specialize a type

martin martin.drautzburg at web.de
Sat Oct 17 10:40:46 UTC 2015


Am 10/17/2015 um 08:08 AM schrieb Ruben Astudillo:


>> data Process = Train      PlaceDep PlaceArr TP
>>              | MovingBelt PlaceDep PlaceArr BP deriving (Eq, Show)
>>
>> data TP = TP Int deriving (Eq, Show)
>> data BP = BP Int deriving (Eq, Show)
> 
> You could use a newtype here. 

Point taken

>> What I don't like about this is that the fact that all Processes have
>> PlaceDep and PlaceArr appears somewhat "coincidental".
>>
>> This in contrast, captures the common parts more clearly:
>>
>> type PlaceDep = Int
>> type PlaceArr = Int
>>
>> data ProcessParams = DepartureTime Int | Speed Int
>>                     deriving (Eq, Show)
>>
>> data Process = Process PlaceDep PlaceArr ProcessParams
>>                     deriving (Eq, Show)
>>
>> prc1 = Process   10 11 (DepartureTime 1)
>> prc2 = Process   12 13 (Speed 2)
> 
> This just seems a trade-off if you want to know with what are you dealing
> just matching the outer constructor or more of the problem won't care
> about it. Not really a dichotomy worth caring about much anyways

I'll have to think about this. Wouldn't this be a problem when writing functions which operate on a Process of any type?
Like e.g.

	changeDeparture :: PlaceDep -> Process -> Process

I would have to pattern match against all the constructors, wouldn't I? If I don't have just two Processes types, but 20
of them, wouldn't that become messy?

In the second approach I only have to match Process and I can be certain there is a PlaceDep to be altered.





More information about the Haskell-Cafe mailing list