[Haskell-cafe] phantom types for inheritance between types
Andrew U. Frank
frank22 at geoinfo.tuwien.ac.at
Fri Aug 27 17:10:18 EDT 2010
there are packages which use phantom types to express inheritance
(subtype) relations. unfortunately i cannot find a simple example and
seem to misunderstand something. can somebody help with the following
simple example?
i have a most general type Dated, a subtype Note and a subsubtype Task.
i have only instances for Note and Task, but operations for tasks and
dated. my primary question is: why should i have the contexts in the
classes (the code compiles without them)? they do not say more than what
is already expressed in the instance declarations. i do also not think
that i have used phantom types properly (eg. gtk2hs uses phantom types
differently).
what would be the proper solution? would it work for cases with multiple
inheritance?
thank you! andrew
class CDated a
class CDated a => CNotes a
class CNotes a => CTasks a
instance CDated Task
instance CDated Note
instance CNotes Note
instance CNotes Task
instance CTasks Task
class Dated n where
datedop :: n -> n
class Notes1 n where
noteop :: n -> n
class Tasks1 n where
taskop :: n -> n
data Note = Note Int
data Task = Task Int
instance CTasks x => Tasks1 x where
taskop n = n
instance CDated x => Dated x where
datedop n = n
t:: Task = Task 1
n :: Note = Note 1
tt = taskop t
-- nt = taskop n -- compiler error -- as expected!
tx = datedop t
nx = datedop n
More information about the Haskell-Cafe
mailing list