| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
*) |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
open NumCompat |
| 18 |
open Q.Notations |
| 19 |
open Mutils |
| 20 |
module Mc = Micromega |
| 21 |
|
| 22 |
let max_nb_cstr = ref max_int |
| 23 |
|
| 24 |
type var = int |
| 25 |
|
| 26 |
let debug = false |
| 27 |
let ( <+> ) = ( +/ ) |
| 28 |
let ( <*> ) = ( */ ) |
| 29 |
|
| 30 |
module Monomial : sig |
| 31 |
type t |
| 32 |
|
| 33 |
val const : t |
| 34 |
val is_const : t -> bool |
| 35 |
val var : var -> t |
| 36 |
val is_var : t -> bool |
| 37 |
val get_var : t -> var option |
| 38 |
val prod : t -> t -> t |
| 39 |
val factor : t -> var -> t option |
| 40 |
val compare : t -> t -> int |
| 41 |
val pp : out_channel -> t -> unit |
| 42 |
val fold : (var -> int -> 'a -> 'a) -> t -> 'a -> 'a |
| 43 |
val sqrt : t -> t option |
| 44 |
val variables : t -> ISet.t |
| 45 |
val degree : t -> int |
| 46 |
val subset : t -> t -> bool |
| 47 |
val output : out_channel -> t -> unit |
| 48 |
end = |
| 49 |
struct |
| 50 |
type t = int array |
| 51 |
|] where |
| 52 |
e |
| 53 |
|
| 54 |
> 0 |
| 55 |
0 |
| 56 |
|
| 57 |
|
| 58 |
let const = [|0|] |
| 59 |
|
| 60 |
let subset m1 m2 = |
| 61 |
m1.(0) <= m2.(0) && |
| 62 |
|
| 63 |
let len1 = Array.length m1 in |
| 64 |
let len2 = Array.length m2 in |
| 65 |
|
| 66 |
let get_var m c v = |
| 67 |
v+m.(c) , m.(c+1) in |
| 68 |
|
| 69 |
let rec xsubset cur1 v1 e1 cur2 v2 e2 = |
| 70 |
match Int.compare v1 v2 with |
| 71 |
| 0 -> e1 <= e2 && |
| 72 |
(if cur1 + 2 = len1 |
| 73 |
then true |
| 74 |
else if cur2 + 2 = len2 |
| 75 |
then false |
| 76 |
else |
| 77 |
let (v1,e1) = get_var m1 (cur1+2) v1 in |
| 78 |
let (v2,e2) = get_var m2 (cur2+2) v2 in |
| 79 |
xsubset (cur1+2) v1 e1 (cur2+2) v2 e2) |
| 80 |
| -1 -> false |
| 81 |
| _ -> if cur2 + 2 = len2 |
| 82 |
then false |
| 83 |
else let (v2,e2) = get_var m2 (cur2+2) v2 in |
| 84 |
xsubset cur1 v1 e1 (cur2+2) v2 e2 |
| 85 |
in |
| 86 |
if len1 <= 1 then true |
| 87 |
else if len2 <= 1 then false |
| 88 |
else xsubset 1 m1.(1) m1.(2) 1 m2.(1) m2.(2) |
| 89 |
|
| 90 |
let is_const (m : t) = match m with [|_|] -> true | _ -> false |
| 91 |
|
| 92 |
let var x = [|1; x; 1|] |
| 93 |
|
| 94 |
let is_var (m : t) = Int.equal m.(0) 1 |
| 95 |
|
| 96 |
let get_var (m : t) = match m with |
| 97 |
| [|1; x; _|] -> Some x |
| 98 |
| _ -> None |
| 99 |
|
| 100 |
let prod (m1 : t) (m2 : t) = |
| 101 |
let len1 = Array.length m1 in |
| 102 |
let len2 = Array.length m2 in |
| 103 |
|
| 104 |
let rec nvars accu cur1 cur2 i1 i2 = |
| 105 |
if Int.equal i1 len1 && Int.equal i2 len2 then accu |
| 106 |
else if Int.equal i1 len1 then accu + (len2 - i2) |
| 107 |
else if Int.equal i2 len2 then accu + (len1 - i1) |
| 108 |
else |
| 109 |
let ncur1 = cur1 + m1.(i1) in |
| 110 |
let ncur2 = cur2 + m2.(i2) in |
| 111 |
if ncur1 < ncur2 then nvars (accu + 2) ncur1 cur2 (i1 + 2) i2 |
| 112 |
else if ncur1 > ncur2 then nvars (accu + 2) cur1 ncur2 i1 (i2 + 2) |
| 113 |
else nvars (accu + 2) ncur1 ncur2 (i1 + 2) (i2 + 2) |
| 114 |
in |
| 115 |
let n = nvars 1 0 0 1 1 in |
| 116 |
let m = Array.make n 0 in |
| 117 |
let () = m.(0) <- m1.(0) + m2.(0) in |
| 118 |
|
| 119 |
let rec set cur cur1 cur2 i i1 i2 = |
| 120 |
if Int.equal i1 len1 && Int.equal i2 len2 then () |
| 121 |
else if Int.equal i1 len1 then |
| 122 |
let ncur2 = cur2 + m2.(i2) in |
| 123 |
let () = m.(i) <- ncur2 - cur in |
| 124 |
let () = m.(i + 1) <- m2.(i2 + 1) in |
| 125 |
set ncur2 cur1 ncur2 (i + 2) i1 (i2 + 2) |
| 126 |
else if Int.equal i2 len2 then |
| 127 |
let ncur1 = cur1 + m1.(i1) in |
| 128 |
let () = m.(i) <- ncur1 - cur in |
| 129 |
let () = m.(i + 1) <- m1.(i1 + 1) in |
| 130 |
set ncur1 ncur1 cur2 (i + 2) (i1 + 2) i2 |
| 131 |
else |
| 132 |
let ncur1 = cur1 + m1.(i1) in |
| 133 |
let ncur2 = cur2 + m2.(i2) in |
| 134 |
if ncur1 < ncur2 then |
| 135 |
let () = m.(i) <- ncur1 - cur in |
| 136 |
let () = m.(i + 1) <- m1.(i1 + 1) in |
| 137 |
set ncur1 ncur1 cur2 (i + 2) (i1 + 2) i2 |
| 138 |
else if ncur1 > ncur2 then |
| 139 |
let () = m.(i) <- ncur2 - cur in |
| 140 |
let () = m.(i + 1) <- m2.(i2 + 1) in |
| 141 |
set ncur2 cur1 ncur2 (i + 2) i1 (i2 + 2) |
| 142 |
else |
| 143 |
let () = m.(i) <- ncur1 - cur in |
| 144 |
let () = m.(i + 1) <- m1.(i1 + 1) + m2.(i2 + 1) in |
| 145 |
set ncur1 ncur1 ncur2 (i + 2) (i1 + 2) (i2 + 2) |
| 146 |
in |
| 147 |
let () = set 0 0 0 1 1 1 in |
| 148 |
m |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
let factor (m : t) (x : var) = |
| 153 |
let len = Array.length m in |
| 154 |
let rec factor cur i = |
| 155 |
if Int.equal i len then None |
| 156 |
else |
| 157 |
let ncur = cur + m.(i) in |
| 158 |
let k = m.(i + 1) in |
| 159 |
if ncur < x then factor ncur (i + 2) |
| 160 |
else if x < ncur then None |
| 161 |
else if Int.equal k 1 then |
| 162 |
|
| 163 |
let ans = Array.make (len - 2) 0 in |
| 164 |
let () = ans.(0) <- m.(0) - 1 in |
| 165 |
let () = Array.blit m 1 ans 1 (i - 1) in |
| 166 |
let () = Array.blit m (i + 2) ans i (len - i - 2) in |
| 167 |
|
| 168 |
let () = if not (Int.equal len (i + 2)) then ans.(i) <- ans.(i) + m.(i) in |
| 169 |
Some ans |
| 170 |
else |
| 171 |
let ans = Array.copy m in |
| 172 |
let () = ans.(0) <- ans.(0) - 1 in |
| 173 |
let () = ans.(i + 1) <- ans.(i + 1) - 1 in |
| 174 |
Some ans |
| 175 |
in |
| 176 |
factor 0 1 |
| 177 |
|
| 178 |
let compare (m1 : t) (m2 : t) = CArray.compare Int.compare m1 m2 |
| 179 |
|
| 180 |
let sqrt (m : t) = match m with |
| 181 |
| [|_|] -> Some const |
| 182 |
| _ -> |
| 183 |
let m = Array.copy m in |
| 184 |
let len = Array.length m in |
| 185 |
let () = m.(0) <- m.(0) / 2 in |
| 186 |
let rec set i = |
| 187 |
if Int.equal i len then () |
| 188 |
else |
| 189 |
let v = m.(i + 1) in |
| 190 |
let () = if v mod 2 = 0 then m.(i + 1) <- v / 2 else raise_notrace Exit in |
| 191 |
set (i + 2) |
| 192 |
in |
| 193 |
try let () = set 1 in Some m with Exit -> None |
| 194 |
|
| 195 |
let degree (m : t) = m.(0) |
| 196 |
|
| 197 |
let fold f (m : t) accu = |
| 198 |
let len = Array.length m in |
| 199 |
let rec fold accu cur i = |
| 200 |
if Int.equal i len then accu |
| 201 |
else |
| 202 |
let cur = cur + m.(i) in |
| 203 |
let accu = f cur m.(i + 1) accu in |
| 204 |
fold accu cur (i + 2) |
| 205 |
in |
| 206 |
fold accu 0 1 |
| 207 |
|
| 208 |
let output o m = fold (fun v i () -> Printf.fprintf o "x%i^%i" v i) m () |
| 209 |
|
| 210 |
|
| 211 |
let variables (m : t) = |
| 212 |
fold (fun x _ accu -> ISet.add x accu) m ISet.empty |
| 213 |
|
| 214 |
let pp o m = |
| 215 |
let pp_elt o (k, v) = |
| 216 |
if v = 1 then Printf.fprintf o "x%i" k else Printf.fprintf o "x%i^%i" k v |
| 217 |
in |
| 218 |
let rec pp_list o l = |
| 219 |
match l with |
| 220 |
| [] -> () |
| 221 |
| [e] -> pp_elt o e |
| 222 |
| e :: l -> Printf.fprintf o "%a*%a" pp_elt e pp_list l |
| 223 |
in |
| 224 |
pp_list o (List.rev @@ fold (fun x v accu -> (x, v) :: accu) m []) |
| 225 |
|
| 226 |
end |
| 227 |
|
| 228 |
module MonMap = struct |
| 229 |
include Map.Make (Monomial) |
| 230 |
|
| 231 |
let union f = |
| 232 |
merge (fun x v1 v2 -> |
| 233 |
match (v1, v2) with |
| 234 |
| None, None -> None |
| 235 |
| Some v, None | None, Some v -> Some v |
| 236 |
| Some v1, Some v2 -> f x v1 v2) |
| 237 |
end |
| 238 |
|
| 239 |
let pp_mon o (m, i) = |
| 240 |
if Monomial.is_const m then |
| 241 |
if Q.zero =/ i then () else Printf.fprintf o "%s" (Q.to_string i) |
| 242 |
else if Q.one =/ i then Monomial.pp o m |
| 243 |
else if Q.minus_one =/ i then Printf.fprintf o "-%a" Monomial.pp m |
| 244 |
else if Q.zero =/ i then () |
| 245 |
else Printf.fprintf o "%s*%a" (Q.to_string i) Monomial.pp m |
| 246 |
|
| 247 |
module Poly : |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
sig |
| 254 |
type t |
| 255 |
|
| 256 |
val pp : out_channel -> t -> unit |
| 257 |
val get : Monomial.t -> t -> Q.t |
| 258 |
val variable : var -> t |
| 259 |
val add : Monomial.t -> Q.t -> t -> t |
| 260 |
val constant : Q.t -> t |
| 261 |
val product : t -> t -> t |
| 262 |
val addition : t -> t -> t |
| 263 |
val uminus : t -> t |
| 264 |
val fold : (Monomial.t -> Q.t -> 'a -> 'a) -> t -> 'a -> 'a |
| 265 |
val factorise : var -> t -> t * t |
| 266 |
end = struct |
| 267 |
|
| 268 |
module P = Map.Make (Monomial) |
| 269 |
open P |
| 270 |
|
| 271 |
type t = Q.t P.t |
| 272 |
|
| 273 |
let pp o p = P.iter (fun mn i -> Printf.fprintf o "%a + " pp_mon (mn, i)) p |
| 274 |
|
| 275 |
|
| 276 |
let get : Monomial.t -> t -> Q.t = |
| 277 |
fun mn p -> try find mn p with Not_found -> Q.zero |
| 278 |
|
| 279 |
|
| 280 |
let variable : var -> t = fun x -> add (Monomial.var x) Q.one empty |
| 281 |
|
| 282 |
|
| 283 |
let constant : Q.t -> t = fun c -> add Monomial.const c empty |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
let add : Monomial.t -> Q.t -> t -> t = |
| 288 |
fun mn v p -> |
| 289 |
if Q.sign v = 0 then p |
| 290 |
else |
| 291 |
let vl = get mn p <+> v in |
| 292 |
if Q.sign vl = 0 then remove mn p else add mn vl p |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
let mult : Monomial.t -> Q.t -> t -> t = |
| 300 |
fun mn v p -> |
| 301 |
if Q.sign v = 0 then constant Q.zero |
| 302 |
else |
| 303 |
fold |
| 304 |
(fun mn' v' res -> P.add (Monomial.prod mn mn') (v <*> v') res) |
| 305 |
p empty |
| 306 |
|
| 307 |
let addition : t -> t -> t = |
| 308 |
fun p1 p2 -> fold (fun mn v p -> add mn v p) p1 p2 |
| 309 |
|
| 310 |
let product : t -> t -> t = |
| 311 |
fun p1 p2 -> fold (fun mn v res -> addition (mult mn v p2) res) p1 empty |
| 312 |
|
| 313 |
let uminus : t -> t = fun p -> map (fun v -> Q.neg v) p |
| 314 |
let fold = P.fold |
| 315 |
|
| 316 |
let factorise x p = |
| 317 |
P.fold |
| 318 |
(fun m v (px, cx) -> |
| 319 |
match Monomial.factor m x with |
| 320 |
| None -> (px, add m v cx) |
| 321 |
| Some mx -> |
| 322 |
(add mx v px, cx)) |
| 323 |
p |
| 324 |
(constant Q.zero, constant Q.zero) |
| 325 |
end |
| 326 |
|
| 327 |
type vector = Vect.t |
| 328 |
|
| 329 |
type cstr = {coeffs : vector; op : op; cst : Q.t} |
| 330 |
|
| 331 |
and op = Eq | Ge | Gt |
| 332 |
|
| 333 |
exception Strict |
| 334 |
|
| 335 |
let is_strict c = c.op = Gt |
| 336 |
let eval_op = function Eq -> ( =/ ) | Ge -> ( >=/ ) | Gt -> ( >/ ) |
| 337 |
let string_of_op = function Eq -> "=" | Ge -> ">=" | Gt -> ">" |
| 338 |
|
| 339 |
let compare_op o1 o2 = |
| 340 |
match (o1, o2) with |
| 341 |
| Eq, Eq -> 0 |
| 342 |
| Eq, _ -> -1 |
| 343 |
| _, Eq -> 1 |
| 344 |
| Ge, Ge -> 0 |
| 345 |
| Ge, _ -> -1 |
| 346 |
| _, Ge -> 1 |
| 347 |
| Gt, Gt -> 0 |
| 348 |
|
| 349 |
let output_cstr o {coeffs; op; cst} = |
| 350 |
Printf.fprintf o "%a %s %s" Vect.pp coeffs (string_of_op op) (Q.to_string cst) |
| 351 |
|
| 352 |
let opMult o1 o2 = |
| 353 |
match (o1, o2) with Eq, _ | _, Eq -> Eq | Ge, _ | _, Ge -> Ge | Gt, Gt -> Gt |
| 354 |
|
| 355 |
let opAdd o1 o2 = |
| 356 |
match (o1, o2) with Eq, x | x, Eq -> x | Gt, x | x, Gt -> Gt | Ge, Ge -> Ge |
| 357 |
|
| 358 |
module LinPoly = struct |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
type t = Vect.t |
| 364 |
|
| 365 |
module MonT = struct |
| 366 |
module MonoMap = Map.Make (Monomial) |
| 367 |
module IntMap = Map.Make (Int) |
| 368 |
|
| 369 |
|
| 370 |
let (index_of_monomial : int MonoMap.t ref) = ref MonoMap.empty |
| 371 |
|
| 372 |
let (monomial_of_index : Monomial.t IntMap.t ref) = ref IntMap.empty |
| 373 |
let fresh = ref 0 |
| 374 |
|
| 375 |
let reserve vr = |
| 376 |
if !fresh > vr then failwith (Printf.sprintf "Cannot reserve %i" vr) |
| 377 |
else fresh := vr + 1 |
| 378 |
|
| 379 |
let safe_reserve vr = if !fresh > vr then () else fresh := vr + 1 |
| 380 |
|
| 381 |
let get_fresh () = |
| 382 |
let vr = !fresh in |
| 383 |
incr fresh; vr |
| 384 |
|
| 385 |
let register m = |
| 386 |
try MonoMap.find m !index_of_monomial |
| 387 |
with Not_found -> |
| 388 |
let res = !fresh in |
| 389 |
index_of_monomial := MonoMap.add m res !index_of_monomial; |
| 390 |
monomial_of_index := IntMap.add res m !monomial_of_index; |
| 391 |
incr fresh; |
| 392 |
res |
| 393 |
|
| 394 |
let retrieve i = IntMap.find i !monomial_of_index |
| 395 |
|
| 396 |
let clear () = |
| 397 |
index_of_monomial := MonoMap.empty; |
| 398 |
monomial_of_index := IntMap.empty; |
| 399 |
fresh := 0; |
| 400 |
ignore (register Monomial.const) |
| 401 |
|
| 402 |
let _ = register Monomial.const |
| 403 |
end |
| 404 |
|
| 405 |
let var v = Vect.set (MonT.register (Monomial.var v)) Q.one Vect.null |
| 406 |
|
| 407 |
let of_monomial m = |
| 408 |
let v = MonT.register m in |
| 409 |
Vect.set v Q.one Vect.null |
| 410 |
|
| 411 |
let linpol_of_pol p = |
| 412 |
Poly.fold |
| 413 |
(fun mon num vct -> |
| 414 |
let vr = MonT.register mon in |
| 415 |
Vect.set vr num vct) |
| 416 |
p Vect.null |
| 417 |
|
| 418 |
let pol_of_linpol v = |
| 419 |
Vect.fold |
| 420 |
(fun p vr n -> Poly.add (MonT.retrieve vr) n p) |
| 421 |
(Poly.constant Q.zero) v |
| 422 |
|
| 423 |
let rocq_poly_of_linpol cst p = |
| 424 |
let pol_of_mon m = |
| 425 |
Monomial.fold |
| 426 |
(fun x v p -> |
| 427 |
Mc.PEmul (Mc.PEpow (Mc.PEX (CamlToCoq.positive x), CamlToCoq.n v), p)) |
| 428 |
m |
| 429 |
(Mc.PEc (cst Q.one)) |
| 430 |
in |
| 431 |
Vect.fold |
| 432 |
(fun acc x v -> |
| 433 |
let mn = MonT.retrieve x in |
| 434 |
Mc.PEadd (Mc.PEmul (Mc.PEc (cst v), pol_of_mon mn), acc)) |
| 435 |
(Mc.PEc (cst Q.zero)) |
| 436 |
p |
| 437 |
|
| 438 |
let pp_var o vr = |
| 439 |
try Monomial.pp o (MonT.retrieve vr) |
| 440 |
with Not_found -> Printf.fprintf o "v%i" vr |
| 441 |
|
| 442 |
let pp o p = Vect.pp_gen pp_var o p |
| 443 |
let constant c = if Q.sign c = 0 then Vect.null else Vect.set 0 c Vect.null |
| 444 |
|
| 445 |
let is_linear p = |
| 446 |
Vect.for_all |
| 447 |
(fun v _ -> |
| 448 |
let mn = MonT.retrieve v in |
| 449 |
Monomial.is_var mn || Monomial.is_const mn) |
| 450 |
p |
| 451 |
|
| 452 |
let is_variable p = |
| 453 |
let (x, v), r = Vect.decomp_fst p in |
| 454 |
if Vect.is_null r && v >/ Q.zero then Monomial.get_var (MonT.retrieve x) |
| 455 |
else None |
| 456 |
|
| 457 |
let factorise x p = |
| 458 |
let px, cx = Poly.factorise x (pol_of_linpol p) in |
| 459 |
(linpol_of_pol px, linpol_of_pol cx) |
| 460 |
|
| 461 |
let is_linear_for x p = |
| 462 |
let a, b = factorise x p in |
| 463 |
Vect.is_constant a |
| 464 |
|
| 465 |
let search_all_linear p l = |
| 466 |
Vect.fold |
| 467 |
(fun acc x v -> |
| 468 |
if p v then |
| 469 |
let x' = MonT.retrieve x in |
| 470 |
match Monomial.get_var x' with |
| 471 |
| None -> acc |
| 472 |
| Some x -> if is_linear_for x l then x :: acc else acc |
| 473 |
else acc) |
| 474 |
[] l |
| 475 |
|
| 476 |
let min_list (l : int list) = |
| 477 |
match l with [] -> None | e :: l -> Some (List.fold_left min e l) |
| 478 |
|
| 479 |
let search_linear p l = min_list (search_all_linear p l) |
| 480 |
|
| 481 |
let mul_cst c p = Vect.mul c p |
| 482 |
|
| 483 |
let product p1 p2 = |
| 484 |
linpol_of_pol (Poly.product (pol_of_linpol p1) (pol_of_linpol p2)) |
| 485 |
|
| 486 |
let addition p1 p2 = Vect.add p1 p2 |
| 487 |
|
| 488 |
let of_vect v = |
| 489 |
Vect.fold |
| 490 |
(fun acc v vl -> addition (product (var v) (constant vl)) acc) |
| 491 |
Vect.null v |
| 492 |
|
| 493 |
let variables p = |
| 494 |
Vect.fold |
| 495 |
(fun acc v _ -> ISet.union (Monomial.variables (MonT.retrieve v)) acc) |
| 496 |
ISet.empty p |
| 497 |
|
| 498 |
let monomials p = Vect.fold (fun acc v _ -> ISet.add v acc) ISet.empty p |
| 499 |
|
| 500 |
let pp_goal typ o l = |
| 501 |
let vars = |
| 502 |
List.fold_left |
| 503 |
(fun acc p -> ISet.union acc (variables (fst p))) |
| 504 |
ISet.empty l |
| 505 |
in |
| 506 |
let pp_vars o i = |
| 507 |
ISet.iter (fun v -> Printf.fprintf o "(x%i : %s) " v typ) vars |
| 508 |
in |
| 509 |
Printf.fprintf o "forall %a\n" pp_vars vars; |
| 510 |
List.iteri |
| 511 |
(fun i (p, op) -> |
| 512 |
Printf.fprintf o "(H%i : %a %s 0)\n" i pp p (string_of_op op)) |
| 513 |
l; |
| 514 |
Printf.fprintf o ", False\n" |
| 515 |
|
| 516 |
let collect_square p = |
| 517 |
Vect.fold |
| 518 |
(fun acc v _ -> |
| 519 |
let m = MonT.retrieve v in |
| 520 |
match Monomial.sqrt m with None -> acc | Some s -> MonMap.add s m acc) |
| 521 |
MonMap.empty p |
| 522 |
end |
| 523 |
|
| 524 |
module ProofFormat = struct |
| 525 |
type prf_rule = |
| 526 |
| Annot of string * prf_rule |
| 527 |
| Hyp of int |
| 528 |
| Def of int |
| 529 |
| Ref of int |
| 530 |
| Cst of Q.t |
| 531 |
| Zero |
| 532 |
| Square of Vect.t |
| 533 |
| MulC of Vect.t * prf_rule |
| 534 |
| Gcd of Z.t * prf_rule |
| 535 |
| MulPrf of prf_rule * prf_rule |
| 536 |
| AddPrf of prf_rule * prf_rule |
| 537 |
| CutPrf of prf_rule |
| 538 |
| LetPrf of prf_rule * prf_rule |
| 539 |
|
| 540 |
type proof = |
| 541 |
| Done |
| 542 |
| Step of int * prf_rule * proof |
| 543 |
| Split of int * Vect.t * proof * proof |
| 544 |
| Enum of int * prf_rule * Vect.t * prf_rule * proof list |
| 545 |
| ExProof of int * int * int * var * var * var * proof |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
let rec output_prf_rule o = function |
| 550 |
| Annot (s, p) -> Printf.fprintf o "(%a)@%s" output_prf_rule p s |
| 551 |
| Hyp i -> Printf.fprintf o "Hyp %i" i |
| 552 |
| Def i -> Printf.fprintf o "Def %i" i |
| 553 |
| Ref i -> Printf.fprintf o "Ref %i" i |
| 554 |
| LetPrf (p1, p2) -> |
| 555 |
Printf.fprintf o "Let (%a) in %a" output_prf_rule p1 output_prf_rule p2 |
| 556 |
| Cst c -> Printf.fprintf o "Cst %s" (Q.to_string c) |
| 557 |
| Zero -> Printf.fprintf o "Zero" |
| 558 |
| Square s -> Printf.fprintf o "(%a)^2" Poly.pp (LinPoly.pol_of_linpol s) |
| 559 |
| MulC (p, pr) -> |
| 560 |
Printf.fprintf o "(%a) * (%a)" Poly.pp (LinPoly.pol_of_linpol p) |
| 561 |
output_prf_rule pr |
| 562 |
| MulPrf (p1, p2) -> |
| 563 |
Printf.fprintf o "(%a) * (%a)" output_prf_rule p1 output_prf_rule p2 |
| 564 |
| AddPrf (p1, p2) -> |
| 565 |
Printf.fprintf o "%a + %a" output_prf_rule p1 output_prf_rule p2 |
| 566 |
| CutPrf p -> Printf.fprintf o "[%a]" output_prf_rule p |
| 567 |
| Gcd (c, p) -> Printf.fprintf o "(%a)/%s" output_prf_rule p (Z.to_string c) |
| 568 |
|
| 569 |
let rec output_proof o = function |
| 570 |
| Done -> Printf.fprintf o "." |
| 571 |
| Step (i, p, pf) -> |
| 572 |
Printf.fprintf o "%i:= %a\n ; %a" i output_prf_rule p output_proof pf |
| 573 |
| Split (i, v, p1, p2) -> |
| 574 |
Printf.fprintf o "%i:=%a ; { %a } { %a }" i Vect.pp v output_proof p1 |
| 575 |
output_proof p2 |
| 576 |
| Enum (i, p1, v, p2, pl) -> |
| 577 |
Printf.fprintf o "%i{%a<=%a<=%a}%a" i output_prf_rule p1 Vect.pp v |
| 578 |
output_prf_rule p2 (pp_list ";" output_proof) pl |
| 579 |
| ExProof (i, j, k, x, z, t, pr) -> |
| 580 |
Printf.fprintf o "%i := %i = %i - %i ; %i := %i >= 0 ; %i := %i >= 0 ; %a" |
| 581 |
i x z t j z k t output_proof pr |
| 582 |
|
| 583 |
module OrdPrfRule = struct |
| 584 |
type t = prf_rule |
| 585 |
|
| 586 |
let id_of_constr = function |
| 587 |
| Annot _ -> 0 |
| 588 |
| Hyp _ -> 1 |
| 589 |
| Def _ -> 2 |
| 590 |
| Ref _ -> 3 |
| 591 |
| Cst _ -> 4 |
| 592 |
| Zero -> 5 |
| 593 |
| Square _ -> 6 |
| 594 |
| MulC _ -> 7 |
| 595 |
| Gcd _ -> 8 |
| 596 |
| MulPrf _ -> 9 |
| 597 |
| AddPrf _ -> 10 |
| 598 |
| CutPrf _ -> 11 |
| 599 |
| LetPrf _ -> 12 |
| 600 |
|
| 601 |
let cmp_pair c1 c2 (x1, x2) (y1, y2) = |
| 602 |
match c1 x1 y1 with 0 -> c2 x2 y2 | i -> i |
| 603 |
|
| 604 |
let rec compare p1 p2 = |
| 605 |
match (p1, p2) with |
| 606 |
| Annot (s1, p1), Annot (s2, p2) -> |
| 607 |
if s1 = s2 then compare p1 p2 else String.compare s1 s2 |
| 608 |
| Hyp i, Hyp j -> Int.compare i j |
| 609 |
| Def i, Def j -> Int.compare i j |
| 610 |
| Ref i, Ref j -> Int.compare i j |
| 611 |
| Cst n, Cst m -> Q.compare n m |
| 612 |
| Zero, Zero -> 0 |
| 613 |
| Square v1, Square v2 -> Vect.compare v1 v2 |
| 614 |
| MulC (v1, p1), MulC (v2, p2) -> |
| 615 |
cmp_pair Vect.compare compare (v1, p1) (v2, p2) |
| 616 |
| Gcd (b1, p1), Gcd (b2, p2) -> |
| 617 |
cmp_pair Z.compare compare (b1, p1) (b2, p2) |
| 618 |
| MulPrf (p1, q1), MulPrf (p2, q2) -> |
| 619 |
cmp_pair compare compare (p1, q1) (p2, q2) |
| 620 |
| AddPrf (p1, q1), AddPrf (p2, q2) -> |
| 621 |
cmp_pair compare compare (p1, q1) (p2, q2) |
| 622 |
| CutPrf p, CutPrf p' -> compare p p' |
| 623 |
| LetPrf(p1,q1) , LetPrf(p2,q2) -> |
| 624 |
cmp_pair compare compare (p1, q1) (p2, q2) |
| 625 |
| _, _ -> Int.compare (id_of_constr p1) (id_of_constr p2) |
| 626 |
end |
| 627 |
|
| 628 |
module PrfRuleMap = Map.Make (OrdPrfRule) |
| 629 |
|
| 630 |
let rec pr_size = function |
| 631 |
| Annot (_, p) -> pr_size p |
| 632 |
| Zero | Square _ -> Q.zero |
| 633 |
| Hyp _ -> Q.one |
| 634 |
| Def _ -> Q.one |
| 635 |
| Ref _ -> Q.one |
| 636 |
| Cst n -> n |
| 637 |
| Gcd (i, p) -> pr_size p // Q.of_bigint i |
| 638 |
| MulPrf (p1, p2) | AddPrf (p1, p2) | LetPrf (p1, p2) -> |
| 639 |
pr_size p1 +/ pr_size p2 |
| 640 |
| CutPrf p -> pr_size p |
| 641 |
| MulC (v, p) -> pr_size p |
| 642 |
|
| 643 |
let rec pr_unit = function |
| 644 |
| Annot (_, p) -> pr_unit p |
| 645 |
| Zero | Square _ -> true |
| 646 |
| Hyp _ -> true |
| 647 |
| Def _ -> true |
| 648 |
| Cst n -> true |
| 649 |
| _ -> false |
| 650 |
|
| 651 |
let rec pr_rule_max_hyp = function |
| 652 |
| Annot (_, p) -> pr_rule_max_hyp p |
| 653 |
| Hyp i -> i |
| 654 |
| Def i -> -1 |
| 655 |
| Ref i -> -1 |
| 656 |
| Cst _ | Zero | Square _ -> -1 |
| 657 |
| MulC (_, p) | CutPrf p | Gcd (_, p) -> pr_rule_max_hyp p |
| 658 |
| MulPrf (p1, p2) | AddPrf (p1, p2) | LetPrf (p1, p2) -> |
| 659 |
max (pr_rule_max_hyp p1) (pr_rule_max_hyp p2) |
| 660 |
|
| 661 |
let rec pr_rule_max_def = function |
| 662 |
| Annot (_, p) -> pr_rule_max_hyp p |
| 663 |
| Hyp i -> -1 |
| 664 |
| Def i -> i |
| 665 |
| Ref _ -> -1 |
| 666 |
| Cst _ | Zero | Square _ -> -1 |
| 667 |
| MulC (_, p) | CutPrf p | Gcd (_, p) -> pr_rule_max_def p |
| 668 |
| MulPrf (p1, p2) | AddPrf (p1, p2) | LetPrf (p1, p2) -> |
| 669 |
max (pr_rule_max_def p1) (pr_rule_max_def p2) |
| 670 |
|
| 671 |
let rec proof_max_def = function |
| 672 |
| Done -> -1 |
| 673 |
| Step (i, pr, prf) -> max i (max (pr_rule_max_def pr) (proof_max_def prf)) |
| 674 |
| Split (i, _, p1, p2) -> max i (max (proof_max_def p1) (proof_max_def p2)) |
| 675 |
| Enum (i, p1, _, p2, l) -> |
| 676 |
let m = max (pr_rule_max_def p1) (pr_rule_max_def p2) in |
| 677 |
List.fold_left (fun i prf -> max i (proof_max_def prf)) (max i m) l |
| 678 |
| ExProof (i, j, k, _, _, _, prf) -> |
| 679 |
max (max (max i j) k) (proof_max_def prf) |
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
let pr_rule_def_cut m id p = |
| 684 |
let rec pr_rule_def_cut m id = function |
| 685 |
| Annot (_, p) -> pr_rule_def_cut m id p |
| 686 |
| MulC (p, prf) -> |
| 687 |
let bds, m, id', prf' = pr_rule_def_cut m id prf in |
| 688 |
(bds, m, id', MulC (p, prf')) |
| 689 |
| MulPrf (p1, p2) -> |
| 690 |
let bds1, m, id, p1 = pr_rule_def_cut m id p1 in |
| 691 |
let bds2, m, id, p2 = pr_rule_def_cut m id p2 in |
| 692 |
(bds2 @ bds1, m, id, MulPrf (p1, p2)) |
| 693 |
| AddPrf (p1, p2) -> |
| 694 |
let bds1, m, id, p1 = pr_rule_def_cut m id p1 in |
| 695 |
let bds2, m, id, p2 = pr_rule_def_cut m id p2 in |
| 696 |
(bds2 @ bds1, m, id, AddPrf (p1, p2)) |
| 697 |
| LetPrf (p1, p2) -> |
| 698 |
let bds1, m, id, p1 = pr_rule_def_cut m id p1 in |
| 699 |
let bds2, m, id, p2 = pr_rule_def_cut m id p2 in |
| 700 |
(bds2 @ bds1, m, id, LetPrf (p1, p2)) |
| 701 |
| CutPrf p | Gcd (_, p) -> ( |
| 702 |
let bds, m, id, p = pr_rule_def_cut m id p in |
| 703 |
try |
| 704 |
let id' = PrfRuleMap.find p m in |
| 705 |
(bds, m, id, Def id') |
| 706 |
with Not_found -> |
| 707 |
let m = PrfRuleMap.add p id m in |
| 708 |
((id, p) :: bds, m, id + 1, Def id) ) |
| 709 |
| (Square _ | Cst _ | Def _ | Hyp _ | Ref _ | Zero) as x -> ([], m, id, x) |
| 710 |
in |
| 711 |
pr_rule_def_cut m id p |
| 712 |
|
| 713 |
|
| 714 |
let pr_rule_def_cut m id = function |
| 715 |
| CutPrf p -> |
| 716 |
let bds, m, ids, p' = pr_rule_def_cut m id p in |
| 717 |
(bds, m, ids, CutPrf p') |
| 718 |
| p -> pr_rule_def_cut m id p |
| 719 |
|
| 720 |
let rec implicit_cut p = match p with CutPrf p -> implicit_cut p | _ -> p |
| 721 |
|
| 722 |
let rec pr_rule_collect_defs pr = |
| 723 |
match pr with |
| 724 |
| Annot (_, pr) -> pr_rule_collect_defs pr |
| 725 |
| Def i -> ISet.add i ISet.empty |
| 726 |
| Hyp i -> ISet.empty |
| 727 |
| Ref i -> ISet.empty |
| 728 |
| Cst _ | Zero | Square _ -> ISet.empty |
| 729 |
| MulC (_, pr) | Gcd (_, pr) | CutPrf pr -> pr_rule_collect_defs pr |
| 730 |
| MulPrf (p1, p2) | AddPrf (p1, p2) | LetPrf (p1, p2) -> |
| 731 |
ISet.union (pr_rule_collect_defs p1) (pr_rule_collect_defs p2) |
| 732 |
|
| 733 |
|
| 734 |
let add_proof x y = |
| 735 |
match (x, y) with Zero, p | p, Zero -> p | _ -> AddPrf (x, y) |
| 736 |
|
| 737 |
let rec mul_cst_proof c p = |
| 738 |
match p with |
| 739 |
| Annot (s, p) -> Annot (s, mul_cst_proof c p) |
| 740 |
| MulC (v, p') -> MulC (Vect.mul c v, p') |
| 741 |
| _ -> ( |
| 742 |
match Q.sign c with |
| 743 |
| 0 -> Zero |
| 744 |
| -1 -> |
| 745 |
MulC (LinPoly.constant c, p) |
| 746 |
| 1 -> if Q.one =/ c then p else MulPrf (Cst c, p) |
| 747 |
| _ -> assert false ) |
| 748 |
|
| 749 |
let sMulC v p = |
| 750 |
let c, v' = Vect.decomp_cst v in |
| 751 |
if Vect.is_null v' then mul_cst_proof c p else MulC (v, p) |
| 752 |
|
| 753 |
let mul_proof p1 p2 = |
| 754 |
match (p1, p2) with |
| 755 |
| Zero, _ | _, Zero -> Zero |
| 756 |
| Cst c, p | p, Cst c -> mul_cst_proof c p |
| 757 |
| _, _ -> MulPrf (p1, p2) |
| 758 |
|
| 759 |
let prf_rule_of_map m = |
| 760 |
PrfRuleMap.fold (fun k v acc -> add_proof (sMulC v k) acc) m Zero |
| 761 |
|
| 762 |
let rec dev_prf_rule p = |
| 763 |
match p with |
| 764 |
| Annot (s, p) -> dev_prf_rule p |
| 765 |
| Hyp _ | Def _ | Ref _ | Cst _ | Zero | Square _ -> |
| 766 |
PrfRuleMap.singleton p (LinPoly.constant Q.one) |
| 767 |
| MulC (v, p) -> |
| 768 |
PrfRuleMap.map (fun v1 -> LinPoly.product v v1) (dev_prf_rule p) |
| 769 |
| AddPrf (p1, p2) -> |
| 770 |
PrfRuleMap.merge |
| 771 |
(fun k o1 o2 -> |
| 772 |
match (o1, o2) with |
| 773 |
| None, None -> None |
| 774 |
| None, Some v | Some v, None -> Some v |
| 775 |
| Some v1, Some v2 -> Some (LinPoly.addition v1 v2)) |
| 776 |
(dev_prf_rule p1) (dev_prf_rule p2) |
| 777 |
| MulPrf (p1, p2) -> ( |
| 778 |
let p1' = dev_prf_rule p1 in |
| 779 |
let p2' = dev_prf_rule p2 in |
| 780 |
let p1'' = prf_rule_of_map p1' in |
| 781 |
let p2'' = prf_rule_of_map p2' in |
| 782 |
match p1'' with |
| 783 |
| Cst c -> PrfRuleMap.map (fun v1 -> Vect.mul c v1) p2' |
| 784 |
| _ -> PrfRuleMap.singleton (MulPrf (p1'', p2'')) (LinPoly.constant Q.one) |
| 785 |
) |
| 786 |
| Gcd (c, p) -> |
| 787 |
PrfRuleMap.singleton |
| 788 |
(Gcd (c, prf_rule_of_map (dev_prf_rule p))) |
| 789 |
(LinPoly.constant Q.one) |
| 790 |
| CutPrf p -> |
| 791 |
PrfRuleMap.singleton |
| 792 |
(CutPrf (prf_rule_of_map (dev_prf_rule p))) |
| 793 |
(LinPoly.constant Q.one) |
| 794 |
| LetPrf (p1, p2) -> |
| 795 |
let p1' = dev_prf_rule p1 in |
| 796 |
let p2' = dev_prf_rule p2 in |
| 797 |
let p1'' = prf_rule_of_map p1' in |
| 798 |
let p2'' = prf_rule_of_map p2' in |
| 799 |
PrfRuleMap.singleton (LetPrf (p1'', p2'')) (LinPoly.constant Q.one) |
| 800 |
|
| 801 |
let simplify_prf_rule p = prf_rule_of_map (dev_prf_rule p) |
| 802 |
|
| 803 |
|
| 804 |
let rec simplify_proof p = |
| 805 |
match p with |
| 806 |
| Done -> (Done, ISet.empty) |
| 807 |
| Step (i, pr, Done) -> (p, ISet.add i (pr_rule_collect_defs pr)) |
| 808 |
| Step (i, pr, prf) -> |
| 809 |
let prf', hyps = simplify_proof prf in |
| 810 |
if not (ISet.mem i hyps) then (prf', hyps) |
| 811 |
else |
| 812 |
( Step (i, pr, prf') |
| 813 |
, ISet.add i (ISet.union (pr_rule_collect_defs pr) hyps) ) |
| 814 |
| Split (i, v, p1, p2) -> |
| 815 |
let p1, h1 = simplify_proof p1 in |
| 816 |
let p2, h2 = simplify_proof p2 in |
| 817 |
if not (ISet.mem i h1) then (p1, h1) |
| 818 |
else if not (ISet.mem i h2) then (p2, h2) |
| 819 |
else (Split (i, v, p1, p2), ISet.add i (ISet.union h1 h2)) |
| 820 |
| Enum (i, p1, v, p2, pl) -> |
| 821 |
let pl, hl = List.split (List.map simplify_proof pl) in |
| 822 |
let hyps = List.fold_left ISet.union ISet.empty hl in |
| 823 |
( Enum (i, p1, v, p2, pl) |
| 824 |
, ISet.add i |
| 825 |
(ISet.union |
| 826 |
(ISet.union (pr_rule_collect_defs p1) (pr_rule_collect_defs p2)) |
| 827 |
hyps) ) |
| 828 |
| ExProof (i, j, k, x, z, t, prf) -> |
| 829 |
let prf', hyps = simplify_proof prf in |
| 830 |
if |
| 831 |
(not (ISet.mem i hyps)) |
| 832 |
&& (not (ISet.mem j hyps)) |
| 833 |
&& not (ISet.mem k hyps) |
| 834 |
then (prf', hyps) |
| 835 |
else |
| 836 |
( ExProof (i, j, k, x, z, t, prf') |
| 837 |
, ISet.add i (ISet.add j (ISet.add k hyps)) ) |
| 838 |
|
| 839 |
let rec normalise_proof id prf = |
| 840 |
match prf with |
| 841 |
| Done -> (id, Done) |
| 842 |
| Step (i, Gcd (c, p), Done) -> normalise_proof id (Step (i, p, Done)) |
| 843 |
| Step (i, p, prf) -> |
| 844 |
let bds, m, id, p' = |
| 845 |
pr_rule_def_cut PrfRuleMap.empty id (simplify_prf_rule p) |
| 846 |
in |
| 847 |
let id, prf = normalise_proof id prf in |
| 848 |
let prf = |
| 849 |
List.fold_left |
| 850 |
(fun acc (i, p) -> Step (i, CutPrf p, acc)) |
| 851 |
(Step (i, p', prf)) |
| 852 |
bds |
| 853 |
in |
| 854 |
(id, prf) |
| 855 |
| Split (i, v, p1, p2) -> |
| 856 |
let id, p1 = normalise_proof id p1 in |
| 857 |
let id, p2 = normalise_proof id p2 in |
| 858 |
(id, Split (i, v, p1, p2)) |
| 859 |
| ExProof (i, j, k, x, z, t, prf) -> |
| 860 |
let id, prf = normalise_proof id prf in |
| 861 |
(id, ExProof (i, j, k, x, z, t, prf)) |
| 862 |
| Enum (i, p1, v, p2, pl) -> |
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
let bds1, m, id, p1' = |
| 871 |
pr_rule_def_cut PrfRuleMap.empty id (implicit_cut p1) |
| 872 |
in |
| 873 |
let bds2, m, id, p2' = pr_rule_def_cut m id (implicit_cut p2) in |
| 874 |
let ids, prfs = List.split (List.map (normalise_proof id) pl) in |
| 875 |
( List.fold_left max 0 ids |
| 876 |
, List.fold_left |
| 877 |
(fun acc (i, p) -> Step (i, CutPrf p, acc)) |
| 878 |
(Enum (i, p1', v, p2', prfs)) |
| 879 |
(bds2 @ bds1) ) |
| 880 |
|
| 881 |
let normalise_proof id prf = |
| 882 |
let prf = fst (simplify_proof prf) in |
| 883 |
let res = normalise_proof id prf in |
| 884 |
if debug then |
| 885 |
Printf.printf "normalise_proof %a -> %a" output_proof prf output_proof |
| 886 |
(snd res); |
| 887 |
res |
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
let proof_of_farkas env vect = |
| 913 |
Vect.fold |
| 914 |
(fun prf x n -> add_proof (mul_cst_proof n (IMap.find x env)) prf) |
| 915 |
Zero vect |
| 916 |
|
| 917 |
module Env : |
| 918 |
sig |
| 919 |
type t |
| 920 |
val make : int -> t |
| 921 |
val id_of_def : int -> t -> int |
| 922 |
val id_of_hyp : int -> t -> int |
| 923 |
val push_ref : t -> t |
| 924 |
val push_def : int -> t -> t |
| 925 |
end = |
| 926 |
struct |
| 927 |
|
| 928 |
|
| 929 |
type t = |
| 930 |
{ |
| 931 |
lref : int; |
| 932 |
ndefs : int; |
| 933 |
ldefs : int Int.Map.t; |
| 934 |
nhyps : int; |
| 935 |
} |
| 936 |
|
| 937 |
let push_ref { nhyps; ndefs; lref; ldefs } = |
| 938 |
{ nhyps; ndefs; lref = lref + 1; ldefs } |
| 939 |
|
| 940 |
let push_def i { nhyps; ndefs; lref; ldefs } = |
| 941 |
let () = if lref <> 0 then failwith "Cannot push def" in |
| 942 |
{ nhyps; ndefs = ndefs + 1; lref; ldefs = Int.Map.add i ndefs ldefs } |
| 943 |
|
| 944 |
let make n = { nhyps = n; ndefs = 0; lref = 0; ldefs = Int.Map.empty } |
| 945 |
|
| 946 |
let id_of_def def { nhyps; ndefs; lref; ldefs } = |
| 947 |
try |
| 948 |
let pos = Int.Map.find def ldefs in |
| 949 |
lref + (ndefs - pos - 1) |
| 950 |
with Not_found -> failwith "Cannot find def" |
| 951 |
|
| 952 |
let id_of_hyp h { nhyps; ndefs; lref; ldefs } = |
| 953 |
if 0 <= h && h < nhyps then lref + ndefs + h |
| 954 |
else failwith "Cannot find hyp" |
| 955 |
|
| 956 |
end |
| 957 |
|
| 958 |
|
| 959 |
let cmpl_prf_rule norm (cst : Q.t -> 'a) env prf = |
| 960 |
let rec cmpl env = function |
| 961 |
| Annot (s, p) -> cmpl env p |
| 962 |
| Ref i -> Mc.PsatzIn (CamlToCoq.nat i) |
| 963 |
| Hyp h -> Mc.PsatzIn (CamlToCoq.nat (Env.id_of_hyp h env)) |
| 964 |
| Def d -> Mc.PsatzIn (CamlToCoq.nat (Env.id_of_def d env)) |
| 965 |
| Cst i -> Mc.PsatzC (cst i) |
| 966 |
| Zero -> Mc.PsatzZ |
| 967 |
| MulPrf (p1, p2) -> Mc.PsatzMulE (cmpl env p1, cmpl env p2) |
| 968 |
| AddPrf (p1, p2) -> Mc.PsatzAdd (cmpl env p1, cmpl env p2) |
| 969 |
| LetPrf (p1, p2) -> Mc.PsatzLet (cmpl env p1, cmpl (Env.push_ref env) p2) |
| 970 |
| MulC (lp, p) -> |
| 971 |
let lp = norm (LinPoly.rocq_poly_of_linpol cst lp) in |
| 972 |
Mc.PsatzMulC (lp, cmpl env p) |
| 973 |
| Square lp -> Mc.PsatzSquare (norm (LinPoly.rocq_poly_of_linpol cst lp)) |
| 974 |
| _ -> failwith "Cuts should already be compiled" |
| 975 |
in |
| 976 |
cmpl env prf |
| 977 |
|
| 978 |
let cmpl_prf_rule_z env r = |
| 979 |
cmpl_prf_rule Mc.normZ (fun x -> CamlToCoq.bigint (Q.num x)) env r |
| 980 |
|
| 981 |
let cmpl_pol_z lp = |
| 982 |
try |
| 983 |
let cst x = CamlToCoq.bigint (Q.num x) in |
| 984 |
Mc.normZ (LinPoly.rocq_poly_of_linpol cst lp) |
| 985 |
with x -> |
| 986 |
Printf.printf "cmpl_pol_z %s %a\n" (Printexc.to_string x) LinPoly.pp lp; |
| 987 |
raise x |
| 988 |
|
| 989 |
let rec cmpl_proof env prf = |
| 990 |
match prf with |
| 991 |
| Done -> Mc.DoneProof |
| 992 |
| Step (i, p, prf) -> ( |
| 993 |
match p with |
| 994 |
| CutPrf p' -> |
| 995 |
Mc.CutProof (cmpl_prf_rule_z env p', cmpl_proof (Env.push_def i env) prf) |
| 996 |
| _ -> Mc.RatProof (cmpl_prf_rule_z env p, cmpl_proof (Env.push_def i env) prf) |
| 997 |
) |
| 998 |
| Split (i, v, p1, p2) -> |
| 999 |
Mc.SplitProof |
| 1000 |
( cmpl_pol_z v |
| 1001 |
, cmpl_proof (Env.push_def i env) p1 |
| 1002 |
, cmpl_proof (Env.push_def i env) p2 ) |
| 1003 |
| Enum (i, p1, _, p2, l) -> |
| 1004 |
Mc.EnumProof |
| 1005 |
( cmpl_prf_rule_z env p1 |
| 1006 |
, cmpl_prf_rule_z env p2 |
| 1007 |
, List.map (cmpl_proof (Env.push_def i env)) l ) |
| 1008 |
| ExProof (i, j, k, x, _, _, prf) -> |
| 1009 |
Mc.ExProof |
| 1010 |
(CamlToCoq.positive x, cmpl_proof (Env.push_def i (Env.push_def j (Env.push_def k env))) prf) |
| 1011 |
|
| 1012 |
let compile_proof env prf = |
| 1013 |
let id = 1 + proof_max_def prf in |
| 1014 |
let _, prf = normalise_proof id prf in |
| 1015 |
cmpl_proof env prf |
| 1016 |
|
| 1017 |
end |
| 1018 |
|
| 1019 |
module WithProof = struct |
| 1020 |
type t = (LinPoly.t * op) * ProofFormat.prf_rule |
| 1021 |
|
| 1022 |
let repr p = p |
| 1023 |
|
| 1024 |
let proof p = snd p |
| 1025 |
|
| 1026 |
let polynomial ((p, _), _) = p |
| 1027 |
|
| 1028 |
|
| 1029 |
let compare : t -> t -> int = |
| 1030 |
fun ((lp1, o1), _) ((lp2, o2), _) -> |
| 1031 |
let c = Vect.compare lp1 lp2 in |
| 1032 |
if c = 0 then compare_op o1 o2 else c |
| 1033 |
|
| 1034 |
let annot s (p, prf) = (p, ProofFormat.Annot (s, prf)) |
| 1035 |
|
| 1036 |
let output o ((lp, op), prf) = |
| 1037 |
Printf.fprintf o "%a %s 0 by %a\n" LinPoly.pp lp (string_of_op op) |
| 1038 |
ProofFormat.output_prf_rule prf |
| 1039 |
|
| 1040 |
let output_sys o l = List.iter (Printf.fprintf o "%a\n" output) l |
| 1041 |
|
| 1042 |
exception InvalidProof |
| 1043 |
|
| 1044 |
let zero = ((Vect.null, Eq), ProofFormat.Zero) |
| 1045 |
let const n = ((LinPoly.constant n, Ge), ProofFormat.Cst n) |
| 1046 |
let of_cstr (c, prf) = ((Vect.set 0 (Q.neg c.cst) c.coeffs, c.op), prf) |
| 1047 |
|
| 1048 |
let product : t -> t -> t = |
| 1049 |
fun ((p1, o1), prf1) ((p2, o2), prf2) -> |
| 1050 |
((LinPoly.product p1 p2, opMult o1 o2), ProofFormat.mul_proof prf1 prf2) |
| 1051 |
|
| 1052 |
let addition : t -> t -> t = |
| 1053 |
fun ((p1, o1), prf1) ((p2, o2), prf2) -> |
| 1054 |
((Vect.add p1 p2, opAdd o1 o2), ProofFormat.add_proof prf1 prf2) |
| 1055 |
|
| 1056 |
let neg : t -> t = |
| 1057 |
fun ((p1, o1), prf1) -> |
| 1058 |
match o1 with |
| 1059 |
| Eq -> |
| 1060 |
((Vect.mul Q.minus_one p1, o1), ProofFormat.mul_cst_proof Q.minus_one prf1) |
| 1061 |
| _ -> failwith "neg: invalid proof" |
| 1062 |
|
| 1063 |
let mul_cst c ((p1, o1), prf1) = |
| 1064 |
let () = match o1 with |
| 1065 |
| Eq -> () |
| 1066 |
| Gt | Ge -> assert (c >/ Q.zero) |
| 1067 |
in |
| 1068 |
let p = LinPoly.mul_cst c p1 in |
| 1069 |
let prf = ProofFormat.mul_cst_proof c prf1 in |
| 1070 |
((p, o1), prf) |
| 1071 |
|
| 1072 |
let mult p ((p1, o1), prf1) = |
| 1073 |
match o1 with |
| 1074 |
| Eq -> ((LinPoly.product p p1, o1), ProofFormat.sMulC p prf1) |
| 1075 |
| Gt | Ge -> |
| 1076 |
let n, r = Vect.decomp_cst p in |
| 1077 |
if Vect.is_null r && n >/ Q.zero then |
| 1078 |
((LinPoly.product p p1, o1), ProofFormat.mul_cst_proof n prf1) |
| 1079 |
else ( |
| 1080 |
if debug then |
| 1081 |
Printf.printf "mult_error %a [*] %a\n" LinPoly.pp p output |
| 1082 |
((p1, o1), prf1); |
| 1083 |
raise InvalidProof ) |
| 1084 |
|
| 1085 |
let def p op i = ((p, op), ProofFormat.Def i) |
| 1086 |
|
| 1087 |
let mkhyp p op i = ((p, op), ProofFormat.Hyp i) |
| 1088 |
|
| 1089 |
let square p q = ((p, Ge), ProofFormat.Square q) |
| 1090 |
|
| 1091 |
let cutting_plane ((p, o), prf) = |
| 1092 |
let c, p' = Vect.decomp_cst p in |
| 1093 |
let g = Vect.gcd p' in |
| 1094 |
if Z.equal Z.one g || c =/ Q.zero || not (Z.equal (Q.den c) Z.one) then None |
| 1095 |
|
| 1096 |
else |
| 1097 |
let c1 = c // Q.of_bigint g in |
| 1098 |
let c1' = Q.floor c1 in |
| 1099 |
if c1 =/ c1' then None |
| 1100 |
else |
| 1101 |
match o with |
| 1102 |
| Eq -> |
| 1103 |
Some ((Vect.set 0 Q.minus_one Vect.null, Eq), ProofFormat.CutPrf prf) |
| 1104 |
| Gt -> failwith "cutting_plane ignore strict constraints" |
| 1105 |
| Ge -> |
| 1106 |
|
| 1107 |
Some |
| 1108 |
( (Vect.set 0 c1' (Vect.div (Q.of_bigint g) p), o) |
| 1109 |
, ProofFormat.CutPrf prf ) |
| 1110 |
|
| 1111 |
let construct_sign p = |
| 1112 |
let c, p' = Vect.decomp_cst p in |
| 1113 |
if Vect.is_null p' then |
| 1114 |
Some |
| 1115 |
( match Q.sign c with |
| 1116 |
| 0 -> (true, Eq, ProofFormat.Zero) |
| 1117 |
| 1 -> (true, Gt, ProofFormat.Cst c) |
| 1118 |
| _ -> (false, Gt, ProofFormat.Cst (Q.neg c)) ) |
| 1119 |
else None |
| 1120 |
|
| 1121 |
let get_sign l p = |
| 1122 |
match construct_sign p with |
| 1123 |
| None -> ( |
| 1124 |
try |
| 1125 |
let (p', o), prf = |
| 1126 |
List.find (fun ((p', o), prf) -> Vect.equal p p') l |
| 1127 |
in |
| 1128 |
Some (true, o, prf) |
| 1129 |
with Not_found -> ( |
| 1130 |
let p = Vect.uminus p in |
| 1131 |
try |
| 1132 |
let (p', o), prf = |
| 1133 |
List.find (fun ((p', o), prf) -> Vect.equal p p') l |
| 1134 |
in |
| 1135 |
Some (false, o, prf) |
| 1136 |
with Not_found -> None ) ) |
| 1137 |
| Some s -> Some s |
| 1138 |
|
| 1139 |
let mult_sign : bool -> t -> t = |
| 1140 |
fun b ((p, o), prf) -> if b then ((p, o), prf) else ((Vect.uminus p, o), prf) |
| 1141 |
|
| 1142 |
let rec linear_pivot sys ((lp1, op1), prf1) x ((lp2, op2), prf2) = |
| 1143 |
|
| 1144 |
let a1, b1 = LinPoly.factorise x lp1 in |
| 1145 |
|
| 1146 |
let a2, b2 = LinPoly.factorise x lp2 in |
| 1147 |
if Vect.is_null a2 then |
| 1148 |
Some ((lp2, op2), prf2) |
| 1149 |
else |
| 1150 |
match (op1, op2) with |
| 1151 |
| Eq, (Ge | Gt) -> ( |
| 1152 |
match get_sign sys a1 with |
| 1153 |
| None -> None |
| 1154 |
| Some (b, o, prf) -> |
| 1155 |
let sa1 = mult_sign b ((a1, o), prf) in |
| 1156 |
let sa2 = if b then Vect.uminus a2 else a2 in |
| 1157 |
let (lp2, op2), prf2 = |
| 1158 |
addition |
| 1159 |
(product sa1 ((lp2, op2), prf2)) |
| 1160 |
(mult sa2 ((lp1, op1), prf1)) |
| 1161 |
in |
| 1162 |
linear_pivot sys ((lp1, op1), prf1) x ((lp2, op2), prf2) ) |
| 1163 |
| Eq, Eq -> |
| 1164 |
let (lp2, op2), prf2 = |
| 1165 |
addition |
| 1166 |
(mult a1 ((lp2, op2), prf2)) |
| 1167 |
(mult (Vect.uminus a2) ((lp1, op1), prf1)) |
| 1168 |
in |
| 1169 |
linear_pivot sys ((lp1, op1), prf1) x ((lp2, op2), prf2) |
| 1170 |
| (Ge | Gt), (Ge | Gt) -> ( |
| 1171 |
match (get_sign sys a1, get_sign sys a2) with |
| 1172 |
| Some (b1, o1, p1), Some (b2, o2, p2) -> |
| 1173 |
if b1 <> b2 then |
| 1174 |
let (lp2, op2), prf2 = |
| 1175 |
addition |
| 1176 |
(product (mult_sign b1 ((a1, o1), p1)) ((lp2, op2), prf2)) |
| 1177 |
(product (mult_sign b2 ((a2, o2), p2)) ((lp1, op1), prf1)) |
| 1178 |
in |
| 1179 |
linear_pivot sys ((lp1, op1), prf1) x ((lp2, op2), prf2) |
| 1180 |
else None |
| 1181 |
| _ -> None ) |
| 1182 |
| (Ge | Gt), Eq -> failwith "pivot: equality as second argument" |
| 1183 |
|
| 1184 |
let linear_pivot sys ((lp1, op1), prf1) x ((lp2, op2), prf2) = |
| 1185 |
match linear_pivot sys ((lp1, op1), prf1) x ((lp2, op2), prf2) with |
| 1186 |
| None -> None |
| 1187 |
| Some (c, p) -> Some (c, ProofFormat.simplify_prf_rule p) |
| 1188 |
|
| 1189 |
let is_substitution strict ((p, o), prf) = |
| 1190 |
let pred v = if strict then v =/ Q.one || v =/ Q.minus_one else true in |
| 1191 |
match o with Eq -> LinPoly.search_linear pred p | _ -> None |
| 1192 |
|
| 1193 |
let sort (sys : t list) = |
| 1194 |
let size ((p, o), prf) = |
| 1195 |
let _, p' = Vect.decomp_cst p in |
| 1196 |
let (x, q), p' = Vect.decomp_fst p' in |
| 1197 |
Vect.fold |
| 1198 |
(fun (l, (q, x)) x' q' -> |
| 1199 |
let q' = Q.abs q' in |
| 1200 |
(l + 1, if q </ q then (q, x) else (q', x'))) |
| 1201 |
(1, (Q.abs q, x)) |
| 1202 |
p |
| 1203 |
in |
| 1204 |
let cmp ((l1, (q1, _)), ((_, o), _)) ((l2, (q2, _)), ((_, o'), _)) = |
| 1205 |
if l1 < l2 then -1 else if l1 = l2 then Q.compare q1 q2 else 1 |
| 1206 |
in |
| 1207 |
List.sort cmp (List.rev_map (fun wp -> (size wp, wp)) sys) |
| 1208 |
|
| 1209 |
let iterate_pivot p sys0 = |
| 1210 |
let elim sys = |
| 1211 |
let oeq, sys' = extract p sys in |
| 1212 |
match oeq with |
| 1213 |
| None -> None |
| 1214 |
| Some (v, pc) -> simplify (linear_pivot sys0 pc v) sys' |
| 1215 |
in |
| 1216 |
iterate_until_stable elim (List.map snd (sort sys0)) |
| 1217 |
|
| 1218 |
let subst_constant is_int sys = |
| 1219 |
let is_integer q = Q.(q =/ floor q) in |
| 1220 |
let is_constant ((c, o), p) = |
| 1221 |
match o with |
| 1222 |
| Ge | Gt -> None |
| 1223 |
| Eq -> ( |
| 1224 |
Vect.Bound.( |
| 1225 |
match of_vect c with |
| 1226 |
| None -> None |
| 1227 |
| Some b -> |
| 1228 |
if (not is_int) || is_integer (b.cst // b.coeff) then |
| 1229 |
Monomial.get_var (LinPoly.MonT.retrieve b.var) |
| 1230 |
else None) ) |
| 1231 |
in |
| 1232 |
iterate_pivot is_constant sys |
| 1233 |
|
| 1234 |
let subst sys0 = iterate_pivot (is_substitution true) sys0 |
| 1235 |
|
| 1236 |
let saturate_subst b sys0 = |
| 1237 |
let select = is_substitution b in |
| 1238 |
let gen (v, pc) ((c, op), prf) = |
| 1239 |
if ISet.mem v (LinPoly.variables c) then |
| 1240 |
linear_pivot sys0 pc v ((c, op), prf) |
| 1241 |
else None |
| 1242 |
in |
| 1243 |
saturate select gen sys0 |
| 1244 |
|
| 1245 |
let simple_pivot (q1, x) ((v1, o1), prf1) ((v2, o2), prf2) = |
| 1246 |
let q2 = Vect.get x v2 in |
| 1247 |
if q2 =/ Q.zero then None |
| 1248 |
else |
| 1249 |
let cv1, cv2 = |
| 1250 |
if Q.sign q1 <> Q.sign q2 then (Q.abs q2, Q.abs q1) |
| 1251 |
else |
| 1252 |
match (o1, o2) with |
| 1253 |
| Eq, _ -> (q2, Q.abs q1) |
| 1254 |
| _, Eq -> (Q.abs q2, q2) |
| 1255 |
| _, _ -> (Q.zero, Q.zero) |
| 1256 |
in |
| 1257 |
if cv2 =/ Q.zero then None |
| 1258 |
else |
| 1259 |
Some |
| 1260 |
( (Vect.mul_add cv1 v1 cv2 v2, opAdd o1 o2) |
| 1261 |
, ProofFormat.add_proof |
| 1262 |
(ProofFormat.mul_cst_proof cv1 prf1) |
| 1263 |
(ProofFormat.mul_cst_proof cv2 prf2) ) |
| 1264 |
|
| 1265 |
end |
| 1266 |
|
| 1267 |
module BoundWithProof = |
| 1268 |
struct |
| 1269 |
|
| 1270 |
type t = Vect.Bound.t * op * ProofFormat.prf_rule |
| 1271 |
|
| 1272 |
let make ((p, o), prf) = match Vect.Bound.of_vect p with |
| 1273 |
| None -> None |
| 1274 |
| Some b -> Some (b, o, prf) |
| 1275 |
|
| 1276 |
let padd (o1, prf1) (o2, prf2) = (opAdd o1 o2, ProofFormat.add_proof prf1 prf2) |
| 1277 |
|
| 1278 |
let pmul (o1, prf1) (o2, prf2) = (opMult o1 o2, ProofFormat.mul_proof prf1 prf2) |
| 1279 |
|
| 1280 |
let plet (o1,p1) (o2,p2) f = |
| 1281 |
match ProofFormat.pr_unit p1 , ProofFormat.pr_unit p2 with |
| 1282 |
| true , true -> f (o1,p1) (o2,p2) |
| 1283 |
| false , false -> |
| 1284 |
let (o,prf) = f (o1,ProofFormat.Ref 1) (o2,ProofFormat.Ref 0) in |
| 1285 |
(o, ProofFormat.LetPrf(p1,ProofFormat.LetPrf(p2,prf))) |
| 1286 |
| true , false -> |
| 1287 |
let (o,prf) = f (o1,p1) (o2,ProofFormat.Ref 0) in |
| 1288 |
(o, ProofFormat.LetPrf(p2,prf)) |
| 1289 |
| false , true -> |
| 1290 |
let (o,prf) = f (o1,ProofFormat.Ref 0) (o2,p2) in |
| 1291 |
(o,ProofFormat.LetPrf(p1,prf)) |
| 1292 |
|
| 1293 |
let pext c (o, prf) = |
| 1294 |
if c =/ Q.zero then (Eq, ProofFormat.Zero) |
| 1295 |
else (o, ProofFormat.mul_cst_proof c prf) |
| 1296 |
|
| 1297 |
let mul_bound (b1, o1, prf1) (b2, o2, prf2) = |
| 1298 |
let open Vect.Bound in |
| 1299 |
match (b1, b2) with |
| 1300 |
| {cst = c1; var = v1; coeff = c1'}, |
| 1301 |
{cst = c2; var = v2; coeff = c2'} -> |
| 1302 |
let good_coeff b o = |
| 1303 |
match o with |
| 1304 |
| Eq -> Some (Q.neg b) |
| 1305 |
| _ -> if b <=/ Q.zero then Some (Q.neg b) else None |
| 1306 |
in |
| 1307 |
match (good_coeff c1 o2, good_coeff c2 o1) with |
| 1308 |
| None, _ | _, None -> None |
| 1309 |
| Some c1, Some c2 -> |
| 1310 |
let w1 = (o1, prf1) in |
| 1311 |
let w2 = (o2, prf2) in |
| 1312 |
let (o, prf) = plet w1 w2 (fun w1 w2 -> padd (padd (pmul w1 w2) (pext c1 w2)) (pext c2 w1)) in |
| 1313 |
let b = { |
| 1314 |
cst = Q.neg (c1 */ c2); |
| 1315 |
var = LinPoly.MonT.register (Monomial.prod (LinPoly.MonT.retrieve v1) (LinPoly.MonT.retrieve v2)); |
| 1316 |
coeff = c1' */ c2'; |
| 1317 |
} in |
| 1318 |
Some (b, o, prf) |
| 1319 |
|
| 1320 |
let bound (b, _, _) = b |
| 1321 |
|
| 1322 |
let proof (b, o, w) = |
| 1323 |
let p = Vect.Bound.to_vect b in |
| 1324 |
((p, o), w) |
| 1325 |
end |
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
|