Python from Wise Guy's Viewpoint

Matthias Blume find at my.address.elsewhere
Mon Oct 27 22:24:03 EST 2003


Pascal Costanza <costanza at web.de> writes:

> Can you give a better example of a program that would render
> meaningless without type annotations?

   fun f A = "A"
     | f B = "B"

This could be the function that always returns "A" for arbitrary
arguments, it could be the function that can only be legally applied
to the value (constructed by) A where it returns "A", it could be the
function that can be applied precisely to values A and B, it could
accept more than those two and raise an exception if the argument is
neither A nor B, it could be the function that can be applied to A
where it returns "A" and also many other values where it always
returns "B", ...

Which of these versions you get depends in part on the typing
environment that is in effect when the above code is encountered by
the compiler.

Matthias

PS: Just in case you wonder, here are examples of type definitions
which would trigger the results outlined above.  I go in the same
order:

1.   (* no type definition in effect *)
2.   datatype t = A
3.   datatype t = A | B
4.   datatype t = A | B | C
5.   datatype t = A | C | D of int | E of real | F of t -> t

Notice that the compiler will probably warn about a redundant match in
cases 1. and 2. and about a non-exhaustive match in case 4.




More information about the Python-list mailing list