ALP I Turing-Maschine Simulator WS 2012/2013
Haskell-Implementierung eines TM-Simulators module Turing (sim, start) data Move = L R S deriving (Show, Eq) type State = Int type Input = Char type Output = Char type Tape = ([Char], Char, [Char]) type StateTable = [(State, Input, Output, Move, State)] type History = [(([Char], Char, [Char]), State)]
Haskell-Implementierung eines TM-Simulators step :: (Tape, State) -> StateTable -> (Tape, State) step ((l, input, r), state) s_table state==0 = ((l, input, r), state) otherwise = ((new_l, new_input, new_r), n_state) (out, dir, n_state) = find_next (state, input) s_table (new_l, new_input, new_r) dir==l = move_left (l, out, r) Die step-funktion berechnet nur einen Zustandsübergang der simulierten TM dir==r = move_right (l, out, r) dir==s = (l, out, r)
{-- --} Haskell-Implementierung eines TM-Simulators Ein Zustand für entsprechende Eingabe des Lesekopfs wird in der Zustandstabelle gesucht und die Ausgabe und der Folgezustand werden ermittelt. find_next :: (State, Input) -> StateTable -> (Output, Move, State) find_next (state, input) [] = error "END OF STATE-TABLE and not Match founded" find_next (state, input) ((s, i, out, dir, ns):rest) (state==s && input==i) = (out, dir, ns) (state==s && '#' == i) = (input, dir, ns) otherwise = find_next (state, input) rest
Haskell-Implementierung einer TM-Simulation -- Die Position des Lesekopfs wird hier aktualisiert. move_left :: Tape -> Tape move_left (l, h, r) new_h == '_' = error "left margin reached" otherwise = (tail l, new_h, h:r) new_h = head l move_right :: Tape -> Tape move_right (l, h, r) new_h == '_' = error "right margin reached" otherwise = (h:l, new_h, tail r) new_h = head r
Haskell-Implementierung einer TM-Simulation {-- Die sim-funktion startet die Simulation der Maschine nach Eingabe einer Start-Konfiguration und einer Zustandstabelle. Eine Liste mit allen Zwischenzuständen wird als Ergebnis zurückgegeben. --} sim :: (Tape, State) -> StateTable -> History -> History sim ((l, input, r), 0) s_table history = history sim ((l, input, r), state) s_table history = sim new_tm s_table (new_tm:history) new_tm = step ((l,input,r), state) s_table
Haskell-Implementierung einer TM-Simulation -- Hilfsfunktionen show_states [] = "" show_states (x:xs) = (show_states xs)++" "++show_state(x) show_state ((l,input,r), state) = " "++rev_l++[input]++(cut_tape r)++" "++(show state) ++"\n" ++(spaces (4 + (length rev_l) ))++"T\n" rev_l = reverse_finite l [] spaces n = [' ' _<-[1..n]] cut_tape ('_':x) = [] cut_tape (x:y) = x:(cut_tape y) reverse_finite ('_':x) y = y reverse_finite (x:xs) y = reverse_finite xs (x:y)
Haskell-Implementierung einer TM-Simulation -- Hilfsfunktionen -- Simuliert eine unendliche leere Liste empty_tape :: [Char] empty_tape = '_':empty_tape -- Die Simulation wird gestartet und alle Zwischenzustände ausgegeben start ((l,input,r), state, steps) s_table = putstr (show_states (sim start_config s_table [start_config])) l1 = reverse l ++ empty_tape r1 = r ++ empty_tape start_config = ( (l1, input, r1), state )
Turing-Maschinen weitere Beispiele an der Tafel...