Datasets:

Modalities:
Text
Languages:
English
Libraries:
Datasets
License:
File size: 5,244 Bytes
4365a98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

(*open dimacsTools;;*)

(* Functions for parsing the DIMACS-compliant output of SAT solvers,
   This is generic. Parser for minisat proof log is in minisatParse.ml  *)

(*
** Use Binaryset to encode mapping between HOL variable names
** and DIMACS  variable numbers as a set of string*int pairs.
*)

(*
** substringContains s ss
** tests whether substring ss contains string s
*)

let substringContains s ss =
  let re = Str.regexp_string s in
  match
    (try Str.search_forward re ss 0 with
      Not_found -> -1) with
    -1 -> false
  | _  -> true


(*
** parseSat (s1,s2) ss
** returns a list of numbers corresponding to the tokenised
** substring of ss (tokenised wrt Char.isSpace) that starts immediately
** after the first occurrence of s1 and ends just before the first
** occurrence of s2 that is after the first occurrence of s1
*)

let parseSat (s1,s2) ss =
  let p1 = Str.search_forward (Str.regexp s1) ss 0 in
  let p2 = Str.search_backward (Str.regexp s2) ss (String.length ss) in
  let ss1 = Str.string_before ss p2 in
  let ss2 = Str.string_after ss1 (p1+String.length s1) in
  let ssl = Str.split (Str.regexp "[ \n\t\r]") ss2 in
  List.map int_of_string ssl


(*
** invokeSat solver t
** invokes solver on t and returns SOME s (where s is the satisfying instance
** as a string of integers) or NONE, if unsatisfiable
*)

(*
** Reference containing last command used to invoke a SAT solver
*)

let sat_command = ref "undef"

(*
** Test for success of the result of Process.system
** N.B. isSuccess expected to primitive in next release of
** Moscow ML, and Process.status will lose eqtype status
*)

let satdir = "";;

(* if fname is NONE, then use a temp file, otherwise assume fname.cnf alredy exists*)
let invokeSat sat_solver fname t vc =
  let {name=name;
       url=url;
       executable=executable;
       good_exit=good_exit;
       notime_run=notime_run;
       time_run=time_run;
       only_true=only_true;
       failure_string=failure_string;
       start_string=start_string;
       end_string=end_string} = sat_solver in
  let var_count  =
    match vc with
      Some n -> n |
      None -> List.length(variables t) in
  let tmp        =
    match fname with
      Some fnm ->
        (initSatVarMap var_count;
         ignore (termToDimacs t); (*FIXME: this regenerates sat_var_map:
                                    better to save/load it*)
         fnm)
    | None     -> termToDimacsFile None t var_count in
  let infile     = tmp ^ ".cnf" in
  let outfile    = tmp ^ "." ^ name in
  let ex         = Filename.concat satdir executable in
  let run_cmd    = notime_run ex (infile,outfile) in
  let _          = (sat_command := run_cmd) in
  let code       = Sys.command run_cmd in
  let _          =
    if ((name = "minisat") || (name = "minisatp") || (code = good_exit))
    then ()
    else print_string("Warning:\n Failure signalled by\n " ^ run_cmd ^ "\n") in
  let ins        = Pervasives.open_in outfile in
  let sat_res    = input_all ins in
  let _          = close_in ins in
  let result     = substringContains failure_string sat_res in
  if result
  then None
  else
    let model1 = parseSat(start_string,end_string) sat_res in
    let model2 =
      if only_true
      then model1
        @
          (List.map
             (fun n -> 0-n)
             (subtract (List.map snd (snd(showSatVarMap()))) model1))
      else model1 in
    Some (List.map intToLiteral model2)


(*
** satOracle sat_solver t
** invokes sat_solver on t and returns a theorem tagged by the solver name
** of the form |- (l1 /\ ... ln) ==> t (satisfied with literals l1,...,ln)
** or |- ~t (failure)
*)

let satOracle sat_solver t =
  let res = invokeSat sat_solver None t None in
  match res with
    Some l -> mk_thm ([], mk_imp(list_mk_conj l, t))
  | None   -> mk_thm ([], mk_neg t)

(*
** satProve sat_solver t
** invokes sat_solver on t and if a model is found then
** then it is verified using proof in HOL and a theorem
** |- (l1 /\ ... /\ ln) ==> t is returned
** (where l1,...,ln are the literals making up the model);
** Raises satProveError if no model is found.
** Raises satCheckError if the found model is bogus
*)

(*
** satCheck [l1,...,ln] t
** attempts to prove (l1 /\ ... /\ ln) ==> t
** if it succeeds then the theorem is returned, else
** exception satCheckError is raised
*)

let EQT_Imp1 = TAUT `!b. b ==> (b<=>T)`
let EQF_Imp1 = TAUT `!b. (~b) ==> (b<=>F)`
let EQT_Imp2 = TAUT `!b. (b<=>T) ==> b`;;

exception Sat_check_error

let satCheck model t =
 try
   let mtm  = list_mk_conj model in
   let th1  = ASSUME mtm in
   let thl  = List.map
       (fun th ->
         if is_neg(concl th)
         then MP (SPEC (dest_neg(concl th)) EQF_Imp1) th
         else MP (SPEC (concl th) EQT_Imp1) th)
       (CONJUNCTS th1) in
   let th3 = SUBS_CONV thl t in
   let th4 = CONV_RULE(RAND_CONV(REWRITE_CONV[])) th3 in
   let th5 = MP (SPEC t EQT_Imp2) th4 in
   DISCH mtm th5
 with
   Sys.Break -> raise Sys.Break
 | _         -> raise Sat_check_error;;

exception Sat_prove_error

(* old interface by MJCG. assumes t is in cnf; only for finding SAT *)
let satProve sat_solver t  =
 match invokeSat sat_solver None t None with
   Some model -> satCheck model t
 | None       -> raise Sat_prove_error