/
turing_machine.hs
169 lines (135 loc) · 5 KB
/
turing_machine.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
{-- Práctica 3
-- Autómatas y Lenguajes Formales
-- 2017-2
-- Prof: Noé Salomón Hernández Sánchez
-- Ayudante: Albert Manuel Orozco Camacho
-- Ayudante: Cenobio Moisés Vázquez Reyes
-- Version: 0.1
-- Author1: AlOrozco53
-- Author2: <NOMBRE_DE_LOS_INTEGRANTES_DEL_EQUIPO>
--}
module TM where
-- | Basic types
type Simbolo = Char
type Estado = [Char]
type Alfabeto = [Simbolo]
-- | Ejercicio 1, TM motion values
-- data Movimiento = ... deriving (Show, Eq)
-- | Ejercicio 2, transition function type alias
-- type Delta = ...
-- | Turing machine structure
data MaqT = MaqT { q :: [Estado], -- conjunto de estados
q0 :: Estado, -- estado inicial
qf :: Estado, -- estado de aceptación
qr :: Estado, -- estado de rechazo
s :: Alfabeto, -- alfabeto de entrada
g :: Alfabeto -- alfabeto de cinta
} deriving Show
-- | Turing machine definition
data MT = MT { mtupla :: MaqT, -- Turing machine tuple structure
dltfun :: Delta -- transition function
}
-- | Show functions for standard Turing Machines
pintaEstados :: [Estado] -> String
pintaEstados [] = ""
pintaEstados (l:le) = l ++ " " ++ pintaEstados le
pintaEstado :: Estado -> String
pintaEstado e = e ++ ""
pintaAlfabeto :: Alfabeto -> String
pintaAlfabeto [] = ""
pintaAlfabeto (l:la) = [l] ++ " " ++ pintaAlfabeto la
-- | Auxiliar function to produce the cross product between
-- | a set of states and an alphabet
generaPares :: [Estado] -> Alfabeto -> [(Estado, Simbolo)]
generaPares le ls = [(e, s)| e <- le, s <- ls]
-- | Show functions for a TM delta function
pintaDeltaAux :: Delta -> [(Estado, Simbolo)] -> String
pintaDeltaAux _ [] = "\n"
pintaDeltaAux t ((e, s):xs) =
case t e s of
(e', s', m) -> " d " ++
e ++ " " ++ [s] ++ " = " ++ " " ++e' ++
" " ++ [s'] ++ " " ++ show m ++ "\n" ++ pintaDeltaAux t xs
pintaDelta :: [Estado] -> Alfabeto -> Delta -> String
pintaDelta le ls t = pintaDeltaAux t (generaPares le ls)
instance Show MT where
show mt = "\nEstados:: "
++ pintaEstados (estados mt) ++ "\n" ++
"\nEstado Inicial:: "
++ pintaEstado (estadoInicial mt) ++ "\n" ++
"\nEstado de Aceptación:: "
++ pintaEstado (estadoAcept mt) ++ "\n" ++
"\nEstado de Rechazo:: "
++ pintaEstado (estadoRechazo mt) ++ "\n" ++
"\nAlfabeto de Entrada:: "
++ pintaAlfabeto (sigma mt) ++ "\n" ++
"\nAlfabeto de la Cinta:: "
++ pintaAlfabeto (gamma mt) ++ "\n" ++
"\nFunción de Transición::\n"
++ pintaDelta (estados mt) (gamma mt) d
where
d = funTransicion mt
-- | Gets the input alphabet from a Turing Machine
sigma :: MT -> Alfabeto
sigma = s . mtupla
-- | Gets the tape alphabet from a Turing Machine
gamma :: MT -> Alfabeto
gamma = g . mtupla
-- | Gets the state set from a Turing Machine
estados :: MT -> [Estado]
estados = q . mtupla
-- | Gets the initial state from a Turing Machine
estadoInicial :: MT -> Estado
estadoInicial = q0 . mtupla
-- | Gets the accepting state from a Turing Machine
estadoAcept :: MT -> Estado
estadoAcept = qf . mtupla
-- | Gets the rejecting state from a Turing Machine
estadoRechazo :: MT -> Estado
estadoRechazo = qr . mtupla
-- | Gets the transition function
funTransicion :: MT -> Delta
funTransicion = dltfun
-- | Ejercicio 3, TM that accepts binary strings with even number of 0's
tmPares :: MT
tmPares = error "to be implemented..."
-- | Ejercicio 4, TM for a^n b^n c^n
tmanbncn :: MT
tmanbncn = error "to be implemented..."
-- | Configuration-useful types
type Cadena = [Simbolo]
type Configuracion = (Estado, Cadena, Int)
-- | This function takes a string str, an integer i, and a symbol s
-- | and substitutes s in the i-th position of str
sustituye :: Cadena -> Int -> Simbolo -> Cadena
sustituye [] _ _ = []
sustituye (w:ws) 0 a = a:ws
sustituye (w:ws) n a
| n < 0 = error "invalid index!"
| otherwise = w: sustituye ws (n-1) a
-- | Ejercicio 5, delta function
delta :: Delta -> Configuracion -> Configuracion
delta = error "to be implemented..."
-- | Closure for delta function
deltaEstrella :: MT -> Configuracion -> Bool
deltaEstrella mt (q, w, n)
| q == estadoAcept mt = True
| q == estadoRechazo mt = False
| otherwise = deltaEstrella mt (delta d (q,w,n))
where
d = funTransicion mt
-- | Ejercicio 6, decides if the given string is accepted by the given TM
aceptaCadena :: MT -> Cadena -> Bool
aceptaCadena = error "to be implemented..."
-- | Auxiliar for Kleene star operation
kln :: Alfabeto -> Int -> [Cadena]
kln s 0 = [""]
kln s n
| n < 0 = error "invalid index!"
| otherwise = [a:w | a <- s , w <- kln s (n-1)]
-- | Kleene star of an alphabet
klns :: Alfabeto -> [Cadena]
klns s = concat [kln s n | n <- [0..] ]
-- | Ejercicio 7, lazy construction of the language accepted by a TM
lenguajeAceptado :: MT -> [Cadena]
lenguajeAceptado = error "to be implemented..."