# Lecture 9: Lazy PCF

Teacher: Thomas Ehrhard

Lecture 9


ehrhard at irif.fr www.irif.fr/~ehrhard

Connection between syntax and semantics:

1. Programming language ⟶ a version of Plotkin’s PCF with lazy integers
2. What is a model of linear logic?
• Scott model of LL

# Denotational semantics

1969 - Dana Scott (Logician), Christopher Stratchey (Computer scientist) met in Oxford

Christopher Stratchey wondered what is the meaning of a program, independently on the programming language. He had equations on datatypes, but no solution in $Set$.

When Dana Scott met him, he has the idea of interpreting types as complete lattices and programs as continuous functions

Logicians Curry-Howard: Programs ⟺ Proofs

Jean-Yves Girard: denotational interpretation of System F ⟶ Coherence Spaces

PCF (Programming language with Computable Functions): abstract programming language to study the relation between syntax and semantics.

PCF:
• simply typed $λ$-calculus
• with a ground type for integers (booleans encoded as integers)
• basic functions ($suc, pred$, conditional)
• fixpoint constructions ⟶ full recursion

NB: PCF is a Turing-complete language

## LPCF (Lazy PCF)

### Syntax

Types:
$A, B, … ≝ ι \; \mid \; A ⇒ B$
Terms:
$M, N, P, … ≝ x \; \mid \; \underline 0 \; \mid \; \underline {suc} (M) \\ \; \mid \; \texttt{if }(M, \underbrace{N}_{\text{if } M \, = \, \underline 0}, \underbrace{x. P}_{\text{if } M \, = \, \underline{suc } \, x})\\ \; \mid \; λx^A . M \; \mid \; (M)N\\ \; \mid \; \texttt{fix } x^A. M$

### Typing rules

Typing context $Γ ≝ (x_1: A_1, …, x_n: A_n)$

Typing judgement $Γ ⊢ M: A$

Rules:

$\cfrac{}{Γ, x:A ⊢ x:A} \qquad \cfrac{}{Γ ⊢ \underline 0 :ι} \qquad \cfrac{Γ ⊢ M:ι}{Γ ⊢ \underline {suc } (M): ι}\\ \, \\ \, \\ \cfrac{Γ ⊢ M:ι \quad Γ ⊢ N:A \quad Γ, x:ι ⊢ P:A}{Γ ⊢ \texttt{if }(M, N, x.P): A}\\ \, \\ \, \\ \cfrac{Γ, x:A ⊢ M: B}{Γ⊢ λx^A.M: A ⇒ B} \qquad \cfrac{Γ ⊢ M: A ⇒ B \quad Γ ⊢ N:A}{Γ⊢ (M)N: B}\\ \, \\ \, \\ \cfrac{Γ, x:A ⊢ M: A}{Γ ⊢ \texttt{fix } x^A. M: A}$

add(x, 0) = x


i.e, for a given $x$:

add_x(0) = x

$add \; ≝ \; λ x^ι. \texttt{fix } a^{ι ⇒ ι}. λ y^ι. \texttt{ if }(y, x, z. \underline {suc}((a)z))$

### $β$-reduction

Rewriting relation on terms:

$\cfrac{}{(λx^A. M)N \quad β \quad M[N/x]}$ $\cfrac{}{\texttt{if }(\underline 0, N, x.P) \quad β \quad N} \qquad \cfrac{}{\texttt{if }(\underline {suc } \, M, N, x.P) \quad β \quad P[M/x]}$ $\cfrac{}{\texttt{fix } x^A.M \quad β \quad M[\texttt{fix } x^A.M/x]}$

And then, it goes through context:

$\cfrac{M \quad β \quad M'}{\underline {suc } \, (M) \quad β \quad \underline {suc } \, (M')}$ $\cfrac{M \quad β \quad M'}{\texttt{ if }(M, N, x.P) \quad β \quad \texttt{ if }(M', N, x.P)}$ $\cfrac{N \quad β \quad N'}{\texttt{ if }(M, N, x.P) \quad β \quad \texttt{ if }(M, N', x.P)}$ $\cfrac{M \quad β \quad M'}{(M)N \quad β \quad (M')N} \qquad \cfrac{N \quad β \quad N'}{(M)N \quad β \quad (M)N'}$

etc…

### Why “lazy”?

Let $Ω^ι ≝ \texttt{fix } x^ι. x$

