Download code

Jump to: navigation, search

Back to Turing_machine_simulator_(OCaml)

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

test_driver.ml

 1 (* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Turing_machine_simulator_(OCaml)?oldid=13915
14 *)
15 
16 open Simulate_turing_machine;;
17 
18 let test_states = Hashtbl.create 10;;
19 Hashtbl.add test_states (0,'#') (4,'#',Right);;
20 Hashtbl.add test_states (0,'a') (1,'#',Right);;
21 Hashtbl.add test_states (4,'#') (5,'#',Right);;
22 Hashtbl.add test_states (1,'a') (1,'a',Right);;
23 Hashtbl.add test_states (1,'b') (1,'b',Right);;
24 Hashtbl.add test_states (1,'#') (2,'#',Left);;
25 Hashtbl.add test_states (2,'b') (3,'#',Left);;
26 Hashtbl.add test_states (3,'a') (3,'a',Left);;
27 Hashtbl.add test_states (3,'b') (3,'b',Left);;
28 Hashtbl.add test_states (3,'#') (0,'#',Right);;
29 
30 let test_anbn_trans_func state symbol =
31   find_trans_state test_states state symbol '#'
32 
33 let anbn_test initial_tape =
34   simulate initial_tape (Some 0) 0 test_anbn_trans_func [5] '#';;
35 
36 anbn_test ['a'; 'b'];;


hijacker
hijacker
hijacker
hijacker

simulate_turing_machine.ml

 1 (* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Turing_machine_simulator_(OCaml)?oldid=13915
14 *)
15 
16 type dir = Left | Right
17 
18 let trace_tape_chars = 78
19 
20 let symbol_at tape position blank_symbol =
21   if position < 0 then
22     failwith "Invalid tape position"
23   else if position >= List.length tape then
24     blank_symbol
25   else
26     List.nth tape position
27 let replace_symbol tape position symbol =
28   let rec replace_helper (x::xs) n acc =
29     if n = 0 then
30       List.rev acc @ symbol :: xs
31     else
32       replace_helper xs (pred n) (x :: acc)
33   in
34   replace_helper tape position []
35 let find_trans_state states state symbol blank_symbol =
36   try
37     let state, symbol, dir = Hashtbl.find states (state, symbol) in
38     Some state, symbol, dir
39   with Not_found ->
40     None, blank_symbol, Left
41 let write_to_tape tape position symbol =
42   let l = List.length tape in
43   if position < 0 || position > l then
44     failwith "Invalid tape position"
45   else if position = l then
46     tape @ [symbol]
47   else
48     replace_symbol tape position symbol
49 let trace_state tape head_position blank_symbol =
50   let print_n_times s n =
51     for i = 1 to n do
52       print_char s
53     done in
54   let rec print_tape tape n =
55     match tape, n with
56        _, 0
57      | [], _ -> ()
58      | x :: xs, _ -> print_char x;
59                      print_tape xs (pred n)
60     in
61   if head_position < trace_tape_chars then begin
62     print_n_times ' ' head_position;
63     print_endline "v"
64   end;
65   print_tape tape trace_tape_chars;
66   print_n_times blank_symbol (max 0 (trace_tape_chars - List.length tape));
67   print_newline ()
68 let rec simulate tape state head_position transition_func accepting_states blank_symbol =
69   trace_state tape head_position blank_symbol
70 ;
71   match state with
72    | None -> false
73    | Some s when List.mem s accepting_states -> true
74    | Some s ->
75       let symbol = symbol_at tape head_position blank_symbol@ text
76  in
77       let newstate, newsymbol, movedir = transition_func s symbol in
78       simulate (write_to_tape tape head_position newsymbol@ text
79 )
80                newstate
81                (match movedir with
82 		 | Left -> pred head_position
83 		 | Right -> succ head_position@ text
84 )
85                transition_func
86                accepting_states
87                blank_symbol@ text
88 


hijacker
hijacker
hijacker
hijacker