-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmain.ml
148 lines (134 loc) · 5.26 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
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
(* This code derives from a GPL'ed code and so is GPL'ed *)
open Fotypes;;
open Fofunctions;;
(* main function *)
let main verbose useSimplification useFOrenaming atoms inFilename outFilename =
try
(*print_string ( Filename.basename Sys.argv.(0) );*)
let fo = String.starts_with ~prefix:"fo" (Filename.basename Sys.argv.(0)) in
let inx =
match inFilename with
| None -> stdin
| Some name -> open_in name
in
let outx =
match outFilename with
| None -> stdout
| Some name -> open_out name
in
let lexbuf = Lexing.from_channel inx in
let result = Foyacc.start Folex.lexer lexbuf in
let constList = constsOf result in
if atoms then begin
let atomList = getAtoms result in
output_string outx ("order(" ^ (string_of_clause atomList) ^ ").\n");
end;
if verbose then begin
output_string outx ("Input: " ^ string_of_formula result^"\n");
flush outx
end;
let inNNF = nnf result in
if verbose then begin
output_string outx "In NNF: ";
output_string outx (string_of_formula inNNF^"\n");
debug("DONE");
flush outx;
debug("DONE");
end;
debug("done ");
let simplified =
if useSimplification then
simplify inNNF outx verbose
else inNNF
in
if verbose then begin
output_string outx "After all simplifications:\n";
output_string outx (string_of_formula simplified^"\n");
flush outx
end;
let (iP,uP,sP,eP) = dsnfWrap ~useFOrenaming simplified in
if verbose then begin
output_string outx "After transformations, the DSNF is\n";
output_string outx "iP = {\n";
output_string outx ((string_of_formula iP)^"\n}\n");
output_string outx "uP = {\n";
output_string outx ((string_of_formulas uP)^"\n}\n");
output_string outx "sP = {\n";
output_string outx ((string_of_formulas sP)^"\n}\n");
output_string outx "eP = {\n";
output_string outx ((string_of_formulas eP)^"\n}\n");
flush outx
end;
(* Skolemization *)
let skolemisedIP = eliminateQ iP
and skolemisedUP = eliminateQl uP
and skolemisedSP = eliminateQl sP
and skolemisedEP = if fo then (eliminateQl (flood eP constList)) else (eliminateQl eP)
in
(* FO transformations *)
let processedIP = if fo then (processFOconstants skolemisedIP) else skolemisedIP
and processedUP = if fo then (processFOconstantsl skolemisedUP) else skolemisedUP
and processedSP = if fo then (foStepClauses skolemisedSP) else skolemisedSP
and processedEP = if fo then (processFOconstantsl skolemisedEP) else skolemisedEP
in
(**)
let cnfedIP = cnf processedIP
and cnfedUP = cnfl processedUP
in
let clausesIP = clausify cnfedIP
and clausesUP = clausifyl cnfedUP
and clausesSP = clausifyl processedSP
and clausesEP = clausifyl processedEP in
(* prepare to print *)
let preamble = "and([\n"
and ending = "]).\n" in
let printed = ref true in
(* translate into strings *)
let icstringAux = string_of_i_clauses clausesIP
and ucstringAux = string_of_u_clauses clausesUP
and scstringAux = string_of_s_clauses clausesSP
and ecstringAux = string_of_e_clauses clausesEP in
(* Add commas depending on whether the next string is empty *)
let icstring = if ((String.length ucstringAux = 0) ||
(String.length icstringAux = 0)) then icstringAux
else icstringAux ^",\n"
and ucstring = if ((String.length scstringAux = 0) ||
(((String.length ucstringAux)+(String.length icstringAux)) = 0 ))
then ucstringAux
else ucstringAux ^",\n"
and scstring = if String.length ecstringAux = 0 then scstringAux
else scstringAux ^",\n"
(* no comma after ucstring *)
and ecstring = ecstringAux in
(* Collect strings *)
let resultStr = preamble^icstring^ucstring^scstring^ecstring^ending^"\n" in
(*print_string (string_of_clause (!newNamesList));*)
output_string outx (resultStr); if outx != stdout then close_out outx
with Parsing.Parse_error -> print_endline ("Parse error line " ^
string_of_int (!Folex.currentLine) ^ " characters " ^
string_of_int (!Folex.posmin) ^ "-" ^ string_of_int (!Folex.posmax))
| Sys_error astring -> print_endline (astring);;
open Cmdliner
let verbose =
let doc = "Be verbose (print intermediate transformations)." in
Arg.(value & flag & info ["v"] ~doc)
let useSimplification =
let doc = "Use simplifications." in
Arg.(value & flag & info ["s"] ~doc)
let useFOrenaming =
let doc = "Transform to CNF by renaming." in
Arg.(value & flag & info ["r"] ~doc)
let atoms =
let doc = "Include the 'order' statement with the list of all atoms in the input formula (experimental feature)." in
Arg.(value & flag & info ["al"] ~doc)
let inFilename =
let doc = "Specify the input file. If not given, stdin is used." in
Arg.(value & opt (some file) None & info ["i"] ~doc)
let outFilename =
let doc = "Specify the output file. If not given, stdout is used." in
Arg.(value & opt (some string) None & info ["o"] ~doc)
let main_t = Term.(const main $ verbose $ useSimplification $ useFOrenaming $ atoms $ inFilename $ outFilename)
let cmd =
let info = Cmd.info "translate" in
Cmd.v info main_t
let () = exit (Cmd.eval cmd)