[Haskell-cafe] Instances and multiple inheritance
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 ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Instances and multiple inheritance
I fail to understand why instantiating a four-argument class with five arguments seems obvious to you. Отправлено с iPhone Jun 12, 2011, в 12:37, Patrick Browne patrick.bro...@dit.ie написал(а): 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' ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Instances and multiple inheritance
On 12/06/2011 10:43, MigMit wrote: I fail to understand why instantiating a four-argument class with five arguments seems obvious to you. class (Surfaces v o, Paths a b (v o)) = Vehicles v o a b where Obviously I am wrong! But my incorrect thinking is as follows: Surfaces takes 2 arguments, Paths take 3 arguments (giving 5). I do not know how to group those 5 arguments to make the required 4 for Vehicles. The original classes were defined in [1]. Thanks, Pat [1] Modeling the Semantics of Geographic Categories through Conceptual Integration (2002) by Werner Kuhn http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.109.6853 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 ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Instances and multiple inheritance
On 6/12/11 8:48 AM, Patrick Browne wrote: On 12/06/2011 10:43, MigMit wrote: I fail to understand why instantiating a four-argument class with five arguments seems obvious to you. class (Surfaces v o, Paths a b (v o)) = Vehicles v o a b where Obviously I am wrong! But my incorrect thinking is as follows: Surfaces takes 2 arguments, Paths take 3 arguments (giving 5). I do not know how to group those 5 arguments to make the required 4 for Vehicles. The original classes were defined in [1]. Well, the third argument to Paths is defined as being (v o). So, given the first two arguments to Vehicles, we implicitly know the third argument to Paths already (as well as the arguments to Surfaces). It may be more helpful to think of typeclasses as functions which take type parameters and return proof of the implementation of whatever functions and associated types are in the class (colloquially refered to as the type class's dictionary). So, given the following: class (Surfaces v o, Paths a b (v o)) = Vehicles v o a b where... We're defining a function Vehicles which takes four parameters: v, o, a, and b; and returns a dictionary (Vehicles v o a b), or rather a kind of proof that such a dictionary could be returned upon request. Any implementation (aka instance) of this function (aka class) will call the function Surfaces with the arguments v and o in order to get the (Surfaces v o) dictionary. It'll then call Paths with the arguments a, b, and (v o) to get the (Paths a b (v o)) dictionary. Finally, with those two prerequisite dictionaries in hand the instance will package up whatever definitions the instance gives for the members of the typeclass. -- Live well, ~wren ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe