[Haskell-cafe] Correction: subclasses and classes with same type in instance

2011-10-16 Thread Patrick Browne
Hi,
Does the subclass relation have any meaning when two classes have instances with the same type?
I get the same results from Listing 1 and Listing 2 below.
Regards,
Pat

-- Listing 1- Subclass
data Shed = Shed 

class Building building where
 addressB :: building - Integer
 addressB b = 1

--  subclass, but none in Listing 2
class Building house = House house where
 addressH :: house - Integer
 addressH b = 0

instance Building Shed where
instance House Shed where


-- Listing 2 -- No subclass
data Shed = Shed 

class Building building where
 addressB :: building - Integer
 addressB b = 1

-- No subclass 
class House house where
 addressH :: house - Integer
 addressH b = 0

instance Building Shed where
instance House Shed where


-- Test runs give same result for Listing 1 and Listing 2
--  addressH Shed
--  addressB Shed


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Correction: subclasses and classes with same type in instance

2011-10-16 Thread Daniel Fischer
On Sunday 16 October 2011, 20:03:02, Patrick Browne wrote:
 Hi,
 Does the subclass relation have any meaning when two classes
 have instances with the same type?
 I get the same results from Listing 1 and Listing 2 below.
 Regards,
 Pat

The only effect of a superclass constraint is that you can't make a type an 
instance of the subclass without a superclass instance for the type in 
scope.
Usually, a superclass constraint means there is a connection between the 
methods of both classes (like for Eq/Ord), and then it is expected that the 
instances respect that connection, but the compiler can't enforce that.

In your example, the only difference is that with the superclass constraint

foo :: House h = h - Integer
foo h = addressB h + addressH h

works, while without superclass constraint, foo would need both classes in 
its context.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Correction: subclasses and classes with same type in instance

2011-10-16 Thread Patrick Browne
In the current example does the following totally or partially ignore the type class system.boo :: Shed - Integerboo h = addressB h + addressH hOn 16/10/11, Daniel Fischer  daniel.is.fisc...@googlemail.com wrote:In your example, the only difference is that with the superclass constraintfoo :: House h = h - Integerfoo h = addressB h + addressH hworks, while without superclass constraint, foo would need both classes in its context.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Correction: subclasses and classes with same type in instance

2011-10-16 Thread Daniel Fischer
On Sunday 16 October 2011, 21:50:13, Patrick Browne wrote:
 In the current example does the following totally or partially ignore
 the type class system.
 boo :: Shed - Integer
 boo h = addressB h + addressH h

It doesn't ignore the type class system at all.
It's a monomorphic function using methods from the classes Building and 
House, so it just has to verify that Shed is an instance of both classes.
Without the superclass constraint on House, it's two unrelated lookups, 
with the superclass constraint, the compiler can choose to lookup both 
separately, or it could first determine that due to the superclass 
constraint, it needs only look up the House instance.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe