-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.ml
94 lines (67 loc) · 1.85 KB
/
main.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
(** A main routine for a Datalog prover. *)
open Prover
open Reader
(** This section is all for argument processing *)
let get_args() =
(* file names *)
(** defaults to stdout *)
let output_file = ref "-" in
(** defaults to stdin *)
let input_file = ref "-" in
(** [anon_var] is used to track the number of free-floating
arguments. 0 means we have seen none, 1 means we have seen 1, 2
means we have seen too many. *)
let anon_var = ref 0 in
let anon_fun str =
match !anon_var with
0 ->
input_file := str;
anon_var := 1
| _ ->
anon_var := 2 in
(* set up Arg.parse *)
let arg_spec =
[("-o", Arg.String(fun s -> output_file := s),
"name - send output to this file (default stdout)" );
("--", Arg.Rest(anon_fun),
" - treat remaining args as file names, where - means stdin")] in
let usage_msg = "Usage: datalog [-o output] [input]" in
Arg.parse arg_spec anon_fun usage_msg;
if !anon_var > 1 then
begin
prerr_string "bad arg count";
prerr_newline();
prerr_string usage_msg;
prerr_newline();
exit 1
end;
!input_file, !output_file
let run (input_file_name, output_file_name) =
try
(** read input *)
let (clauses, atom) = Reader.read_program input_file_name in
(** load assumptions *)
let theory = create 128 in
List.iter (assume theory) clauses;
(** run query *)
let atoms = prove theory atom in
(** open output file *)
let out =
if output_file_name = "-" then
stdout
else
open_out output_file_name in
Format.set_formatter_out_channel out;
(** display output *)
let show atom =
print_atom atom;
Format.print_string ".";
Format.print_newline() in
List.iter show atoms;
exit 0
with Failure s ->
prerr_string s;
prerr_newline();
exit 1
let _ =
run (get_args())