Original-Via: uk.ac.nsf; Fri, 29 Nov 91 19:37:26 GMT
X-Comment1: #############################################################
X-Comment2: # uk.ac.glasgow.cs has changed to uk.ac.glasgow.dcs #
X-Comment3: # If this address does not work please ask your mail #
X-Comment4: # administrator to update your NRS & mailer tables. #
X-Comment5: #############################################################
Original-Sender: [EMAIL PROTECTED]
| From: Satish Thatte <[EMAIL PROTECTED]>
| Date: Fri, 22 Nov 91 10:28:20 EST
| Suppose I define
|
| f x y z = if y==z then x*x else x
|
| the typing will be
|
| f :: Num a, Eq b => a -> b -> b -> a
|
| with a and b quantified. As far as I understand, the instantiations of Eq
| Num in scope at this point are irrelevant in the meaning of f.
| The rationale (presumably) is that since f is a closure, we might as
| well wait until it is applied to circumscribe its applicability.
| Now suppose I define
|
| g = f 3::Int
|
| the type for g will be
|
| Num Int, Eq b => b -> b -> Int
|
| My understanding is that, if Int is not an instance of Num at this
| point, an error will be declared (although g is also a function, and
| there is no particular reason not to wait until it is applied before
| checking that Int is an instance of Num). This of course ignores
| the fact that Int is declared an instance of Num in the prelude.
Actually, the typing of g should be
g :: Eq b => b -> b -> Int
Haskell takes the view that since only one instance Eq of Int can hold in the
program, it should be in scope at the declaration of g, and should be resolved
there.
| I guess I have two questions:
|
| 1. Am I right in my interpretation of the meaning of contexts?
| Is there some place in the report where this is explained?
|
I agree that this is inexplicit in the report.
It is confoundedly hard to be explicit about everything without giving the
formal semantics. We have a paper in preparation which gives a formal static
semantics for a miniHaskell language, which hopefully contains the essence of
overloading without all the syntactic richness of Haskell.
| 2. If I am, what is the rationale for this decision? It seems to me
| that if such checking is delayed at all, it ought to be delayed until
| it is time to print something!
You are right that in theory everything could be postponed.
But, the type of main would then be something like:
main :: (Ord Int, Enum Foo, Ix Baz, Eq [(Baz,Foo)], ...) => Dialogue
This is possible, but horribly inefficient (all those dictionaries getting
passed up the way). Given that only one instance can hold, it seems much
nicer to resolve the overloading on the spot.
Simon