Parsing the untyped λ-calculus with Parsec
mattwetmore.me
mattwetmore.me
Even if you're familiar with parsec and parsing in Haskell, this post includes a fairly good explanation of De Bruijn indices. I've seen them a few times already but this explanation made it click particularly well.
[0] Learn You a Haskell: http://learnyouahaskell.com/
A lot of the same ideas. And indeed, much of the parser is pretty close to the same:
def parseAbst(self):
self.expect('λ')
varName = self.expect(self.parseVar)
self.pushVar(varName)
self.expect('.')
exprTree = self.expect(self.parseExpr)
return abst(varName, exprTree)
(I also have self.accept, etc.)(Of course, much of the complexity is hosted to the class surrounding this)
Posting just in case anyone finds it interesting...here's an (admittedly weak) lambda calculus interpreter, in Oz:
Reading the wikipedia entry, I thought it would be written as λ.1