Wednesday, March 19, 2008

Forth + λ-calculus

The more that I mess around with Forth, the more that I like messing around with it. So I discovered that pForth (the forth implementation that I'm using right now by virtue of the fact that it is actually easily compiled on all systems that have an ANSI C compliant compiler) has several useful words (forth parlance for functions/procedures) that can implement the concept of functional programming style. The two words which are useful here are ' and execute.

So ' takes the given word and pushes the address of the word onto the stack (as opposed to the return stack), while execute will pop the stack and execute the code that is available from the stack. Now, this means that we now have a means of passing functions as parameters on the stack to various words, which means that if I wanted to compute the integral of a function using Simpson's Rule, I can just set it up such that I can do it generically for some univariate function on the stack, and when I'm using the Simpson's Rule word, all I need to do is to use ' to obtain the address of the univariate function and get on with it.

Amazing. This also means that I could potentially hack out a word that is equivalent to creating anonymous functions using λ-calculus. Speaking of which, λ-calculus is ridiculously useful; I use this extensively in python and sage together with object-oriented programming in order to produce generic functions that work on various types of functions (like integration or differentiation).

Cool stuff. Heheheheheheh....

3 comments:

Anonymous said...

lmao lol too much info dah~ XD

roticv said...

Technically it's just a simple push and pop if you are using an assembler.

The_Laptop said...

Yes, that is true. But the premise of Forth is to be extensible, and not relying on register-based hocus-pocus. The ability is there, but the question is how to access it based on the Forth word list. I'm trying to use the abstraction given in Forth to obtain the effect that I want.

Forth is a stack machine (more accurately, 2-stack or 3-stack machine if floating point is included), while normal architectures are register-stack hybrids.