Archive for March 30, 2003

Sunday, March 30, 2003

Reference to Kinds in English

Kai von Fintel:

This is exciting. Greg Carlson has made a pdf-version of his 1977 dissertation Reference to Kinds in English available on the semanticsarchive.

(In this case, kinds means nouns like dogs, where the meaning depends on context.)

Blocks in Objective-C

Joe Osborn’s OSFoundation framework adds blocks to Objective-C. The context for the block is passed in manually, and the code is specified as a string that’s interpreted at runtime.

Chris Kane mentions that you can do something similar using GCC 3.1’s nested functions and statement expressions. Blocks created in this way have lexical scope, but not dynamic extent, and the syntax is not great.

Eric Raymond on Metadata

Harold Martin:

So Eric Raymond says that meta data or, as he puts it, file attributes, are important. Just as Mac OS X drops support for such meta data. So the Linux community learns this from Mac OS X, but where did OS X learn no meta data from?

Iterators

Erik Barzeski posted about iterators in Cocoa, so I thought I’d give some perspectives from other languages. I think there are really two concepts here: iteration and generation.

I don’t know that everyone agrees with these definitions, but to my mind iteration is about obtaining a sequence of values, without caring about how those values are produced. This is embodied by the NSEnumerator interface, java.util.Iterator, and the Python iteration protocols. Iteration is about how you get the values that you want to consume.

Here’s what iteration looks like in Cocoa:

NSEnumerator *enumerator = [objectStructure objectEnumerator];
id o;
while ( o = [enumerator nextObject] )
    // do something with o

I use a C macro so that I can write it like this:

foreach ( o, [objectStructure objectEnumerator] )
    // do something with o

Some languages, like Python, have a syntax for iteration:

for o in objectStructure:
    # do something with o

In the above example, Python will call objectStructure’s __iter__() method to get an iterator object.

Generation is about how to produce the values. The generator returned by -[NSArray objectEnumerator] probably keeps a reference to the array and remembers an index into that array. When you send it -nextObject, it increments its counter and returns another object. java.util.Iterator is similar. This is straightforward to implement for list-like things. For instance, my SQLite wrapper includes an NSEnumerator subclass for rows in a database table. It took seconds to write. But how do you write this kind of generator for the nodes in a tree? Or the interesting tokens in a mail message? You need to remember more state than just an index, and it can be a real pain. In practice, people often perform a full traversal of the structure, stuffing the objects into an array, and then return the array’s generator. That makes the code much simpler, but inefficient.

I’ve seen two other ways of writing generators, which work better in the more complex cases. What I call the closure approach inverts the problem. Instead of writing code that gets values from a generator object and then does something with them, you put your code in a closure (a.k.a. lambda or block) and give the closure to the generator. This is the approach taken by Smalltalk and Ruby, and it could also work in other languages with closures, such as Lisp and Perl. From the caller’s perspective, we get something like this (in Ruby):

objectStructure.each { |o|
    # do something with o
}

Here, the block (in curly braces) becomes an argument to the method call. The each method traverses objectStructure and evaluates the block many times, passing in different o’s.

Other languages use what I call the yield approach, which was popularized by CLU in the mid-70s. Under this approach, generation is handled by a function that can, in effect, return multiple times. Instead of using a return statement to return, it uses a yield statement. yield is like return, except that the next time the function is called, it resumes after the last-executed yield statement.

Here’s an example in Python:

# A recursive generator that generates Tree leaves in in-order.
def inorder(t):
    if t:
        for x in inorder(t.left):
            yield x
        yield t.label
        for x in inorder(t.right):
            yield x 

Is that beautiful or what?

Interestingly, the keyword that Ruby uses to evaluate a block parameter is also called yield. It’s used for roughly the same reasons, but what’s going on under the hood is quite different. Ruby’s yield is like a function call, whereas CLU/Python’s is like a function return.

I like return-style yield better. It can even be used to create infinite generators (a.k.a. streams):

def odds():
    i = 1
    while True:
        yield i
        i = i + 2

Unfortunately, there is no yield, of either kind, in the C family. You can kind of fake it using macros.

Quote of the Day

Facts do not cease to exist because they are ignored. —Aldous Huxley