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

\[μC. (C ⟶ C)\]
  • $\texttt{in}$
  • $\texttt{let fix}$

Sytem F:

\[D ≝ \; ∀C, ((C ⟶ C) ⟶ C) ⟶ C\]
  • $λ$
  • $\texttt{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: D ⟶ D$:

\[tf ⟶^β ftf ⟶ fftf ⟶ ffftf ⟶ ⋯\]

We will write the $Y$-combinator:

\[Y ≝ λf. (λx. f(x \, x)) \; (λx. f(x \, x))\]

The thing is:

Untyped $λ$-calculus = Simply-typed $λ$-calculus + Coercion $D = D ⟶ D$

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

\[app: D → (D → D)\]

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 ≝ \; (c_1: P \mid ⋯ \mid c_n: P) \mid μX. A\\ P ≝ \; A \mid (a: A) \otimes P \mid X \mid X \otimes P\]

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 $\prod_{x:A} B$
  • if $X$ occurs negatively in $B$, then $X$ occurs positively in $\prod_{x:A} B$
  • if $X$ occurs stricly positively in $A$ then it occurs positively in $A$

NB:

$\otimes$ (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: \(η_\otimes: \texttt{match } t \texttt{ with } (x,y) ⟶ E(x, y) \texttt{end} = E[t]\)

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

$\&$ (the “with” of linear logic):

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

    \[η_{\&}: t = \lbrace fst = fst \, t; \; snd = snd \; t \rbrace\]
match (t, u) with
    (x, y) => v
end.

 v[t/x][u/y]
\[fst \; \lbrace fst=t; \; snd=u \rbrace ⟶ t\\ snd \; \lbrace fst=t; \; snd=u \rbrace ⟶ 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\, ( f \, x )$

\[u := x \mid λx. u \mid uv\\ A := b \mid A → B\]
Var : False -> L False
App : L False -> L False -> L False
Lam : L (option False) -> L False
  • $x^A \; ≝ \;$ 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 $PA_1$

System F:

is the language if terms canonically associated to $PA_2$

Types of system F:

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

\[A, B ≝ A → B \mid ∀X. A \mid X\]
Terms of system F:
\[t, u ≝ \; a \mid λa. t \mid t u \mid λX. t \mid t A\]

Typing rules:

\[\cfrac{Γ, a:A ⊢ t:B}{Γ ⊢ λa. t: A → B}\] \[\cfrac{Γ ⊢ t: A → B \qquad Γ ⊢ u: A}{Γ ⊢ tu: B}\] \[\cfrac{Γ, X:𝒰 ⊢ t: A}{Γ ⊢ λX. t: ∀X. A}\] \[\cfrac{Γ ⊢ t: ∀X. A \qquad fv(A) ⊆ Γ}{Γ ⊢ t B: A[B/x]}\]

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

Church encoding of natural numbers

\[ℕ ≝ \; ∀X. X ⟶ (X ⟶ X) ⟶ X\\ 0 ≝ \; λX. λa, f. a \\ 1 ≝ \; λX. λa, f. f \, a \\ 2 ≝ \; λX. λa, f. f \, (f \, a)\\ \texttt{succ } n ≝ \; λX, a, f. f \, (n \, X \, a \, f)\\ ∃ X. A(X) ≝ \; ∀C. (∀X. A(X) → C) → C\] \[\texttt{iter } n \, (t:C) \, (u: C → C) ≝ \; n \, C \, t \, u\] \[\texttt{pred } n ≝ \; \texttt{iter } n \; (0, 0) \; (λa. (\texttt{snd } \, a, \texttt{succ } (\texttt{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

\[⟦(c_1: P_1 \mid ⋯ \mid c_n: P_n)⟧_X \; ≝ \; ⟦P_1⟧'_X → ⋯ → ⟦P_n⟧'_X → X \\ ⟦μX. A⟧ \; ≝ \; ∀X. ⟦A⟧_X \\ ⟦A⟧'_X \; ≝ \; A → X \\ ⟦(a: P) \otimes P⟧'_X \; ≝ \; ⟦a: P⟧''_X → ⟦P⟧'_X \\ ⟦\prod_{a:A} P⟧_X \; ≝ \; \prod_{a:A} ⟦P⟧''_X → X \\ ⟦\prod_{a:A} P⟧''_X ≝ \; \prod_{a:A} ⟦P⟧''_X\\ ⟦A⟧''_X \; ≝ \; A \\ ⟦P_1 \otimes P_2⟧''_X \; ≝ \; ⟦P_1⟧'' × ⟦P_2⟧'' \\\]

Right Kan extension ($\int_X$)

\[list \; A ≝ ∀X. (X ⟶ (A ⟶ X ⟶ X) ⟶ X)\\ nil ≝ \; λX. λb, f. b \\ cons \, a \, l ≝ \; λX. λb, f. f \, a \, (l \, X \, b \, f)\\\]

Left Kan extension ($\int^X$)

\[stream \; A ≝ ∃X. X × (X ⟶ A × X)\]

Leave a comment