> For the complete documentation index, see [llms.txt](https://mandober.gitbook.io/math-debrief/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mandober.gitbook.io/math-debrief/340-lambda-calculi/lambda-calculus/lambda-calculus-formal.md).

# Lambda Calculus

## Formal Definition

The syntax of LC defines some expressions (sequence of symbols allowed in LC) as valid and some as invalid. **Lambda term** is a valid LC expression.

These rules give an inductive definition that can be applied to build all syntactically valid lambda terms:

* 0: a variable (e.g. $$x$$) is a valid lambda term
* 1: if $$x$$ and $$M$$ are lambda terms, then $$(\lambda x.M)$$ is a lambda term
* 2: if $$M$$ and $$N$$ are lambda terms, then $$(MN)$$ is a lambda term
* nothing else is a lambda term.

**Variables**\
Above, the zero rule just states that $$x$$ is a lambda term, which would imply that $$x$$ on its own is a lambda term, but that's pretty vague; that $$x$$ alone is obviously not a function, and it couldn't stand for (represent) some other lambda term (that would be external binding). It can only be a **free variable**, meaning we're not seeing the entire expression; we have to zoom out to see the acual binding site for that $$x$$.

, called a *lambda abstraction*. , called an *application* (function application, lambda application).

Application, $$MN$$, associate to the left: $$fxyz$$ is $$((fx)y)z$$

Abstraction, $$\lambda x.M$$, associate to the right: $$\lambda f.xyz$$ $$\lambda f.(xyz)$$

Assume given an infinite set $$\mathcal{V}$$ of variables, denoted by $$x, y, z\dots$$\
The set of lambda terms is given by the following Backus-Naur Form\
($$M$$ and $$N$$ are lambda terms, $$x$$ is a variable):

$$
M, N ::= x \ |\ (MN)\ |\ (\lambda{x}.M)
$$

Traditional definition:

* Assume given an infinite set $$\mathcal{V}$$ of variables
* Let $$A$$ be an alphabet consisting of the elements of $$\mathcal{V}$$ and the special symbols, $$\lambda$$, `.`, `(`, `)`
* Let $$A^∗$$ be the set of strings (finite sequences) over the alphabet $$A$$
* The set of lambda terms is the smallest subset $$\Lambda \subseteq A^∗$$ such that:
  * Whenever $$x\in \mathcal{V}$$ then $$x\in \Lambda$$
  * Whenever $$M,N\in \Lambda$$ then $$(MN)\in \Lambda$$
  * Whenever $$x\in \mathcal{V}$$ and $$M\in \Lambda$$ then $$\lambda{x}.M \in \Lambda$$

Examples:

* Variables: `x`, `y`
* Applications: `xx`, `xy`, `(λx.(xx))(λy.(yy))`
* Abstractions: `λx.x`, `λf.ff`, `λf.(λx.(f(fx)))`
* Abstraction and application: `(λx.x)(2)`, `((λf.f(f))(λx.x+2))(3)`

This, `λx.x+1`, defines an anonymous function that takes a parameter `x` and evaluates (returns) it to `x+1`. This applies this function to the argument 5: `(λx.x+1)(5)`; the 5 replaces the `x` in the function's body, so this expression evaluates to 6.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://mandober.gitbook.io/math-debrief/340-lambda-calculi/lambda-calculus/lambda-calculus-formal.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
