Type classes and code generation

Bayley, Alistair Alistair_Bayley@ldn.invesco.com
Tue, 17 Jun 2003 09:09:26 +0100


I had a discussion with someone over the type class mechanism and would like
to clarify something.

When I compile this trivial program:

> module Main where
> main = putStrLn (show (1 + 2))

with ghc -Wall, the compiler says:

Main.lhs:3:
    Warning: Defaulting the following constraint(s) to type `Integer'
	     `Num a' arising from the literal `2' at Main.lhs:3

This implies to me that the compiler is generating the code for (+) for the
particular instance, rather than using a run-time dispatch mechanism to
select the correct (+) function. Is this correct, or am I way off? Does the
compiler *always* know what the actual instances being used are? Is there
some way of preventing the type mechanism from generating code for the
instance type, as opposed to the class? 


If I am correct, does it work the same way across module boundaries? (I
would think so.) If a module exports a class but no instances for that
class, then a user of that class would have to install their own instances.
OTOH, if the class plus one or more instances were exported, then a user
could use the supplied instance types, and the compiler would still generate
code to use the specific instances.


*****************************************************************
The information in this email and in any attachments is 
confidential and intended solely for the attention and use 
of the named addressee(s). This information may be 
subject to legal professional or other privilege or may 
otherwise be protected by work product immunity or other 
legal rules.  It must not be disclosed to any person without 
our authority.

If you are not the intended recipient, or a person 
responsible for delivering it to the intended recipient, you 
are not authorised to and must not disclose, copy, 
distribute, or retain this message or any part of it.
*****************************************************************