How to define a factorial function?

Fixed-point combinator in use

Using recursion and functions defined in advance, we can define

However, the definition of should not contain itself. We can change it a little bit:

Define , then interestingly:

which means it is the fixed-point of !

Then we can define where is the Y combinator.

Example

Data Structures in Calculus

Pair & Tuple

Define to be the pair of term and . Alternatively, we can write

As a pair, we should be able to get the first and second part:

We can easily verify that and .

Similarly, define tuple and .

List

Define as the empty list.

We write to denote a list of three elements.

We define and . (The definition is not unique)

Then we can define head, tail and empty.

List Operations

We now define operations for a list.

map

Given a list and a function , applies to all entries in the list to obtain a new list .

filter

For a Boolean function , returns the list containing only elemtents s.t. .

reduce

Given a list and a function taking two arguments, and a term considered as the neutral element of , returns a term that is reduced by going through , beginning with .

Neutral

“Neutral” term can be considered as a term that does nothing when is applied to another term with it. For example, informally, 0 is neutral to sum operation, and 1 is neutral to product operation.

How to define them with calc?

We can define and from and define using fixed-point combinator.

To define , first we want . Then we want to set to .

So we may define as the fixed-point of

Functional Programming

It has several advantages:

  1. No side effects
  2. First-class and higher-order functions
  3. Declarative style
  4. Concurrency made easier (don’t really understand this now…)

Church-Turing Thesis

We now try to prove that calculus is equivalent with Turing Machine.

Facts about TM

  • TM can be encoded as binary strings (or integers).
  • A configuration of a TM at certain time step is the collection of
    • tape content
    • current state
    • head position
  • There is universal TM that when given and a string as input, it can simulates the computation of on input .
  • Variants of TMs are equivalent
  • A non deterministic TM accepts if there exists an accepting branch.

Undecidability

  • A languae or problem is Turing recognizable if it is the collection of strings accepted by a Turing machine .
  • A language or problem is decidable if there is a machine recognizing it and the machine always halt.
  • Halting problem is undecidable

Reduction

Reduction can be used to prove undecidability of more problems.

Problem reduces to means that it’s possible to decide using an algorithm for as a subroutine.

We write to denote is Turing reducible to . If is decidable, then is also decidable.

Calculus is Turing Complete

A computational model is said to be Turing Complete if it can simulate any Turing machine.

is a Turing machine, we define a term simulating the machine in the sense that for all list representing a possible initial configuration of the machine. if the machine terminates with final configuration . The leftmost reduction does not lead to a normal form if the machine does not halt.

Note

Note that here the information of the position of HEAD is encoded in the state list . It can be seen as we double the possible character on the tape, one representing that HEAD is currently pointing this cell while the other representing the opposite.

A useful insight is that the next state of a cell is only determined by the states of the two ceils next to it and itself. Then we can claim that there is a term representing this function:

We can also easily define a term that pack the list into a list of this kind of tuple. Similarly, there is a term that checks whether a configuration is terminating.

Then we define , which depends on only.

Finally we get:

Let’s check what happen if doesn’t halt. It means that always returns false.

It is trivial that it will never end.