Python from Wise Guy's Viewpoint

Joachim Durchholz joachim.durchholz at web.de
Sat Oct 25 11:05:28 EDT 2003


prunesquallor at comcast.net wrote:

> "Marshall Spight" <mspight at dnai.com> writes:
> 
> 
>>It would be really interesting to see a small but useful example
>>of a program that will not pass a statically typed language.
>>It seems to me that how easy it is to generate such programs
>>will be an interesting metric.
>
> Would this count?
> 
> (defun noisy-apply (f arglist)
>   (format t "I am now about to apply ~s to ~s" f arglist)
>   (apply f arglist))

It wouldn't typecheck in Haskell because you don't restrict the elements 
of the arglist to be of the "Show" type class, which is the group of 
types that have a printable representation.

Other than that, the declaration of this function would be (in a 
Pascal-inspired notation)

   noisy_apply (f: function (x: a): b, x: a): b

Note that a and b are type parameters here: if noisy_apply is given a 
function with input type a and output type b, it will /demand/ that its 
second parameter is of type a, and its result type will be b.

I.e. in C++, you'd write something like

   template <a, b> {
     b noisy_apply ( (b) (f (a)), a x );
   }

(syntax is most likely dead wrong, it's been a while since I was 
actively working with C++).

For completeness, here's the Haskell type:

   (a -> b) -> a -> b

(I hope I got this one right.)
The first "a -> b" translates as "a function that takes any type a and 
returns any type b".
The x -> y -> z notations translates as "a function taking input values 
of types x and y and returning a value of z type".
If the same type letter occurs more than once, if must be the same type 
in calls.
(Yes, the function is polymorphic, though in a different way than OO 
polymorphism: most OO languages don't allow expressing the restriction 
that the two "a" parameters must be of the same type but the caller is 
free to choose any type.)
To sum it all up: the above specifications are all intended to say the 
same, namely "noisy_apply is a function that takes an arbitrary 
function, and another parameter that must be of the same type as the 
input parameter for the supplied function, and that will return a value 
of the same type as the supplied function will return".
Modern static type systems can express such types :-)


You might ask "where's the argument list"?
The answer is that I'm assuming a "currying" language. All functions in 
such a language have a single argument; functions with multiple 
arguments are written as a function that takes an argument and returns 
another function which expects the next argument.

I.e.
   add (3, 4)
is first evaluated as
   [***] (4)
where [***] is an internally-created function that adds 3 to its single 
argument.
(That's just the theory how the operation is defined. Currying languages 
will usually be executed in the obvious manner, unless the code takes 
specific advantage of currying.)


HTH
Jo





More information about the Python-list mailing list