Compare commits

...

2 Commits

Author SHA1 Message Date
c7928ec703 Hand ins 2025-12-09 15:43:08 +01:00
df5c80b5df Almost done 2025-12-09 13:49:27 +01:00
4 changed files with 96 additions and 6 deletions

View File

@@ -24,7 +24,7 @@ The Turing machine is the one described by the following:
- States: $S = \{ s_0, s_1 \}$ - States: $S = \{ s_0, s_1 \}$
- Initial state: $s_0$ - Initial state: $s_0$
- Input alphabet: $\{c_1,c_2\}$ - Input alphabet: $\{c_1,c_2\}$
- Tape alphabet: $\{\text{\textvisiblespace}},c_1,c_2\}$ - Tape alphabet: $\{\text{\textvisiblespace},c_1,c_2\}$
- Transition function: - Transition function:
+ $\delta (s_0, c_1) = (s_1,c_1,R)$ + $\delta (s_0, c_1) = (s_1,c_1,R)$
+ $\delta (s_0, c_2) = (s_1,c_2,R)$ + $\delta (s_0, c_2) = (s_1,c_2,R)$
@@ -39,7 +39,7 @@ Implement a Turing machine interpreter using $\chi$.
The interpreter should be a closed $\chi$ expression. If we denote this expression by run, then it should satisfy the following property (but you do not have to prove that it does): The interpreter should be a closed $\chi$ expression. If we denote this expression by run, then it should satisfy the following property (but you do not have to prove that it does):
+ For every Turing machine tm and input string $xs \in \text{List}\ \{0, 1\}$ the following equation should hold: + For every Turing machine tm and input string $xs \in \text{List}\ \{0, 1\}$ the following equation should hold:
\[ \llbracket \text{apply}\ (\text{apply}\ \text{\textit{run}}\ \ulcorner \text{tm} \urcorner) \ulcorner xs \urcorner \rrbracket = \ulcorner \llbracket \text{tm} \rrbracket \text{xs} \urcorner \] \[ \llbracket \text{apply}\ (\text{apply}\ \text{\textit{run}}\ \ulcorner \text{tm} \urcorner) \ulcorner xs \urcorner \rrbracket = \ulcorner \llbracket \text{tm} \rrbracket\ \text{xs} \urcorner \]
(The $\llbracket \_ \rrbracket$ brackets to the left stand for the $\chi$ semantics, and the $\llbracket \_ \rrbracket$ brackets to the right stand for the Turing machine semantics.) (The $\llbracket \_ \rrbracket$ brackets to the left stand for the $\chi$ semantics, and the $\llbracket \_ \rrbracket$ brackets to the right stand for the Turing machine semantics.)
Turing machines should be represented in the following way: Turing machines should be represented in the following way:
@@ -57,8 +57,13 @@ The input and output strings should use the usual representation of lists, with
Please test that addition, implemented as in Tutorial 5, Exercise 6 (with 0 instead of #), works as it should when run on your Turing machine interpreter. A testing procedure that you can use is included in the wrapper module (documentation). Please test that addition, implemented as in Tutorial 5, Exercise 6 (with 0 instead of #), works as it should when run on your Turing machine interpreter. A testing procedure that you can use is included in the wrapper module (documentation).
** Answer ** Answer
See =Turing.hs=
* (2p) * (2p)
Prove that every Turing-computable partial function in $\mathbb{N} \rightharpoonup \mathbb{N}$ is also $\chi$ computable. You can assume that the definition of “Turing-computable” uses Turing machines of the kind used in the previous exercise. Prove that every Turing-computable partial function in $\mathbb{N} \rightharpoonup \mathbb{N}$ is also $\chi$ computable. You can assume that the definition of “Turing-computable” uses Turing machines of the kind used in the previous exercise.
Hint: Use the interpreter from the previous exercise. Do not forget to convert the input and output to the right formats. Hint: Use the interpreter from the previous exercise. Do not forget to convert the input and output to the right formats.
** Answer
We know that we can construct an interpreter for Turing machines, and a translation of $\mathbb{N}$ to the language a Turing machine.
We also know that one can construct an interpreter in the form of a Turing machine of $\chi$, and the opposite.
This means that a partial function can be translated from $\chi$ to a Turing machine.
So, given a partial function, one can translate it to a Turing machine, and translate the input, and run the implemented Turing interpreter from the last task, and then translate the result back into $\chi$.

View File

@@ -1,4 +1,87 @@
-- | {-# Language LambdaCase, Strict #-}
module Interpreter.Turing where module Interpreter.Turing where
import Chi
import Interpreter.Haskell -- from assignment 3
equalExp :: Exp
equalExp = parse
"rec equal = \\m. \\n. case m of \
\ { Zero() -> case n of { Blank() -> False(); Zero() -> True(); Suc(n) -> False() } \
\ ; Suc(m) -> case n of { Blank() -> False(); Zero() -> False(); Suc(n) -> equal m n } \
\ ; Blank() -> case n of { Blank() -> True(); Zero() -> False(); Suc(n) -> False() } \
\ }"
lookupExp :: Exp
lookupExp =
subst (Variable "equal") equalExp $ parse
"\\s. \\head. rec lookup = \\rules. case rules of \
\ { Nil() -> Done() \
\ ; Cons(x,xs) -> case x of \
\ { Rule(s1, x1, s2, x2, d) -> case equal s s1 of \
\ { True() -> case equal head x1 of \
\ { True() -> Trip(s2,x2,d)\
\ ; False() -> lookup xs \
\ } \
\ ; False() -> lookup xs \
\ } \
\ }\
\ } \
\ "
joinExp :: Exp
joinExp = parse
"rec join = \\xs. \\ys. case xs of \
\ { Nil() -> ys \
\ ; Cons(x,xs) -> join xs Cons(x,ys) \
\ }"
appendExp :: Exp
appendExp = parse
"rec append = \\xs. \\ys. case xs of \
\ { Nil() -> ys \
\ ; Cons(x,xs) -> Cons(x,append xs ys) \
\ }"
reverseExp :: Exp
reverseExp =
subst (Variable "append") appendExp $ parse
"rec reverse = \\xs. case xs of \
\ { Nil() -> Nil() \
\ ; Cons(x,xs) -> append xs Cons(x,Nil())\
\ }"
removeLastBlanksExp :: Exp
removeLastBlanksExp = parse
"rec removeLastBlanks = \\xs. case xs of \
\ { Nil() -> Nil() \
\ ; Cons(x,xs) -> case x of \
\ { Blank() -> removeLastBlanks xs \
\ ; Zero() -> Cons(x,xs) \
\ ; Suc(n) -> Cons(x,xs) \
\ } \
\ }\
\ "
runExp :: Exp
runExp =
subst (Variable "removeLastBlanks") removeLastBlanksExp .
subst (Variable "reverse") reverseExp .
subst (Variable "join") joinExp .
subst (Variable "lookup") lookupExp $ parse
"(rec run = \\rev. \\tm. \\tape. case tm of \
\ { TM(state,deltas) -> case tape of \
\ { Nil() -> run rev TM(state,deltas) Cons(Blank(), Nil()) \
\ ; Cons(head,xs) -> case lookup state head deltas of \
\ { Done() -> join rev (reverse (removeLastBlanks (reverse tape))) \
\ ; Trip(s2, x2, d) -> case d of \
\ { L() -> case rev of \
\ { Nil() -> run Nil() TM(s2,deltas) Cons(x2,xs) \
\ ; Cons(y,ys) -> run ys TM(s2,deltas) Cons(y,Cons(x2,xs)) \
\ } \
\ ; R() -> run Cons(x2,rev) TM(s2,deltas) xs \
\ } \
\ } \
\ } \
\ }) Nil()"

View File

@@ -26,6 +26,7 @@ library
PrintChi PrintChi
Interpreter.Haskell Interpreter.Haskell
Interpreter.Self Interpreter.Self
Interpreter.Turing
hs-source-dirs: hs-source-dirs:
. .

5
5.org
View File

@@ -28,7 +28,7 @@ Which means
&= \llbracket \underline{\text{has-fixpoint}}\ \ulcorner \lambda n. ((\lambda \_. n)\ p) \urcorner \rrbracket \\ &= \llbracket \underline{\text{has-fixpoint}}\ \ulcorner \lambda n. ((\lambda \_. n)\ p) \urcorner \rrbracket \\
&= \ulcorner \text{has-fixpoint}(\lambda n. ((\lambda \_. n)\ p)) \urcorner \\ &= \ulcorner \text{has-fixpoint}(\lambda n. ((\lambda \_. n)\ p)) \urcorner \\
&= \begin{cases} &= \begin{cases}
\ulcorner \text{true} \urcorner &\quad \text{if}\ \exists v \in Exp. \llbracket p \rrbracket = v,\\ \ulcorner \text{true} \urcorner &\quad \text{if}\ \exists v \in Exp. \llbracket p \rrbracket = v\ \text{(due to strictness in application)},\\
\ulcorner \text{false} \urcorner &\quad otherwise \ulcorner \text{false} \urcorner &\quad otherwise
\end{cases} \end{cases}
\end{align*} \end{align*}
@@ -138,7 +138,8 @@ Let a two-tape Turing machine be defined by the following:
\UnaryInfC{$\text{\textvisiblespace} \notin \Sigma$} \UnaryInfC{$\text{\textvisiblespace} \notin \Sigma$}
\AxiomC{$\Gamma$ is a finite set} \AxiomC{$\Gamma$ is a finite set}
\UnaryInfC{$\Sigma \cup \{\text{\textvisiblespace}\} \subseteq \Gamma$} \UnaryInfC{$\Sigma \cup \{\text{\textvisiblespace}\} \subseteq \Gamma$}
\AxiomC{$\delta \in S \times \Gamma \times \Gamma \rightharpoonup S \times (\Gamma \times \{L,R\}) \times (\Gamma \times \{L,R\})$} \AxiomC{$\delta \in S \times \Gamma \times \Gamma \rightharpoonup$}
\UnaryInfC{$S \times (\Gamma \times \{L,R\}) \times (\Gamma \times \{L,R\})$}
\alwaysSingleLine \alwaysSingleLine
\QuaternaryInfC{$(S,s_0, \Sigma, \Gamma, \delta) \in \text{TM2}$} \QuaternaryInfC{$(S,s_0, \Sigma, \Gamma, \delta) \in \text{TM2}$}
\end{prooftree} \end{prooftree}