$Ω^ι ⟶_β Ω^ι ⟶_β Ω^ι ⟶_β ⋯$

It’s a fully undefined term. But

$\underline{suc } \, (Ω^ι)$

is such that

$\texttt{if } \, (\underline{suc } \, Ω^ι, \underline 0, x. \underline 0) ⟶_β \underline 0$

Whereas in ordinary PCF, you have a term $\underline n$ for each integer, and

$\underline { suc } \, \underline n ⟶_β \underline{n+1}$

So that:

$\underline{suc } \, (Ω^ι) ⟶_β \underline{suc } \, (Ω^ι) ⟶_β ⋯$

And in ordinary PCF:

$\cfrac{}{\texttt{if }(\underline 0, M, x.N) \quad β \quad M} \qquad \cfrac{}{\texttt{if }(\underline {n+1}, M, x.N) \quad β \quad N[\underline n /x]}$

Whereas in LPCF, $\texttt{if}$ statements don’t need to have a integer value as first argument to reduce, which enables $\texttt{if } \, (\underline{suc } \, Ω^ι, \underline 0, x. \underline 0)$ to (weakly) terminate.

## Subject Reduction

Th (Subject reduction): If $Γ ⊢ M:A$ and $M ⟶_β M’$, then $Γ ⊢ M’: A$

Lemma (Substitution lemma): if $Γ, x:A ⊢ M:B$ and $Γ ⊢ N:A$, then $Γ ⊢ M[N/x]: B$

Notation: we denote by $β^\ast$ the reflexive-transitive closure of $β$.

Th (Confluence): If $M ⟶_{β}^\ast M_1$ and $M ⟶_β^\ast M_2$, then there exists $M_0$ s.t. $M_1 ⟶_β^\ast M_0 \text{ and } M_2 ⟶_β^\ast M_0$

$\begin{xy} \xymatrix{ & M \ar[ld]_{\ast}^{β} \ar[rd]^{\ast}_{β} & \\ M_1 \ar@{.>}[rd]_{\ast}^{β} & & M_2 \ar@{.>}[ld]^{\ast}_{β}\\ & M_0 & } \end{xy}$

NB: Local confluence:

$\begin{xy} \xymatrix{ & M \ar[ld]^{β} \ar[rd]_{β} & \\ M_1 \ar@{.>}[rd]_{\ast}^{β} & & M_2 \ar@{.>}[ld]^{\ast}_{β}\\ & M_0 & } \end{xy}$

+ Strong normalization is enough (modulo Neumann’s lemma) to have confluence

But if you don’t have strong normalization, then local confluence is not enough, as shown by this example:

  digraph {
rankdir=TB;
A -> B;
A -> C -> A, E;
}


Th: If $β$ satisfies the diamond property, then $β^\ast$ satisfies it too:

$\begin{xy} \xymatrix{ & M \ar[ld]^{β} \ar[rd]_{β} & \\ M_1 \ar@{.>}[rd]^{β} & & M_2 \ar@{.>}[ld]_{β}\\ & M_0 & } \end{xy} ⟹ \begin{xy} \xymatrix{ & M \ar[ld]_{\ast}^{β} \ar[rd]^{\ast}_{β} & \\ M_1 \ar@{.>}[rd]_{\ast}^{β} & & M_2 \ar@{.>}[ld]^{\ast}_{β}\\ & M_0 & } \end{xy}$

But we don’t have the diamond property (cf. picture)

### Parallel reductions: Tait/Martin-Löf’s method

Idea: Introduce a rewriting relation $ρ$ such that

$β ⊆ ρ ⊆ β^\ast$

(which implies: $ρ^\ast = β^\ast$)

which satisfies:

$\begin{xy} \xymatrix{ & M \ar[ld]^{ρ} \ar[rd]_{ρ} & \\ M_1 \ar@{.>}[rd]^{ρ} & & M_2 \ar@{.>}[ld]_{ρ}\\ & M_0 & } \end{xy}$

where $M ⟶_ρ M’$ means that you reduce an arbitrary number of redexes ocurring in $M$, but no redex created during this process.

Ex:

