Lably
Skip to content

Mogensen–Scott encoding

Way to represent data types in the lambda calculus

  • 7 min read
  • 6 chapters

Related on Lably

Through Computer science, Lambda calculus and Embedding

Mogensen–Scott encoding extends and slightly modifies Scott encoding by applying the encoding to Metaprogramming. This encoding allows the representation of lambda calculus terms, as data, to be operated on by a meta program.

Exposition

Numbers

Scott encoding of numbers follows the Peano definition of natural numbers as a sum of two cases, the zero case and the successor case,

Nat:=Zero⁡|Succ⁡Nat

Correspondingly, Scott numerals are functions which expect two arguments, two handlers, each receiving the corresponding case's data from the number:

0=λzs.zSucc=λn.λzs.sn

The zero case has no data. The successor case data is its Scott numeral, which is served as the argument to the corresponding handler.

When a Scott numeral is supplied with two handlers, it calls the appropriate one with its corresponding data. Scott encoded values embody a choice between the sum data type's cases.

IsZero=λn.nTrue⁡(λm.False)Pred=λn.n0(λm.m)

Recursive operations on Scott numerals require explicit use of recursion, e.g. using Y combinator:

Add=Y⁡λrpq.pq(λm.Succ(rmq))

Church numerals, on the other hand, already embody the primitive recursion and perform the folding / looping on their own:

AddChurch=λpqsz.ps(qsz)

The key difference is that Scott's handler's argument is the number's own predecessor Scott numeral, unprocessed, whereas Church's folding / looping function's argument is the result of folding / looping over its predecessor.

Lists

Scott encoding of lists follows their definition as a sum of two cases, the empty list case and the cons case,

List:=NIL⁡|Cons⟨val⟩List

Correspondingly, Scott lists are functions which expect two arguments, two handlers, each receiving the corresponding data from the list:

NIL=λnc.nCons=λad.λnc.cad

The empty list case has no data. The cons case data are the head element and the list's tail, which are served as the arguments to the corresponding handler.

When a Scott list is supplied with two handlers, it calls the appropriate one with its corresponding data. Scott encoded values embody a choice between the sum data type's cases.

IsEmpty=λl.lTrue(λad.False)Head=λl.lNIL(λad.a)Tail=λl.lNIL(λad.d)

Recursive operations on Scott lists require explicit use of recursion, e.g. using Y combinator:

Append=Y⁡λrpq.pq(λad.Consa(rdq))

Church lists, on the other hand, already embody the primitive recursion and perform the folding on their own:

AppendChurch=λpqcn.pc(qcn)

The key difference is that Scott's handler's second argument is the list's own tail, unprocessed, which is also a Scott list, whereas Church's folding function's second argument is the result of folding over its tail.

This is the argument that corresponds to the recursive datum in the data type definition. There are no differences between the two encodings in the other, non-recursive data fields, except possibly in the corresponding arguments' order.

Sign in to keep reading

5 more chapters are free with a Lably account.

More in Computing and communication