···33\taxon{Lecture}
44\title{Constructions on [cpos](dt-001D) and PCF}
55\author{liamoc}
66-\p{This lecture is based on material from [[haskellhutt]], [[danascott]], [[jstoy]], [[cgunter]], and [[gwinskel]].}
66+\p{This lecture is based on material from [[haskellhutt]], [[jlongley]],[[danascott]], [[jstoy]], [[cgunter]], and [[gwinskel]].}
77\transclude{dt-002K}
88\transclude{dt-002Z}
99\transclude{dt-0030}
1010\transclude{dt-003E}
1111-\subtree{
1212-\title{PCF}
1313-}
1111+\transclude{dt-003T}
+13
trees/dt/dt-003M.tree
···11+\import{dt-macros}
22+\taxon{Definition}
33+\author{liamoc}
44+\title{The language PCF}
55+\p{
66+ The language for \strong{P}rogramming \strong{C}omputable \strong{F}unctions (PCF) is a variant of typed #{\lambda}-calculus with minimal extensions to be Turing-complete.
77+ ##{
88+ \begin{array}{lcl}
99+ e & \Coloneqq & n \mid x \mid \lambda x : \tau.\ e \mid e_1\ e_2\mid\mathsf{succ} \mid \mathsf{pred} \mid \mathsf{ifz}\ e_1\ \mathsf{then}\ e_2\ \mathsf{else}\ e_3 \mid \mathsf{fix}\ x : \tau.\ e \\
1010+ \tau & \Coloneqq & \mathsf{nat} \mid \tau_1 \rightarrow \tau_2
1111+ \end{array}
1212+ }
1313+}
···11+\import{dt-macros}
22+\author{liamoc}
33+\title{Type-dependent denotations}
44+\p{
55+Because [PCF](dt-003M) is [typed](dt-003N), we shall assign denotations only to well-typed expressions.
66+The range of our [denotation function](dt-0001) for expressions is determined by the \em{type} of its input expression. That is, the denotation of a type #{\tau} \em{is} the [cpo](dt-001D) whose elements will be the denotations of expressions of type #{\tau}.
77+88+The denotation of the type #{\mathsf{nat}}, then, is merely the [flat domain](dt-0008) of the natural numbers #{\mathbb{N}_\bot}:
99+##{
1010+\llbracket \mathsf{nat} \rrbracket = \mathbb{N}_\bot
1111+}
1212+And, the denotation of the function type is the domain of [continuous functions](dt-002F) on cpos:
1313+##{
1414+\llbracket \tau_1 \rightarrow \tau_2 \rrbracket = \llbracket \tau_1 \rrbracket \contto \llbracket \tau_2 \rrbracket
1515+}
1616+A \em{closed} term #{e : \tau} with no free variables denotes an element of #{\llbracket \tau \rrbracket}, so we might be tempted to define our denotation function for expressions #{e : \tau} like so: ##{\llbracket e \rrbracket : \llbracket \tau \rrbracket}
1717+However, if #{e : \tau} involves free variables from our context #{\Gamma}, the valuation of #{e} will depend on the values assigned to all the variables. Thus, the denotation of a typed expression #{\Gamma \vdash e : \tau} is defined instead as a [continuous](dt-001J) function:
1818+##{\llbracket e \rrbracket_\Gamma : \llbracket \Gamma \rrbracket \contto \llbracket \tau \rrbracket}
1919+where the meaning of a context #{\Gamma = (x_0 : \tau_0, x_1 : \tau_1,\dots, x_n : \tau_n)} will be a a big #{n}-tuple of the values assigned to each variable:
2020+##{\llbracket \Gamma \rrbracket = \llbracket \tau_0 \rrbracket \times \llbracket \tau_1 \rrbracket \times \cdots \times \llbracket \tau_n \rrbracket}
2121+}
+31
trees/dt/dt-003P.tree
···11+\import{dt-macros}
22+\author{liamoc}
33+\taxon{Definition}
44+\title{Semantics of [PCF](dt-003M)}
55+\subblock{Types}{
66+##{
77+ \begin{array}{lcl}
88+ \llbracket \mathsf{nat} \rrbracket &=& \mathbb{N}_\bot\\
99+ \llbracket \tau_1 \rightarrow \tau_2 \rrbracket &=& \llbracket \tau_1 \rrbracket \contto \llbracket \tau_2 \rrbracket
1010+ \end{array}
1111+}}
1212+\subblock{Expressions}{
1313+ For #{\Gamma : e : \tau}, we have #{\llbracket e \rrbracket_\Gamma : \llbracket \tau \rrbracket } defined as follows:
1414+##{
1515+\begin{array}{lcl}
1616+ \llbracket n \rrbracket_\Gamma(\vec{z}) & =& n\\
1717+ \llbracket x \rrbracket_\Gamma(\vec{z}) & = & z_j\ \text{where}\ j\ \text{is largest}\ j\ \text{s.t.}\ x = x_j \\
1818+\llbracket \lambda x : \tau_1.\ e \rrbracket_\Gamma(\vec{z}) & = & (\boldsymbol{\lambda} v \in \llbracket \tau_1 \rrbracket.\ \llbracket e\rrbracket_{\Gamma, x : \tau_1}(\vec{z}, v))\\
1919+\llbracket e_1\ e_2 \rrbracket_\Gamma(\vec{z}) & = & \llbracket e_1 \rrbracket_\Gamma(\vec{z})(\llbracket e_2 \rrbracket_\Gamma(\vec{z}))\\
2020+\llbracket \mathsf{succ} \rrbracket_\Gamma(\vec{z}) & = & (\boldsymbol{\lambda} v \in \mathbb{N}_\bot.\ v + 1)\\
2121+\llbracket \mathsf{pred} \rrbracket_\Gamma(\vec{z}) & = & (\boldsymbol{\lambda} v \in \mathbb{N}_\bot.\ v - 1)\\
2222+\llbracket \mathsf{ifz}\ e_1\ \mathsf{then}\ e_2\ \mathsf{else}\ e_3 \rrbracket_\Gamma(\vec{z}) & = & \begin{cases}
2323+ \llbracket e_2 \rrbracket_\Gamma(\vec{z}) & \text{if}\ \llbracket e_1 \rrbracket_\Gamma(\vec{z}) > 0 \\
2424+ \llbracket e_3 \rrbracket_\Gamma(\vec{z}) & \text{if}\ \llbracket e_1 \rrbracket_\Gamma(\vec{z}) = 0 \\
2525+ \bot & \text{if}\ \llbracket e_1 \rrbracket_\Gamma(\vec{z}) = \bot
2626+ \end{cases}\\
2727+\llbracket \mathsf{fix}\ x : \tau.\ e \rrbracket_\Gamma(\vec{z}) &= & \mathbf{fix}(\boldsymbol{\lambda} v \in \llbracket \tau \rrbracket.\ \llbracket e\rrbracket_{\Gamma, x : \tau}(\vec{z}, v))
2828+\end{array}
2929+}
3030+In the above definitions a boldface lambda #{\boldsymbol{\lambda}} is used for an anonymous [continuous function](dt-001J) — boldface to distinguish it from the lambda in the [syntax of PCF](dt-003M). Verifying that these functions are indeed [continuous](dt-001J) is straightforward, but is necessary to justify the use of [the #{\mathbf{fix}} operator](dt-001Q).
3131+}
+8
trees/dt/dt-003Q.tree
···11+\import{dt-macros}
22+\author{liamoc}
33+\taxon{Definition}
44+\title{Notation for #{n}-tuples}
55+\p{Given an #{n}-tuple, i.e. elements of the #{n}-ary [product](dm-0005) #{X_0 \times X_1 \times X_2 \times \dots \times X_n}, we typically denote the entire #{n}-tuple as in vector notation: ##{\vec{x} : X_0 \times X_1 \times X_2 \times \dots \times X_n}
66+And individual elements of the tuple are accessed with numeric subscripts:
77+##{x_0 : X_0 \quad x_2 : X_2 \quad \cdots}
88+ }
+25
trees/dt/dt-003R.tree
···11+\import{dt-macros}
22+\author{liamoc}
33+\title{Extending [PCF](dt-003M) with product types}
44+\p{We shall extend [the syntax of PCF](dt-003M) with a new type former, #{\tau_1 \times \tau_2} and three expression-level constructs for constructing and deconstructing pairs:
55+##{
66+ \begin{array}{lcl}
77+ e & \Coloneqq & \cdots \mid (e_1,e_2) \mid \mathsf{fst}\ e \mid \mathsf{snd}\ e \\
88+ \tau & \Coloneqq & \cdots \mid \tau_1 \times \tau_2
99+ \end{array}
1010+}
1111+Adding to our [typing rules](dt-003N) similarly:
1212+##{
1313+ \inferrule{\Gamma \vdash e_1 : \tau_1 \quad e_2 : \tau_2}{(e_1,e_2) : \tau_1 \times \tau_2}\\[2em]
1414+ \inferrule{\Gamma \vdash e : \tau_1 \times \tau_2}{\Gamma \vdash \mathsf{fst}\ e : \tau_1}\quad
1515+ \inferrule{\Gamma \vdash e : \tau_1 \times \tau_2}{\Gamma \vdash \mathsf{snd}\ e : \tau_2}
1616+}
1717+To extend our [semantics](dt-003P), we make the product type #{\tau_1 \times \tau_2} denote the [product cpo](dt-0021) #{\llbracket \tau_1 \rrbracket \times \llbracket \tau_2 \rrbracket}, and our expression semantics are expressed straightforwardly using our [projection](dt-0020) and [split](dt-0023) operations for [products](dt-002K):
1818+##{
1919+ \begin{array}{lcl}
2020+ \llbracket (e_1, e_2) \rrbracket_\Gamma &=& \langle \llbracket e_1 \rrbracket_\Gamma, \llbracket e_2 \rrbracket_\Gamma \rangle \\
2121+ \llbracket \mathsf{fst}\ e \rrbracket_\Gamma & = & \pi_0 \circ \llbracket e \rrbracket_\Gamma \\
2222+ \llbracket \mathsf{snd}\ e \rrbracket_\Gamma & = & \pi_1 \circ \llbracket e \rrbracket_\Gamma \\
2323+ \end{array}
2424+}
2525+}
+29
trees/dt/dt-003S.tree
···11+\import{dt-macros}
22+\author{liamoc}
33+\title{Extending [PCF](dt-003M) with sum types}
44+\p{We shall extend [the syntax of PCF](dt-003M) with a new type former, #{\tau_1 + \tau_2} and three expression-level constructs for constructing and deconstructing sums:
55+##{
66+ \begin{array}{lcl}
77+ e & \Coloneqq & \cdots \mid \mathsf{inl}\ e \mid \mathsf{inr}\ e \mid \mathsf{case}\ e_0\ \mathsf{of}\ \mathsf{inl}\ x \rightarrow e_1 ; \mathsf{inr}\ y \rightarrow e_2 \\
88+ \tau & \Coloneqq & \cdots \mid \tau_1 + \tau_2
99+ \end{array}
1010+}
1111+Adding to our [typing rules](dt-003N) similarly:
1212+##{
1313+ \inferrule{\Gamma \vdash e : \tau_1}{\Gamma \vdash \mathsf{inl}\ e : \tau_1 + \tau_2}\quad
1414+ \inferrule{\Gamma \vdash e : \tau_2}{\Gamma \vdash \mathsf{inr}\ e : \tau_1 + \tau_2}\\[2em]
1515+ \inferrule{\Gamma \vdash e_0 : \tau_1 + \tau_2 \quad \Gamma, x : \tau_1 \vdash e_1 : \tau \quad \Gamma, y : \tau_2 \vdash e_2 : \tau}{ \Gamma \vdash \mathsf{case}\ e_0\ \mathsf{of}\ \mathsf{inl}\ x \rightarrow e_1 ; \mathsf{inr}\ y \rightarrow e_2 : \tau}
1616+}
1717+To extend our [semantics](dt-003P), we make the sum type #{\tau_1 + \tau_2} denote the [sum cpo](dt-0031) #{\llbracket \tau_1 \rrbracket + \llbracket \tau_2 \rrbracket}, and our expression semantics are expressed using our [primitive functions for sums](dt-003D).
1818+##{
1919+ \begin{array}{lcl}
2020+ \llbracket \mathsf{inl}\ e \rrbracket_\Gamma & = & \mathit{inl} \circ \llbracket e \rrbracket_\Gamma \\
2121+ \llbracket \mathsf{inr}\ e \rrbracket_\Gamma & = & \mathit{inr} \circ \llbracket e \rrbracket_\Gamma
2222+ \end{array}
2323+}
2424+##{
2525+ \begin{array}{l}\llbracket \mathsf{case}\ e_0\ \mathsf{of}\ \mathsf{inl}\ x \rightarrow e_1 ; \mathsf{inr}\ y \rightarrow e_2 \rrbracket_\Gamma(\vec{z}) \\
2626+ \quad =
2727+ [ \boldsymbol{\lambda}v. \llbracket e_1 \rrbracket_{\Gamma,x:\tau_1}(\vec{z},v), \boldsymbol{\lambda}v. \llbracket e_2 \rrbracket_{\Gamma,y:\tau_2}(\vec{z},v)] (\llbracket e_0 \rrbracket_\Gamma(\vec{z}))\end{array}
2828+}
2929+}
+10
trees/dt/dt-003T.tree
···11+\title{PCF}
22+\author{liamoc}
33+\p{For most purposes in [semantics](dt-0001), describing semantics in terms of ([continuous](dt-001J) functions on) [cpos](dt-001D) is enough. To demonstrate, we will give semantics to PCF: a Turing-complete, higher order functional programming language. We will then extend PCF to include more features, namely pairs (product types) and sum types.}
44+\transclude{dt-003M}
55+\transclude{dt-003N}
66+\transclude{dt-003O}
77+\transclude{dt-003Q}
88+\transclude{dt-003P}
99+\transclude{dt-003R}
1010+\transclude{dt-003S}