Correction: subclasses and classes with same type in instance

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.

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.
participants (2)
-
Daniel Fischer
-
Patrick Browne