| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open CErrors |
| 12 |
open Util |
| 13 |
open Constr |
| 14 |
|
| 15 |
open Utile |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
module BigInt = struct |
| 22 |
open Big_int_Z |
| 23 |
|
| 24 |
type t = big_int |
| 25 |
let of_int = big_int_of_int |
| 26 |
let coef0 = of_int 0 |
| 27 |
let of_num = Q.to_bigint |
| 28 |
let to_num = Q.of_bigint |
| 29 |
let equal = eq_big_int |
| 30 |
let lt = lt_big_int |
| 31 |
let le = le_big_int |
| 32 |
let abs = abs_big_int |
| 33 |
let plus =add_big_int |
| 34 |
let mult = mult_big_int |
| 35 |
let sub = sub_big_int |
| 36 |
let opp = minus_big_int |
| 37 |
let div = div_big_int |
| 38 |
let modulo = mod_big_int |
| 39 |
let to_string = string_of_big_int |
| 40 |
let hash x = |
| 41 |
try (int_of_big_int x) |
| 42 |
with Failure _ -> 1 |
| 43 |
let puis = power_big_int_positive_int |
| 44 |
|
| 45 |
) |
| 46 |
let rec pgcd a b = |
| 47 |
if equal b coef0 |
| 48 |
then a |
| 49 |
else if lt a b then pgcd b a else pgcd b (modulo a b) |
| 50 |
|
| 51 |
end |
| 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 |
type vname = string |
| 102 |
|
| 103 |
type term = |
| 104 |
| Zero |
| 105 |
| Const of Q.t |
| 106 |
| Var of vname |
| 107 |
| Opp of term |
| 108 |
| Add of term * term |
| 109 |
| Sub of term * term |
| 110 |
| Mul of term * term |
| 111 |
| Pow of term * int |
| 112 |
|
| 113 |
let const n = |
| 114 |
if Q.(equal zero) n then Zero else Const n |
| 115 |
let pow(p,i) = if Int.equal i 1 then p else Pow(p,i) |
| 116 |
let add = function |
| 117 |
(Zero,q) -> q |
| 118 |
| (p,Zero) -> p |
| 119 |
| (p,q) -> Add(p,q) |
| 120 |
let mul = function |
| 121 |
(Zero,_) -> Zero |
| 122 |
| (_,Zero) -> Zero |
| 123 |
| (p,Const n) when Q.(equal one) n -> p |
| 124 |
| (Const n,q) when Q.(equal one) n -> q |
| 125 |
| (p,q) -> Mul(p,q) |
| 126 |
|
| 127 |
let gen_constant n = lazy (UnivGen.constr_of_monomorphic_global (Global.env ()) (Rocqlib.lib_ref n)) |
| 128 |
|
| 129 |
let tpexpr = gen_constant "plugins.ring.pexpr" |
| 130 |
let ttconst = gen_constant "plugins.ring.const" |
| 131 |
let ttvar = gen_constant "plugins.ring.var" |
| 132 |
let ttadd = gen_constant "plugins.ring.add" |
| 133 |
let ttsub = gen_constant "plugins.ring.sub" |
| 134 |
let ttmul = gen_constant "plugins.ring.mul" |
| 135 |
let ttopp = gen_constant "plugins.ring.opp" |
| 136 |
let ttpow = gen_constant "plugins.ring.pow" |
| 137 |
|
| 138 |
let tlist = gen_constant "core.list.type" |
| 139 |
let lnil = gen_constant "core.list.nil" |
| 140 |
let lcons = gen_constant "core.list.cons" |
| 141 |
|
| 142 |
let tz = gen_constant "num.Z.type" |
| 143 |
let z0 = gen_constant "num.Z.Z0" |
| 144 |
let zpos = gen_constant "num.Z.Zpos" |
| 145 |
let zneg = gen_constant "num.Z.Zneg" |
| 146 |
|
| 147 |
let pxI = gen_constant "num.pos.xI" |
| 148 |
let pxO = gen_constant "num.pos.xO" |
| 149 |
let pxH = gen_constant "num.pos.xH" |
| 150 |
|
| 151 |
let nN0 = gen_constant "num.N.N0" |
| 152 |
let nNpos = gen_constant "num.N.Npos" |
| 153 |
|
| 154 |
let mkt_app name l = mkApp (Lazy.force name, Array.of_list l) |
| 155 |
|
| 156 |
let tlp () = mkt_app tlist [mkt_app tpexpr [Lazy.force tz]] |
| 157 |
let tllp () = mkt_app tlist [tlp()] |
| 158 |
|
| 159 |
let mkt_pos n = |
| 160 |
let rec mkt_pos n = |
| 161 |
if Z.(equal one) n then Lazy.force pxH |
| 162 |
else if Z.is_even n then |
| 163 |
mkt_app pxO [mkt_pos Z.(n asr 1)] |
| 164 |
else |
| 165 |
mkt_app pxI [mkt_pos Z.(n asr 1)] |
| 166 |
in mkt_pos (Q.to_bigint n) |
| 167 |
|
| 168 |
let mkt_n n = |
| 169 |
if Q.(equal zero) n |
| 170 |
then Lazy.force nN0 |
| 171 |
else mkt_app nNpos [mkt_pos n] |
| 172 |
|
| 173 |
let mkt_z z = |
| 174 |
if Q.(equal zero) z then Lazy.force z0 |
| 175 |
else if Q.(lt zero) z then |
| 176 |
mkt_app zpos [mkt_pos z] |
| 177 |
else |
| 178 |
mkt_app zneg [mkt_pos (Q.neg z)] |
| 179 |
|
| 180 |
let rec mkt_term t = match t with |
| 181 |
| Zero -> mkt_term (Const Q.zero) |
| 182 |
| Const r -> let n = r |> Q.num |> Q.of_bigint in |
| 183 |
mkt_app ttconst [Lazy.force tz; mkt_z n] |
| 184 |
| Var v -> mkt_app ttvar [Lazy.force tz; mkt_pos (Q.of_string v)] |
| 185 |
| Opp t1 -> mkt_app ttopp [Lazy.force tz; mkt_term t1] |
| 186 |
| Add (t1,t2) -> mkt_app ttadd [Lazy.force tz; mkt_term t1; mkt_term t2] |
| 187 |
| Sub (t1,t2) -> mkt_app ttsub [Lazy.force tz; mkt_term t1; mkt_term t2] |
| 188 |
| Mul (t1,t2) -> mkt_app ttmul [Lazy.force tz; mkt_term t1; mkt_term t2] |
| 189 |
| Pow (t1,n) -> if Int.equal n 0 then |
| 190 |
mkt_app ttconst [Lazy.force tz; mkt_z Q.one] |
| 191 |
else |
| 192 |
mkt_app ttpow [Lazy.force tz; mkt_term t1; mkt_n (Q.of_int n)] |
| 193 |
|
| 194 |
let rec parse_pos p = |
| 195 |
match Constr.kind p with |
| 196 |
| App (a,[|p2|]) -> |
| 197 |
if Constr.equal a (Lazy.force pxO) then Q.(mul (of_int 2)) (parse_pos p2) |
| 198 |
else Q.(add one) Q.(mul (of_int 2) (parse_pos p2)) |
| 199 |
| _ -> Q.one |
| 200 |
|
| 201 |
let parse_z z = |
| 202 |
match Constr.kind z with |
| 203 |
| App (a,[|p2|]) -> |
| 204 |
if Constr.equal a (Lazy.force zpos) then parse_pos p2 else Q.neg (parse_pos p2) |
| 205 |
| _ -> Q.zero |
| 206 |
|
| 207 |
let parse_n z = |
| 208 |
match Constr.kind z with |
| 209 |
| App (a,[|p2|]) -> |
| 210 |
parse_pos p2 |
| 211 |
| _ -> Q.zero |
| 212 |
|
| 213 |
let rec parse_term p = |
| 214 |
match Constr.kind p with |
| 215 |
| App (a,[|_;p2|]) -> |
| 216 |
if Constr.equal a (Lazy.force ttvar) then Var (Q.to_string (parse_pos p2)) |
| 217 |
else if Constr.equal a (Lazy.force ttconst) then Const (parse_z p2) |
| 218 |
else if Constr.equal a (Lazy.force ttopp) then Opp (parse_term p2) |
| 219 |
else Zero |
| 220 |
| App (a,[|_;p2;p3|]) -> |
| 221 |
if Constr.equal a (Lazy.force ttadd) then Add (parse_term p2, parse_term p3) |
| 222 |
else if Constr.equal a (Lazy.force ttsub) then Sub (parse_term p2, parse_term p3) |
| 223 |
else if Constr.equal a (Lazy.force ttmul) then Mul (parse_term p2, parse_term p3) |
| 224 |
else if Constr.equal a (Lazy.force ttpow) then |
| 225 |
Pow (parse_term p2, Q.to_int (parse_n p3)) |
| 226 |
else Zero |
| 227 |
| _ -> Zero |
| 228 |
|
| 229 |
let rec parse_request lp = |
| 230 |
match Constr.kind lp with |
| 231 |
| App (_,[|_|]) -> [] |
| 232 |
| App (_,[|_;p;lp1|]) -> |
| 233 |
(parse_term p)::(parse_request lp1) |
| 234 |
|_-> assert false |
| 235 |
|
| 236 |
let set_nvars_term nvars t = |
| 237 |
let rec aux t nvars = |
| 238 |
match t with |
| 239 |
| Zero -> nvars |
| 240 |
| Const r -> nvars |
| 241 |
| Var v -> let n = int_of_string v in |
| 242 |
max nvars n |
| 243 |
| Opp t1 -> aux t1 nvars |
| 244 |
| Add (t1,t2) -> aux t2 (aux t1 nvars) |
| 245 |
| Sub (t1,t2) -> aux t2 (aux t1 nvars) |
| 246 |
| Mul (t1,t2) -> aux t2 (aux t1 nvars) |
| 247 |
| Pow (t1,n) -> aux t1 nvars |
| 248 |
in aux t nvars |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
module Coef = BigInt |
| 255 |
|
| 256 |
module Poly = Polynom.Make(Coef) |
| 257 |
module PIdeal = Ideal.Make(Poly) |
| 258 |
open PIdeal |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
let term_pol_sparse nvars np t= |
| 265 |
let d = nvars in |
| 266 |
let rec aux t = |
| 267 |
|
| 268 |
let res = |
| 269 |
match t with |
| 270 |
| Zero -> zeroP |
| 271 |
| Const r -> |
| 272 |
if Q.(equal zero) r |
| 273 |
then zeroP |
| 274 |
else polconst d (Poly.Pint (Coef.of_num r)) |
| 275 |
| Var v -> |
| 276 |
let v = int_of_string v in |
| 277 |
if v <= np |
| 278 |
then polconst d (Poly.x v) |
| 279 |
else gen d v |
| 280 |
| Opp t1 -> oppP (aux t1) |
| 281 |
| Add (t1,t2) -> plusP (aux t1) (aux t2) |
| 282 |
| Sub (t1,t2) -> plusP (aux t1) (oppP (aux t2)) |
| 283 |
| Mul (t1,t2) -> multP (aux t1) (aux t2) |
| 284 |
| Pow (t1,n) -> puisP (aux t1) n |
| 285 |
in |
| 286 |
|
| 287 |
res |
| 288 |
in |
| 289 |
let res= aux t in |
| 290 |
res |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
let polrec_to_term p = |
| 295 |
let rec aux p = |
| 296 |
match p with |
| 297 |
|Poly.Pint n -> const (Coef.to_num n) |
| 298 |
|Poly.Prec (v,coefs) -> |
| 299 |
let fold i c res = add (res, mul (aux c, pow (Var (string_of_int v), i))) in |
| 300 |
Array.fold_right_i fold coefs Zero |
| 301 |
in aux p |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
let pol_sparse_to_term n2 p = |
| 306 |
|
| 307 |
let p = PIdeal.repr p in |
| 308 |
let rec aux p = |
| 309 |
match p with |
| 310 |
[] -> const Q.zero |
| 311 |
| (a,m)::p1 -> |
| 312 |
let m = Ideal.Monomial.repr m in |
| 313 |
let n = (Array.length m)-1 in |
| 314 |
let (i0,e0) = |
| 315 |
List.fold_left (fun (r,d) (a,m) -> |
| 316 |
let m = Ideal.Monomial.repr m in |
| 317 |
let i0= ref 0 in |
| 318 |
for k=1 to n do |
| 319 |
if m.(k)>0 |
| 320 |
then i0:=k |
| 321 |
done; |
| 322 |
if Int.equal !i0 0 |
| 323 |
then (r,d) |
| 324 |
else if !i0 > r |
| 325 |
then (!i0, m.(!i0)) |
| 326 |
else if Int.equal !i0 r && m.(!i0)<d |
| 327 |
then (!i0, m.(!i0)) |
| 328 |
else (r,d)) |
| 329 |
(0,0) |
| 330 |
p in |
| 331 |
if Int.equal i0 0 |
| 332 |
then |
| 333 |
let mp = polrec_to_term a in |
| 334 |
if List.is_empty p1 then mp else add (mp, aux p1) |
| 335 |
else |
| 336 |
let fold (p1, p2) (a, m) = |
| 337 |
if (Ideal.Monomial.repr m).(i0) >= e0 then begin |
| 338 |
let m0 = Array.copy (Ideal.Monomial.repr m) in |
| 339 |
let () = m0.(i0) <- m0.(i0) - e0 in |
| 340 |
let m0 = Ideal.Monomial.make m0 in |
| 341 |
((a, m0) :: p1, p2) |
| 342 |
end else |
| 343 |
(p1, (a, m) :: p2) |
| 344 |
in |
| 345 |
let (p1, p2) = List.fold_left fold ([], []) p in |
| 346 |
let vm = |
| 347 |
if Int.equal e0 1 |
| 348 |
then Var (string_of_int (i0)) |
| 349 |
else pow (Var (string_of_int (i0)),e0) in |
| 350 |
add (mul(vm, aux (List.rev p1)), aux (List.rev p2)) |
| 351 |
in |
| 352 |
aux p |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
let remove_zeros lci = |
| 365 |
let m = List.length lci in |
| 366 |
let u = Array.make m false in |
| 367 |
let rec utiles k = |
| 368 |
|
| 369 |
if k >= m || u.(k) then () |
| 370 |
else |
| 371 |
let () = u.(k) <- true in |
| 372 |
let lc = List.nth lci k in |
| 373 |
let iter i c = if not (PIdeal.equal c zeroP) then utiles (i + k + 1) in |
| 374 |
List.iteri iter lc |
| 375 |
in |
| 376 |
let () = utiles 0 in |
| 377 |
let filter i l = |
| 378 |
let f j l = if m <= i + j + 1 then true else u.(i + j + 1) in |
| 379 |
if u.(i) then Some (List.filteri f l) |
| 380 |
else None |
| 381 |
in |
| 382 |
let lr = CList.map_filter_i filter lci in |
| 383 |
info (fun () -> Printf.sprintf "useless spolynomials: %i" (m-List.length lr)); |
| 384 |
info (fun () -> Printf.sprintf "useful spolynomials: %i " (List.length lr)); |
| 385 |
lr |
| 386 |
|
| 387 |
let theoremedeszeros metadata nvars lpol p = |
| 388 |
let t1 = Unix.gettimeofday() in |
| 389 |
let m = nvars in |
| 390 |
let cert = in_ideal metadata m lpol p in |
| 391 |
info (fun () -> Printf.sprintf "time: @[%10.3f@]s" (Unix.gettimeofday ()-.t1)); |
| 392 |
cert |
| 393 |
|
| 394 |
open Ideal |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
let clean_pol lp = |
| 403 |
let t = Hashpol.create 12 in |
| 404 |
let find p = try Hashpol.find t p |
| 405 |
with |
| 406 |
Not_found -> Hashpol.add t p true; false in |
| 407 |
let rec aux lp = |
| 408 |
match lp with |
| 409 |
| [] -> [], [] |
| 410 |
| p :: lp1 -> |
| 411 |
let clp, lb = aux lp1 in |
| 412 |
if equal p zeroP || find p then clp, true::lb |
| 413 |
else |
| 414 |
(p :: clp, false::lb) in |
| 415 |
aux lp |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
let expand_pol lb lp = |
| 424 |
let rec aux lb lp = |
| 425 |
match lb with |
| 426 |
| [] -> lp |
| 427 |
| true :: lb1 -> zeroP :: aux lb1 lp |
| 428 |
| false :: lb1 -> |
| 429 |
match lp with |
| 430 |
[] -> assert false |
| 431 |
| p :: lp1 -> p :: aux lb1 lp1 |
| 432 |
in List.rev (aux lb (List.rev lp)) |
| 433 |
|
| 434 |
let theoremedeszeros_termes lp = |
| 435 |
let nvars = List.fold_left set_nvars_term 0 lp in |
| 436 |
match lp with |
| 437 |
| Const sugarparam :: Const nparam :: lp -> |
| 438 |
let nparam = Q.to_int nparam in |
| 439 |
((match Q.to_int sugarparam with |
| 440 |
|0 -> sinfo "computation without sugar"; |
| 441 |
lexico:=false; |
| 442 |
|1 -> sinfo "computation with sugar"; |
| 443 |
lexico:=false; |
| 444 |
|2 -> sinfo "ordre lexico computation without sugar"; |
| 445 |
lexico:=true; |
| 446 |
|3 -> sinfo "ordre lexico computation with sugar"; |
| 447 |
lexico:=true; |
| 448 |
|4 -> sinfo "computation without sugar, division by pairs"; |
| 449 |
lexico:=false; |
| 450 |
|5 -> sinfo "computation with sugar, division by pairs"; |
| 451 |
lexico:=false; |
| 452 |
|6 -> sinfo "ordre lexico computation without sugar, division by pairs"; |
| 453 |
lexico:=true; |
| 454 |
|7 -> sinfo "ordre lexico computation with sugar, division by pairs"; |
| 455 |
lexico:=true; |
| 456 |
| _ -> user_err Pp.(str "nsatz: bad parameter") |
| 457 |
); |
| 458 |
let lvar = List.init nvars (fun i -> Printf.sprintf "x%i" (i + 1)) in |
| 459 |
let lvar = ["a";"b";"c";"d";"e";"f";"g";"h";"i";"j";"k";"l";"m";"n";"o";"p";"q";"r";"s";"t";"u";"v";"w";"x";"y";"z"] @ lvar in |
| 460 |
|
| 461 |
let metadata = { name_var = lvar } in |
| 462 |
let lp = List.map (term_pol_sparse nvars nparam) lp in |
| 463 |
match lp with |
| 464 |
| [] -> assert false |
| 465 |
| p::lp1 -> |
| 466 |
let lpol = List.rev lp1 in |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
let lpol, lb = clean_pol lpol in |
| 472 |
let cert = theoremedeszeros metadata nvars lpol p in |
| 473 |
sinfo "cert ok"; |
| 474 |
let lc = cert.last_comb::List.rev cert.gb_comb in |
| 475 |
match remove_zeros lc with |
| 476 |
| [] -> assert false |
| 477 |
| (lq::lci) -> |
| 478 |
|
| 479 |
let lq = expand_pol lb lq in |
| 480 |
|
| 481 |
let m = nvars in |
| 482 |
let c = pol_sparse_to_term m (polconst m cert.coef) in |
| 483 |
let r = Pow(Zero,cert.power) in |
| 484 |
let lci = List.rev lci in |
| 485 |
|
| 486 |
let lci = List.map (expand_pol lb) lci in |
| 487 |
let lci = List.map (List.map (pol_sparse_to_term m)) lci in |
| 488 |
let lq = List.map (pol_sparse_to_term m) lq in |
| 489 |
info (fun () -> Printf.sprintf "number of parameters: %i" nparam); |
| 490 |
sinfo "term computed"; |
| 491 |
(c,r,lci,lq) |
| 492 |
) |
| 493 |
|_ -> assert false |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
let nsatz lpol = |
| 514 |
let lp= parse_request lpol in |
| 515 |
let (c,r,lci,lq) = theoremedeszeros_termes lp in |
| 516 |
let res = [c::r::lq]@lci in |
| 517 |
let res = List.map (fun lx -> List.map mkt_term lx) res in |
| 518 |
let res = |
| 519 |
List.fold_right |
| 520 |
(fun lt r -> |
| 521 |
let ltterm = |
| 522 |
List.fold_right |
| 523 |
(fun t r -> |
| 524 |
mkt_app lcons [mkt_app tpexpr [Lazy.force tz];t;r]) |
| 525 |
lt |
| 526 |
(mkt_app lnil [mkt_app tpexpr [Lazy.force tz]]) in |
| 527 |
mkt_app lcons [tlp ();ltterm;r]) |
| 528 |
res |
| 529 |
(mkt_app lnil [tlp ()]) in |
| 530 |
sinfo "term computed"; |
| 531 |
res |
| 532 |
|
| 533 |
let return_term t = |
| 534 |
let a = |
| 535 |
mkApp (UnivGen.constr_of_monomorphic_global (Global.env ()) @@ Rocqlib.lib_ref "core.eq.refl",[|tllp ();t|]) in |
| 536 |
let a = EConstr.of_constr a in |
| 537 |
Generalize.generalize [a] |
| 538 |
|
| 539 |
let nsatz_compute t = |
| 540 |
let lpol = |
| 541 |
try nsatz t |
| 542 |
with Ideal.NotInIdeal -> |
| 543 |
user_err Pp.(str "nsatz cannot solve this problem") in |
| 544 |
return_term lpol |