Python's Lisp heritage

Jonathan Hogg jonathan at onegoodidea.com
Tue Jul 16 07:43:06 EDT 2002


On 15/7/2002 21:35, in article agvbln$ouqal$2 at ID-125932.news.dfncis.de,
"Christopher Browne" <cbbrowne at acm.org> wrote:

> If all you have is lexical scoping, parameters have to get explicitly
> passed down the chain in order to get from function A to function C.
> That means introducing additional parameters to function B, which
> didn't actually care about those extra values.
> 
> Most of the time, lexical scope is likely to be more useful.  But
> there are places where dynamic scope is to be preferred.

I don't have an opinion either way really, but I just thought I'd throw
another reference into the discussion. There is an extension to Haskell
which is implemented by GHC (at least) to solve just this problem:

    Implicit Parameters

    Functions may take implicit parameters due to a statically-typed
    extension that gives the advantages of dynamic scope without its
    worst excesses (see the POPL paper for the details).

    <http://www.cse.ogi.edu/~jlewis/implicit.ps.gz>

So you don't need to read the paper to get an example. It looks something
like this:

> sort :: (?cmp :: a -> a -> Bool) => [a] -> [a]

is a type definition of a 'sort' function that takes an implicit 'cmp'
parameter for the comparison function. Any function that uses 'sort'
inherits 'cmp' as an implicit parameter, e.g.: the type of

> least xs = fst (sort xs)

is

> least :: (?cmp :: a -> a -> Bool) => [a] -> a

At some point, you can bind the implicit parameter with:

> min :: [a] -> a
> min  = let ?cmp = (<=) in least

Of course this is all statically-typed, but it's an interesting way to
"tunnel" parameters through existing code.

Jonathan




More information about the Python-list mailing list