| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
*) |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
open NumCompat |
| 23 |
module Z_ = NumCompat.Z |
| 24 |
|
| 25 |
module ISet = struct |
| 26 |
include Int.Set |
| 27 |
|
| 28 |
let pp o s = iter (fun i -> Printf.fprintf o "%i " i) s |
| 29 |
end |
| 30 |
|
| 31 |
module IMap = struct |
| 32 |
include Int.Map |
| 33 |
|
| 34 |
let from k m = |
| 35 |
let _, _, r = split (k - 1) m in |
| 36 |
r |
| 37 |
end |
| 38 |
|
| 39 |
let rec pp_list s f o l = |
| 40 |
match l with |
| 41 |
| [] -> () |
| 42 |
| [e] -> f o e |
| 43 |
| e :: l -> f o e; output_string o s; pp_list s f o l |
| 44 |
|
| 45 |
let finally f rst = |
| 46 |
try |
| 47 |
let res = f () in |
| 48 |
rst (); res |
| 49 |
with reraise -> |
| 50 |
(try rst () with any -> raise reraise); |
| 51 |
raise reraise |
| 52 |
|
| 53 |
let all_pairs f l = |
| 54 |
let pair_with acc e l = List.fold_left (fun acc x -> f e x :: acc) acc l in |
| 55 |
let rec xpairs acc l = |
| 56 |
match l with [] -> acc | e :: lx -> xpairs (pair_with acc e l) lx |
| 57 |
in |
| 58 |
xpairs [] l |
| 59 |
|
| 60 |
let rec is_sublist f l1 l2 = |
| 61 |
match (l1, l2) with |
| 62 |
| [], _ -> true |
| 63 |
| e :: l1', [] -> false |
| 64 |
| e :: l1', e' :: l2' -> |
| 65 |
if f e e' then is_sublist f l1' l2' else is_sublist f l1 l2' |
| 66 |
|
| 67 |
let extract pred l = |
| 68 |
List.fold_left |
| 69 |
(fun (fd, sys) e -> |
| 70 |
match fd with |
| 71 |
| None -> ( |
| 72 |
match pred e with None -> (fd, e :: sys) | Some v -> (Some (v, e), sys) |
| 73 |
) |
| 74 |
| _ -> (fd, e :: sys)) |
| 75 |
(None, []) l |
| 76 |
|
| 77 |
let extract_all pred l = |
| 78 |
List.fold_left |
| 79 |
(fun (s1, s2) e -> |
| 80 |
match pred e with None -> (s1, e :: s2) | Some v -> (v :: s1, s2)) |
| 81 |
([], []) l |
| 82 |
|
| 83 |
let simplify f sys = |
| 84 |
let sys', b = |
| 85 |
List.fold_left |
| 86 |
(fun (sys', b) c -> |
| 87 |
match f c with None -> (c :: sys', b) | Some c' -> (c' :: sys', true)) |
| 88 |
([], false) sys |
| 89 |
in |
| 90 |
if b then Some sys' else None |
| 91 |
|
| 92 |
let generate_acc f acc sys = |
| 93 |
List.fold_left |
| 94 |
(fun sys' c -> match f c with None -> sys' | Some c' -> c' :: sys') |
| 95 |
acc sys |
| 96 |
|
| 97 |
let generate f sys = generate_acc f [] sys |
| 98 |
|
| 99 |
let saturate p f sys = |
| 100 |
let rec sat acc l = |
| 101 |
match extract p l with |
| 102 |
| None, _ -> acc |
| 103 |
| Some r, l' -> |
| 104 |
let n = generate (f r) (l' @ acc) in |
| 105 |
sat (n @ acc) l' |
| 106 |
in |
| 107 |
try sat [] sys |
| 108 |
with x -> |
| 109 |
Printexc.print_backtrace stdout; |
| 110 |
raise x |
| 111 |
|
| 112 |
let iterate_until_stable f x = |
| 113 |
let rec iter x = match f x with None -> x | Some x' -> iter x' in |
| 114 |
iter x |
| 115 |
|
| 116 |
let rec app_funs l x = |
| 117 |
match l with |
| 118 |
| [] -> None |
| 119 |
| f :: fl -> ( match f x with None -> app_funs fl x | Some x' -> Some x' ) |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
module CoqToCaml = struct |
| 126 |
open Micromega |
| 127 |
|
| 128 |
let rec nat = function O -> 0 | S n -> nat n + 1 |
| 129 |
|
| 130 |
let rec positive p = |
| 131 |
match p with |
| 132 |
| XH -> 1 |
| 133 |
| XI p -> 1 + (2 * positive p) |
| 134 |
| XO p -> 2 * positive p |
| 135 |
|
| 136 |
let n nt = match nt with N0 -> 0 | Npos p -> positive p |
| 137 |
|
| 138 |
let rec index i = |
| 139 |
|
| 140 |
match i with XH -> 1 | XI i -> 1 + (2 * index i) | XO i -> 2 * index i |
| 141 |
|
| 142 |
let rec positive_big_int p = |
| 143 |
match p with |
| 144 |
| XH -> Z_.one |
| 145 |
| XI p -> Z_.add Z_.one (Z_.mul Z_.two (positive_big_int p)) |
| 146 |
| XO p -> Z_.mul Z_.two (positive_big_int p) |
| 147 |
|
| 148 |
let z_big_int x = |
| 149 |
match x with |
| 150 |
| Z0 -> Z_.zero |
| 151 |
| Zpos p -> positive_big_int p |
| 152 |
| Zneg p -> Z_.neg (positive_big_int p) |
| 153 |
|
| 154 |
let z x = match x with Z0 -> 0 | Zpos p -> index p | Zneg p -> -index p |
| 155 |
|
| 156 |
let q_to_num {qnum = x; qden = y} = |
| 157 |
let open Q.Notations in |
| 158 |
Q.of_bigint (z_big_int x) // Q.of_bigint (z_big_int (Zpos y)) |
| 159 |
end |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
module CamlToCoq = struct |
| 166 |
open Micromega |
| 167 |
|
| 168 |
let rec nat = function 0 -> O | n -> S (nat (n - 1)) |
| 169 |
|
| 170 |
let rec positive n = |
| 171 |
if Int.equal n 1 then XH |
| 172 |
else if Int.equal (n land 1) 1 then XI (positive (n lsr 1)) |
| 173 |
else XO (positive (n lsr 1)) |
| 174 |
|
| 175 |
let n nt = |
| 176 |
if nt < 0 then assert false |
| 177 |
else if Int.equal nt 0 then N0 |
| 178 |
else Npos (positive nt) |
| 179 |
|
| 180 |
let rec index n = |
| 181 |
if Int.equal n 1 then XH |
| 182 |
else if Int.equal (n land 1) 1 then XI (index (n lsr 1)) |
| 183 |
else XO (index (n lsr 1)) |
| 184 |
|
| 185 |
let z x = |
| 186 |
match compare x 0 with |
| 187 |
| 0 -> Z0 |
| 188 |
| 1 -> Zpos (positive x) |
| 189 |
| _ -> |
| 190 |
|
| 191 |
Zneg (positive (-x)) |
| 192 |
|
| 193 |
let positive_big_int n = |
| 194 |
let rec _pos n = |
| 195 |
if Z_.equal n Z_.one then XH |
| 196 |
else |
| 197 |
let q, m = Z_.quomod n Z_.two in |
| 198 |
if Z_.equal Z_.one m then XI (_pos q) else XO (_pos q) |
| 199 |
in |
| 200 |
_pos n |
| 201 |
|
| 202 |
let bigint x = |
| 203 |
match Z_.sign x with |
| 204 |
| 0 -> Z0 |
| 205 |
| 1 -> Zpos (positive_big_int x) |
| 206 |
| _ -> Zneg (positive_big_int (Z_.neg x)) |
| 207 |
|
| 208 |
let q n = |
| 209 |
{ Micromega.qnum = bigint (Q.num n) |
| 210 |
; Micromega.qden = positive_big_int (Q.den n) } |
| 211 |
end |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
module Cmp = struct |
| 219 |
let rec compare_lexical l = |
| 220 |
match l with |
| 221 |
| [] -> 0 |
| 222 |
| f :: l -> |
| 223 |
let cmp = f () in |
| 224 |
if Int.equal cmp 0 then compare_lexical l else cmp |
| 225 |
|
| 226 |
let rec compare_list cmp l1 l2 = |
| 227 |
match (l1, l2) with |
| 228 |
| [], [] -> 0 |
| 229 |
| [], _ -> -1 |
| 230 |
| _, [] -> 1 |
| 231 |
| e1 :: l1, e2 :: l2 -> |
| 232 |
let c = cmp e1 e2 in |
| 233 |
if Int.equal c 0 then compare_list cmp l1 l2 else c |
| 234 |
end |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
module type Tag = sig |
| 244 |
type t = int |
| 245 |
|
| 246 |
val from : int -> t |
| 247 |
val next : t -> t |
| 248 |
val pp : out_channel -> t -> unit |
| 249 |
val compare : t -> t -> int |
| 250 |
val max : t -> t -> t |
| 251 |
val to_int : t -> int |
| 252 |
end |
| 253 |
|
| 254 |
module Tag : Tag = struct |
| 255 |
type t = int |
| 256 |
|
| 257 |
let from i = i |
| 258 |
let next i = i + 1 |
| 259 |
let max : int -> int -> int = max |
| 260 |
let pp o i = output_string o (string_of_int i) |
| 261 |
let compare : int -> int -> int = Int.compare |
| 262 |
let to_int x = x |
| 263 |
end |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
module TagSet = struct |
| 270 |
include Set.Make (Tag) |
| 271 |
end |
| 272 |
|
| 273 |
module McPrinter = struct |
| 274 |
module Mc = Micromega |
| 275 |
|
| 276 |
let pp_nat o n = Printf.fprintf o "%i" (CoqToCaml.nat n) |
| 277 |
|
| 278 |
let pp_positive o x = Printf.fprintf o "%i" (CoqToCaml.positive x) |
| 279 |
|
| 280 |
let pp_z o x = |
| 281 |
Printf.fprintf o "%s" (NumCompat.Z.to_string (CoqToCaml.z_big_int x)) |
| 282 |
|
| 283 |
let pp_pol pp_c o e = |
| 284 |
let rec pp_pol o e = |
| 285 |
match e with |
| 286 |
| Mc.Pc n -> Printf.fprintf o "Pc %a" pp_c n |
| 287 |
| Mc.Pinj (p, pol) -> |
| 288 |
Printf.fprintf o "Pinj(%a,%a)" pp_positive p pp_pol pol |
| 289 |
| Mc.PX (pol1, p, pol2) -> |
| 290 |
Printf.fprintf o "PX(%a,%a,%a)" pp_pol pol1 pp_positive p pp_pol pol2 |
| 291 |
in |
| 292 |
pp_pol o e |
| 293 |
|
| 294 |
let pp_psatz pp_z o e = |
| 295 |
let rec pp_cone o e = |
| 296 |
match e with |
| 297 |
| Mc.PsatzLet (e1, e2) -> |
| 298 |
Printf.fprintf o "(Let %a %a)%%nat" pp_cone e1 pp_cone e2 |
| 299 |
| Mc.PsatzIn n -> Printf.fprintf o "(In %a)%%nat" pp_nat n |
| 300 |
| Mc.PsatzMulC (e, c) -> |
| 301 |
Printf.fprintf o "( %a [*] %a)" (pp_pol pp_z) e pp_cone c |
| 302 |
| Mc.PsatzSquare e -> Printf.fprintf o "(%a^2)" (pp_pol pp_z) e |
| 303 |
| Mc.PsatzAdd (e1, e2) -> |
| 304 |
Printf.fprintf o "(%a [+] %a)" pp_cone e1 pp_cone e2 |
| 305 |
| Mc.PsatzMulE (e1, e2) -> |
| 306 |
Printf.fprintf o "(%a [*] %a)" pp_cone e1 pp_cone e2 |
| 307 |
| Mc.PsatzC p -> Printf.fprintf o "(%a)%%positive" pp_z p |
| 308 |
| Mc.PsatzZ -> Printf.fprintf o "0" |
| 309 |
in |
| 310 |
pp_cone o e |
| 311 |
|
| 312 |
let rec pp_proof_term o = function |
| 313 |
| Micromega.DoneProof -> Printf.fprintf o "D" |
| 314 |
| Micromega.RatProof (cone, rst) -> |
| 315 |
Printf.fprintf o "R[%a,%a]" (pp_psatz pp_z) cone pp_proof_term rst |
| 316 |
| Micromega.CutProof (cone, rst) -> |
| 317 |
Printf.fprintf o "C[%a,%a]" (pp_psatz pp_z) cone pp_proof_term rst |
| 318 |
| Micromega.SplitProof (p, p1, p2) -> |
| 319 |
Printf.fprintf o "S[%a,%a,%a]" (pp_pol pp_z) p pp_proof_term p1 |
| 320 |
pp_proof_term p2 |
| 321 |
| Micromega.EnumProof (c1, c2, rst) -> |
| 322 |
Printf.fprintf o "EP[%a,%a,%a]" (pp_psatz pp_z) c1 (pp_psatz pp_z) c2 |
| 323 |
(pp_list "," pp_proof_term) |
| 324 |
rst |
| 325 |
| Micromega.ExProof (p, prf) -> |
| 326 |
Printf.fprintf o "Ex[%a,%a]" pp_positive p pp_proof_term prf |
| 327 |
|
| 328 |
end |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
let rec waitpid_non_intr pid = |
| 334 |
try snd (Unix.waitpid [] pid) |
| 335 |
with Unix.Unix_error (Unix.EINTR, _, _) -> waitpid_non_intr pid |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
let command exe_path args vl = |
| 342 |
|
| 343 |
let stdin_read, stdin_write = Unix.pipe () |
| 344 |
and stdout_read, stdout_write = Unix.pipe () |
| 345 |
and stderr_read, stderr_write = Unix.pipe () in |
| 346 |
|
| 347 |
let pid = |
| 348 |
Unix.create_process exe_path args stdin_read stdout_write stderr_write |
| 349 |
in |
| 350 |
|
| 351 |
let outch = Unix.out_channel_of_descr stdin_write in |
| 352 |
output_value outch vl; |
| 353 |
flush outch; |
| 354 |
|
| 355 |
let status = waitpid_non_intr pid in |
| 356 |
finally |
| 357 |
|
| 358 |
(fun () -> |
| 359 |
match status with |
| 360 |
| Unix.WEXITED 0 -> ( |
| 361 |
let inch = Unix.in_channel_of_descr stdout_read in |
| 362 |
try Marshal.from_channel inch |
| 363 |
with (End_of_file | Failure _) as exn -> |
| 364 |
failwith |
| 365 |
(Printf.sprintf "command \"%s\" exited with code 0 but result could not be read back: %s" |
| 366 |
exe_path |
| 367 |
(Printexc.to_string exn)) ) |
| 368 |
| Unix.WEXITED i -> |
| 369 |
failwith (Printf.sprintf "command \"%s\" exited %i" exe_path i) |
| 370 |
| Unix.WSIGNALED i -> |
| 371 |
failwith (Printf.sprintf "command \"%s\" killed %i" exe_path i) |
| 372 |
| Unix.WSTOPPED i -> |
| 373 |
failwith (Printf.sprintf "command \"%s\" stopped %i" exe_path i)) |
| 374 |
|
| 375 |
(fun () -> |
| 376 |
List.iter |
| 377 |
(fun x -> try Unix.close x with Unix.Unix_error _ -> ()) |
| 378 |
[ stdin_read |
| 379 |
; stdin_write |
| 380 |
; stdout_read |
| 381 |
; stdout_write |
| 382 |
; stderr_read |
| 383 |
; stderr_write ]) |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
module Hash = struct |
| 388 |
module Mc = Micromega |
| 389 |
open Hashset.Combine |
| 390 |
|
| 391 |
let int_of_eq_op1 = |
| 392 |
Mc.(function Equal -> 0 | NonEqual -> 1 | Strict -> 2 | NonStrict -> 3) |
| 393 |
|
| 394 |
let int_of_eq_op2 = |
| 395 |
Mc.( |
| 396 |
function |
| 397 |
| OpEq -> 0 | OpNEq -> 1 | OpLe -> 2 | OpGe -> 3 | OpLt -> 4 | OpGt -> 5) |
| 398 |
|
| 399 |
let eq_op1 o1 o2 = Int.equal (int_of_eq_op1 o1) (int_of_eq_op1 o2) |
| 400 |
let eq_op2 o1 o2 = Int.equal (int_of_eq_op2 o1) (int_of_eq_op2 o2) |
| 401 |
let hash_op1 h o = combine h (int_of_eq_op1 o) |
| 402 |
|
| 403 |
let rec eq_positive p1 p2 = |
| 404 |
match (p1, p2) with |
| 405 |
| Mc.XH, Mc.XH -> true |
| 406 |
| Mc.XI p1, Mc.XI p2 -> eq_positive p1 p2 |
| 407 |
| Mc.XO p1, Mc.XO p2 -> eq_positive p1 p2 |
| 408 |
| _, _ -> false |
| 409 |
|
| 410 |
let eq_z z1 z2 = |
| 411 |
match (z1, z2) with |
| 412 |
| Mc.Z0, Mc.Z0 -> true |
| 413 |
| Mc.Zpos p1, Mc.Zpos p2 | Mc.Zneg p1, Mc.Zneg p2 -> eq_positive p1 p2 |
| 414 |
| _, _ -> false |
| 415 |
|
| 416 |
let eq_q {Mc.qnum = qn1; Mc.qden = qd1} {Mc.qnum = qn2; Mc.qden = qd2} = |
| 417 |
eq_z qn1 qn2 && eq_positive qd1 qd2 |
| 418 |
|
| 419 |
let rec eq_pol eq p1 p2 = |
| 420 |
match (p1, p2) with |
| 421 |
| Mc.Pc c1, Mc.Pc c2 -> eq c1 c2 |
| 422 |
| Mc.Pinj (i1, p1), Mc.Pinj (i2, p2) -> eq_positive i1 i2 && eq_pol eq p1 p2 |
| 423 |
| Mc.PX (p1, i1, p1'), Mc.PX (p2, i2, p2') -> |
| 424 |
eq_pol eq p1 p2 && eq_positive i1 i2 && eq_pol eq p1' p2' |
| 425 |
| _, _ -> false |
| 426 |
|
| 427 |
let eq_pair eq1 eq2 (x1, y1) (x2, y2) = eq1 x1 x2 && eq2 y1 y2 |
| 428 |
|
| 429 |
let hash_pol helt = |
| 430 |
let rec hash acc = function |
| 431 |
| Mc.Pc c -> helt (combine acc 1) c |
| 432 |
| Mc.Pinj (p, c) -> hash (combine (combine acc 1) (CoqToCaml.index p)) c |
| 433 |
| Mc.PX (p1, i, p2) -> |
| 434 |
hash (hash (combine (combine acc 2) (CoqToCaml.index i)) p1) p2 |
| 435 |
in |
| 436 |
hash |
| 437 |
|
| 438 |
let hash_pair h1 h2 h (e1, e2) = h2 (h1 h e1) e2 |
| 439 |
let hash_elt f h e = combine h (f e) |
| 440 |
let hash_string h (e : string) = hash_elt Hashtbl.hash h e |
| 441 |
let hash_z = hash_elt CoqToCaml.z |
| 442 |
let hash_q = hash_elt (fun q -> Hashtbl.hash (CoqToCaml.q_to_num q)) |
| 443 |
end |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|