$(λx.(x)\underline 0) (λy.y) \underbrace{⟶_β}_{⟶_ρ \text{ as well}} (λy.y) \underline 0 \underbrace{⟶_β}_{\text{but NOT } ⟶_ρ \text{ as it's a created redex!}} \underline 0$

## Definition of $ρ$

$\cfrac{}{x \quad ρ \quad x} \qquad \cfrac{}{\underline 0 \quad ρ \quad \underline 0} \\ \, \\ \, \\ \cfrac{M \quad ρ \quad M'}{\underline {suc } \, (M) \quad ρ \quad \underline {suc } \, (M')} \\ \, \\ \, \\ \cfrac{M \quad ρ \quad M' \qquad N \quad ρ \quad N' \qquad P \quad ρ \quad P'}{\texttt{ if }(M, N, x.P) \quad ρ \quad \texttt{ if }(M', N', x.P')} \\ \, \\ \, \\ \cfrac{N \quad ρ \quad N'}{\texttt{ if }(\underline 0, N, x.M) \quad ρ \quad N'} \qquad \cfrac{M \quad ρ \quad M' \qquad P \quad ρ \quad P'}{\texttt{ if }(\underline{suc } \, M, N, x.P) \quad ρ \quad P'[M'/x]}$ $\cfrac{M \quad ρ \quad M' \qquad N \quad ρ \quad N'}{(M)N \quad ρ \quad (M')N'}$ $\cfrac{M \quad ρ \quad M'}{λx^A. M \quad ρ \quad λx^A. M'} \qquad \cfrac{M \quad ρ \quad M' \qquad N \quad ρ \quad N'}{(λx^A. M)N \quad ρ \quad M'[N'/x]}$ $\cfrac{M \quad ρ \quad M'}{\texttt{fix } x^A. M \quad ρ \quad \texttt{fix } x^A. M'} \qquad \cfrac{M \quad ρ \quad M'}{\texttt{fix } x^A. M \quad ρ \quad M'[\texttt{fix }x^A. M'/x]}$

BUT NOT:

$\cfrac{M \quad ρ \quad \underline{suc } \, M' \qquad P \quad ρ \quad P'}{\texttt{ if }(M, N, x.P) \quad ρ \quad P'[M'/x]}$

Lemma 1: $M ⟶_ρ M$

Lemma 2: $M ⟶_ρ M’$ and $N ⟶_ρ N’$, then $M[N/x] ⟶_ρ M’[N’/x]$

Proof: by induction on the derivation $M \quad ρ \quad M’$.

Th: If $M ⟶_ρ M_i$ for $i=1, 2$ , there exists $M_0$ such that $M_i ⟶_ρ M_0$ for $i=1,2$.

Proof: By induction on the max of the sizes of the deduction trees of $M ⟶_ρ M_1$ and $M ⟶ M_2$.

1. Assume $M = (P)Q$

$M ⟶_ρ M_1 \text{ and } M ⟶_ρ M_2$

there are the following possibilities:

• $P ⟶_ρ P_i \text{ and } Q ⟶_ρ Q_i \text{ and } M_i = (P_i) Q_i$

By IH, there is $P_0, Q_0$ such that

$P_i ⟶_ρ P_0 \text{ and } Q_i ⟶_ρ Q_0 \qquad i=1,2$

Then

$M_i ⟶_ρ (P_0) Q_0$
• $P = λy^A. H$

(cf. picture)

Lemma: if $M ⟶_ρ M’$ and $N ⟶_ρ N’$ then $M[N/x] ⟶_ρ M’[N’/x]$

Induction on the derivation of $M ⟶_ρ M’$

cf. picture

## LPCF

Weak head reduction $β_{wh}$:

it’s a reduction strategy

Any term contains at most one redex for this reduction.

Rules for $β_{wh}$:

$\cfrac{M \quad β_{wh} \quad M'}{\texttt{ if } (M, N, x.P) \quad β_{wh} \quad \texttt{ if }(M', N, x.P)}\qquad \cfrac{}{\texttt{if }(\underline 0, N, x.P) \quad β_{wh} \quad N}\\ \, \\ \cfrac{}{\texttt{if }(\underline{suc } \, M, N, x.P) \quad β_{wh} \quad P[M/x]}\\ \, \\ \cfrac{M \quad β_{wh} \quad M'}{(M)N \quad β_{wh} \quad (M')N} \qquad \cfrac{}{(λx^A. M)N \quad β_{wh} \quad M[N/x]} \\ \, \\ \cfrac{}{\texttt{fix } \, x^A.M \quad β_{wh} \quad M[\texttt{fix } \, x^A.M/x]}$

Forbidden: reduce $N$ in

