Download Analysis and research into errors produced while

Transcript
- - --
-
-
- ·- - --
-
--
Anah'sis o( 111elfwds ro rC' .wll'<' <'rrnrs
However. it may or may not have been noticed that the formal parameter z in the function
foe has two different type variables associated with it. namely T: and T.-. As all occurrences of a
formal parameter in a function body must have the same type~. it must be the case that T: is the
same type as T.-. so any occurrence ofT.. in the derived type equations can be replaced with T 2 .
Also replacing T' with Til -7 T1. this gives the revised type equations :
T1 = T2 -7 (Tr, -7 T,J
T1 = T2 -7 T()
So. the overall types of the three formal parameters of the function foo are x :: T 2 -7
-7 T- ). y :: T 2 -7 Til and z :: T2. and finally the return type of the function is T7.
z
y
X
(Th
Return type
Looking at the complete function signature for foo. it looks similar to something which
may be output as the description of a type error when a failed compilation has taken place. for
example
cannot unify nurn -7
[nurn]
-> bool with
[nurn) -7
[nurn] -7 bool
When it is reported that the compiler cannot unify a with b, where a and b are some type
expressions. a is what the compiler is unable to unify consistently with some previously defined
equation with the type expression, b. So in the example above, nurn -7 [nurn] -7 bool is not
consistent with what the compiler is expecting to see based on an equation that has occurred
before- [nurn) -7 [nurn] -7 bool.
4.5.1 Moving from arbitrary to concrete types
Having inferred an arbitrary type equation using parse trees, it is still undetermined which
arguments have which types. Inferring a type equation for a particular function is only really of
any use if there is some chance that specific or concrete types can be attributed to the formal
parameters.
Once the parse tree has been created, the actual types of the arguments can be deduced by
looking at the operators or connectives which link them together. When the function foo was
considered earlier, no operators were shown, merely abstract applications to demonstrate how
type equations could be inferred in the absence of a type signature. To show how concrete types
can be assigned. consider the simple function one_to_list, which adds I to each element in a
numbered list.
one_to_list x = map
(+1) x
' This restriction on the types of formal parameters within the body of a function is often referred to as the
mo11nmorphism limitation - each formal parameter must be used monomorphically .
27