Lecture 6: Encoding of λ-calculus, System T & System F

μC.(CC)
  • in
  • let fix

Sytem F:

DC,((CC)C)C
  • λ
  • app

OCamL:

type d =
    Lam of d -> d

(* Or with GADT: *)
Lam: (d -> d) -> d
  • fun
  • application (no symbol)
  • Lam
  • match

Coq:

Inductive D :=
    Lam: (D -> D) -> D
  • fun
  • application (no symbol)
  • Lam
  • match

Find a term t such that, for f:DD:

tfβftffftfffftf

We will write the Y-combinator:

Yλf.(λx.f(xx))(λx.f(xx))

The thing is:

Untyped λ-calculus = Simply-typed λ-calculus + Coercion D=DD

We already have Lam:(DD)D, so all we have to do is to construct a term

app:D(DD)

We will use the match of Coq (Coq = the meta-language):


app := fun x:D =>
            fun y:D =>
                match x with
                    | Lam f => f y

so that app (Lam f) y = f y

Now:

δ := Lam (fun x => app x x)
Ω := app δ δ

δ' := Lam (fun f => Lam (fun x => app f (app x x)))
Y := Lam (fun f => app (app δ' f) (app δ' f))
A(c1:Pcn:P)μX.APA(a:A)PXXP

In the definition of μX.A, X should occur strictly positively, i.e. only in the indicated position of P.

Inductive tree (A: Type) :=
    Node: A -> (nat -> tree A) -> tree A
    Leaf: A -> tree A

We can have element which haven’t a bounded depth.

Other example:

Inductive I :=
    C : ((I -> nat) -> nat) -> I

⟶ The first I is in a positive, but not strictly positive, position.

Postive/Negative positions

  • X occurs strictly positively in X
  • if X occurs positively in A, then X occurs negatively in x:AB
  • if X occurs negatively in B, then X occurs positively in x:AB
  • if X occurs stricly positively in A then it occurs positively in A

NB:

(the tensor product of linear logic):

  • Constructor: (t,u)
  • Destructor:

      match t with
          (x, y) => x (* pattern *)
      end
    
  • has an η rule on the side of the match destructor: η:match t with (x,y)E(x,y)end=E[t]

    where E is an evaluation context (a term with a hole)

& (the “with” of linear logic):

  • Constructor: {fst=t;snd=u} ⟶ definition by co-pattern
  • Destructor: fst,snd
  • has an η rule on the side of the match constructor:

    η&:t={fst=fstt;snd=sndt}
match (t, u) with
    (x, y) => v
end.

 v[t/x][u/y]
fst{fst=t;snd=u}tsnd{fst=t;snd=u}u

cf. “Linear intuitionistic logic” by Nick Benton




Inductive L (A: Type): Type :=
    Var : A -> LA
    App : LA -> LA -> LA
    Lam : L (option A) -> LA  (* recursively non uniform parameter of inductive type *)

where

Inductive option (A: Type): Type :=
    None : option A
    Some : A -> option A

and

Inductive False := .

Show that simply-typed λ-terms can be encoded in the type L False and encode λx,y.x and λf,x.f(fx)

u:=xλx.uuvA:=bAB
Var : False -> L False
App : L False -> L False -> L False
Lam : L (option False) -> L False
  • xA Var

Another interesting “high-tech” type:

Inductive tree (A: Type) :=
    Node : tree (tree A) -> tree A
    Leaf : A -> tree A

System T and System F

System T:

is the language if terms canonically associated to PA1

System F:

is the language if terms canonically associated to PA2

Types of system F:

it’s the ones of simply-typed λ-calculus + second order quantification:

A,BABX.AX
Terms of system F:
t,uaλa.ttuλX.ttA

Typing rules:

Γ,a:At:BΓλa.t:AB Γt:ABΓu:AΓtu:B Γ,X:𝒰t:AΓλX.t:X.A Γt:X.Afv(A)ΓΓtB:A[B/x]

NB: System F encompasses System T, and is even much more powerful.

Church encoding of natural numbers

X.X(XX)X0λX.λa,f.a1λX.λa,f.fa2λX.λa,f.f(fa)succ nλX,a,f.f(nXaf)X.A(X)C.(X.A(X)C)C iter n(t:C)(u:CC)nCtu pred niter n(0,0)(λa.(snd a,succ (snd a)))

NB: Kleene found the expression of the predecessor at the dentist’s.

Problem: the predecessor is computed in linear time, which is annoying: we would like to have it in constant time. That’s part of the reason why inductive types became so successful, in comparison.

Curryfication

(c1:P1cn:Pn)XP1XPnXXμX.AX.AXAXAX(a:P)PXa:PXPXa:APXa:APXXa:APXa:APXAXAP1P2XP1×P2

Right Kan extension (X)

listAX.(X(AXX)X)nilλX.λb,f.bconsalλX.λb,f.fa(lXbf)

Left Kan extension (X)

streamAX.X×(XA×X)

Leave a comment