-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.ml
306 lines (273 loc) · 9.29 KB
/
interpreter.ml
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
type variable = string
type symbol = string
type signature = (symbol * int) list
type term = V of variable | Num of int | Node of symbol * (term list)
type atom = A of symbol * (term list)
type head = H of atom
type body = B of atom list
type clause = F of head | R of head * body
type program = clause list
type goal = G of atom list
type substitution = (variable * term) list
exception NOT_UNIFIABLE
exception NotFound
exception InvalidProgram
exception NotPossible
let rec exists x y = match y with
[] -> false
| z::ys -> (x = z) || (exists x ys)
;;
let rec foldl f e l = match l with
[] -> e
| x::xs -> foldl f (f e x) xs
;;
let rec map f l = match l with
[] -> []
| x::xs -> (f x)::map f xs
;;
let rec combine l1 l2 = match l1 with
[] -> []
| x::xs -> (x, (List.hd l2))::combine xs (List.tl l2)
;;
let rec union l1 l2 = match l1 with
[] -> l2
| x::xs -> if (exists x l2) then union xs l2
else x::(union xs l2)
;;
let rec checkProgram (prog:program): bool = match prog with
[] -> true
| (F(H(a)))::xs | (R(H(a), _))::xs -> match a with
A("_eq", _) | A("_not_eq", _) | A("_cut", _)
| A(">", _) | A("<", _)-> raise InvalidProgram
| _ -> checkProgram xs
;;
let rec modifyTerm (i:int) (t:term): term = match t with
V(v) -> V((string_of_int i) ^ v)
| Node(s, l) -> Node(s, map (modifyTerm i) l)
| _ -> t
;;
let rec modifyAtom (i:int) (a:atom): atom = match a with
A(s, l) -> A(s, map (modifyTerm i) l)
;;
let rec modifyClause (cl:clause) (i:int): clause = match cl with
F(H(a)) -> F(H(modifyAtom i a))
| R(H(a), B(l)) -> R(H(modifyAtom i a), B(map (modifyAtom i) l))
;;
let rec modifyInitialProg (prog:program) (i:int): program = match prog with
[] -> []
| cl::ps -> (modifyClause cl i)::modifyInitialProg ps (i+1)
;;
let rec modifyProg2 (prog:program) (A(s, _): atom): program = match prog with
[] -> []
| cl::ps -> match cl with F(H(A(s', _))) | R(H(A(s', _)), _) ->
if s = s' then (modifyClause cl 0)::modifyProg2 ps (A(s, []))
else cl::modifyProg2 ps (A(s, []))
;;
let rec vars_term (t:term): variable list =
match t with
V(v) -> [v]
| Node(s, l) -> foldl union [] (map vars_term l)
| _ -> []
;;
let vars_atom (A(s, l): atom): variable list = vars_term (Node(s, l))
;;
let rec vars_goal (G(g): goal): variable list = foldl union [] (map vars_atom g)
;;
let rec subst (s:substitution) (t:term): term =
match t with
Node(s', l) -> Node(s', map (subst s) l)
| Num(_) -> t
| V(x) -> match s with
[] -> t
| s'::xs -> if fst s' = x then snd s' else subst xs t
;;
let rec subst_atom (s:substitution) (A(s', l): atom): atom = A(s', map (subst s) l)
;;
let rec variableInTerm (v:variable) (t:term): bool =
match t with
V(x) -> x = v
| Node(s, l) -> foldl (||) false (map (variableInTerm v) l)
| _ -> false
;;
let compose (s1:substitution) (s2:substitution): substitution =
let f s x = (fst x, subst s (snd x)) in (map (f s2) s1) @ s2
;;
let rec mgu_term (t1:term) (t2:term): substitution =
match (t1, t2) with
(V(x), V(y)) -> if x = y then []
else [(x, V(y))]
| (V(x), Node(_, _)) -> if variableInTerm x t2 then raise NOT_UNIFIABLE
else [(x, t2)]
| (Node(_, _), V(y)) -> if variableInTerm y t1 then raise NOT_UNIFIABLE
else [(y, t1)]
| (Num(n1), Num(n2)) -> if n1 = n2 then [] else raise NOT_UNIFIABLE
| (Num(n1), V(x)) -> [(x, t1)]
| (V(x), Num(n2)) -> [(x, t2)]
| (Node(s1, l1), Node(s2, l2)) ->
if s1 <> s2 || (List.length l1 <> List.length l2) then raise NOT_UNIFIABLE
else
let f s tt = compose s (mgu_term (subst s (fst tt)) (subst s (snd tt))) in
foldl f [] (combine l1 l2)
| _ -> raise NOT_UNIFIABLE
;;
let mgu_atom (A(s1, l1): atom) (A(s2, l2): atom): substitution = mgu_term (Node(s1, l1)) (Node(s2, l2))
;;
let rec print_term_list (tl:term list) = match tl with
[] -> Printf.printf ""
| [t] -> print_term t
| t::tls -> (
print_term t;
Printf.printf ",";
print_term_list tls;
)
and print_list_body (t:term) = match t with
Node("_empty_list", []) -> Printf.printf ""
| Node("_list", [t1; Node("_empty_list", [])]) -> print_term t1
| Node("_list", [t1; t2]) -> (
print_term t1;
Printf.printf ",";
print_list_body t2;
)
| _ -> raise NotPossible
and print_term (t:term) = match t with
V(v) -> Printf.printf " %s " v
| Node("_empty_list", []) -> Printf.printf " [] "
| Node(s, []) -> Printf.printf " %s " s
| Node("_list", _) -> (
Printf.printf " [";
print_list_body t;
Printf.printf "] ";
)
| Node(s, l) -> (
Printf.printf " %s ( " s;
print_term_list l;
Printf.printf " ) ";
)
| Num(n) -> Printf.printf " %d " n
;;
let rec getSolution (unif:substitution) (vars:variable list) = match vars with
[] -> []
| v::vs ->
let rec occurs l = match l with
[] -> raise NotFound
| x::xs -> if (fst x) = v then x
else occurs xs
in
try (occurs unif)::getSolution unif vs
with NotFound -> getSolution unif vs
;;
let get1char () =
let termio = Unix.tcgetattr Unix.stdin in
let () = Unix.tcsetattr Unix.stdin Unix.TCSADRAIN
{ termio with Unix.c_icanon = false } in
let res = input_char stdin in
Unix.tcsetattr Unix.stdin Unix.TCSADRAIN termio;
res
let rec printSolution (unif:substitution) = match unif with
[] -> Printf.printf "true. "
| [(v, t)] -> (
Printf.printf "%s =" v;
print_term t;
)
| (v, t)::xs -> (
Printf.printf "%s =" v;
print_term t;
Printf.printf ", ";
printSolution xs;
)
;;
let solve_atom_atom (a1:atom) (a2:atom) (unif:substitution): substitution =
compose unif (mgu_atom (subst_atom unif a1) (subst_atom unif a2))
;;
let solve_term_term (t1:term) (t2:term) (unif:substitution): substitution =
compose unif (mgu_term (subst unif t1) (subst unif t2))
;;
let rec simplify_term (t:term): term = match t with
Num(_) -> t
| Node("+", [t1; t2]) -> (
match ((simplify_term t1), (simplify_term t2)) with
(Num(n1), Num(n2)) -> Num(n1 + n2)
| _ -> raise NOT_UNIFIABLE
)
| Node("-", [t1; t2]) -> (
match ((simplify_term t1), (simplify_term t2)) with
(Num(n1), Num(n2)) -> Num(n1 - n2)
| _ -> raise NOT_UNIFIABLE
)
| Node("*", [t1; t2]) -> (
match ((simplify_term t1), (simplify_term t2)) with
(Num(n1), Num(n2)) -> Num(n1 * n2)
| _ -> raise NOT_UNIFIABLE
)
| Node("/", [t1; t2]) -> (
match ((simplify_term t1), (simplify_term t2)) with
(Num(n1), Num(n2)) -> Num(n1 / n2)
| _ -> raise NOT_UNIFIABLE
)
| _ -> t
;;
let eval (a:atom) (unif:substitution): substitution = match a with
A("_eq", [t1; t2])
| A("_not_eq", [t1; t2]) -> compose unif (mgu_term (simplify_term (subst unif t1)) (simplify_term (subst unif t2)))
| A(">", [t1; t2]) -> (
match (simplify_term (subst unif t1), simplify_term (subst unif t2)) with
(Num(n1), Num(n2)) -> if n1 > n2 then unif else raise NOT_UNIFIABLE
| _ -> raise NOT_UNIFIABLE
)
| A("<", [t1; t2]) -> (
match (simplify_term (subst unif t1), simplify_term (subst unif t2)) with
(Num(n1), Num(n2)) -> if n1 < n2 then unif else raise NOT_UNIFIABLE
| _ -> raise NOT_UNIFIABLE
)
| _ -> unif
;;
let rec solve_goal (prog:program) (g:goal) (unif:substitution) (vars:variable list): (bool * substitution) =
match g with
G([]) -> (
printSolution (getSolution unif vars);
flush stdout;
let choice = ref (get1char()) in
while(!choice <> '.' && !choice <> ';') do
Printf.printf "\nUnknown Action: %c \nAction? " (!choice);
flush stdout;
choice := get1char();
done;
Printf.printf "\n";
if !choice = '.' then (true, [])
else (false, [])
)
| G(a::gs) -> match a with
A("_eq", _) | A(">", _) | A("<", _) -> (
try solve_goal prog (G(gs)) (eval a unif) vars
with NOT_UNIFIABLE -> (false, [])
)
| A("_not_eq", _) -> (
try (false, eval a unif)
with NOT_UNIFIABLE -> solve_goal prog (G(gs)) unif vars
)
| A("_cut", _) -> let _ = solve_goal prog (G(gs)) unif vars in (true, [])
| _ ->
let new_prog = modifyProg2 prog a in
let rec iter prog' = match prog' with
[] -> (false, [])
| cl::ps -> match cl with
F(H(a')) -> (
try
let u = (solve_atom_atom a' a unif) in
match (solve_goal new_prog (G(gs)) u vars) with
(true, u') -> (true, u')
| _ -> iter ps
with NOT_UNIFIABLE -> iter ps
)
| R(H(a'), B(al)) -> (
try
let u = (solve_atom_atom a' a unif) in
match (solve_goal new_prog (G(al @ gs)) u vars) with
(true, u') -> (true, u')
| _ -> iter ps
with NOT_UNIFIABLE -> iter ps
)
in iter prog
;;
let interpret_goal (prog:program) (g:goal) = solve_goal prog g [] (vars_goal g)
;;