-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.ml
279 lines (251 loc) · 8.5 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
type var = string
type symbol = string
type signature = (symbol * int) list
type term =
V of var
| N of int
| Node of symbol * (term list)
| Underscore
type atom = Atom of symbol * term list
type goal = Goal of atom list
type head = Head of atom
type body = Body of atom list
type clause = Fact of head | Rule of head * body
type program = clause list
type substitution = (var * term) list
exception NotUnifiable
exception NotFound
exception InvalidProgram
exception NotPossible
let rec print_term_list tl = match tl with
[] -> ()
| [t] -> print_term t
| t::tls ->
print_term t;
Printf.printf ",";
print_term_list tls
and print_list_body = function
| Node("_empty_list", []) -> ()
| 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 = function
| V(v) -> Printf.printf " %s " v
| Node("_empty_list", []) -> Printf.printf " [] "
| Node(s, []) -> Printf.printf " %s " s
| Node("_list", _) as t ->
Printf.printf " [";
print_list_body t;
Printf.printf "] "
| Node(s, l) ->
Printf.printf " %s ( " s;
print_term_list l;
Printf.printf " ) "
| N(n) -> Printf.printf " %d " n
| Underscore -> Printf.printf " _ "
;;
let union l1 l2 =
let combined = l1 @ l2 in
List.sort_uniq compare combined
;;
let rec modifyTerm i t = match t with
V(v) -> V((string_of_int i) ^ v)
| Node(s, l) -> Node(s, List.map (modifyTerm i) l)
| _ -> t
;;
let rec modifyAtom i a = match a with
| Atom(s, l) -> Atom(s, List.map (modifyTerm i) l)
;;
let rec modifyClause cl i = match cl with
Fact(Head(a)) -> Fact(Head(modifyAtom i a))
| Rule(Head(a), Body(l)) -> Rule(Head(modifyAtom i a), Body(List.map (modifyAtom i) l))
;;
let rec modifyInitialProg prog i = match prog with
[] -> []
| cl::ps -> (modifyClause cl i)::modifyInitialProg ps (i+1)
;;
let rec modifyProg2 prog a = match prog, a with
[], _ -> []
| cl::ps, Atom(s, _) -> match cl with Fact(Head(Atom(s', _))) | Rule(Head(Atom(s', _)), _) ->
if s = s' then (modifyClause cl 0)::modifyProg2 ps (Atom(s, []))
else cl::modifyProg2 ps (Atom(s, []))
;;
let rec vars_term t =
match t with
V(v) -> [v]
| Node(s, l) -> List.fold_left union [] (List.map vars_term l)
| _ -> []
;;
let vars_atom a = match a with Atom(s, l) -> vars_term (Node(s, l))
;;
let rec vars_goal goal = match goal with Goal(g) -> List.fold_left union [] (List.map vars_atom g)
;;
let rec subst s t =
match t with
Node(s', l) -> Node(s', List.map (subst s) l)
| N(_) -> t
| Underscore -> 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 a = match a with Atom(s', l) -> Atom(s', List.map (subst s) l)
;;
let rec varInTerm v t =
match t with
V(x) -> x = v
| Node(s, l) -> List.fold_left (||) false (List.map (varInTerm v) l)
| _ -> false
;;
let compose s1 s2 =
let f s x = (fst x, subst s (snd x)) in (List.map (f s2) s1) @ s2
;;
let rec mgu_term t1 t2= match (t1, t2) with
| V x, V y when x = y -> []
| (N(n1), N(n2)) when n1 = n2 -> []
| (Underscore, _) | (_, Underscore) -> []
| V x, Node(_, _) | V x, N _ when not (varInTerm x t2) -> [(x, t2)]
| Node(_, _), V y | N _, V y when not (varInTerm y t1) -> [(y, t1)]
| (Node(s1, l1), Node(s2, l2)) ->
if s1 <> s2 || (List.length l1 <> List.length l2) then raise NotUnifiable
else
let f s tt = compose s (mgu_term (subst s (fst tt)) (subst s (snd tt))) in
List.fold_left f [] (List.combine l1 l2)
| _, _ -> raise NotUnifiable
;;
let mgu_atom a1 a2 = match a1, a2 with Atom(s1, l1), Atom(s2, l2) -> mgu_term (Node(s1, l1)) (Node(s2, l2))
;;
let rec getSolution unif vars = 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 = 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 a2 unif =
compose unif (mgu_atom (subst_atom unif a1) (subst_atom unif a2))
;;
let solve_term_term t1 t2 unif =
compose unif (mgu_term (subst unif t1) (subst unif t2))
;;
let rec simplify_term t= match t with
N(_) -> t
| Node("+", [t1; t2]) -> (
match ((simplify_term t1), (simplify_term t2)) with
(N(n1), N(n2)) -> N(n1 + n2)
| _ -> raise NotUnifiable
)
| Node("-", [t1; t2]) -> (
match ((simplify_term t1), (simplify_term t2)) with
(N(n1), N(n2)) -> N(n1 - n2)
| _ -> raise NotUnifiable
)
| Node("*", [t1; t2]) -> (
match ((simplify_term t1), (simplify_term t2)) with
(N(n1), N(n2)) -> N(n1 * n2)
| _ -> raise NotUnifiable
)
| Node("/", [t1; t2]) -> (
match ((simplify_term t1), (simplify_term t2)) with
(N(n1), N(n2)) -> N(n1 / n2)
| _ -> raise NotUnifiable
)
| _ -> t
;;
let eval a unif = match a with
Atom("_equal", [t1; t2])
| Atom("_not_equal", [t1; t2]) -> compose unif (mgu_term (simplify_term (subst unif t1)) (simplify_term (subst unif t2)))
| Atom(">", [t1; t2]) -> (
match (simplify_term (subst unif t1), simplify_term (subst unif t2)) with
(N(n1), N(n2)) -> if n1 > n2 then unif else raise NotUnifiable
| _ -> raise NotUnifiable
)
| Atom("<", [t1; t2]) -> (
match (simplify_term (subst unif t1), simplify_term (subst unif t2)) with
(N(n1), N(n2)) -> if n1 < n2 then unif else raise NotUnifiable
| _ -> raise NotUnifiable
)
| _ -> unif
;;
let rec solve_goal prog g unif vars=
match g with
Goal([]) -> (
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, [])
)
| Goal(a::gs) -> match a with
Atom("_equal", _) | Atom(">", _) | Atom("<", _) -> (
try solve_goal prog (Goal(gs)) (eval a unif) vars
with NotUnifiable -> (false, [])
)
| Atom("_not_equal", _) -> (
try (false, eval a unif)
with NotUnifiable -> solve_goal prog (Goal(gs)) unif vars
)
| Atom("_ofcourse", _) -> let _ = solve_goal prog (Goal(gs)) unif vars in (true, [])
| Atom("integer", [t]) -> (
match (simplify_term (subst unif t)) with
N(_) -> solve_goal prog (Goal(gs)) unif vars
| _ -> (false, [])
)
| _ ->
let new_prog = modifyProg2 prog a in
let rec iter prog' = match prog' with
[] -> (false, [])
| cl::ps -> match cl with
Fact(Head(a')) -> (
try
let u = (solve_atom_atom a' a unif) in
match (solve_goal new_prog (Goal(gs)) u vars) with
(true, u') -> (true, u')
| _ -> iter ps
with NotUnifiable -> iter ps
)
| Rule(Head(a'), Body(al)) -> (
try
let u = (solve_atom_atom a' a unif) in
match (solve_goal new_prog (Goal(al @ gs)) u vars) with
(true, u') -> (true, u')
| _ -> iter ps
with NotUnifiable -> iter ps
)
in iter new_prog
;;
let interpret_goal g prog = solve_goal prog g [] (vars_goal g)
;;