Expressions, Types, and Functions |
It is hard to justify a text about functional programming without a fair treatment of types. In this chapter we will go over the most important concepts of types, as relevant in the functional programming paradigm.
5.1 Types | 5.4 An example of type checking |
5.2 Type checking | 5.5 Types in functional programming languages |
5.3 Static type checking |
5.1. Types
Contents Up Previous Next Slide Subject index Program index Exercise index
In general, we see three advantages of dealing with types:
The notion of type is used to make programs more readable, make them run more efficient, and
to detect certain errors before they cause errors in the calculation. |
Let us now make a more detailed characteristics of these advantages in the following itemized list.
|
The correctness quality is often brought into focus. In the next few sections we will therefore discuss type checking issues.
5.2. Type checking
Contents Up Previous Next Slide Subject index Program index Exercise index
Type checking is the processes of identifying errors in a program based on explicitly or implicitly stated type information |
Below we will identify three kinds of 'typing', which are related to three different approaches to type checking.
|
According to section 1.1 the Scheme Report (R5RS) 'Scheme has latent as opposed to manifest types. Types are associated with values (also called objects) rather than with variables.' In our categorization, Scheme is strongly typed and types are dealt with at run time (on values) as a contrast to compile time (on variables).
It is worth noticing that we classify Scheme as supporting strong typing. Many programmers will probably be surprised by this categorization, because the 'typing' in Scheme is experienced to be relatively 'weak' and 'dynamic'. However, type errors in Scheme do not cause erroneous calculations. Type errors are discovered at a low and basic level. As such we find it justifiable to classify the typing in Scheme as being strong. Notice however, that there is no trace of static type checking in Scheme. Static type checking is the rule in most modern programming languages today, and static type checking is also an absolutely key aspect in functional programming languages such as ML [Harper88] and Haskell [Hudak92].
5.3. Static type checking
Contents Up Previous Next Slide Subject index Program index Exercise index
There are two main kinds of static type checking: explicit type decoration and implicit type inference |
For the sake of the discussion we will involve the following example:
|
Explicit type decoration is well-known to most computer science students.
If you want to study additional details about implicit type inference you should consult a textbook of ML or Haskell programming.
5.4. An example of type checking
Contents Up Previous Next Slide Subject index Program index Exercise index
We will now discuss type checking relative to the three kinds of 'typing', which we identified in Section 5.2.
Is the expression (+ 1 (if (even? x) 5 "five")) correct with respect to types? |
|
When we use the word conservative for static type checking, we refer to the caution of the type checker. Independent of branching, and in 'worst cases scenarios', the type constraints should be guaranteed to hold.
5.5. Types in functional programming languages
Contents Up Previous Next Slide Subject index Program index Exercise index
Scheme is not representative for the handling of types in most contemporary functional programming languages |
|
Due to the handling of types, Scheme and Lisp are elastic and flexible compared with ML, Haskell, and other similar language
which are quite stiff and rigid. |
5.6. References