6_Reading Chapter7

6_Reading Chapter7 - 7 Type Classes A symbol is overloaded...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 7 Type Classes A symbol is overloaded if it has two (or more) meanings, distinguished by type, that are resolved at compile time. For example, in Haskell, as in many other languages, the operator + has (at least) two distinct implementations associated with it, one of type Int -> Int -> Int , the other of type Float -> Float -> Float . The reason that both of these operations are given the name + is that both compute numeric addition. However, at the implementation level, the two operations are re- ally very different. Because integers are represented in one way (as binary numbers) and floating point numbers in another (as exponent and mantissa, following scien- tific notation), the way that integer addition combines the bits of its arguments to produce the bits of its result is very different from the way this is done in floating point addition. A characteristic of overloading is that overloading is resolved at compile time. If a function is overloaded, then the compiler must choose between the possible algorithms at compile time. The process of choosing one algorithm from among the possible algorithms associated with an overloaded function is called overload resolution. In many languages, if a function is overloaded, then only the function arguments are used to resolve overloading. For example, consider the following two expressions: 3 + 2 {- add two integers - } 3.0 + 2.0 {- add two floating point numbers - } Here is how the compiler will produce code for evaluating each expression: 3 + 2 : The parsing phase of the compiler will build the parse tree of this ex- pression, and the type-checking phase will compute a type for each symbol. Because the type-checking phase will determine that + must have type Int -> Int -> Int , the code-generation phase of the compiler will produce machine instructions that perform integer addition. 3.0 + 2.0 : The parsing phase of the compiler will build the parse tree of this 153 154 Type Classes expression, and the type-checking phase will compute a type for each symbol. Because the type-checking phase will determine that + must have type Float-> Float -> Float , the code-generation phase of the compiler will produce machine instructions that perform floating point addition. Automatic conversion is a separate mechanism that may be combined with overloading. However, it is possible for a language to have overloading and not to have automatic conversion. Haskell, for example, does not do automatic conversion. 7.1 OVERLOADING VERSUS POLYMORPHISM Parametric polymorphism can be contrasted with overloading. In an influential his- torical paper, Christopher Strachey referred to Haskell-style polymorphism as para- metric polymorphism (although Haskell had not been invented yet) and overloading as ad hoc polymorphism . The key difference between parametric polymorphism and overloading is that parameteric polymorphic functions use one algorithm to oper- ate on arguments of many different types, whereas overloaded functions may use a...
View Full Document

{[ snackBarMessage ]}

Page1 / 21

6_Reading Chapter7 - 7 Type Classes A symbol is overloaded...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online