Python from Wise Guy's Viewpoint
Dirk Thierbach
dthierbach at gmx.de
Sun Oct 26 03:29:20 EST 2003
Brian McNamara! <gt5163b at prism.gatech.edu> wrote:
> prunesquallor at comcast.net once said:
>>(defun black-hole (x)
>> #'black-hole)
>>(for non lispers, the funny #' is a namespace operator.
>> The black-hole function gobbles an argument and returns
>> the black-hole function.)
> Finally, an example that I don't think you can type in Haskell.
It's a bit tricky. As Remi has shown, you need recursive types.
Recursive types in Haskell always need an intervening data type
constructor. That's a conscious design decision, because recursive
types are very often a result of a real typing error. (IIRC, that's
why OCaml added an option to enable recursive typing after having it
enabled as default for some time, but Remi should know that better
than I do.)
We also need an existential type in this constructor, because the
argument can be of different type for each application of the black hole.
> data BlackHole = BH (forall a. a -> BlackHole)
Now we can write the black hole function itself:
> black_hole :: BlackHole
> black_hole = BH (\_ -> black_hole)
That's it. However, we cannot apply it directly. We have to "unfold"
it explicitely by taking it out of the data constructor. We define an
auxiliary infix function to take care of that.
> infixl 0 $$
> ($$) :: BlackHole -> a -> BlackHole
> (BH f) $$ x = f x
Now we can write a function like
> f = black_hole $$ "12" $$ 5 $$ True
which will nicely typecheck.
That's the first time one actually has to add non-obvious stuff to
"please" the type checker. OTOH, the black hole function is pretty
bogus, so one can argue that this is a realistic price to pay to say
that you really really want this strange function. I would be very
surprised if this function is of any use in a real-world program.
And there is nothing else you can do with arguments of arbitary type
but silently discard them, so I guess there are not many other examples
like this.
- Dirk
More information about the Python-list
mailing list