[Haskell-cafe] Instances and multiple inheritance
Patrick Browne
patrick.browne at dit.ie
Sun Jun 12 10:37:08 CEST 2011
How do I make an instance of the Vehicles class below?
Thanks,
Pat
class Containers x y where
insert :: y -> x y -> x y
remove :: y -> x y -> x y
whatsIn :: x y -> [y]
instance Containers [] Char where
insert y [] = y:[]
insert y m = y:m
remove _ [] = []
remove x (y:ys) | x == y = remove x ys
| otherwise = y : remove x ys
whatsIn = id
instance Containers [] Integer where
insert y [] = y:[]
insert y m = y:m
remove _ [] = []
remove x (y:ys) | x == y = remove x ys
| otherwise = y : remove x ys
whatsIn = id
instance Containers [] [a] where
insert x y = x:y
-- lists of lists
-- insert [1] [[3,6],[45]]
class Surfaces a b where
put :: b -> a b -> a b
takeOff :: b -> a b -> a b
whatsOn :: a b -> [b]
instance Surfaces [] Integer where
put y [] = y:[]
put y m = y:m
takeOff _ [] = []
takeOff x (y:ys) | x == y = takeOff x ys
| otherwise = y : takeOff x ys
whatsOn = id
class Contacts a b where
attach :: b -> a b -> a b
detach :: b -> a b -> a b
whatsAt :: a b -> [b]
instance Contacts [] Integer where
attach y [] = y:[]
attach y m = y:m
detach _ [] = []
detach x (y:ys) | x == y = detach x ys
| otherwise = y : takeOff x ys
whatsAt = id
class Paths a b c where
move :: c -> a b c -> a b c
origin, destination :: a b c -> b
whereIs :: a b c -> c -> b
instance Paths (,) Integer Integer where
-- `Integer -> (Integer, Integer) -> (Integer, Integer)'
move c (a,b) = (a+1,b+1)
origin (a,b) = a
destination (a,b) = b
whereIs (a, b) c = b
-- Test
-- move (1::Integer) (3::Integer,5::Integer)
-- origin (3::Integer,5::Integer)
class People p where
class HeavyLoads l where
class Surfaces w o => WaterBodies w o where
instance WaterBodies [] Integer where
class Containers h o => Houses h o where
class (Surfaces v o, Paths a b (v o)) => Vehicles v o a b where
-- I do not know how to make an instance of Vehicles
-- What seems to be the *obvious* does not work
-- instance Vehicles [] Integer (,) Integer Integer where
-- Kind error: `Vehicles' is applied to too many type arguments
-- In the instance declaration for
-- `Vehicles [] Integer (,) Integer Integer'
This message has been scanned for content and viruses by the DIT Information Services E-Mail Scanning Service, and is believed to be clean. http://www.dit.ie
More information about the Haskell-Cafe
mailing list