R – Closures and Context Free Grammars

closurescomputer sciencecontext-free-grammargrammar

I'm looking over my syllabus for my theoretical computer science class and within the heading of Context Free Grammars it lists "closure properties". I have looked through my textbook on this subject and found quite little. The little it does have is a bit above my head at the moment (I haven't taken the course yet) but I understand a little.

I was wondering if this idea of closures within context free grammars is the same as or related to the idea of closures within functional programming. It talks about combining grammars and resolving overlaps as far as I can tell. There are a lot of parts to the section within the book I don't understand yet, so I'm unsure about whether these ideas are the same.

(A little more context: I'm writing an email to the professor asking if the course can be switched to Ruby or Python from Perl. If these concepts are related, that could be another reason we should use Ruby over Perl.)

Best Answer

The term "closure" is used an a variety of ways, mostly tracking back to a Mathematical concept of completion, in some sense.

  • An operator is "closed over" a set of values if applying that operator to values from the set always produces a value from the given set. For example, addition is closed over the integers, but division isn't (4 / 2 is integral, but 5 / 2 isn't). So addition of integers is somehow "complete" in a sense that division isn't.

  • The "transitive" closure of a relation "completes" the relation by following (all possible) multiple applications. In everyday terms, the concept of "is a descendent of" is the transitive closure of the relation "is a child of".

  • A functional "closure" is "completed" by e.g. specifying how the free variables are to be resolved. In the pseudo-code expression:

    bump = function(x) (x + y)
    

    x is the argument to bump, but the definition seems to leave "open" the question of resolving y. On the other hand, if we define:

    bumper = function(y) (function(x) (x + y))
    

    then invoking bumper returns a function which adds the original argument of bumper to the created function's argument, so that:

    add3 = bumper(3)
    

    is equivalent to defining:

    add3 = function(x) (x + 3)
    

    The nested definition is "closed over" (or completed by) the variables available at the point of its definition.

So, in effect, the uses of "closure" above all have different specific meanings, and at first glance seem unrelated, but there's a subtle underlying relationship.