├── index.js ├── .mocha.opts ├── .travis.yml ├── .gitignore ├── .babelrc ├── tsconfig.json ├── public ├── lambda.png ├── index.html └── style.css ├── src ├── components │ ├── Repl │ │ ├── Info.jsx │ │ ├── Assignment.jsx │ │ ├── Computation.jsx │ │ ├── Error.jsx │ │ ├── LambdaMetadata.jsx │ │ └── index.jsx │ ├── App │ │ ├── ProblemPrompter.jsx │ │ └── index.jsx │ └── LambdaInput │ │ └── index.jsx ├── lambdaActor │ ├── errors.js │ ├── actor.js │ ├── worker.js │ ├── timeoutWatcher.js │ ├── astToMetadata.js │ └── executionContext.js ├── util │ ├── generateGoldens.js │ └── persist.js ├── index.jsx ├── lib │ └── lambda │ │ ├── errors.ts │ │ ├── equality.ts │ │ ├── renderer.ts │ │ ├── index.ts │ │ ├── cannonize.ts │ │ ├── __tests__ │ │ ├── generated_suite.spec.js │ │ ├── lexer.spec.js │ │ ├── cannonize.spec.js │ │ ├── util.spec.js │ │ ├── normalize.spec.js │ │ ├── parser.spec.js │ │ ├── generated_suite.data.js │ │ └── bReduction.spec.js │ │ ├── types.ts │ │ ├── churchPrimitives.ts │ │ ├── normalize.ts │ │ ├── lexer.ts │ │ ├── util.ts │ │ ├── parser.ts │ │ └── operations.ts └── game │ ├── inlineDefinitions │ ├── InlineDefinition.jsx │ └── definitions.js │ └── problems │ ├── readme.md │ └── index.js ├── .mocharc.json ├── README.md ├── webpack.config.cjs ├── LICENSE └── package.json /index.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.mocha.opts: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "node" 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | npm-debug.log 3 | build 4 | public/build 5 | -------------------------------------------------------------------------------- /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["@babel/env", "@babel/preset-react"] 3 | } 4 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "strict": true 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /public/lambda.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evinism/lambda-explorer/HEAD/public/lambda.png -------------------------------------------------------------------------------- /src/components/Repl/Info.jsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | 3 | export default props => (
[solved]
)} 13 | {problem.prompt} 14 |An expression is any valid combination of lambda abstractions, applications, and variables. You're typing expressions into the interpreter!
10 |An application is a term in the lambda calculus where two expressions are "applied" to each other.
15 |This is akin to calling A with B as an argument
16 |A lambda abstraction term of the form λ [head] . [body]
21 |Lambda abstractions represent functions in the lambda calculus.
22 |The parameter is the variable that goes in the head of the lambda abstraction
27 |When beta-reducing, all instances of the parameter get replaced with the
28 |The body of a lambda abstraction is section that follows the dot
36 |This represents what the function returns.
37 |When your expression is an application, the argument is the right side of the application.
42 |For example, in the expression ab, the argument is b
43 |An expression is beta reducible if the expression is an application where the left side is a lambda abstraction.
49 |In this case, the left side of the application is λa.aba and the right side is c
50 |11 | Interactive REPL and tutorial for the untyped Lambda Calculus 12 |
13 |14 | Click to begin the tutorial 15 |
16 |Let's get acquainted with some basic syntax. First, type a₁. Letters followed optionally by numbers represent variables in this REPL.
In the actual lambda calculus, it's a bit broader, but we'll keep it simple right now.
67 |You just wrote an expression which contains only the variable a₁, which is just a symbol, and not currently bound to anything. In the lambda calculus, variables can be bound to functions, and variables can be applied to one another.
To apply the variable a₁ to the variable b₁, type in a₁b₁. This represents calling the function a₁ with b₁ as an argument.
Remember, the variable or function you're applying always goes first
79 |Try applying one variable to another.
80 |Since lots of variables in the Lambda Calculus are single letters, there's often a semantic ambiguity when written down. For example, if I type in hi, do I mean one variable hi, or the variable h applied to variable i?
For ease of use in this REPL, we'll make a small comprimise: upper case letters are interpreted as multi-letter variables, and lower case letters are interpreted as single-letter variables.
92 |Try typing MULT, and observe that it's interpreted as one variable, and NOT an application.
Now we'll get into lambda abstractions. Lambda abstractions represent functions in the lambda calculus. A lambda abstraction takes the form λ [head] . [body] where [head] is the parameter of the function, and [body] is what the function resolves to.
Let's write the identity function; a function which takes its argument, does nothing to it, and spits it back out. In the lambda calculus, that looks something like λa.a
as a reminder, you can type backslash (\) for λ
Schweet! This takes one argument a and outputs that same argument! Now go ahead and wrap the whole thing in parentheses
Perfect! In the lambda calculus, you can always wrap
Now in the same way that we can apply variables to other variables, we can apply lambda expressions to variables. Try applying your identity function to the variable b, by writing (λa.a)b.
Don't worry if this doesn't make sense yet, we'll go a bit more in depth in the future.
131 |Nice! What happened here is your identity function took b as the input and spit it right back out. The process of evaluating a function like this is called beta reduction.
The result you're seeing here is in what's called normal form, which we'll also go through a little later.
141 |Just like we can evaluate functions with variables, we can also evaluate them with other functions! Try typing (λa.a)λb.b
So we can perform beta reductions with other functions as the argument!
151 |With that, we've just introduced the main elements of the syntax of the lambda calculus:
152 || Variables | a₁ |
| Applying one expression to another | a₁b₁ |
| A lambda abstraction | λx.y |
| Parentheses | (λx.y) |
We've also introduced a few ways in which these can be combined.
159 || Applying one lambda expression to a variable | (λx.x)b₁ |
| Applying one lambda expression to another | (λa.a)λb.b |
It's time to solidify our understanding of how these combine syntactically. Write any expression to continue.
164 |Repeated
To make this clearer, if we were to explicity write out the parentheses for the expression abcd, we'd end up with ((ab)c)d. That is, in the expression abcd, a will first be applied to b, then the result of ab will be applied to c, so on and so forth.
Write out the parentheses explicitly for ijkmn
This means that if we write the expression λx.yz, it would be parenthesized as λx.(yz) and NOT (λx.y)z.
As a rule of thumb, the body of a lambda abstraction (i.e. the part of the lambda expression after the dot) extends all the way to the end of the expression unless parentheses tell it not to.
195 |Explicitly write the parentheses around λw.xyz, combining this new knowledge with what you learned in the last question around how applications are parenthesized.
Solution: λw.((xy)z)
197 |So what if we DID want to apply a
For example, if we wanted to apply the lambda abstraction λx.y to variable z, we'd write it out as (λx.y)z
Write an expression that applies the lambda abstraction λa.bc to the variable d.
Fortunately, the other direction requires fewer parentheses. If we wanted to apply a variable to a lambda abstraction instead of the other way around, we'd just write them right next to each other, like any other application.
224 |Concretely, applying a to lambda abstraction λb.c is written as aλb.c
Try applying w to λx.yz!
As you may have noticed before, functions can only take one argument, which is kind of annoying.
235 |Let's say we quite reasonably want to write a function which takes more than one argument. Fortunately, we can sort of get around the single argument restriction by making it so that a function returns another function, which when evaluated subsequently gives you the result. Make sense?
236 |In practice, this looks like λa.λb. [some expression]. Go ahead and write any 'multi-argument' function!
Getting the hang of it!
250 |Representing functions with multiple arguments like this is so convenient, we're going to introduce a special syntax. We'll write λab. [some expression] as shorthand for λa.λb. [some expression]. Try writing a function using that syntax!
We've just gone through a whirlwind of syntax in the Lambda Calculus, but fortunately, it's almost everything you need to know.
278 |As a final challenge for this section on syntax, try writing out the expression that applies the expression aλb.c to variable d
Let's take a deeper look at Beta Reductions.
288 |When an
Here are a few examples of beta reducible expressions:
290 || Expression | 294 |Explanation | 295 |
|---|---|
(λx.y)z | Lambda abstraction λx.y applied to z |
(λa.b)λc.d | Lambda abstraction λa.b applied to λc.d |
(λzz.top)λy.ee | Lambda abstraction λz.λz.top applied to λy.ee |
And here are a few examples of expressions that are NOT beta reducible:
304 || Expression | 308 |Explanation | 309 |
|---|---|
zλx.y | Variable z applied to λx.y |
λa.bcd | Lambda abstraction λa.bcd, but not applied to anything |
bee | Application be applied to e |
f(λg.h)i | Application f(λg.h) applied to i (This one's tricky! Remember that applications are left-associative). |
Write any beta reducible expression that does not appear in the above table.
319 |As you might guess, if something is beta reducible, that means we can perform an operation called beta reduction on the expression.
337 |Beta reduction works as follows:
338 || Expression | 342 |Step | 343 |
|---|---|
(λa.aba)c | Start with a |
(λa.cbc)c | In the |
λa.cbc | Erase the argument. |
cbc | Erase the |
That's all there is to it!
353 |Write any expression that beta reduces to pp.
As we showed in the beginning, this works on functions as well!
365 |Let's work through an example for a function:
366 || Expression | 370 |Step | 371 |
|---|---|
(λx.yx)λa.a | Start with a beta reducible expression. |
(λx.y(λa.a))λa.a | In the |
λx.y(λa.a) | Erase the argument. |
y(λa.a) | Erase the |
Write any expression that beta reduces to iλj.k.
It's prudent to make a distinction between bound and free variables. When a function takes an argument, every occurrence of the variable in the body of the function is bound to that parameter.
392 |For quick example, if you've got the expression λx.xy, the variable x is bound in the lambda expression, whereas the variable y is currently unbound. We call unbound variables like y free variables.
Write a lambda expression with a free variable c (hint: this can be extremely simple).
Easy enough. In this REPL you can see what free variables are in an expression (as well as a lot of other information) by clicking the (+) that appears next to results.
403 | 404 |It might be obvious that there are multiple ways to write a single lambda abstraction. For example, let's take that identity function we wrote all the way in the beginning, λa.a. We could have just as easily used x as the parameter, yielding λx.x.
The lambda calculus's word for "renaming a parameter" is alpha-conversion.
406 |Manually perform an alpha conversion for the expression λz.yz, by replacing z with t
Occasionally, we'll get into a situation where a variable that previously was unbound is suddenly bound to a parameter that it shouldn't be. For example, if we tried beta-reducing (λab.ab)b without renaming to resolve the conflict, we'd get λb.bb. What originally was a free variable b is now (accidentally) bound to the parameter of the lambda expression!
To eliminate this conflict, we have to do an alpha-conversion prior to doing the beta reduction.
420 |Try inputting an expression (like (λab.ab)b) that requires an alpha conversion to see how the REPL handles this situation.
Notice that epsilon that pops up? That's this REPL's placeholder variable for when it needs to rename a variable due to a conflict.
433 |Often, an expression is not beta reducible itself, but contains one or more beta reducible expressions (redexes) nested within. We can still evaluate the expression!
434 |Try writing a function with a nested redex!
435 |Possible solution: λa.(λb.b)c
436 |"But wait," I hear you shout. "What if I have more than one reducible subexpression in my expression? Which do I evaluate first?"
447 |Let's traverse the expression, left to right, outer scope to inner scope, find the leftmost outermost redex, and evaluate that one. This is called the normal order.
448 |Try typing and expanding ((λb.b)c)((λd.d)e) to see what I mean.
If we do this repeatedly until there's nothing more to reduce, we get to what's called the "normal form". Finding the normal form is analogous to executing the lambda expression, and is in fact exactly what this REPL does when you enter an expression.
459 |In this REPL you can see the steps it took to get to normal form by pressing the (+) button beside the evaluated expression.
460 |Type in any expression to continue.
461 |It's possible that this process never halts, meaning that a normal form for that expression doesn't exist.
470 |See if you can find an expression whose normal form doesn't exist!
471 |Possible answer: (λa.aa)λa.aa
472 |You can expand that error that pops up to see the first few iterations. If you went with (λa.aa)λa.aa, you can see that performing a beta reduction gives you the exact same expression back!
The famed Y-Combinator is one of these expressions without a normal form. Try inputting the Y-Combinator, and see what happens:
485 |Y: λg.(λx.g(xx))(λx.g(xx))
In the lambda calculus, there's no formal notion of assigning variables, but it's far easier for us to refer to functions by name than just copy/paste the expression every time we want to use it.
495 |In this REPL, we've added a basic syntax around assign variables. (Note: You can't assign an expression with free variables.)
496 |This kind of lexical environment around the lambda calculus comes very close to the original sense of a closure, as presented in The mechanical evaluation of expressions.
497 |Try assigning ID to your identity function by typing ID := λa.a
Now that ID is defined in the lexical environment, we can use it as if it's a previously bound variable
Try writing ID b in order to apply your newly defined identity function to b, with predictable results.
Now we're well equipped enough to start working with actual, meaningful values.
531 |Let's start off by introducing the booleans! The two booleans are:
532 |true: λab.a
false: λab.b
You'll notice that these values themselves are just functions. That's true of any value in the lambda calculus -- all values are just functions that take a certain form. They're called the Church booleans, after Alonzo Church, the mathematician who came up with the lambda calculus, as well as these specific encodings.
535 |It'll be helpful to assign them to TRUE and FALSE respectively. Do that.
We're gonna work our way to defining the XOR (exclusive or) function on booleans.
552 |Our first step along the way is to define the NOT function. To do this, let's look at the structure of what a boolean looks like.
553 |True is just a two parameter function that selects the first, whereas false is just a two parameter function that selects the second argument. We can therefore call a potential true or false value like a function to select either the first or second parameter!
554 |For example, take the application mxy. If m is Church Boolean true, then mxy beta reduces to x. However, if m is Church Boolean false, mxy beta reduces to y
Try writing the NOT function, and assign that to NOT.
Answer: NOT := λm.m FALSE TRUE
557 |Nice! We've now done the heavy mental lifting of how to use the structure of the value to our advantage.
575 |You should be well equipped enough to come up with the OR function, a function which takes two booleans and outputs true if either of parameters are true, otherwise false.
576 |Give it a shot, and assign it to OR
Answer: OR := λmn.m TRUE n
578 |Closer and closer.
599 |This one's very similar to the previous one. See if you can define the AND function, a function which takes two booleans and outputs true if both parameters are true, otherwise false.
600 |Assign your answer to AND
Answer: AND := λmn.m n FALSE
602 |The NOR and NAND functions are the opposite of OR and AND. For example, if AND returns true, NAND returns false, and vice versa. The same follows for OR and NOR
622 |Since we've already defined the NOT, AND, and OR functions, we can just compose those together to get NAND and NOR
Define NAND and NOR, and assign them to NAND and NOR.
Answers:
625 |NOR := λab. NOT (OR a b)
626 |NAND := λab. NOT (AND a b)
627 |One last step!
659 |For reference, the XOR operation is true iff one parameter or the other is true, but not both. So XOR(true, false) would be true, but XOR(true, true) would be false.
Let's see if you can translate that into a composition of the functions you've defined so far. Assign your answer to XOR
(There is, of course, a simpler way of defining XOR without composing functions, and that will work here too)
Answer: XOR := λmn. AND (OR m n) (NAND m n)
663 |Well, that was a marathon. Take a little break, you've earned it.
683 |Now we're getting into the meat of it. We can encode numbers in the lambda calculus. Church numerals are 2 parameter functions in the following format:
684 |685 |
686 | {`
687 | 0: λfn.n
688 | 1: λfn.f(n)
689 | 2: λfn.f(f(n))
690 | 3: λfn.f(f(f(n)))
691 | `}
692 |
693 |
694 | Write Church Numeral 5
695 |Answer: λfn.f(f(f(f(fn))))
696 |We can write functions for these numbers. For example, let's look at the successor function, a function which simply adds 1 to its argument.
705 |If you're feeling brave, you can attempt to write the successor function yourself. It's a pretty interesting exercise. Otherwise, just copy/paste from the answer key, but feel a little defeated while doing so.
706 |Answer: λn.λf.λx.f(nfx)
707 |So here's what we just did: Let's say we were adding 1 to λfn.f(f(f(f(n)))). We just wrote a function that replaced all the f's with f's again, and then replaced the n with a f(n), thus creating a stack one higher than we had before! Magic!
Assign the successor function to SUCC, we'll need it later
The nice thing about Church numerals as we've defined them is they encode "compose this function n times", so in order to compose a function 3 times, just apply the target function to the Church numeral 3.
744 |For example, let's say we had the function APPLY_C := λa.a c that applied free variable c to whatever function was passed in. If we wanted to write a function that applied c 3 times, we would write (λfn.f(f(fn))) APPLY_C
Write the "add 4" function by composing the successor function 4 times.
746 |What's convenient about this is in order to add the numbers a and b, we just create the (add a) function and apply it to b
You can take this structure and abstract it out a little, turning it into a function.
765 |Go ahead and define ADD to be your newly crafted addition function.
Answer: ADD := λab.a SUCC b
767 |Let's go ahead write the Multiply function by composing adds together. One possible way to think about a multiply function that takes x and y "Compose the Add x function y times, and evaluate that at zero".
Go ahead and assign that to MULT
Answer: MULT := λab.b(ADD a)λfn.n
787 |This shouldn't be too difficult, as it's very similar to the previous problem.
805 |Compose together a bunch of multiplications, for some starting position to get the exponentiation function. What's cool is that constructing the exponentiation this way means the function behaves correctly for the number 0 straight out of the box, without eta-reduction
806 |Assign your exponentiation function to EXP to win, and complete the tutorial.
807 |Answer is: EXP := λab.b (MULT a) λfn.fn
808 |You made it through! Not bad at all!
827 |Miscellaneous Challenges:
828 |(full disclosure: I haven't attempted these)
829 |1: Write the Subtract 1 function. (there are a number of tutorials you can find on this on the internet)
830 |2: Write the Max(a, b) function, a function that takes two numbers and outputs the larger of the two.
3: Write a function that computes the decimal equivalent of its input in Gray code. In other words, compute A003188
832 |