Paper/Paper.thy
changeset 117 b27f4bd98078
parent 116 11fd52dceb9b
child 118 7d8a1bfb8925
equal deleted inserted replaced
116:11fd52dceb9b 117:b27f4bd98078
    81   inv_begin ("I\<^bsub>cbegin\<^esub>") and
    81   inv_begin ("I\<^bsub>cbegin\<^esub>") and
    82   inv_loop1 ("J\<^isub>1") and
    82   inv_loop1 ("J\<^isub>1") and
    83   inv_loop0 ("J\<^isub>0") and
    83   inv_loop0 ("J\<^isub>0") and
    84   inv_end1 ("K\<^isub>1") and
    84   inv_end1 ("K\<^isub>1") and
    85   inv_end0 ("K\<^isub>0") and
    85   inv_end0 ("K\<^isub>0") and
    86   measure_begin_step ("M\<^bsub>cbegin\<^esub>")
    86   measure_begin_step ("M\<^bsub>cbegin\<^esub>") and
       
    87   layout_of ("layout") and
       
    88   findnth ("find'_nth")
    87 
    89 
    88  
    90  
    89 lemma inv_begin_print:
    91 lemma inv_begin_print:
    90   shows "s = 0 \<Longrightarrow> inv_begin n (s, tp) = inv_begin0 n tp" and
    92   shows "s = 0 \<Longrightarrow> inv_begin n (s, tp) = inv_begin0 n tp" and
    91         "s = 1 \<Longrightarrow> inv_begin n (s, tp) = inv_begin1 n tp" and 
    93         "s = 1 \<Longrightarrow> inv_begin n (s, tp) = inv_begin1 n tp" and 
   152 
   154 
   153 
   155 
   154 lemma layout:
   156 lemma layout:
   155   shows "layout_of [] = []"
   157   shows "layout_of [] = []"
   156   and   "layout_of ((Inc R\<iota>)#os) = (2 * R\<iota> + 9)#(layout_of os)"
   158   and   "layout_of ((Inc R\<iota>)#os) = (2 * R\<iota> + 9)#(layout_of os)"
   157   and   "layout_of ((Dec R\<iota> o\<iota>)#os) = (2 * R\<iota> + 16)#(layout_of os)"
   159   and   "layout_of ((Dec R\<iota> i)#os) = (2 * R\<iota> + 16)#(layout_of os)"
   158   and   "layout_of ((Goto o\<iota>)#os) = 1#(layout_of os)"
   160   and   "layout_of ((Goto i)#os) = 1#(layout_of os)"
   159 by(auto simp add: layout_of.simps length_of.simps)
   161 by(auto simp add: layout_of.simps length_of.simps)
   160 
   162 
   161 
   163 
   162 (*>*)
   164 (*>*)
   163 
   165 
  1138   for making it less laborious to write Turing machine
  1140   for making it less laborious to write Turing machine
  1139   programs. Abacus machines operate over a set of registers $R_0$,
  1141   programs. Abacus machines operate over a set of registers $R_0$,
  1140   $R_1$, \ldots{} each being able to hold an arbitrary large natural
  1142   $R_1$, \ldots{} each being able to hold an arbitrary large natural
  1141   number.  We use natural numbers to refer to registers; we also use a natural number
  1143   number.  We use natural numbers to refer to registers; we also use a natural number
  1142   to represent a program counter and jumping ``addresses''. An abacus 
  1144   to represent a program counter and jumping ``addresses''. An abacus 
  1143   program is a list of \emph{statements} defined by the datatype:
  1145   program is a list of \emph{instructions} defined by the datatype:
  1144 
  1146 
  1145   \begin{center}
  1147   \begin{center}
  1146   \begin{tabular}{rcl@ {\hspace{10mm}}l}
  1148   \begin{tabular}{rcl@ {\hspace{10mm}}l}
  1147   @{text "o"} & $::=$  & @{term "Inc R\<iota>"} & increment register $R$ by one\\
  1149   @{text "i"} & $::=$  & @{term "Inc R\<iota>"} & increment register $R$ by one\\
  1148   & $\mid$ & @{term "Dec R\<iota> o\<iota>"} & if content of $R$ is non-zero,\\
  1150   & $\mid$ & @{term "Dec R\<iota> i"} & if content of $R$ is non-zero,\\
  1149   & & & then decrement it by one\\
  1151   & & & then decrement it by one\\
  1150   & & & otherwise jump to statement $o$\\
  1152   & & & otherwise jump to instruction $i$\\
  1151   & $\mid$ & @{term "Goto o\<iota>"} & jump to statement $o$
  1153   & $\mid$ & @{term "Goto i"} & jump to instruction $i$
  1152   \end{tabular}
  1154   \end{tabular}
  1153   \end{center}
  1155   \end{center}
  1154 
  1156 
  1155   \noindent
  1157   \noindent
  1156   For example the program clearing the register $R$ (that is setting
  1158   For example the program clearing the register $R$ (that is setting
  1157   it to @{term "(0::nat)"}) can be defined as follows:
  1159   it to @{term "(0::nat)"}) can be defined as follows:
  1158 
  1160 
  1159   \begin{center}
  1161   \begin{center}
  1160   @{thm clear.simps[where n="R\<iota>" and e="o\<iota>", THEN eq_reflection]}
  1162   @{thm clear.simps[where n="R\<iota>" and e="i", THEN eq_reflection]}
  1161   \end{center}
  1163   \end{center}
  1162 
  1164 
  1163   \noindent
  1165   \noindent
  1164   Running such a program means we start with the first instruction
  1166   Running such a program means we start with the first instruction
  1165   then execute one statements after the other, unless there is a jump.  For
  1167   then execute one instructions after the other, unless there is a jump.  For
  1166   example the second statement the jump @{term "Goto 0"} in @{term clear} means
  1168   example the second instruction the jump @{term "Goto 0"} in @{term clear} means
  1167   we jump back to the first statement closing the loop.  Like with our
  1169   we jump back to the first instruction closing the loop.  Like with our
  1168   Turing machines, we fetch statements from an abacus program such
  1170   Turing machines, we fetch instructions from an abacus program such
  1169   that a jump out of ``range'' behaves like a @{term "Nop"}-action. In
  1171   that a jump out of ``range'' behaves like a @{term "Nop"}-action. In
  1170   this way it is easy to define a function @{term steps} that
  1172   this way it is easy to define a function @{term steps} that
  1171   executes @{term n} statements of an abacus program.
  1173   executes @{term n} instructions of an abacus program.
  1172   
  1174   
  1173   The main point of abacus programs is to be able to translate them to 
  1175   The main point of abacus programs is to be able to translate them to 
  1174   Turing machine programs. Because of the jumps in abacus programs, it
  1176   Turing machine programs. Registers and their content are represented by
       
  1177   standard tapes.
       
  1178   Because of the jumps in abacus programs, it
  1175   seems difficult to build Turing machine programs using @{text "\<oplus>"}.
  1179   seems difficult to build Turing machine programs using @{text "\<oplus>"}.
  1176   To overcome this difficulty we calculate a \emph{layout} as follows
  1180   To overcome this difficulty, we calculate a \emph{layout} as follows
  1177 
  1181 
  1178   \begin{center}
  1182   \begin{center}
  1179   @{thm layout}
  1183   \begin{tabular}[t]{@ {}l@ {\hspace{1mm}}c@ {\hspace{1mm}}l@ {}}
  1180   \end{center}
  1184   @{thm (lhs) layout(1)} & @{text "\<equiv>"} & @{thm (rhs) layout(1)}\\
  1181 
  1185   @{thm (lhs) layout(2)} & @{text "\<equiv>"} & @{thm (rhs) layout(2)}\\
  1182 
  1186   @{thm (lhs) layout(3)} & @{text "\<equiv>"} & @{thm (rhs) layout(3)}\\
  1183 
  1187   @{thm (lhs) layout(4)} & @{text "\<equiv>"} & @{thm (rhs) layout(4)}\\
  1184   %Running an abacus program means to start 
  1188   \end{tabular}
  1185   %A \emph{program} of an abacus machine is a list of such
  1189   \end{center}
  1186   %statements. 
  1190 
  1187   %The \emph{memory} $m$ of an abacus machine holding the values
  1191   \noindent
  1188   %of the registers is represented as a list of natural numbers.
  1192   This gives us a list of natural numbers specifying how many states
  1189   %We have a lookup function for this memory, written @{term "abc_lm_v m R\<iota>"},
  1193   are needed to translate each abacus instruction. The @{text Goto}
  1190   %which looks up the content of register $R$; if $R$
  1194   instruction is easiest to translate requiring only one state in
  1191   %is not in this list, then we return 0. Similarly we
  1195   the corresponding Turing machine:
  1192   %have a setting function, written @{term "abc_lm_s m R\<iota> n"}, which
  1196 
  1193   %sets the value of $R$ to $n$, and if $R$ was not yet in $m$
  1197   \begin{center}
  1194   %it pads it approriately with 0s.
  1198   @{thm (rhs) tgoto.simps[where n="i"]}
  1195   %Abacus machine halts when it jumps out of range.
  1199   \end{center}
       
  1200 
       
  1201   \noindent
       
  1202   where @{term "i"} is the corresponding state in the Turing machine program 
       
  1203   to jump to. For translating the instruction @{term Inc}
       
  1204   one has to remember that the content of the registers are encoded
       
  1205   in the Turing machine as standard tape. Therefore the translated Turing machine 
       
  1206   needs to first find the number corresponding to the register @{text "R"}. This needs a machine
       
  1207   with @{term "(2::nat) * R\<iota>"} states and can be constructed as follows: 
       
  1208 
       
  1209   \begin{center}
       
  1210   \begin{tabular}[t]{@ {}l@ {\hspace{1mm}}c@ {\hspace{1mm}}l@ {}}
       
  1211   @{thm (lhs) findnth.simps(1)} & @{text "\<equiv>"} & @{thm (rhs) findnth.simps(1)}\\
       
  1212   @{thm (lhs) findnth.simps(2)} & @{text "\<equiv>"}\\
       
  1213   \multicolumn{3}{@ {}l@ {}}{\hspace{8mm}@{thm (rhs) findnth.simps(2)}}\\
       
  1214   \end{tabular}
       
  1215   \end{center}
       
  1216 
       
  1217   \noindent
       
  1218   Then we need to increase the ``number'' on the tape by one,
       
  1219   and adjust the following registers. By adjusting we only need to 
       
  1220   replace the first @{term Oc} of each number by @{term Bk} and the last
       
  1221   one from @{term Bk} to @{term Oc}.
       
  1222   Finally we need to transition the head of the
       
  1223   Turing machine back into the standard position. This requires a Turing machine
       
  1224   with 9 states. Similarly for the translation of @{term Dec}, where the 
       
  1225   translated Turing machine needs to first check whether the content of the
       
  1226   corresponding register is @{text 0}. For this we have a Turing machine program
       
  1227   with @{text 16} states. 
       
  1228 
       
  1229   Finally, having a turing machine for each abacus instruction we need
       
  1230   to ``stitch'' the Turing machines together into one so that each
       
  1231   Turing machine component transitions to next one, just like in
       
  1232   are needed to translate each abacus instructionabacus
       
  1233   programs. While generating the Turing machine program for an abacus
       
  1234   program is not too difficult to formalise, the problem is that it
       
  1235   contains @{text Goto}s all over the place. The unfortunate result is
       
  1236   are needed to translate each abacus instructionthat we cannot
       
  1237   use our Hoare-rules for reasoning about sequentially composed
       
  1238   programs. Instead we have to treat the Turing machine as one ``block''
       
  1239   and show as invariant that it performs the same operations
       
  1240   as the abacus program.
       
  1241 
       
  1242 
  1196 *}
  1243 *}
  1197 
  1244 
  1198 
  1245 
  1199 section {* Recursive Functions *}
  1246 section {* Recursive Functions *}
  1200 
  1247