• $(M)N$
• $λx^A.N$
• $\texttt{if } \, (M, N, x.P)$, $\texttt{if } \, (M, P, x.N)$
• $\overline{suc } \, N$

Th (Completeness of $β_{wh}$): Assume $⊢ M: ι$

• $M \sim_β \underline 0 ⟹ M \quad β^\ast_{wh} \quad \underline 0$
• $M \sim_β \underline{ suc } \, N ⟹ M \quad β^\ast_{wh} \quad \underline {suc } \, N’$

## Model of LL

What is a model of LL? A symmetric monoidal category

$⟨ℒ, ⊗, 1, λ, ρ, α, γ⟩$

which is closed

$X, Y ∈ ℒ \quad \leadsto \quad (X ⊸ Y, \texttt{ev})$

such that

$∀ f ∈ ℒ(Z ⊗ X, Y), ∃! \, \texttt{cur}(f) ∈ ℒ(Z, X ⊸ Y)$

such that

$\begin{xy} \xymatrix{ (X ⊸ Y) ⊗ X \ar[r]^-{\texttt{ev}} & Y \\ Z ⊗ X\ar@{->}[u]^{\texttt{cur}(f) × id} \ar[ur]_f & } \end{xy}$

commutes.

On top of that: we have a dualizing object $⊥$ for which we have $\star$-autonomy:

$η_X^Z = \texttt{cur}(\texttt{ev} \, γ) ∈ ℒ(X, (X ⊸ Z) ⊸ Z)\\ X ⊗ (X ⊸ Z) \xto {γ} (X ⊸ Z) ⊗ X \xto{\texttt{ev}} Z$

for all $Z$, this is a natural transformation.

$η_X^⊥$ is an iso for all $X$

$X ≅ (X ⊸ ⊥) ⊸ ⊥$
$X ⊸ ⊥$:

linear negation

We have a functor:

$(-)^⊥: ℒ^{op} ⟶ ℒ$

Moreover, $ℒ$ is cartesian: there is a terminal object $⊤$, binary products $(X_1 \& X_2, pr_1, pr_2)$ inducing a bifunctor

$\&: \begin{cases} ℒ × ℒ &⟶ ℒ \\ (X_1, X_2) &⟼ X_1 \& X_2 \end{cases}$

$ℒ$ is also co-cartesian ⟹ there is an initial object $0 = ⊤^⊥$ and coproducts

$X_1 ⊕ X_2 = (X_1^⊥ \& X_2^⊥)^⊥\\ in_i ≝ pr_i^⊥$

where

$pr_i: X_1^⊥ \& X_2^⊥ ⟶ X_1^⊥\\ pr_i^⊥: X_1 ≅ (X_1^⊥)^⊥ ⟶ (X_1^⊥ \& X_2^⊥)^⊥ ≅ X_1 ⊕ X_2$

We have a functor:

$!: ℒ ⟶ ℒ$

such that $(!, der, dig)$ is a comonad:

$der_X ∈ ℒ(!X, X)\\ dig_X ∈ ℒ(!X, !!X)$

On top of that, we’d like to state that

$!⊤ \, = \, 1 \qquad !(X_1 \& X_2) \; = \; !X_1 ⊗ !X_2$

So we introduce:

$m^0 ∈ ℒ(1, !⊤) \text{ is an iso}\\ m^2_{X_1, X_2} ∈ ℒ(!X_1 ⊗ !X_2, !(X_1 \& X_2)) \text{ is a natural iso}\\$

+ monoidality diagrams:

$\begin{xy} \xymatrix{ (!X_1 ⊗ !X_2)⊗!X_3 \ar[r]^{m^2_{X_1,X_2} ⊗ !X_3 } \ar[d]_{ α_{!X_1, !X_2, !X_3} } & !(X_1 \& X_2)⊗!X_3 \ar[d]^{ } \\ !X_1⊗(!X_2⊗!X_3) \ar[r]_{ } & ⋯ } \end{xy}$

cf. pictures

## Derived structures

Weakening and contraction

$\cfrac{⊢ Γ}{⊢ Γ, ?A} \qquad \cfrac{⊢ Γ, ?A, ?A}{⊢ Γ, ?A}$

comes from the fact that $!X$ has a canonical structure of commutative comonoid with $wf_X ∈ ℒ(!X, 1)$ (weakening free) and $cf_X ∈ ℒ(!X, !X ⊗ !X)$ (contraction free)

Tags:

Updated: