[Haskell-cafe] How to specialize a type

martin martin.drautzburg at web.de
Fri Oct 16 07:32:19 UTC 2015


Hello all,

Suppose I have a type "Process" which stands either for a Train or a moving Belt. In both cases there is a place of
departure and a place of arrival. However the other parameters which describe a Process differ. A Train has departure
and arrival times, but a Belt has a speed.

I tried the following:


type PlaceDep = Int
type PlaceArr = Int

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)

prc1 = Train      10 11 (TP 1)
prc2 = MovingBelt 12 13 (BP 2)


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)


Is this the classic way of specializing a type or are there better options

See also:
http://stackoverflow.com/questions/33156656/subclassing-a-type-in-haskell/33156888?noredirect=1#comment54137571_33156888


More information about the Haskell-Cafe mailing list