5.0 KiB
Assignment 4
In order to pass this assignment you have to get at least three points.
Please provide your source code in a form which is easy to test, not, say, a PDF file.
(1p)
When is a function $f \in \mathbb{N} \rightarrow \mathbb{N}$ $\chi$ computable?
Answer
Whenever there is a closed expression $e$ such that $\forall n \in \mathbb{N}.\ e\ \ulcorner n \urcorner \Downarrow \ulcorner f\ n \urcorner$. Further, $f$ is $\chi$ computable if one can reduce it to another function $g$ that is $\chi$ computable.
(2p)
Prove that the following functions is not $\chi$ computable (where Exp is the abstract syntax of $\chi$): \[ f \in \{ p \in Exp |\ p\ \text{is closed} \} \rightarrow \mathbb{B} \] \[ f\ p = \text{if}\ p\ \text{terminates with the value}\ Zero()\ \text{then}\ true\ \text{else}\ false \]
You should prove this by reducing the "intensional" halting problem (see the lecture slides) to this function.
Answer
The halting problem can be reduced to this problem, by the expression: \[ \lambda p. f\ ((\lambda \_. Zero()) p) \]
In the case where p terminates, the $(\lambda \_. Zero()) p$ would evaluate to $Zero()$, and therefore $f ((\lambda \_. Zero()) p)$ would evaluate to true. In the case where p does not terminate, then $(\lambda \_. Zero()) p$ would not evaluate to $Zero()$ as $p$ would not terminate with a value, therefore $f ((\lambda \_. Zero()) p)$ would evaluate to false.
(2p)
Implement $\chi$ substitution as a $\chi$ program. You should construct a $\chi$ expressions, let us call it subst, that given the representation of a variable $x$, a closed expression $e$, and an expression $e'$, produces the representation of the expression obtained by substituting $e$ for every free occurrence of $x$ in $e'$: \[ subst \ulcorner x \urcorner \ulcorner e \urcorner \ulcorner e' \urcorner \Downarrow \ulcorner e' [ x \leftarrow e] \urcorner \]
You do not need to prove formally that this property is satisfied, but please test your code. You can for instance test that this implementation of substitution matches the one from the last assignment. The wrapper module (documentation) contains some testing procedures, as well as routines for representing natural numbers and $\chi$ abstract syntax as constructor trees.
Answer
See Self.hs
(2p)
Implement a $\chi$ self-interpreter. You should construct a $\chi$ expression, let us call it eval, that satisfies the following properties:
- For any closed expressions $e$ and $v$, if $e \Downarrow v$ then $\text{eval}\ \ulcorner e \urcorner \Downarrow \ulcorner v \urcorner$.
- For any closed expressions $e$ and $v$, if $\text{eval}\ \ulcorner e \urcorner \Downarrow v$, then there should be some $v'$ such that $e \Downarrow v'$ and $v = \ulcorner v' \urcorner$.
You do not need to prove formally that these properties are satisfied, but please test your code. Make sure that the following examples are implemented correctly:
-
The program $\text{eval}\ \ulcorner e \urcorner$ should fail to terminate when e is any of the following programs:
- $\text{C}()\ \text{C}()$
- $\text{case}\ \lambda x.x\ \text{of}\ \{\}$
- $\text{case}\ \text{C}()\ \text{of}\ \{ \text{C}(x) \rightarrow \text{C}() \}$
- $\text{case}\ \text{C}(\text{C}())\ \text{of}\ \{ \text{C}() \rightarrow \text{C}() \}$
- $\text{case}\ \text{C}(\text{C}())\ \text{of}\ \{ \text{C}() \rightarrow \text{C}(); \text{C}(x) \rightarrow x \}$
- $\text{case}\ \text{C}()\ \text{of}\ \{ \text{D}() \rightarrow \text{D}() \}$
- $(\lambda x.\lambda y.x) (\text{rec}\ x = x)$
-
The following programs should terminate with specific results:
- The program $\text{eval}\ \ulcorner \text{case}\ C(D(),E())\ \text{of}\ \{ C(x, x) \rightarrow x \} \urcorner$ should terminate with the value $\ulcorner E() \urcorner$.
- The program $\text{eval}\ \ulcorner \text{case}\ C(\lambda x.x, Zero())\ \text{of}\ \{ C(f, x) \rightarrow f x \} \urcorner$ should terminate with the value $\ulcorner Zero() \urcorner$.
- The program $\text{eval}\ \ulcorner \text{case}\ (\lambda x.x) C()\ \text{of}\ \{ C() \rightarrow C() \} \urcorner$ should terminate with the value $\ulcorner C() \urcorner$.
- The program $\text{eval}\ \ulcorner ((\lambda x.x)(\lambda x.x))(\lambda x.x) \urcorner$ should terminate with the value $\ulcorner \lambda x.x \urcorner$.
For full credit your implementation must evaluate addition of natural numbers correctly, i.e. $\text{eval} \ulcorner \text{add}\ \ulcorner m \urcorner \ \ulcorner n \urcorner \urcorner$ must terminate with the value $\ulcorner \ulcorner m + n \urcorner \urcorner$, for arbitrary (small) $m, n \in \mathbb{N}$ (where add is an implementation of addition). The wrapper module contains some testing procedures that you can use.
Answer
See Self.hs