text‹
previous sections have focused on ``formatted'' symbol sequences for
and lists, in this section we devise some Turing machines dealing with
`unstructured'' arbitrary symbol sequences. The only ``structure'' that is
imposed is that of not containing a blank symbol because when reading a
sequence, say from the input tape, a blank would signal the end of the
sequence. ›
subsection‹Checking for being over an alphabet›
text‹
this section we devise a Turing machine that checks if a proper symbol sequence
over a given alphabet represented by an upper bound symbol $z$. ›
abbreviation proper_symbols_lt :: "symbol ==> symbol list ==> bool"where "proper_symbols_lt z zs ≡ proper_symbols zs ∧ symbols_lt z zs"
text‹
next Turing machine checks if the symbol sequence (up until the first blank)
tape $j_1$ contains only symbols from $\{2, \dots, z - 1\}$, where $z$ is a
of the TM, and writes to tape $j_2$ the number~1 or~0, representing
or False, respectively. It assumes that $j_2$ initially contains at most
symbol. ›
definition tm_proper_symbols_lt :: "tapeidx ==> tapeidx ==> symbol ==> machine"where "tm_proper_symbols_lt j1 j2 z ≡ tm_write j2 1 ;; WHILE [] ; λrs. rs ! j1 ≠◻ DO IF λrs. rs ! j1 < 2 ∨ rs ! j1 ≥ z THEN tm_write j2 ◻ ELSE [] ENDIF ;; tm_right j1 DONE ;; tm_cr j1"
lemma tm_proper_symbols_lt_tm: assumes"0 < j2""j1 < k""j2 < k"and"G ≥ 4" shows"turing_machine k G (tm_proper_symbols_lt j1 j2 z)" using assms tm_write_tm tm_right_tm tm_cr_tm Nil_tm tm_proper_symbols_lt_def
turing_machine_loop_turing_machine turing_machine_branch_turing_machine by simp
locale turing_machine_proper_symbols_lt = fixes j1 j2 :: tapeidx and z :: symbol begin
definition"tps1 t ≡ tps0 [j1 := (⌊zs⌋, Suc t), j2 := (if proper_symbols_lt z (take t zs) then ⌊[1]⌋ else ⌊[]⌋, 1)]"
lemma tm1 [transforms_intros]: "transforms tm1 tps0 1 (tps1 0)" unfolding tm1_def proof (tform tps: jk tps0) have"(if proper_symbols_lt z (take 0 zs) then ⌊[1]⌋ else ⌊[]⌋, 1) = (⌊[1]⌋, 1)" by simp moreoverhave"tps0 ! j2 |:=| 1 = (⌊[1]⌋, 1)" using tps0(2) contents_def by auto moreoverhave"tps0[j1 := (⌊zs⌋, Suc 0)] = tps0" using tps0(1) by (metis One_nat_def list_update_id) ultimatelyshow"tps1 0 = tps0[j2 := tps0 ! j2 |:=| 1]" unfolding tps1_def by auto qed
definition"tps2 t ≡ tps0 [j1 := (⌊zs⌋, Suc t), j2 := (if proper_symbols_lt z (take (Suc t) zs) then ⌊[1]⌋ else ⌊[]⌋, 1)]"
lemma tm2 [transforms_intros]: assumes"t < length zs" shows"transforms tm2 (tps1 t) 3 (tps2 t)" unfolding tm2_def proof (tform tps: jk tps1_def) have"tps1 t ! j1 = (⌊zs⌋, Suc t)" using tps1_def jk by simp moreoverhave"read (tps1 t) ! j1 = tps1 t :.: j1" using tapes_at_read' jk tps1_def by (metis (no_types, lifting) length_list_update) ultimatelyhave *: "read (tps1 t) ! j1 = zs ! t" using contents_inbounds assms(1) by simp have j2: "tps1 t ! j2 = (if proper_symbols_lt z (take t zs) then ⌊[1]⌋ else ⌊[]⌋, 1)" using tps1_def jk by simp show"tps2 t = (tps1 t)[j2 := tps1 t ! j2 |:=| ◻]"if"read (tps1 t) ! j1 < 2 ∨ z ≤read (tps1 t) ! j1" proof - have c3: "(⌊[1]⌋, 1) |:=| ◻ = (⌊[]⌋, 1)" using contents_def by auto have"(if proper_symbols_lt z (take t zs) then ⌊[1]⌋ else ⌊[]⌋, 1) |:=| ◻ = (if proper_symbols_lt z (take (Suc t) zs) then ⌊[1]⌋ else ⌊[]⌋, 1)" proof (cases "proper_symbols_lt z (take t zs)") case True have"zs ! t < 2 ∨ z ≤ zs ! t" using that * by simp thenhave"¬ proper_symbols_lt z (take (Suc t) zs)" using assms(1) by auto thenshow ?thesis using c3 by auto next case False thenhave"¬ proper_symbols_lt z (take (Suc t) zs)" by auto thenshow ?thesis using c3 False by auto qed thenhave"tps1 t ! j2 |:=| ◻ = (if proper_symbols_lt z (take (Suc t) zs) then ⌊[1]⌋ else ⌊[]⌋, 1)" using j2 by simp thenshow"tps2 t = (tps1 t)[j2 := tps1 t ! j2 |:=| ◻]" unfolding tps2_def tps1_def using c3 jk(1,4) by simp qed show"tps2 t = tps1 t"if"¬ (read (tps1 t) ! j1 < 2 ∨ z ≤ read (tps1 t) ! j1)" proof - have1: "zs ! t ≥ 2 ∧ z > zs ! t" using that * by simp show"tps2 t = tps1 t" proof (cases "proper_symbols_lt z (take t zs)") case True have"proper_symbols_lt z (take (Suc t) zs)" using True 1 assms(1) zs by (metis length_take less_antisym min_less_iff_conj nth_take) thenshow ?thesis using tps1_def tps2_def jk by simp next case False thenhave"¬ proper_symbols_lt z (take (Suc t) zs)" by auto thenshow ?thesis using tps1_def tps2_def jk False by auto qed qed qed
lemma tm3 [transforms_intros]: assumes"t < length zs" shows"transforms tm3 (tps1 t) 4 (tps1 (Suc t))" unfolding tm3_def proof (tform tps: assms jk tps2_def) have"tps2 t ! j1 |+| 1 = (⌊zs⌋, Suc (Suc t))" using tps2_def jk by simp thenshow"tps1 (Suc t) = (tps2 t)[j1 := tps2 t ! j1 |+| 1]" unfolding tps1_def tps2_def by (metis (no_types, lifting) jk(2) list_update_overwrite list_update_swap) qed
lemma tm4 [transforms_intros]: assumes"ttt = 1 + 6 * length zs" shows"transforms tm4 (tps1 0) ttt (tps1 (length zs))" unfolding tm4_def proof (tform time: assms) show"read (tps1 t) ! j1 ≠◻"if"t < length zs"for t proof - have"tps1 t ! j1 = (⌊zs⌋, Suc t)" using tps1_def jk by simp moreoverhave"read (tps1 t) ! j1 = tps1 t :.: j1" using tapes_at_read' jk tps1_def by (metis (no_types, lifting) length_list_update) ultimatelyhave"read (tps1 t) ! j1 = zs ! t" using contents_inbounds that by simp thenshow ?thesis using zs that by auto qed show"¬ read (tps1 (length zs)) ! j1 ≠◻" proof - have"tps1 (length zs) ! j1 = (⌊zs⌋, Suc (length zs))" using tps1_def jk by simp moreoverhave"read (tps1 (length zs)) ! j1 = tps1 (length zs) :.: j1" using tapes_at_read' jk tps1_def by (metis (no_types, lifting) length_list_update) ultimatelyshow ?thesis by simp qed qed
text‹
Turing machine in this section reads the input tape until the first blank
increments a counter on tape $j$ for every symbol read. In the end
performs a carriage return on the input tape, and tape $j$ contains the
of the input as binary number. For this to work, tape $j$ must initially
empty. ›
lemma proper_tape_read: assumes"proper_symbols zs" shows"|.| (⌊zs⌋, i) = ◻⟷ i > length zs" proof - have"|.| (⌊zs⌋, i) = ◻"if"i > length zs"for i using that contents_outofbounds by simp moreoverhave"|.| (⌊zs⌋, i) ≠◻"if"i ≤ length zs"for i using that contents_inbounds assms contents_def proper_symbols_ne0 by simp ultimatelyshow ?thesis by (meson le_less_linear) qed
text‹
next Turing machines reads the symbols on tape $j_1$ until the first blank
alternates between numbers~0 and~1 on tape $j_2$. Then tape $j_2$
the parity of the length of the symbol sequence on tape $j_1$. Finally, the TM
the output once more, so that tape $j_2$ contains a Boolean indicating
the length was even or not. We assume tape $j_2$ is initially empty,
is, represents the number~0. ›
text‹
next Turing machine implements a slightly idiosyncratic operation that we
in the next section for checking if a symbol sequence represents a list of
. The operation consists in checking if the symbol sequence on tape $j_1$
is empty or ends with the symbol $z$, which is another parameter of the
. If the condition is met, the number~1 is written to tape $j_2$, otherwise
number~0. ›
definition tm_empty_or_endswith :: "tapeidx ==> tapeidx ==> symbol ==> machine"where "tm_empty_or_endswith j1 j2 z ≡ tm_right_until j1 {◻} ;; tm_left j1 ;; IF λrs. rs ! j1 ∈ {▹, z} THEN tm_setn j2 1 ELSE [] ENDIF ;; tm_cr j1"
lemma tm_empty_or_endswith_tm: assumes"k ≥ 2"and"G ≥ 4"and"0 < j2"and"j1 < k"and"j2 < k" shows"turing_machine k G (tm_empty_or_endswith j1 j2 z)" using assms Nil_tm tm_right_until_tm tm_left_tm tm_setn_tm tm_cr_tm
turing_machine_branch_turing_machine tm_empty_or_endswith_def by simp
locale turing_machine_empty_or_endswith = fixes j1 j2 :: tapeidx and z :: symbol begin
lemma tm4: assumes"ttt = 18 + 2 * length zs" shows"transforms tm4 tps0 ttt tps4" unfolding tm4_def proof (tform time: assms tps3_def jk tps: tps3_def jk zs) have"tps3 ! j1 |#=| 1 = (⌊zs⌋, 1)" using tps3_def jk zs by simp thenshow"tps4 = tps3[j1 := tps3 ! j1 |#=| 1]" using tps4_def tps3_def jk tps0(1) by (metis list_update_id list_update_overwrite list_update_swap) qed
end(* context *)
end(* locale *)
lemma transforms_tm_empty_or_endswithI [transforms_intros]: fixes j1 j2 :: tapeidx and z :: symbol fixes tps tps' :: "tape list"and k :: nat and zs :: "symbol list" assumes"j1 ≠ j2""j1 < k""j2 < k" and"length tps = k" and"proper_symbols zs" assumes "tps ! j1 = (⌊zs⌋, 1)" "tps ! j2 = (⌊0⌋N, 1)" assumes"ttt = 18 + 2 * length zs" assumes"tps' = tps [j2 := (⌊zs = [] ∨ last zs = z⌋B, 1)]" shows"transforms (tm_empty_or_endswith j1 j2 z) tps ttt tps'" proof - interpret loc: turing_machine_empty_or_endswith j1 j2 z . show ?thesis using assms loc.tps4_def loc.tm4 loc.tm4_eq_tm_empty_or_endswith by simp qed
subsection‹Stripping trailing symbols›
text‹
the symbol $z$ from the end of a symbol sequence @{term zs} means: ›
definition rstrip :: "symbol ==> symbol list ==> symbol list"where "rstrip z zs ≡ take (LEAST i. i ≤ length zs ∧ set (drop i zs) ⊆ {z}) zs"
lemma length_rstrip: "length (rstrip z zs) = (LEAST i. i ≤ length zs ∧ set (drop i zs) ⊆ {z})" using rstrip_def wellorder_Least_lemma[where ?P="λi. i ≤ length zs ∧ set (drop i zs)⊆ {z}"] by simp
lemma length_rstrip_le: "length (rstrip z zs) ≤ length zs" using rstrip_def by simp
lemma rstrip_stripped: assumes"i ≥ length (rstrip z zs)"and"i < length zs" shows"zs ! i = z" proof - let ?P = "λi. i ≤ length zs ∧ set (drop i zs) ⊆ {z}" have"?P (length zs)" by simp thenhave"?P i" using assms length_rstrip LeastI[where ?P="?P"] Least_le[where ?P="?P"] by (metis (mono_tags, lifting) dual_order.trans order_less_imp_le set_drop_subset_set_drop) thenhave"set (drop i zs) ⊆ {z}" by simp thenshow ?thesis using assms(2) by (metis Cons_nth_drop_Suc drop_eq_Nil2 leD list.set(2) set_empty singleton_insert_inj_eq subset_singletonD) qed
lemma rstrip_replicate: "rstrip z (replicate n z) = []" using rstrip_def by (metis (no_types, lifting) Least_eq_0 empty_replicate set_drop_subset set_replicate take_eq_Nil zero_le)
lemma rstrip_not_ex: assumes"¬ (∃i<length zs. zs ! i ≠ z)" shows"rstrip z zs = []" using assms rstrip_def by (metis in_set_conv_nth replicate_eqI rstrip_replicate)
lemma assumes"∃i<length zs. zs ! i ≠ z" shows rstrip_ex_length: "length (rstrip z zs) > 0" and rstrip_ex_last: "last (rstrip z zs) ≠ z" proof - let ?P = "λi. i ≤ length zs ∧ set (drop i zs) ⊆ {z}" obtain i where i: "i < length zs""zs ! i ≠ z" using assms by auto thenhave"¬ set (drop i zs) ⊆ {z}" by (metis Cons_nth_drop_Suc drop_eq_Nil2 leD list.set(2) set_empty singleton_insert_inj_eq' subset_singletonD) thenhave"¬ set (drop 0 zs) ⊆ {z}" by (metis drop.simps(1) drop_0 set_drop_subset set_empty subset_singletonD) thenshow len: "length (rstrip z zs) > 0" using length_rstrip by (metis (no_types, lifting) LeastI bot.extremum drop_all dual_order.refl gr0I list.set(1)) let ?j = "length (rstrip z zs) - 1" have3: "?j < length (rstrip z zs)" using len by simp thenhave4: "?j < Least ?P" using length_rstrip by simp have5: "?P (length (rstrip z zs))" using LeastI_ex[of "?P"] length_rstrip by fastforce show"last (rstrip z zs) ≠ z" proof (rule ccontr) assume"¬ last (rstrip z zs) ≠ z" thenhave"last (rstrip z zs) = z" by simp thenhave"rstrip z zs ! ?j = z" using len by (simp add: last_conv_nth) thenhave2: "zs ! ?j = z" using len length_rstrip rstrip_def by auto have"?P ?j" proof - have"?j ≤ length zs" using3 length_rstrip_le by (meson le_eq_less_or_eq order_less_le_trans) moreoverhave"set (drop ?j zs) ⊆ {z}" using532 by (metis Cons_nth_drop_Suc One_nat_def Suc_pred insert_subset len list.simps(15) order_less_le_trans set_eq_subset) ultimatelyshow ?thesis by simp qed thenshow False using4 Least_le[of "?P"] by fastforce qed qed
text‹
Turing machine stripping the non-blank, non-start symbol $z$ from a proper
sequence works in the obvious way. First it moves to the end of the
sequence, that is, to the first blank. Then it moves left to the first
-$z$ symbol thereby overwriting every symbol with a blank. Finally it
a ``carriage return''. ›
definition"tps3 ≡ tps0 [j := (⌊rstrip z zs⌋, length (rstrip z zs))]"
lemma tm3 [transforms_intros]: assumes"ttt = length zs + 2 + Suc (length zs - length (rstrip z zs))" shows"transforms tm3 tps0 ttt tps3" unfolding tm3_def proof (tform tps: tps2_def tps3_def jk time: assms jk tps2_def) let ?n = "length zs - length (rstrip z zs)" have *: "tps2 ! j = (⌊zs⌋, length zs)" using tps2_def jk by simp show"lneigh (tps2 ! j) (UNIV - {z}) ?n" proof (cases "∃i<length zs. zs ! i ≠ z") case True thenhave1: "length (rstrip z zs) > 0" using rstrip_ex_length by simp show ?thesis proof (rule lneighI) show"(tps2 ::: j) (tps2 :#: j - ?n) ∈ UNIV - {z}" using * 1 contents_inbounds True length_rstrip length_rstrip_le rstrip_def rstrip_ex_last by (smt (verit, best) DiffI One_nat_def UNIV_I diff_diff_cancel diff_less fst_conv last_conv_nth
le_eq_less_or_eq length_greater_0_conv less_Suc_eq_le nth_take singletonD snd_conv) have"∧n'. n' < ?n ==> (tps2 ::: j) (tps2 :#: j - n') = z" using * rstrip_stripped by simp thenshow"∧n'. n' < ?n ==> (tps2 ::: j) (tps2 :#: j - n') ∉ UNIV - {z}" by simp qed next case False thenhave1: "rstrip z zs = []" using rstrip_not_ex by simp show ?thesis proof (rule lneighI) show"(tps2 ::: j) (tps2 :#: j - ?n) ∈ UNIV - {z}" using * 1 z by simp show"∧n'. n' < ?n ==> (tps2 ::: j) (tps2 :#: j - n') ∉ UNIV - {z}" using * rstrip_stripped by simp qed qed
have"lconstplant (⌊zs⌋, length zs) ◻ ?n = (⌊rstrip z zs⌋, length (rstrip z zs))"
(is"?lhs = _") proof - have"?lhs = (λi. if length zs - ?n < i ∧ i ≤ length zs then ◻ else ⌊zs⌋ i, length zs - ?n)" using lconstplant[of "(⌊zs⌋, length zs)"0"?n"] by auto moreoverhave"(λi. if length zs - ?n < i ∧ i ≤ length zs then ◻ else ⌊zs⌋ i) = ⌊rstrip z zs⌋" proof fix i
consider "length zs - ?n < i ∧ i ≤ length zs" | "i > length zs" | "i ≤ length (rstrip z zs)" by linarith thenshow"(if length zs - ?n < i ∧ i ≤ length zs then ◻ else ⌊zs⌋ i) = ⌊rstrip z zs⌋ i" proof (cases) case1 thenshow ?thesis by auto next case2 thenshow ?thesis by (metis contents_outofbounds diff_diff_cancel length_rstrip_le less_imp_diff_less) next case3 thenshow ?thesis using contents_def length_rstrip length_rstrip_le rstrip_def by auto qed qed moreoverhave"length zs - ?n = length (rstrip z zs)" using diff_diff_cancel length_rstrip_le by simp ultimatelyshow ?thesis by simp qed thenhave"lconstplant (tps2 ! j) ◻ ?n = (⌊rstrip z zs⌋, length (rstrip z zs))" using tps2_def jk by simp thenshow"tps3 = tps2 [j := tps2 ! j |-| ?n, j := lconstplant (tps2 ! j) ◻ ?n]" unfolding tps3_def tps2_def by simp qed
definition"tps4 ≡ tps0 [j := (⌊rstrip z zs⌋, 1)]"
lemma tm4: assumes"ttt = length zs + 2 + Suc (length zs - length (rstrip z zs)) + length (rstrip z zs) + 2" shows"transforms tm4 tps0 ttt tps4" unfolding tm4_def proof (tform tps: tps3_def tps4_def jk time: assms tps3_def jk) show"clean_tape (tps3 ! j)" using tps3_def jk zs rstrip_def by simp qed
lemma tm4': assumes"ttt = 3 * length zs + 5" shows"transforms tm4 tps0 ttt tps4" proof - let ?ttt = "length zs + 2 + Suc (length zs - length (rstrip z zs)) + length (rstrip z zs) + 2" have"?ttt = length zs + 5 + (length zs - length (rstrip z zs)) + length (rstrip z zs)" by simp alsohave"... ≤ length zs + 5 + length zs + length (rstrip z zs)" by simp alsohave"... ≤ length zs + 5 + length zs + length zs" using length_rstrip_le by simp alsohave"... = 3 * length zs + 5" by simp finallyhave"?ttt ≤ 3 * length zs + 5" . thenshow ?thesis using assms transforms_monotone tm4 by simp qed
end(* context *)
end(* locale *)
lemma transforms_tm_rstripI [transforms_intros]: fixes z :: symbol and j :: tapeidx fixes tps tps' :: "tape list"and zs :: "symbol list"and k :: nat assumes"z > 1"and"0 < j""j < k" and"proper_symbols zs" and"length tps = k" assumes"tps ! j = (⌊zs⌋, 1)" assumes"ttt = 3 * length zs + 5" assumes"tps' = tps[j := (⌊rstrip z zs⌋, 1)]" shows"transforms (tm_rstrip z j) tps ttt tps'" proof - interpret loc: turing_machine_rstrip z j . show ?thesis using assms loc.tm4' loc.tps4_def loc.tm4_eq_tm_rstrip by simp qed
subsection‹Writing arbitrary length sequences of the same symbol›
text‹
next Turing machine accepts a number $n$ on tape $j_1$ and writes the symbol
$z^n$ to tape $j_2$. The symbol $z$ is a parameter of the TM. The TM
the number on tape $j_1$ until it reaches zero. ›
definition tm_write_replicate :: "symbol ==> tapeidx ==> tapeidx ==> machine"where "tm_write_replicate z j1 j2 ≡ WHILE [] ; λrs. rs ! j1 ≠◻ DO tm_char j2 z ;; tm_decr j1 DONE ;; tm_cr j2"
lemma tm_write_replicate_tm: assumes"0 < j1"and"0 < j2"and"j1 < k"and"j2 < k"and"j1 ≠ j2"and"G ≥ 4"and"z < G" shows"turing_machine k G (tm_write_replicate z j1 j2)" unfolding tm_write_replicate_def using turing_machine_loop_turing_machine Nil_tm tm_char_tm tm_decr_tm tm_cr_tm assms by simp
locale turing_machine_write_replicate = fixes j1 j2 :: tapeidx and z :: symbol begin
lemma tmL1 [transforms_intros]: "transforms tm1 (tpsL t) 1 (tpsL1 t)" unfolding tm1_def proof (tform tps: tpsL_def tpsL1_def tps0 jk) have"tpsL t :#: j2 = Suc t" using tpsL1_def jk by (metis length_list_update nth_list_update_eq snd_conv tpsL_def) moreoverhave"tpsL t ::: j2 = ⌊replicate t z⌋" using tpsL1_def jk by (metis fst_conv length_list_update nth_list_update_eq tpsL_def) moreoverhave"⌊replicate t z⌋(Suc t := z) = ⌊replicate (Suc t) z⌋" using contents_snoc by (metis length_replicate replicate_Suc replicate_append_same) ultimatelyshow"tpsL1 t = (tpsL t)[j2 := tpsL t ! j2 |:=| z |+| 1]" unfolding tpsL1_def tpsL_def by simp qed
text‹
Section~\ref{s:tm-basic-pair} we defined a pairing function for strings. For
, $\langle\bbbI\bbbI, \bbbO\bbbO\rangle$ is first mapped to \bbbI\bbbI\#\bbbO\bbbO$ and ultimately represented as \bbbO\bbbI\bbbO\bbbI\bbbI\bbbI\bbbO\bbbO\bbbO\bbbO$. A Turing machine that is
compute a function for the argument $\langle\bbbI\bbbI, \bbbO\bbbO\rangle$
receive as input the symbols \textbf{0101110000}. Typically the TM would
extract the two components \textbf{11} and \textbf{00}. In this section we
TMs to do just that.
it happens, applying the quaternary alphabet decoding function @{const
} (see Section~\ref{s:tm-quaternary}) to such a symbol sequence gets us
to extracting the elements of the pair. For example, decoding
textbf{0101110000} yields @{text "11♯00"}, and now the TM only has to
the @{text ♯}.
Turing machine cannot rely on being given a well-formed pair. After decoding,
symbol sequence might have more or fewer than one @{text ♯} symbol or even
{text "∣"} symbols. The following functions @{term first} and @{term second}
designed to extract the first and second element of a symbol sequence
a pair, and for other symbol sequences at least allow for an
implementation. Implementations will come further down in this
. ›
definition first :: "symbol list ==> symbol list"where "first ys ≡ take (if ∃i<length ys. ys ! i ∈ {∣, ♯} then LEAST i. i < length ys ∧ ys ! i ∈ {∣, ♯} else length ys) ys"
definition second :: "symbol list ==> symbol list"where "second zs ≡ drop (Suc (length (first zs))) zs"
lemma firstD: assumes"∃i<length ys. ys ! i ∈ {∣, ♯}"and"m = (LEAST i. i < length ys ∧ ys ! i ∈ {∣, ♯})" shows"m < length ys"and"ys ! m ∈ {∣, ♯}"and"∀i<m. ys ! i ∉ {∣, ♯}" using LeastI_ex[OF assms(1)] assms(2) by simp_all (use less_trans not_less_Least in blast)
lemma firstI: assumes"m < length ys"and"ys ! m ∈ {∣, ♯}"and"∀i<m. ys ! i ∉ {∣, ♯}" shows"(LEAST i. i < length ys ∧ ys ! i ∈ {∣, ♯}) = m" using assms by (metis (mono_tags, lifting) LeastI less_linear not_less_Least)
lemma length_first_ex: assumes"∃i<length ys. ys ! i ∈ {∣, ♯}"and"m = (LEAST i. i < length ys ∧ ys ! i ∈ {∣, ♯})" shows"length (first ys) = m" proof - have"m < length ys" using assms firstD(1) by presburger moreoverhave"first ys = take m ys" using assms first_def by simp ultimatelyshow ?thesis by simp qed
lemma first_notex: assumes"¬ (∃i<length ys. ys ! i ∈ {∣, ♯})" shows"first ys = ys" using assms first_def by auto
lemma length_first: "length (first ys) ≤ length ys" using length_first_ex first_notex first_def by simp
lemma length_second_first: "length (second zs) = length zs - Suc (length (first zs))" using second_def by simp
lemma length_second: "length (second zs) ≤ length zs" using second_def by simp
text‹
next goal is to show that @{const first} and @{const second} really extract
first and second element of a pair. ›
lemma bindecode_bitenc: fixes x :: string shows"bindecode (string_to_symbols (bitenc x)) = string_to_symbols x" proof (induction x) case Nil thenshow ?case using less_2_cases_iff by force next case (Cons a x) have"bitenc (a # x) = bitenc [a] @ bitenc x" by simp thenhave"string_to_symbols (bitenc (a # x)) = string_to_symbols (bitenc [a] @ bitenc x)" by simp thenhave"string_to_symbols (bitenc (a # x)) = string_to_symbols (bitenc [a]) @ string_to_symbols (bitenc x)" by simp thenhave"bindecode (string_to_symbols (bitenc (a # x))) = bindecode (string_to_symbols (bitenc [a]) @ string_to_symbols (bitenc x))" by simp alsohave"... = bindecode (string_to_symbols (bitenc [a])) @ bindecode (string_to_symbols (bitenc x))" using bindecode_append length_bitenc by (metis (no_types, lifting) dvd_triv_left length_map) alsohave"... = bindecode (string_to_symbols (bitenc [a])) @ string_to_symbols x" using Cons by simp alsohave"... = string_to_symbols [a] @ string_to_symbols x" using bindecode_def by simp alsohave"... = string_to_symbols ([a] @ x)" by simp alsohave"... = string_to_symbols (a # x)" by simp finallyshow ?case . qed
lemma bindecode_string_pair: fixes x u :: string shows"bindecode ⟨x; u⟩ = string_to_symbols x @ [♯] @ string_to_symbols u" proof - have"bindecode ⟨x; u⟩ = bindecode (string_to_symbols (bitenc x @ [True, True] @ bitenc u))" using string_pair_def by simp alsohave"... = bindecode (string_to_symbols (bitenc x) @ string_to_symbols [𝕀 , 𝕀 ] @ string_to_symbols (bitenc u))" by simp alsohave"... = bindecode (string_to_symbols (bitenc x)) @ bindecode (string_to_symbols [𝕀 , 𝕀 ]) @ bindecode (string_to_symbols (bitenc u))" proof - have"even (length (string_to_symbols [True, True]))" by simp moreoverhave"even (length (string_to_symbols (bitenc y)))"for y by (simp add: length_bitenc) ultimatelyshow ?thesis using bindecode_append length_bindecode length_bitenc by (smt (verit) add_mult_distrib2 add_self_div_2 dvd_triv_left length_append length_map mult_2) qed alsohave"... = string_to_symbols x @ bindecode (string_to_symbols [𝕀 , 𝕀 ]) @ string_to_symbols u" using bindecode_bitenc by simp alsohave"... = string_to_symbols x @ [♯] @ string_to_symbols u" using bindecode_def by simp finallyshow ?thesis . qed
lemma first_pair: fixes ys :: "symbol list"and x u :: string assumes"ys = bindecode ⟨x; u⟩" shows"first ys = string_to_symbols x" proof - have ys: "ys = string_to_symbols x @ [♯] @ string_to_symbols u" using bindecode_string_pair assms by simp have bs: "bit_symbols (string_to_symbols x)" by simp have"ys ! (length (string_to_symbols x)) = ♯" using ys by (metis append_Cons nth_append_length) thenhave ex: "ys ! (length (string_to_symbols x)) ∈ {∣, ♯}" by simp have"(LEAST i. i < length ys ∧ ys ! i ∈ {∣, ♯}) = length (string_to_symbols x)" using ex ys bs by (intro firstI) (simp_all add: nth_append) moreoverhave"length (string_to_symbols x) < length ys" using ys by simp ultimatelyhave"first ys = take (length (string_to_symbols x)) ys" using ex first_def by auto thenshow"first ys = string_to_symbols x" using ys by simp qed
lemma second_pair: fixes ys :: "symbol list"and x u :: string assumes"ys = bindecode ⟨x; u⟩" shows"second ys = string_to_symbols u" proof - have ys: "ys = string_to_symbols x @ [♯] @ string_to_symbols u" using bindecode_string_pair assms by simp let ?m = "length (string_to_symbols x)" have"length (first ys) = ?m" using assms first_pair by presburger moreoverhave"drop (Suc ?m) ys = string_to_symbols u" using ys by simp ultimatelyhave"drop (Suc (length (first ys))) ys = string_to_symbols u" by simp thenshow ?thesis using second_def by simp qed
subsubsection‹A Turing machine for extracting the first element›
text‹
most other Turing machines, the one in this section is not meant to be
, but rather to compute a function, namely the function @{const first}.
this reason there are no tape index parameters. Instead, the encoded pair is
on the input tape, and the output is written to the output tape.
null ›
lemma bit_symbols_first: assumes"ys = bindecode (string_to_symbols x)" shows"bit_symbols (first ys)" proof (cases "∃i<length ys. ys ! i ∈ {∣, ♯}") case True define m where"m = (LEAST i. i < length ys ∧ ys ! i ∈ {∣, ♯})" thenhave m: "m < length ys""ys ! m ∈ {∣, ♯}""∀i<m. ys ! i ∉ {∣, ♯}" using firstD[OF True] by blast+ have len: "length (first ys) = m" using length_first_ex[OF True] m_def by simp have"bit_symbols (string_to_symbols x)" by simp thenhave"∀i<length ys. ys ! i ∈ {2..<6}" using assms bindecode2345 by simp thenhave"∀i<m. ys ! i ∈ {2..<6}" using m(1) by simp thenhave"∀i<m. ys ! i ∈ {2..<4}" using m(3) by fastforce thenshow ?thesis using first_def len by auto next case False thenhave1: "∀i<length ys. ys ! i ∉ {∣, ♯}" by simp have"bit_symbols (string_to_symbols x)" by simp thenhave"∀i<length ys. ys ! i ∈ {2..<6}" using assms bindecode2345 by simp thenhave"∀i<length ys. ys ! i ∈ {2..<4}" using1by fastforce thenshow ?thesis using False first_notex by auto qed
lemma tm_first_tm: "G ≥ 6 ==> k ≥ 3 ==> turing_machine k G tm_first" unfolding tm_first_def using tm_cp_until_tm tm_start_tm tm_bindecode_tm tm_right_many_tm by simp
locale turing_machine_fst_pair = fixes k :: nat assumes k: "k ≥ 3" begin
lemma tm3: assumes"ttt = 8 + 3 * length xs + Suc (length (first (bindecode xs)))" shows"transforms tm3 tps0 ttt tps3" unfolding tm3_def proof (tform tps: k tps2_def time: assms) let ?ys = "bindecode xs" have tps2: "tps2 ! 2 = (⌊?ys⌋, 1)" using tps2_def k by simp show"rneigh (tps2 ! 2) {◻, ∣, ♯} (length (first ?ys))" proof (cases "∃i<length ?ys. ?ys ! i ∈ {∣, ♯}") case ex5: True define m where"m = (LEAST i. i < length ?ys ∧ ?ys ! i ∈ {∣, ♯})" thenhave m: "m = length (first ?ys)" using length_first_ex ex5 by simp show ?thesis proof (rule rneighI) have"?ys ! m ∈ {∣, ♯}" using firstD m_def ex5 by blast thenshow"(tps2 ::: 2) (tps2 :#: 2 + length (first ?ys)) ∈ {◻, ∣, ♯}" using m tps2 contents_def by simp show"(tps2 ::: 2) (tps2 :#: 2 + i) ∉ {◻, ∣, ♯}"if"i < length (first ?ys)"for i proof - have"m < length ?ys" using ex5 firstD(1) length_first_ex m by blast thenhave"length (first ?ys) < length ?ys" using m by simp thenhave"i < length ?ys" using that by simp thenhave"?ys ! i ≠ 0" using proper_bindecode by fastforce moreoverhave"?ys ! i ∉ {∣, ♯}" using ex5 firstD(3) length_first_ex that by blast ultimatelyshow ?thesis using Suc_neq_Zero ‹i < length (bindecode xs)› tps2 by simp qed qed next case notex5: False thenhave ys: "?ys = first ?ys" using first_notex by simp show ?thesis proof (rule rneighI) show"(tps2 ::: 2) (tps2 :#: 2 + length (first ?ys)) ∈ {◻, ∣, ♯}" using ys tps2 by simp show"(tps2 ::: 2) (tps2 :#: 2 + i) ∉ {◻, ∣, ♯}"if"i < length (first ?ys)"for i using notex5 that ys proper_bindecode contents_inbounds by (metis Suc_leI add_gr_0 diff_Suc_1 fst_conv gr_implies_not0 insert_iff
plus_1_eq_Suc snd_conv tps2 zero_less_one) qed qed show"tps3 = tps2[2 := tps2 ! 2 |+| length (first ?ys), 1 := implant (tps2 ! 2) (tps2 ! 1) (length (first ?ys))]"
(is"_ = ?tps") proof - have0: "tps3 ! 0 = ?tps ! 0" using tps2_def tps3_def by simp have1: "tps3 ! 2 = ?tps ! 2" using tps2_def tps3_def k by simp have lentps2: "length tps2 > 2" using k tps2_def by simp have"implant (tps2 ! 2) (tps2 ! 1) (length (first ?ys)) = (⌊first ?ys⌋, Suc (length (first ?ys)))" proof - have len: "length (first ?ys) ≤ length ?ys" using first_def by simp have"tps2 ! 1 = (⌊[]⌋, 1)" using tps2_def lentps2 by simp thenhave"implant (tps2 ! 2) (tps2 ! 1) (length (first ?ys)) = implant (⌊?ys⌋, 1) (⌊[]⌋, 1) (length (first ?ys))" using tps2 by simp alsohave"... = (⌊take (length (first ?ys)) ?ys⌋, Suc (length (first ?ys)))" using implant_contents[of 1"length (first ?ys)" ?ys "[]"] len by simp alsohave"... = (⌊first ?ys⌋, Suc (length (first ?ys)))" using first_def using first_notex length_first_ex by presburger finallyshow ?thesis . qed moreoverhave"length tps2 > 2" using k tps2_def by simp ultimatelyshow ?thesis using01 tps2_def tps3_def tps0_def lentps k tps2 by (smt (verit) length_list_update list_update_overwrite list_update_swap nth_list_update) qed qed
lemma tm3': assumes"ttt = 9 + 4 * length xs" shows"transforms tm3 tps0 ttt tps3" proof - let ?t = "8 + 3 * length xs + Suc (length (first (bindecode xs)))" have"?t ≤ 8 + 3 * length xs + Suc (length (bindecode xs))" using length_first by (meson Suc_le_mono add_le_mono order_refl) alsohave"... ≤ 8 + 3 * length xs + Suc (length xs)" using length_bindecode by simp alsohave"... = 9 + 3 * length xs + length xs" by simp alsohave"... = 9 + 4 * length xs" by simp finallyhave"?t ≤ ttt" using assms(1) by simp moreoverhave"transforms tm3 tps0 ?t tps3" using tm3 by simp ultimatelyshow ?thesis using transforms_monotone by simp qed
end(* context tps *)
lemma tm3_computes: "computes_in_time k tm3 (λx. symbols_to_string (first (bindecode (string_to_symbols x)))) (λn. 9 + 4 * n)" proof - define f where"f = (λx. symbols_to_string (first (bindecode (string_to_symbols x))))" define T :: "nat ==> nat"where"T = (λn. 9 + 4 * n)" have"computes_in_time k tm3 f T" proof fix x :: string let ?xs = "string_to_symbols x" have bs: "bit_symbols ?xs" by simp define tps where"tps = tps3 ?xs" have trans: "transforms tm3 (tps0 ?xs) (9 + 4 * length ?xs) tps" using bs tm3' tps_def by blast have"tps3 ?xs ::: 1 = ⌊first (bindecode ?xs)⌋" using bs tps3_def k by simp moreoverhave"bit_symbols (first (bindecode ?xs))" using bit_symbols_first by simp ultimatelyhave"tps3 ?xs ::: 1 = string_to_contents (symbols_to_string (first (bindecode ?xs)))" using bit_symbols_to_symbols contents_string_to_contents by simp thenhave *: "tps ::: 1 = string_to_contents (f x)" using tps_def f_def by auto thenhave"transforms tm3 (snd (start_config k (string_to_symbols x))) (T (length x)) tps" using trans T_def tps0_def by simp thenshow"∃tps. tps ::: 1 = string_to_contents (f x) ∧ transforms tm3 (snd (start_config k (string_to_symbols x))) (T (length x)) tps" using * by auto qed thenshow ?thesis using f_def T_def by simp qed
end(* locale turing_machine_fst_pair *)
lemma tm_first_computes: assumes"k ≥ 3" shows"computes_in_time k tm_first (λx. symbols_to_string (first (bindecode (string_to_symbols x)))) (λn. 9 + 4 * n)" proof - interpret loc: turing_machine_fst_pair k using turing_machine_fst_pair.intro assms by simp show ?thesis using loc.tm3_eq_tm_first loc.tm3_computes by simp qed
subsubsection‹A Turing machine for splitting pairs›
text‹
next Turing machine expects a proper symbol sequence @{term zs} on tape
j_1$ and outputs @{term "first zs"} and @{term "second zs"} on tapes $j_2$ and
j_3$, respectively. ›
lemma tm1 [transforms_intros]: assumes"ttt = Suc (length (first zs))" shows"transforms tm1 tps0 ttt tps1" unfolding tm1_def proof (tform tps: assms tps0 tps1_def jk) let ?n = "length (first zs)" have *: "tps0 ! j1 = (⌊zs⌋, 1)" using tps0 jk by simp show"rneigh (tps0 ! j1) {◻, ∣, ♯} (length (first zs))" proof (cases "∃i<length zs. zs ! i ∈ {∣, ♯}") case ex5: True define m where"m = (LEAST i. i < length zs ∧ zs ! i ∈ {∣, ♯})" thenhave m: "m = length (first zs)" using length_first_ex ex5 by simp show ?thesis proof (rule rneighI) have"zs ! m ∈ {∣, ♯}" using firstD m_def ex5 by blast thenshow"(tps0 ::: j1) (tps0 :#: j1 + length (first zs)) ∈ {◻, ∣, ♯}" using m * contents_def by simp show"(tps0 ::: j1) (tps0 :#: j1 + i) ∉ {◻, ∣, ♯}"if"i < length (first zs)"for i proof - have"m < length zs" using ex5 firstD(1) length_first_ex m by blast thenhave"length (first zs) < length zs" using m by simp thenhave"i < length zs" using that by simp thenhave"zs ! i ≠◻" using zs by fastforce moreoverhave"zs ! i ∉ {∣, ♯}" using ex5 firstD(3) length_first_ex that by blast ultimatelyshow ?thesis using Suc_neq_Zero `i < length zs` * by simp qed qed next case notex5: False thenhave ys: "zs = first zs" using first_notex by simp show ?thesis proof (rule rneighI) show"(tps0 ::: j1) (tps0 :#: j1 + length (first zs)) ∈ {◻, ∣, ♯}" using ys * by simp show"(tps0 ::: j1) (tps0 :#: j1 + i) ∉ {◻, ∣, ♯}"if"i < length (first zs)"for i using notex5 that ys proper_bindecode contents_inbounds * zs by auto qed qed
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.