|
| 1 | +# lambda-php |
| 2 | + |
| 3 | +Lambda calculus interpreter in PHP. |
| 4 | + |
| 5 | +## What? |
| 6 | + |
| 7 | +Lambda calculus is a very minimal programming language that was invented in |
| 8 | +1936 by Alonzo Church. It is the functional equivalent of the Turing Machine. |
| 9 | + |
| 10 | +Lambda calculus has only three concepts: Function definitions, lexically |
| 11 | +scoped variables, function application. |
| 12 | + |
| 13 | +An example term would be the identity function: |
| 14 | + |
| 15 | + λx.x |
| 16 | + |
| 17 | +The first part `λx` defines a function that takes an `x`, the `.` signifies |
| 18 | +that the part that follows is the function body. The body just returns `x`. |
| 19 | + |
| 20 | +In PHP, you would write the same thing as follows: |
| 21 | + |
| 22 | + function ($x) { |
| 23 | + return $x; |
| 24 | + } |
| 25 | + |
| 26 | +You can nest function definitions. Here is a function returning a function: |
| 27 | + |
| 28 | + λx.λy.x |
| 29 | + |
| 30 | +And you can also *apply* a function to an argument, which just means calling |
| 31 | +the function. |
| 32 | + |
| 33 | + λf.λg.f g |
| 34 | + |
| 35 | +Which is the short hand (left-associative) form of writing |
| 36 | + |
| 37 | + λf.λg.(f g) |
| 38 | + |
| 39 | +Nested calls like: |
| 40 | + |
| 41 | + λf.λg.λh.f g h |
| 42 | + |
| 43 | +Are interpreted as: |
| 44 | + |
| 45 | + λf.λg.λh.((f g) h) |
| 46 | + |
| 47 | +If you want to change the grouping to be right-associative, you need to |
| 48 | +explicitly group them in parentheses: |
| 49 | + |
| 50 | + λf.λg.λh.(f (g h)) |
| 51 | + |
| 52 | +Interestingly, lambda calculus is turing complete. Using just these three |
| 53 | +concepts you can represent *any* computation. |
| 54 | + |
| 55 | +## REPL |
| 56 | + |
| 57 | +This project ships with a read-eval-print-loop that you can use to evaluate |
| 58 | +lambda calculus expressions: |
| 59 | + |
| 60 | + $ php repl.php |
| 61 | + |
| 62 | +By default, it is in *int-mode*, expecting the result of the expression to be |
| 63 | +a church-encoded number. Example: |
| 64 | + |
| 65 | + $ php repl.php |
| 66 | + i> λf.λx.f (f (f x)) |
| 67 | + 3 |
| 68 | + |
| 69 | +You can switch to *bool-mode* by sending the `b` command: |
| 70 | + |
| 71 | + $ php repl.php |
| 72 | + i> b |
| 73 | + b> λx.λy.x |
| 74 | + true |
| 75 | + |
| 76 | +## Further reading |
| 77 | + |
| 78 | +* [Lambda Calculus - Wikipedia](http://en.wikipedia.org/wiki/Lambda_calculus) |
| 79 | +* [Matt Might: 7 lines of code, 3 minutes](http://matt.might.net/articles/implementing-a-programming-language/) |
| 80 | +* [Tom Stuart: Programming with Nothing](http://codon.com/programming-with-nothing) |
| 81 | +* [Erkki Lindpere: Parsing Lambda Calculus in Scala](http://zeroturnaround.com/rebellabs/parsing-lambda-calculus-in-scala/) |
0 commit comments