Debian Codegraph @

plugins/micromega/coq_micromega.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10 (* *)
11 (* Micromega: A reflexive tactic using the Positivstellensatz *)
12 (* *)
13 (* ** Toplevel definition of tactics ** *)
14 (* *)
15 (* - Modules Mc, Env, Cache, CacheZ *)
16 (* *)
17 (* Frédéric Besson (Irisa/Inria) 2006-2019 *)
18 (* *)
19 (************************************************************************)
20
21 open Pp
22 open Names
23 open Goptions
24 open Mutils
25 open Constr
26 open Context
27 open Tactypes
28 open McPrinter
29
30 module ERelevance = EConstr.ERelevance
31 module NamedDecl = Named.Declaration
32
33 (**
34 * Debug flag
35 *)
36
37 let debug = false
38
39 (* Limit the proof search *)
40
41 let max_depth = max_int
42
43 let since_8_14 = Deprecation.make ~since:"8.14" ()
44
45 (* Search limit for provers over Q R *)
46 let { Goptions.get = lra_proof_depth } =
47 declare_int_option_and_ref
48 ~depr:since_8_14
49 ~key:["Lra"; "Depth"]
50 ~value:max_depth
51 ()
52
53 let { Goptions.get = lia_proof_depth } =
54 declare_int_option_and_ref
55 ~depr:since_8_14
56 ~key:["Lia"; "Depth"]
57 ~value:max_depth
58 ()
59
60 let get_lia_option () = lia_proof_depth ()
61
62 (* Enable/disable caches *)
63
64 let { Goptions.get = use_lia_cache } =
65 declare_bool_option_and_ref
66 ~key:["Lia"; "Cache"]
67 ~value:true
68 ()
69
70 let { Goptions.get = use_nia_cache } =
71 declare_bool_option_and_ref
72 ~key:["Nia"; "Cache"]
73 ~value:true
74 ()
75
76 let { Goptions.get = use_nra_cache } =
77 declare_bool_option_and_ref
78 ~key:["Nra"; "Cache"]
79 ~value:true
80 ()
81
82 let { Goptions.get = show_used_hyps } =
83 declare_bool_option_and_ref
84 ~key:["Info";"Micromega"]
85 ~value:false
86 ()
87
88 let use_csdp_cache () = true
89
90 (**
91 * Initialize a tag type to the Tag module declaration (see Mutils).
92 *)
93
94 type tag = Tag.t
95
96 module Mc = Micromega
97
98 (**
99 * An atom is of the form:
100 * pExpr1 \{<,>,=,<>,<=,>=\} pExpr2
101 * where pExpr1, pExpr2 are polynomial expressions (see Micromega). pExprs are
102 * parametrized by 'cst, which is used as the type of constants.
103 *)
104
105 type 'cst atom = 'cst Mc.formula
106
107 type 'cst formula =
108 ('cst atom, EConstr.constr, tag * EConstr.constr, Names.Id.t) Mc.gFormula
109
110 type 'cst clause = ('cst Mc.nFormula, tag * EConstr.constr) Mc.clause
111 type 'cst cnf = ('cst Mc.nFormula, tag * EConstr.constr) Mc.cnf
112
113 let pp_kind o = function
114 | Mc.IsProp -> output_string o "Prop"
115 | Mc.IsBool -> output_string o "bool"
116
117 let kind_is_prop = function Mc.IsProp -> true | Mc.IsBool -> false
118
119 let rec pp_formula o (f : 'cst formula) =
120 Mc.(
121 match f with
122 | TT k -> output_string o (if kind_is_prop k then "True" else "true")
123 | FF k -> output_string o (if kind_is_prop k then "False" else "false")
124 | X (k, c) -> Printf.fprintf o "X %a" pp_kind k
125 | A (_, _, (t, _)) -> Printf.fprintf o "A(%a)" Tag.pp t
126 | AND (_, f1, f2) ->
127 Printf.fprintf o "AND(%a,%a)" pp_formula f1 pp_formula f2
128 | OR (_, f1, f2) -> Printf.fprintf o "OR(%a,%a)" pp_formula f1 pp_formula f2
129 | IMPL (_, f1, n, f2) ->
130 Printf.fprintf o "IMPL(%a,%s,%a)" pp_formula f1
131 (match n with Some id -> Names.Id.to_string id | None -> "")
132 pp_formula f2
133 | NOT (_, f) -> Printf.fprintf o "NOT(%a)" pp_formula f
134 | IFF (_, f1, f2) ->
135 Printf.fprintf o "IFF(%a,%a)" pp_formula f1 pp_formula f2
136 | EQ (f1, f2) -> Printf.fprintf o "EQ(%a,%a)" pp_formula f1 pp_formula f2)
137
138 (**
139 * Given a set of integers s=\{i0,...,iN\} and a list m, return the list of
140 * elements of m that are at position i0,...,iN.
141 *)
142
143 let selecti s m =
144 let rec xselecti i m =
145 match m with
146 | [] -> []
147 | e :: m ->
148 if ISet.mem i s then e :: xselecti (i + 1) m else xselecti (i + 1) m
149 in
150 xselecti 0 m
151
152 (**
153 * MODULE: Mapping of the Rocq data-strustures into Caml and Caml extracted
154 * code. This includes initializing Caml variables based on Rocq terms, parsing
155 * various Rocq expressions into Caml, and dumping Caml expressions into Rocq.
156 *
157 * Opened here and in csdpcert.ml.
158 *)
159
160 (**
161 * Initialization : a large amount of Caml symbols are derived from
162 * ZMicromega.v
163 *)
164
165 let constr_of_ref str =
166 EConstr.of_constr (UnivGen.constr_of_monomorphic_global (Global.env ()) (Rocqlib.lib_ref str))
167
168 let rocq_and = lazy (constr_of_ref "core.and.type")
169 let rocq_or = lazy (constr_of_ref "core.or.type")
170 let rocq_not = lazy (constr_of_ref "core.not.type")
171 let rocq_iff = lazy (constr_of_ref "core.iff.type")
172 let rocq_True = lazy (constr_of_ref "core.True.type")
173 let rocq_False = lazy (constr_of_ref "core.False.type")
174 let rocq_bool = lazy (constr_of_ref "core.bool.type")
175 let rocq_true = lazy (constr_of_ref "core.bool.true")
176 let rocq_false = lazy (constr_of_ref "core.bool.false")
177 let rocq_andb = lazy (constr_of_ref "core.bool.andb")
178 let rocq_orb = lazy (constr_of_ref "core.bool.orb")
179 let rocq_implb = lazy (constr_of_ref "core.bool.implb")
180 let rocq_eqb = lazy (constr_of_ref "core.bool.eqb")
181 let rocq_negb = lazy (constr_of_ref "core.bool.negb")
182 let rocq_cons = lazy (constr_of_ref "core.list.cons")
183 let rocq_nil = lazy (constr_of_ref "core.list.nil")
184 let rocq_list = lazy (constr_of_ref "core.list.type")
185 let rocq_O = lazy (constr_of_ref "num.nat.O")
186 let rocq_S = lazy (constr_of_ref "num.nat.S")
187 let rocq_nat = lazy (constr_of_ref "num.nat.type")
188 let rocq_unit = lazy (constr_of_ref "core.unit.type")
189
190 (* let rocq_option = lazy (init_constant "option")*)
191 let rocq_None = lazy (constr_of_ref "core.option.None")
192 let rocq_tt = lazy (constr_of_ref "core.unit.tt")
193 let rocq_Inl = lazy (constr_of_ref "core.sum.inl")
194 let rocq_Inr = lazy (constr_of_ref "core.sum.inr")
195 let rocq_N0 = lazy (constr_of_ref "num.N.N0")
196 let rocq_Npos = lazy (constr_of_ref "num.N.Npos")
197 let rocq_xH = lazy (constr_of_ref "num.pos.xH")
198 let rocq_xO = lazy (constr_of_ref "num.pos.xO")
199 let rocq_xI = lazy (constr_of_ref "num.pos.xI")
200 let rocq_Z = lazy (constr_of_ref "num.Z.type")
201 let rocq_ZERO = lazy (constr_of_ref "num.Z.Z0")
202 let rocq_POS = lazy (constr_of_ref "num.Z.Zpos")
203 let rocq_NEG = lazy (constr_of_ref "num.Z.Zneg")
204 let rocq_Q = lazy (constr_of_ref "rat.Q.type")
205 let rocq_Qmake = lazy (constr_of_ref "rat.Q.Qmake")
206 let rocq_R = lazy (constr_of_ref "reals.R.type")
207 let rocq_Rcst = lazy (constr_of_ref "micromega.Rcst.type")
208 let rocq_C0 = lazy (constr_of_ref "micromega.Rcst.C0")
209 let rocq_C1 = lazy (constr_of_ref "micromega.Rcst.C1")
210 let rocq_CQ = lazy (constr_of_ref "micromega.Rcst.CQ")
211 let rocq_CZ = lazy (constr_of_ref "micromega.Rcst.CZ")
212 let rocq_CPlus = lazy (constr_of_ref "micromega.Rcst.CPlus")
213 let rocq_CMinus = lazy (constr_of_ref "micromega.Rcst.CMinus")
214 let rocq_CMult = lazy (constr_of_ref "micromega.Rcst.CMult")
215 let rocq_CPow = lazy (constr_of_ref "micromega.Rcst.CPow")
216 let rocq_CInv = lazy (constr_of_ref "micromega.Rcst.CInv")
217 let rocq_COpp = lazy (constr_of_ref "micromega.Rcst.COpp")
218 let rocq_R0 = lazy (constr_of_ref "reals.R.R0")
219 let rocq_R1 = lazy (constr_of_ref "reals.R.R1")
220 let rocq_proofTerm = lazy (constr_of_ref "micromega.ZArithProof.type")
221 let rocq_doneProof = lazy (constr_of_ref "micromega.ZArithProof.DoneProof")
222 let rocq_ratProof = lazy (constr_of_ref "micromega.ZArithProof.RatProof")
223 let rocq_cutProof = lazy (constr_of_ref "micromega.ZArithProof.CutProof")
224 let rocq_splitProof = lazy (constr_of_ref "micromega.ZArithProof.SplitProof")
225 let rocq_enumProof = lazy (constr_of_ref "micromega.ZArithProof.EnumProof")
226 let rocq_ExProof = lazy (constr_of_ref "micromega.ZArithProof.ExProof")
227 let rocq_IsProp = lazy (constr_of_ref "micromega.kind.isProp")
228 let rocq_IsBool = lazy (constr_of_ref "micromega.kind.isBool")
229 let rocq_Zgt = lazy (constr_of_ref "num.Z.gt")
230 let rocq_Zge = lazy (constr_of_ref "num.Z.ge")
231 let rocq_Zle = lazy (constr_of_ref "num.Z.le")
232 let rocq_Zlt = lazy (constr_of_ref "num.Z.lt")
233 let rocq_Zgtb = lazy (constr_of_ref "num.Z.gtb")
234 let rocq_Zgeb = lazy (constr_of_ref "num.Z.geb")
235 let rocq_Zleb = lazy (constr_of_ref "num.Z.leb")
236 let rocq_Zltb = lazy (constr_of_ref "num.Z.ltb")
237 let rocq_Zeqb = lazy (constr_of_ref "num.Z.eqb")
238 let rocq_eq = lazy (constr_of_ref "core.eq.type")
239 let rocq_Zplus = lazy (constr_of_ref "num.Z.add")
240 let rocq_Zminus = lazy (constr_of_ref "num.Z.sub")
241 let rocq_Zopp = lazy (constr_of_ref "num.Z.opp")
242 let rocq_Zmult = lazy (constr_of_ref "num.Z.mul")
243 let rocq_Zpower = lazy (constr_of_ref "num.Z.pow")
244 let rocq_Qle = lazy (constr_of_ref "rat.Q.Qle")
245 let rocq_Qlt = lazy (constr_of_ref "rat.Q.Qlt")
246 let rocq_Qeq = lazy (constr_of_ref "rat.Q.Qeq")
247 let rocq_Qplus = lazy (constr_of_ref "rat.Q.Qplus")
248 let rocq_Qminus = lazy (constr_of_ref "rat.Q.Qminus")
249 let rocq_Qopp = lazy (constr_of_ref "rat.Q.Qopp")
250 let rocq_Qmult = lazy (constr_of_ref "rat.Q.Qmult")
251 let rocq_Qpower = lazy (constr_of_ref "rat.Q.Qpower")
252 let rocq_Rgt = lazy (constr_of_ref "reals.R.Rgt")
253 let rocq_Rge = lazy (constr_of_ref "reals.R.Rge")
254 let rocq_Rle = lazy (constr_of_ref "reals.R.Rle")
255 let rocq_Rlt = lazy (constr_of_ref "reals.R.Rlt")
256 let rocq_Rplus = lazy (constr_of_ref "reals.R.Rplus")
257 let rocq_Rminus = lazy (constr_of_ref "reals.R.Rminus")
258 let rocq_Ropp = lazy (constr_of_ref "reals.R.Ropp")
259 let rocq_Rmult = lazy (constr_of_ref "reals.R.Rmult")
260 let rocq_Rinv = lazy (constr_of_ref "reals.R.Rinv")
261 let rocq_Rpower = lazy (constr_of_ref "reals.R.pow")
262 let rocq_powerZR = lazy (constr_of_ref "reals.R.powerRZ")
263 let rocq_IZR = lazy (constr_of_ref "reals.R.IZR")
264 let rocq_IQR = lazy (constr_of_ref "reals.R.Q2R")
265 let rocq_PEX = lazy (constr_of_ref "micromega.PExpr.PEX")
266 let rocq_PEc = lazy (constr_of_ref "micromega.PExpr.PEc")
267 let rocq_PEadd = lazy (constr_of_ref "micromega.PExpr.PEadd")
268 let rocq_PEopp = lazy (constr_of_ref "micromega.PExpr.PEopp")
269 let rocq_PEmul = lazy (constr_of_ref "micromega.PExpr.PEmul")
270 let rocq_PEsub = lazy (constr_of_ref "micromega.PExpr.PEsub")
271 let rocq_PEpow = lazy (constr_of_ref "micromega.PExpr.PEpow")
272 let rocq_PX = lazy (constr_of_ref "micromega.Pol.PX")
273 let rocq_Pc = lazy (constr_of_ref "micromega.Pol.Pc")
274 let rocq_Pinj = lazy (constr_of_ref "micromega.Pol.Pinj")
275 let rocq_OpEq = lazy (constr_of_ref "micromega.Op2.OpEq")
276 let rocq_OpNEq = lazy (constr_of_ref "micromega.Op2.OpNEq")
277 let rocq_OpLe = lazy (constr_of_ref "micromega.Op2.OpLe")
278 let rocq_OpLt = lazy (constr_of_ref "micromega.Op2.OpLt")
279 let rocq_OpGe = lazy (constr_of_ref "micromega.Op2.OpGe")
280 let rocq_OpGt = lazy (constr_of_ref "micromega.Op2.OpGt")
281 let rocq_PsatzLet = lazy (constr_of_ref "micromega.Psatz.PsatzLet")
282 let rocq_PsatzIn = lazy (constr_of_ref "micromega.Psatz.PsatzIn")
283 let rocq_PsatzSquare = lazy (constr_of_ref "micromega.Psatz.PsatzSquare")
284 let rocq_PsatzMulE = lazy (constr_of_ref "micromega.Psatz.PsatzMulE")
285 let rocq_PsatzMultC = lazy (constr_of_ref "micromega.Psatz.PsatzMulC")
286 let rocq_PsatzAdd = lazy (constr_of_ref "micromega.Psatz.PsatzAdd")
287 let rocq_PsatzC = lazy (constr_of_ref "micromega.Psatz.PsatzC")
288 let rocq_PsatzZ = lazy (constr_of_ref "micromega.Psatz.PsatzZ")
289
290 (* let rocq_GT = lazy (m_constant "GT")*)
291
292 let rocq_DeclaredConstant =
293 lazy (constr_of_ref "micromega.DeclaredConstant.type")
294
295 let rocq_TT = lazy (constr_of_ref "micromega.GFormula.TT")
296 let rocq_FF = lazy (constr_of_ref "micromega.GFormula.FF")
297 let rocq_AND = lazy (constr_of_ref "micromega.GFormula.AND")
298 let rocq_OR = lazy (constr_of_ref "micromega.GFormula.OR")
299 let rocq_NOT = lazy (constr_of_ref "micromega.GFormula.NOT")
300 let rocq_Atom = lazy (constr_of_ref "micromega.GFormula.A")
301 let rocq_X = lazy (constr_of_ref "micromega.GFormula.X")
302 let rocq_IMPL = lazy (constr_of_ref "micromega.GFormula.IMPL")
303 let rocq_IFF = lazy (constr_of_ref "micromega.GFormula.IFF")
304 let rocq_EQ = lazy (constr_of_ref "micromega.GFormula.EQ")
305 let rocq_Formula = lazy (constr_of_ref "micromega.BFormula.type")
306 let rocq_eKind = lazy (constr_of_ref "micromega.eKind")
307
308 (**
309 * Initialization : a few Caml symbols are derived from other libraries;
310 * QMicromega, ZArithRing, RingMicromega.
311 *)
312
313 let rocq_QWitness = lazy (constr_of_ref "micromega.QWitness.type")
314 let rocq_Build = lazy (constr_of_ref "micromega.Formula.Build_Formula")
315 let rocq_Cstr = lazy (constr_of_ref "micromega.Formula.type")
316
317 (**
318 * Parsing and dumping : transformation functions between Caml and Rocq
319 * data-structures.
320 *
321 * dump_* functions go from Micromega to Rocq terms
322 * undump_* functions go from Rocq to Micromega terms (reverse of dump_)
323 * parse_* functions go from Rocq to Micromega terms
324 * pp_* functions pretty-print Rocq terms.
325 *)
326
327 exception ParseError
328
329 (* A simple but useful getter function *)
330
331 let get_left_construct sigma term =
332 match EConstr.kind sigma term with
333 | Construct ((_, i), _) -> (i, [||])
334 | App (l, rst) -> (
335 match EConstr.kind sigma l with
336 | Construct ((_, i), _) -> (i, rst)
337 | _ -> raise ParseError )
338 | _ -> raise ParseError
339
340 (* Access the Micromega module *)
341
342 (* parse/dump/print from numbers up to expressions and formulas *)
343
344 let rec parse_nat sigma term =
345 let i, c = get_left_construct sigma term in
346 match i with
347 | 1 -> Mc.O
348 | 2 -> Mc.S (parse_nat sigma c.(0))
349 | i -> raise ParseError
350
351 let rec dump_nat x =
352 match x with
353 | Mc.O -> Lazy.force rocq_O
354 | Mc.S p -> EConstr.mkApp (Lazy.force rocq_S, [|dump_nat p|])
355
356 let rec parse_positive sigma term =
357 let i, c = get_left_construct sigma term in
358 match i with
359 | 1 -> Mc.XI (parse_positive sigma c.(0))
360 | 2 -> Mc.XO (parse_positive sigma c.(0))
361 | 3 -> Mc.XH
362 | i -> raise ParseError
363
364 let rec dump_positive x =
365 match x with
366 | Mc.XH -> Lazy.force rocq_xH
367 | Mc.XO p -> EConstr.mkApp (Lazy.force rocq_xO, [|dump_positive p|])
368 | Mc.XI p -> EConstr.mkApp (Lazy.force rocq_xI, [|dump_positive p|])
369
370 let parse_n sigma term =
371 let i, c = get_left_construct sigma term in
372 match i with
373 | 1 -> Mc.N0
374 | 2 -> Mc.Npos (parse_positive sigma c.(0))
375 | i -> raise ParseError
376
377 let dump_n x =
378 match x with
379 | Mc.N0 -> Lazy.force rocq_N0
380 | Mc.Npos p -> EConstr.mkApp (Lazy.force rocq_Npos, [|dump_positive p|])
381
382 (** [is_ground_term env sigma term] holds if the term [term]
383 is an instance of the typeclass [DeclConstant.GT term]
384 i.e. built from user-defined constants and functions.
385 NB: This mechanism can be used to customise the reification process to decide
386 what to consider as a constant (see [parse_constant])
387 *)
388
389 let is_declared_term env evd t =
390 match EConstr.kind evd t with
391 | Const _ | Construct _ -> (
392 (* Restrict typeclass resolution to trivial cases *)
393 let typ = Retyping.get_type_of env evd t in
394 try
395 ignore
396 (Class_tactics.resolve_one_typeclass env evd
397 (EConstr.mkApp (Lazy.force rocq_DeclaredConstant, [|typ; t|])));
398 true
399 with Not_found -> false )
400 | _ -> false
401
402 let rec is_ground_term env evd term =
403 match EConstr.kind evd term with
404 | App (c, args) ->
405 is_declared_term env evd c && Array.for_all (is_ground_term env evd) args
406 | Const _ | Construct _ -> is_declared_term env evd term
407 | _ -> false
408
409 let parse_z sigma term =
410 let i, c = get_left_construct sigma term in
411 match i with
412 | 1 -> Mc.Z0
413 | 2 -> Mc.Zpos (parse_positive sigma c.(0))
414 | 3 -> Mc.Zneg (parse_positive sigma c.(0))
415 | i -> raise ParseError
416
417 let dump_z x =
418 match x with
419 | Mc.Z0 -> Lazy.force rocq_ZERO
420 | Mc.Zpos p -> EConstr.mkApp (Lazy.force rocq_POS, [|dump_positive p|])
421 | Mc.Zneg p -> EConstr.mkApp (Lazy.force rocq_NEG, [|dump_positive p|])
422
423
424 let dump_q q =
425 EConstr.mkApp
426 ( Lazy.force rocq_Qmake
427 , [|dump_z q.Micromega.qnum; dump_positive q.Micromega.qden|] )
428
429 let parse_q sigma term =
430 match EConstr.kind sigma term with
431 | App (c, args) ->
432 if EConstr.eq_constr sigma c (Lazy.force rocq_Qmake) then
433 {Mc.qnum = parse_z sigma args.(0); Mc.qden = parse_positive sigma args.(1)}
434 else raise ParseError
435 | _ -> raise ParseError
436
437 let rec pp_Rcst o cst =
438 match cst with
439 | Mc.C0 -> output_string o "C0"
440 | Mc.C1 -> output_string o "C1"
441 | Mc.CQ q -> output_string o "CQ _"
442 | Mc.CZ z -> pp_z o z
443 | Mc.CPlus (x, y) -> Printf.fprintf o "(%a + %a)" pp_Rcst x pp_Rcst y
444 | Mc.CMinus (x, y) -> Printf.fprintf o "(%a - %a)" pp_Rcst x pp_Rcst y
445 | Mc.CMult (x, y) -> Printf.fprintf o "(%a * %a)" pp_Rcst x pp_Rcst y
446 | Mc.CPow (x, y) -> Printf.fprintf o "(%a ^ _)" pp_Rcst x
447 | Mc.CInv t -> Printf.fprintf o "(/ %a)" pp_Rcst t
448 | Mc.COpp t -> Printf.fprintf o "(- %a)" pp_Rcst t
449
450 let rec dump_Rcst cst =
451 match cst with
452 | Mc.C0 -> Lazy.force rocq_C0
453 | Mc.C1 -> Lazy.force rocq_C1
454 | Mc.CQ q -> EConstr.mkApp (Lazy.force rocq_CQ, [|dump_q q|])
455 | Mc.CZ z -> EConstr.mkApp (Lazy.force rocq_CZ, [|dump_z z|])
456 | Mc.CPlus (x, y) ->
457 EConstr.mkApp (Lazy.force rocq_CPlus, [|dump_Rcst x; dump_Rcst y|])
458 | Mc.CMinus (x, y) ->
459 EConstr.mkApp (Lazy.force rocq_CMinus, [|dump_Rcst x; dump_Rcst y|])
460 | Mc.CMult (x, y) ->
461 EConstr.mkApp (Lazy.force rocq_CMult, [|dump_Rcst x; dump_Rcst y|])
462 | Mc.CPow (x, y) ->
463 EConstr.mkApp
464 ( Lazy.force rocq_CPow
465 , [| dump_Rcst x
466 ; ( match y with
467 | Mc.Inl z ->
468 EConstr.mkApp
469 ( Lazy.force rocq_Inl
470 , [|Lazy.force rocq_Z; Lazy.force rocq_nat; dump_z z|] )
471 | Mc.Inr n ->
472 EConstr.mkApp
473 ( Lazy.force rocq_Inr
474 , [|Lazy.force rocq_Z; Lazy.force rocq_nat; dump_nat n|] ) ) |] )
475 | Mc.CInv t -> EConstr.mkApp (Lazy.force rocq_CInv, [|dump_Rcst t|])
476 | Mc.COpp t -> EConstr.mkApp (Lazy.force rocq_COpp, [|dump_Rcst t|])
477
478 let rec dump_list typ dump_elt l =
479 match l with
480 | [] -> EConstr.mkApp (Lazy.force rocq_nil, [|typ|])
481 | e :: l ->
482 EConstr.mkApp
483 (Lazy.force rocq_cons, [|typ; dump_elt e; dump_list typ dump_elt l|])
484
485
486 let undump_var = parse_positive
487
488 let dump_var = dump_positive
489
490 let undump_expr undump_constant sigma e =
491 let is c c' = EConstr.eq_constr sigma c (Lazy.force c') in
492 let rec xundump e =
493 match EConstr.kind sigma e with
494 | App (c, [|_; n|]) when is c rocq_PEX -> Mc.PEX (undump_var sigma n)
495 | App (c, [|_; z|]) when is c rocq_PEc -> Mc.PEc (undump_constant sigma z)
496 | App (c, [|_; e1; e2|]) when is c rocq_PEadd ->
497 Mc.PEadd (xundump e1, xundump e2)
498 | App (c, [|_; e1; e2|]) when is c rocq_PEsub ->
499 Mc.PEsub (xundump e1, xundump e2)
500 | App (c, [|_; e|]) when is c rocq_PEopp -> Mc.PEopp (xundump e)
501 | App (c, [|_; e1; e2|]) when is c rocq_PEmul ->
502 Mc.PEmul (xundump e1, xundump e2)
503 | App (c, [|_; e; n|]) when is c rocq_PEpow ->
504 Mc.PEpow (xundump e, parse_n sigma n)
505 | _ -> raise ParseError
506 in
507 xundump e
508
509 let dump_expr typ dump_z e =
510 let rec dump_expr e =
511 match e with
512 | Mc.PEX n -> EConstr.mkApp (Lazy.force rocq_PEX, [|typ; dump_var n|])
513 | Mc.PEc z -> EConstr.mkApp (Lazy.force rocq_PEc, [|typ; dump_z z|])
514 | Mc.PEadd (e1, e2) ->
515 EConstr.mkApp (Lazy.force rocq_PEadd, [|typ; dump_expr e1; dump_expr e2|])
516 | Mc.PEsub (e1, e2) ->
517 EConstr.mkApp (Lazy.force rocq_PEsub, [|typ; dump_expr e1; dump_expr e2|])
518 | Mc.PEopp e -> EConstr.mkApp (Lazy.force rocq_PEopp, [|typ; dump_expr e|])
519 | Mc.PEmul (e1, e2) ->
520 EConstr.mkApp (Lazy.force rocq_PEmul, [|typ; dump_expr e1; dump_expr e2|])
521 | Mc.PEpow (e, n) ->
522 EConstr.mkApp (Lazy.force rocq_PEpow, [|typ; dump_expr e; dump_n n|])
523 in
524 dump_expr e
525
526 let dump_pol typ dump_c e =
527 let rec dump_pol e =
528 match e with
529 | Mc.Pc n -> EConstr.mkApp (Lazy.force rocq_Pc, [|typ; dump_c n|])
530 | Mc.Pinj (p, pol) ->
531 EConstr.mkApp (Lazy.force rocq_Pinj, [|typ; dump_positive p; dump_pol pol|])
532 | Mc.PX (pol1, p, pol2) ->
533 EConstr.mkApp
534 ( Lazy.force rocq_PX
535 , [|typ; dump_pol pol1; dump_positive p; dump_pol pol2|] )
536 in
537 dump_pol e
538
539
540 (* let pp_clause pp_c o (f: 'cst clause) =
541 List.iter (fun ((p,_),(t,_)) -> Printf.fprintf o "(%a @%a)" (pp_pol pp_c) p Tag.pp t) f *)
542
543 let pp_clause_tag o (f : 'cst clause) =
544 List.iter (fun ((p, _), (t, _)) -> Printf.fprintf o "(_ @%a)" Tag.pp t) f
545
546 (* let pp_cnf pp_c o (f:'cst cnf) =
547 List.iter (fun l -> Printf.fprintf o "[%a]" (pp_clause pp_c) l) f *)
548
549 let pp_cnf_tag o (f : 'cst cnf) =
550 List.iter (fun l -> Printf.fprintf o "[%a]" pp_clause_tag l) f
551
552 let dump_psatz typ dump_z e =
553 let z = Lazy.force typ in
554 let rec dump_cone e =
555 match e with
556 | Mc.PsatzLet (e1, e2) ->
557 EConstr.mkApp (Lazy.force rocq_PsatzLet, [|z; dump_cone e1; dump_cone e2|])
558 | Mc.PsatzIn n -> EConstr.mkApp (Lazy.force rocq_PsatzIn, [|z; dump_nat n|])
559 | Mc.PsatzMulC (e, c) ->
560 EConstr.mkApp
561 (Lazy.force rocq_PsatzMultC, [|z; dump_pol z dump_z e; dump_cone c|])
562 | Mc.PsatzSquare e ->
563 EConstr.mkApp (Lazy.force rocq_PsatzSquare, [|z; dump_pol z dump_z e|])
564 | Mc.PsatzAdd (e1, e2) ->
565 EConstr.mkApp (Lazy.force rocq_PsatzAdd, [|z; dump_cone e1; dump_cone e2|])
566 | Mc.PsatzMulE (e1, e2) ->
567 EConstr.mkApp (Lazy.force rocq_PsatzMulE, [|z; dump_cone e1; dump_cone e2|])
568 | Mc.PsatzC p -> EConstr.mkApp (Lazy.force rocq_PsatzC, [|z; dump_z p|])
569 | Mc.PsatzZ -> EConstr.mkApp (Lazy.force rocq_PsatzZ, [|z|])
570 in
571 dump_cone e
572
573 let undump_op sigma c =
574 let i, c = get_left_construct sigma c in
575 match i with
576 | 1 -> Mc.OpEq
577 | 2 -> Mc.OpNEq
578 | 3 -> Mc.OpLe
579 | 4 -> Mc.OpGe
580 | 5 -> Mc.OpLt
581 | 6 -> Mc.OpGt
582 | _ -> raise ParseError
583
584 let dump_op = function
585 | Mc.OpEq -> Lazy.force rocq_OpEq
586 | Mc.OpNEq -> Lazy.force rocq_OpNEq
587 | Mc.OpLe -> Lazy.force rocq_OpLe
588 | Mc.OpGe -> Lazy.force rocq_OpGe
589 | Mc.OpGt -> Lazy.force rocq_OpGt
590 | Mc.OpLt -> Lazy.force rocq_OpLt
591
592 let undump_cstr undump_constant sigma c =
593 let is c c' = EConstr.eq_constr sigma c (Lazy.force c') in
594 match EConstr.kind sigma c with
595 | App (c, [|_; e1; o; e2|]) when is c rocq_Build ->
596 {Mc.flhs = undump_expr undump_constant sigma e1;
597 Mc.fop = undump_op sigma o;
598 Mc.frhs = undump_expr undump_constant sigma e2}
599 | _ -> raise ParseError
600
601 let dump_cstr typ dump_constant {Mc.flhs = e1; Mc.fop = o; Mc.frhs = e2} =
602 EConstr.mkApp
603 ( Lazy.force rocq_Build
604 , [| typ
605 ; dump_expr typ dump_constant e1
606 ; dump_op o
607 ; dump_expr typ dump_constant e2 |] )
608
609 let assoc_const sigma x l =
610 try
611 snd (List.find (fun (x', y) -> EConstr.eq_constr sigma x (Lazy.force x')) l)
612 with Not_found -> raise ParseError
613
614 let zop_table_prop =
615 [ (rocq_Zgt, Mc.OpGt)
616 ; (rocq_Zge, Mc.OpGe)
617 ; (rocq_Zlt, Mc.OpLt)
618 ; (rocq_Zle, Mc.OpLe) ]
619
620 let zop_table_bool =
621 [ (rocq_Zgtb, Mc.OpGt)
622 ; (rocq_Zgeb, Mc.OpGe)
623 ; (rocq_Zltb, Mc.OpLt)
624 ; (rocq_Zleb, Mc.OpLe)
625 ; (rocq_Zeqb, Mc.OpEq) ]
626
627 let rop_table_prop =
628 [ (rocq_Rgt, Mc.OpGt)
629 ; (rocq_Rge, Mc.OpGe)
630 ; (rocq_Rlt, Mc.OpLt)
631 ; (rocq_Rle, Mc.OpLe) ]
632
633 let rop_table_bool = []
634 let qop_table_prop = [(rocq_Qlt, Mc.OpLt); (rocq_Qle, Mc.OpLe); (rocq_Qeq, Mc.OpEq)]
635 let qop_table_bool = []
636
637 type gl = Environ.env * Evd.evar_map
638
639 let is_convertible env sigma t1 t2 = Reductionops.is_conv env sigma t1 t2
640
641 let parse_operator table_prop table_bool has_equality typ (env, sigma) k
642 (op, args) =
643 match args with
644 | [|a1; a2|] ->
645 ( assoc_const sigma op
646 (match k with Mc.IsProp -> table_prop | Mc.IsBool -> table_bool)
647 , a1
648 , a2 )
649 | [|ty; a1; a2|] ->
650 if
651 has_equality
652 && EConstr.eq_constr sigma op (Lazy.force rocq_eq)
653 && is_convertible env sigma ty (Lazy.force typ)
654 then (Mc.OpEq, args.(1), args.(2))
655 else raise ParseError
656 | _ -> raise ParseError
657
658 let parse_zop = parse_operator zop_table_prop zop_table_bool true rocq_Z
659 let parse_rop = parse_operator rop_table_prop [] true rocq_R
660 let parse_qop = parse_operator qop_table_prop [] false rocq_R
661
662 type 'a op =
663 | Binop of ('a Mc.pExpr -> 'a Mc.pExpr -> 'a Mc.pExpr)
664 | Opp
665 | Power
666 | Ukn of string
667
668 let assoc_ops sigma x l =
669 try
670 snd (List.find (fun (x', y) -> EConstr.eq_constr sigma x (Lazy.force x')) l)
671 with Not_found -> Ukn "Oups"
672
673 (**
674 * MODULE: Env is for environment.
675 *)
676
677 module Env = struct
678 type t =
679 { vars : (EConstr.t * Mc.kind) list
680 ; (* The list represents a mapping from EConstr.t to indexes. *)
681 gl : gl (* The evar_map may be updated due to unification of universes *)
682 }
683
684 let empty gl = {vars = []; gl}
685
686 (** [eq_constr gl x y] returns an updated [gl] if x and y can be unified *)
687 let eq_constr (env, sigma) x y =
688 match EConstr.eq_constr_universes_proj env sigma x y with
689 | Some csts -> (
690 let csts = UnivProblem.Set.force csts in
691 match Evd.add_constraints sigma csts with
692 | sigma -> Some (env, sigma)
693 | exception UGraph.UniverseInconsistency _ -> None )
694 | None -> None
695
696 let compute_rank_add env v is_prop =
697 let rec add gl vars n v =
698 match vars with
699 | [] -> (gl, [(v, is_prop)], n)
700 | (e, b) :: l -> (
701 match eq_constr gl e v with
702 | Some gl' -> (gl', vars, n)
703 | None ->
704 let gl, l', n = add gl l (n + 1) v in
705 (gl, (e, b) :: l', n) )
706 in
707 let gl', vars', n = add env.gl env.vars 1 v in
708 ({vars = vars'; gl = gl'}, CamlToCoq.positive n)
709
710 let get_rank env v =
711 let gl = env.gl in
712 let rec get_rank env n =
713 match env with
714 | [] -> raise (Invalid_argument "get_rank")
715 | (e, _) :: l -> (
716 match eq_constr gl e v with Some _ -> n | None -> get_rank l (n + 1) )
717 in
718 get_rank env.vars 1
719
720 let elements env = env.vars
721
722 (* let string_of_env gl env =
723 let rec string_of_env i env acc =
724 match env with
725 | [] -> acc
726 | e::env -> string_of_env (i+1) env
727 (IMap.add i
728 (Pp.string_of_ppcmds
729 (Printer.pr_econstr_env gl.env gl.sigma e)) acc) in
730 string_of_env 1 env IMap.empty
731 *)
732 let pp (genv, sigma) env =
733 let ppl =
734 List.mapi
735 (fun i (e, _) ->
736 Pp.str "x"
737 ++ Pp.int (i + 1)
738 ++ Pp.str ":"
739 ++ Printer.pr_econstr_env genv sigma e)
740 env
741 in
742 List.fold_right (fun e p -> e ++ Pp.str " ; " ++ p) ppl (Pp.str "\n")
743 end
744
745 (* MODULE END: Env *)
746
747 (**
748 * This is the big generic function for expression parsers.
749 *)
750
751 let parse_expr (genv, sigma) parse_constant parse_exp ops_spec env term =
752 if debug then
753 Feedback.msg_debug
754 (Pp.str "parse_expr: " ++ Printer.pr_leconstr_env genv sigma term);
755 let parse_variable env term =
756 let env, n = Env.compute_rank_add env term Mc.IsBool in
757 (Mc.PEX n, env)
758 in
759 let rec parse_expr env term =
760 let combine env op (t1, t2) =
761 let expr1, env = parse_expr env t1 in
762 let expr2, env = parse_expr env t2 in
763 (op expr1 expr2, env)
764 in
765 try (Mc.PEc (parse_constant (genv, sigma) term), env)
766 with ParseError -> (
767 match EConstr.kind sigma term with
768 | App (t, args) -> (
769 match EConstr.kind sigma t with
770 | Const c -> (
771 match assoc_ops sigma t ops_spec with
772 | Binop f -> combine env f (args.(0), args.(1))
773 | Opp ->
774 let expr, env = parse_expr env args.(0) in
775 (Mc.PEopp expr, env)
776 | Power -> (
777 try
778 let expr, env = parse_expr env args.(0) in
779 let power = parse_exp expr args.(1) in
780 (power, env)
781 with ParseError ->
782 (* if the exponent is a variable *)
783 let env, n = Env.compute_rank_add env term Mc.IsBool in
784 (Mc.PEX n, env) )
785 | Ukn s ->
786 if debug then (
787 Printf.printf "unknown op: %s\n" s;
788 flush stdout );
789 let env, n = Env.compute_rank_add env term Mc.IsBool in
790 (Mc.PEX n, env) )
791 | _ -> parse_variable env term )
792 | _ -> parse_variable env term )
793 in
794 parse_expr env term
795
796 let zop_spec =
797 [ (rocq_Zplus, Binop (fun x y -> Mc.PEadd (x, y)))
798 ; (rocq_Zminus, Binop (fun x y -> Mc.PEsub (x, y)))
799 ; (rocq_Zmult, Binop (fun x y -> Mc.PEmul (x, y)))
800 ; (rocq_Zopp, Opp)
801 ; (rocq_Zpower, Power) ]
802
803 let qop_spec =
804 [ (rocq_Qplus, Binop (fun x y -> Mc.PEadd (x, y)))
805 ; (rocq_Qminus, Binop (fun x y -> Mc.PEsub (x, y)))
806 ; (rocq_Qmult, Binop (fun x y -> Mc.PEmul (x, y)))
807 ; (rocq_Qopp, Opp)
808 ; (rocq_Qpower, Power) ]
809
810 let rop_spec =
811 [ (rocq_Rplus, Binop (fun x y -> Mc.PEadd (x, y)))
812 ; (rocq_Rminus, Binop (fun x y -> Mc.PEsub (x, y)))
813 ; (rocq_Rmult, Binop (fun x y -> Mc.PEmul (x, y)))
814 ; (rocq_Ropp, Opp)
815 ; (rocq_Rpower, Power) ]
816
817 let parse_constant parse ((genv : Environ.env), sigma) t = parse sigma t
818
819 (** [parse_more_constant parse gl t] returns the reification of term [t].
820 If [t] is a ground term, then it is first reduced to normal form
821 before using a 'syntactic' parser *)
822 let parse_more_constant parse (genv, sigma) t =
823 try parse (genv, sigma) t
824 with ParseError ->
825 if debug then Feedback.msg_debug Pp.(str "try harder");
826 if is_ground_term genv sigma t then
827 parse (genv, sigma) (Redexpr.cbv_vm genv sigma t)
828 else raise ParseError
829
830 let zconstant = parse_constant parse_z
831 let qconstant = parse_constant parse_q
832 let nconstant = parse_constant parse_nat
833
834 (** [parse_more_zexpr parse_constant gl] improves the parsing of exponent
835 which can be arithmetic expressions (without variables).
836 [parse_constant_expr] returns a constant if the argument is an expression without variables. *)
837
838 let rec parse_zexpr gl =
839 parse_expr gl zconstant
840 (fun expr (x : EConstr.t) ->
841 let z = parse_zconstant gl x in
842 match z with
843 | Mc.Zneg _ -> Mc.PEc Mc.Z0
844 | _ -> Mc.PEpow (expr, Mc.Z.to_N z))
845 zop_spec
846
847 and parse_zconstant gl e =
848 let e, _ = parse_zexpr gl (Env.empty gl) e in
849 match Mc.zeval_const e with None -> raise ParseError | Some z -> z
850
851 (* NB: R is a different story.
852 Because it is axiomatised, reducing would not be effective.
853 Therefore, there is a specific parser for constant over R
854 *)
855
856 let rconst_assoc =
857 [ (rocq_Rplus, fun x y -> Mc.CPlus (x, y))
858 ; (rocq_Rminus, fun x y -> Mc.CMinus (x, y))
859 ; (rocq_Rmult, fun x y -> Mc.CMult (x, y))
860 (* rocq_Rdiv , (fun x y -> Mc.CMult(x,Mc.CInv y)) ;*) ]
861
862 let rconstant (genv, sigma) term =
863 let rec rconstant term =
864 match EConstr.kind sigma term with
865 | Const x ->
866 if EConstr.eq_constr sigma term (Lazy.force rocq_R0) then Mc.C0
867 else if EConstr.eq_constr sigma term (Lazy.force rocq_R1) then Mc.C1
868 else raise ParseError
869 | App (op, args) -> (
870 try
871 (* the evaluation order is important in the following *)
872 let f = assoc_const sigma op rconst_assoc in
873 let a = rconstant args.(0) in
874 let b = rconstant args.(1) in
875 f a b
876 with ParseError -> (
877 match op with
878 | op when EConstr.eq_constr sigma op (Lazy.force rocq_Rinv) ->
879 let arg = rconstant args.(0) in
880 if Mc.qeq_bool (Mc.q_of_Rcst arg) {Mc.qnum = Mc.Z0; Mc.qden = Mc.XH}
881 then raise ParseError (* This is a division by zero -- no semantics *)
882 else Mc.CInv arg
883 | op when EConstr.eq_constr sigma op (Lazy.force rocq_Rpower) ->
884 Mc.CPow
885 ( rconstant args.(0)
886 , Mc.Inr (parse_more_constant nconstant (genv, sigma) args.(1)) )
887 | op when EConstr.eq_constr sigma op (Lazy.force rocq_IQR) ->
888 Mc.CQ (qconstant (genv, sigma) args.(0))
889 | op when EConstr.eq_constr sigma op (Lazy.force rocq_IZR) ->
890 Mc.CZ (parse_more_constant zconstant (genv, sigma) args.(0))
891 | _ -> raise ParseError ) )
892 | _ -> raise ParseError
893 in
894 rconstant term
895
896 let rconstant (genv, sigma) term =
897 if debug then
898 Feedback.msg_debug
899 (Pp.str "rconstant: " ++ Printer.pr_leconstr_env genv sigma term ++ fnl ());
900 let res = rconstant (genv, sigma) term in
901 if debug then (
902 Printf.printf "rconstant -> %a\n" pp_Rcst res;
903 flush stdout );
904 res
905
906 let parse_qexpr gl =
907 parse_expr gl qconstant
908 (fun expr x ->
909 let exp = zconstant gl x in
910 match exp with
911 | Mc.Zneg _ -> (
912 match expr with
913 | Mc.PEc q -> Mc.PEc (Mc.qpower q exp)
914 | _ -> raise ParseError )
915 | _ ->
916 let exp = Mc.Z.to_N exp in
917 Mc.PEpow (expr, exp))
918 qop_spec
919
920 let parse_rexpr (genv, sigma) =
921 parse_expr (genv, sigma) rconstant
922 (fun expr x ->
923 let exp = Mc.N.of_nat (parse_nat sigma x) in
924 Mc.PEpow (expr, exp))
925 rop_spec
926
927 let parse_arith parse_op parse_expr (k : Mc.kind) env cstr (genv, sigma) =
928 if debug then
929 Feedback.msg_debug
930 ( Pp.str "parse_arith: "
931 ++ Printer.pr_leconstr_env genv sigma cstr
932 ++ fnl () );
933 match EConstr.kind sigma cstr with
934 | App (op, args) ->
935 let op, lhs, rhs = parse_op (genv, sigma) k (op, args) in
936 let e1, env = parse_expr (genv, sigma) env lhs in
937 let e2, env = parse_expr (genv, sigma) env rhs in
938 ({Mc.flhs = e1; Mc.fop = op; Mc.frhs = e2}, env)
939 | _ -> failwith "error : parse_arith(2)"
940
941 let parse_zarith = parse_arith parse_zop parse_zexpr
942 let parse_qarith = parse_arith parse_qop parse_qexpr
943 let parse_rarith = parse_arith parse_rop parse_rexpr
944
945 (* generic parsing of arithmetic expressions *)
946
947 let mkAND b f1 f2 = Mc.AND (b, f1, f2)
948 let mkOR b f1 f2 = Mc.OR (b, f1, f2)
949 let mkIff b f1 f2 = Mc.IFF (b, f1, f2)
950 let mkIMPL b f1 f2 = Mc.IMPL (b, f1, None, f2)
951 let mkEQ f1 f2 = Mc.EQ (f1, f2)
952
953 let mkformula_binary b g term f1 f2 =
954 match (f1, f2) with
955 | Mc.X (b1, _), Mc.X (b2, _) -> Mc.X (b, term)
956 | _ -> g f1 f2
957
958 (**
959 * This is the big generic function for formula parsers.
960 *)
961
962 let is_prop env sigma term =
963 let sort = Retyping.get_sort_of env sigma term in
964 EConstr.ESorts.is_prop sigma sort
965
966 type formula_op =
967 { op_impl : EConstr.t option (* only for booleans *)
968 ; op_and : EConstr.t
969 ; op_or : EConstr.t
970 ; op_iff : EConstr.t
971 ; op_not : EConstr.t
972 ; op_tt : EConstr.t
973 ; op_ff : EConstr.t }
974
975 let prop_op =
976 lazy
977 { op_impl = None (* implication is Prod *)
978 ; op_and = Lazy.force rocq_and
979 ; op_or = Lazy.force rocq_or
980 ; op_iff = Lazy.force rocq_iff
981 ; op_not = Lazy.force rocq_not
982 ; op_tt = Lazy.force rocq_True
983 ; op_ff = Lazy.force rocq_False }
984
985 let bool_op =
986 lazy
987 { op_impl = Some (Lazy.force rocq_implb)
988 ; op_and = Lazy.force rocq_andb
989 ; op_or = Lazy.force rocq_orb
990 ; op_iff = Lazy.force rocq_eqb
991 ; op_not = Lazy.force rocq_negb
992 ; op_tt = Lazy.force rocq_true
993 ; op_ff = Lazy.force rocq_false }
994
995 let is_implb sigma l o =
996 match o with None -> false | Some c -> EConstr.eq_constr sigma l c
997
998 let parse_formula (genv, sigma) parse_atom env tg term =
999 let parse_atom b env tg t =
1000 try
1001 let at, env = parse_atom b env t (genv, sigma) in
1002 (Mc.A (b, at, (tg, t)), env, Tag.next tg)
1003 with ParseError -> (Mc.X (b, t), env, tg)
1004 in
1005 let prop_op = Lazy.force prop_op in
1006 let bool_op = Lazy.force bool_op in
1007 let eq = Lazy.force rocq_eq in
1008 let bool = Lazy.force rocq_bool in
1009 let rec xparse_formula op k env tg term =
1010 match EConstr.kind sigma term with
1011 | App (l, rst) -> (
1012 match rst with
1013 | [|a; b|] when is_implb sigma l op.op_impl ->
1014 let f, env, tg = xparse_formula op k env tg a in
1015 let g, env, tg = xparse_formula op k env tg b in
1016 (mkformula_binary k (mkIMPL k) term f g, env, tg)
1017 | [|a; b|] when EConstr.eq_constr sigma l op.op_and ->
1018 let f, env, tg = xparse_formula op k env tg a in
1019 let g, env, tg = xparse_formula op k env tg b in
1020 (mkformula_binary k (mkAND k) term f g, env, tg)
1021 | [|a; b|] when EConstr.eq_constr sigma l op.op_or ->
1022 let f, env, tg = xparse_formula op k env tg a in
1023 let g, env, tg = xparse_formula op k env tg b in
1024 (mkformula_binary k (mkOR k) term f g, env, tg)
1025 | [|a; b|] when EConstr.eq_constr sigma l op.op_iff ->
1026 let f, env, tg = xparse_formula op k env tg a in
1027 let g, env, tg = xparse_formula op k env tg b in
1028 (mkformula_binary k (mkIff k) term f g, env, tg)
1029 | [|ty; a; b|]
1030 when EConstr.eq_constr sigma l eq && is_convertible genv sigma ty bool
1031 ->
1032 let f, env, tg = xparse_formula bool_op Mc.IsBool env tg a in
1033 let g, env, tg = xparse_formula bool_op Mc.IsBool env tg b in
1034 (mkformula_binary Mc.IsProp mkEQ term f g, env, tg)
1035 | [|a|] when EConstr.eq_constr sigma l op.op_not ->
1036 let f, env, tg = xparse_formula op k env tg a in
1037 (Mc.NOT (k, f), env, tg)
1038 | _ -> parse_atom k env tg term )
1039 | Prod (typ, a, b)
1040 when kind_is_prop k
1041 && (typ.binder_name = Anonymous || EConstr.Vars.noccurn sigma 1 b) ->
1042 let f, env, tg = xparse_formula op k env tg a in
1043 let g, env, tg = xparse_formula op k env tg b in
1044 (mkformula_binary Mc.IsProp (mkIMPL Mc.IsProp) term f g, env, tg)
1045 | _ ->
1046 if EConstr.eq_constr sigma term op.op_tt then (Mc.TT k, env, tg)
1047 else if EConstr.eq_constr sigma term op.op_ff then Mc.(FF k, env, tg)
1048 else (Mc.X (k, term), env, tg)
1049 in
1050 xparse_formula prop_op Mc.IsProp env tg (*Reductionops.whd_zeta*) term
1051
1052 (* let dump_bool b = Lazy.force (if b then rocq_true else rocq_false)*)
1053
1054 let undump_kind sigma k =
1055 if EConstr.eq_constr sigma k (Lazy.force rocq_IsProp) then Mc.IsProp
1056 else Mc.IsBool
1057
1058 let dump_kind k =
1059 Lazy.force (match k with Mc.IsProp -> rocq_IsProp | Mc.IsBool -> rocq_IsBool)
1060
1061 let undump_formula undump_atom tg sigma f =
1062 let is c c' = EConstr.eq_constr sigma c (Lazy.force c') in
1063 let kind k = undump_kind sigma k in
1064 let rec xundump f =
1065 match EConstr.kind sigma f with
1066 | App (c, [|_; _; _; _; k|]) when is c rocq_TT -> Mc.TT (kind k)
1067 | App (c, [|_; _; _; _; k|]) when is c rocq_FF -> Mc.FF (kind k)
1068 | App (c, [|_; _; _; _; k; f1; f2|]) when is c rocq_AND ->
1069 Mc.AND (kind k, xundump f1, xundump f2)
1070 | App (c, [|_; _; _; _; k; f1; f2|]) when is c rocq_OR ->
1071 Mc.OR (kind k, xundump f1, xundump f2)
1072 | App (c, [|_; _; _; _; k; f1; _; f2|]) when is c rocq_IMPL ->
1073 Mc.IMPL (kind k, xundump f1, None, xundump f2)
1074 | App (c, [|_; _; _; _; k; f|]) when is c rocq_NOT ->
1075 Mc.NOT (kind k, xundump f)
1076 | App (c, [|_; _; _; _; k; f1; f2|]) when is c rocq_IFF ->
1077 Mc.IFF (kind k, xundump f1, xundump f2)
1078 | App (c, [|_; _; _; _; f1; f2|]) when is c rocq_EQ ->
1079 Mc.EQ (xundump f1, xundump f2)
1080 | App (c, [|_; _; _; _; k; x; _|]) when is c rocq_Atom ->
1081 Mc.A (kind k, undump_atom sigma x, tg)
1082 | App (c, [|_; _; _; _; k; x|]) when is c rocq_X ->
1083 Mc.X (kind k, x)
1084 | _ -> raise ParseError
1085 in
1086 xundump f
1087
1088 let dump_formula typ dump_atom f =
1089 let app_ctor c args =
1090 EConstr.mkApp
1091 ( Lazy.force c
1092 , Array.of_list
1093 ( typ :: Lazy.force rocq_eKind :: Lazy.force rocq_unit
1094 :: Lazy.force rocq_unit :: args ) )
1095 in
1096 let rec xdump f =
1097 match f with
1098 | Mc.TT k -> app_ctor rocq_TT [dump_kind k]
1099 | Mc.FF k -> app_ctor rocq_FF [dump_kind k]
1100 | Mc.AND (k, x, y) -> app_ctor rocq_AND [dump_kind k; xdump x; xdump y]
1101 | Mc.OR (k, x, y) -> app_ctor rocq_OR [dump_kind k; xdump x; xdump y]
1102 | Mc.IMPL (k, x, _, y) ->
1103 app_ctor rocq_IMPL
1104 [ dump_kind k
1105 ; xdump x
1106 ; EConstr.mkApp (Lazy.force rocq_None, [|Lazy.force rocq_unit|])
1107 ; xdump y ]
1108 | Mc.NOT (k, x) -> app_ctor rocq_NOT [dump_kind k; xdump x]
1109 | Mc.IFF (k, x, y) -> app_ctor rocq_IFF [dump_kind k; xdump x; xdump y]
1110 | Mc.EQ (x, y) -> app_ctor rocq_EQ [xdump x; xdump y]
1111 | Mc.A (k, x, _) ->
1112 app_ctor rocq_Atom [dump_kind k; dump_atom x; Lazy.force rocq_tt]
1113 | Mc.X (k, t) -> app_ctor rocq_X [dump_kind k; t]
1114 in
1115 xdump f
1116
1117 let prop_env_of_formula gl form =
1118 Mc.(
1119 let rec doit env = function
1120 | TT _ | FF _ | A (_, _, _) -> env
1121 | X (b, t) -> fst (Env.compute_rank_add env t b)
1122 | AND (b, f1, f2) | OR (b, f1, f2) | IMPL (b, f1, _, f2) | IFF (b, f1, f2)
1123 ->
1124 doit (doit env f1) f2
1125 | NOT (b, f) -> doit env f
1126 | EQ (f1, f2) -> doit (doit env f1) f2
1127 in
1128 doit (Env.empty gl) form)
1129
1130 let var_env_of_formula form =
1131 let rec vars_of_expr = function
1132 | Mc.PEX n -> ISet.singleton (CoqToCaml.positive n)
1133 | Mc.PEc z -> ISet.empty
1134 | Mc.PEadd (e1, e2) | Mc.PEmul (e1, e2) | Mc.PEsub (e1, e2) ->
1135 ISet.union (vars_of_expr e1) (vars_of_expr e2)
1136 | Mc.PEopp e | Mc.PEpow (e, _) -> vars_of_expr e
1137 in
1138 let vars_of_atom {Mc.flhs; Mc.fop; Mc.frhs} =
1139 ISet.union (vars_of_expr flhs) (vars_of_expr frhs)
1140 in
1141 Mc.(
1142 let rec doit = function
1143 | TT _ | FF _ | X _ -> ISet.empty
1144 | A (_, a, (t, c)) -> vars_of_atom a
1145 | AND (_, f1, f2)
1146 |OR (_, f1, f2)
1147 |IMPL (_, f1, _, f2)
1148 |IFF (_, f1, f2)
1149 |EQ (f1, f2) ->
1150 ISet.union (doit f1) (doit f2)
1151 | NOT (_, f) -> doit f
1152 in
1153 doit form)
1154
1155 type 'cst dump_expr =
1156 { (* 'cst is the type of the syntactic constants *)
1157 interp_typ : EConstr.constr
1158 ; dump_cst : 'cst -> EConstr.constr
1159 ; dump_add : EConstr.constr
1160 ; dump_sub : EConstr.constr
1161 ; dump_opp : EConstr.constr
1162 ; dump_mul : EConstr.constr
1163 ; dump_pow : EConstr.constr
1164 ; dump_pow_arg : Mc.n -> EConstr.constr
1165 ; dump_op_prop : (Mc.op2 * EConstr.constr) list
1166 ; dump_op_bool : (Mc.op2 * EConstr.constr) list }
1167
1168 let dump_zexpr =
1169 lazy
1170 { interp_typ = Lazy.force rocq_Z
1171 ; dump_cst = dump_z
1172 ; dump_add = Lazy.force rocq_Zplus
1173 ; dump_sub = Lazy.force rocq_Zminus
1174 ; dump_opp = Lazy.force rocq_Zopp
1175 ; dump_mul = Lazy.force rocq_Zmult
1176 ; dump_pow = Lazy.force rocq_Zpower
1177 ; dump_pow_arg = (fun n -> dump_z (CamlToCoq.z (CoqToCaml.n n)))
1178 ; dump_op_prop = List.map (fun (x, y) -> (y, Lazy.force x)) zop_table_prop
1179 ; dump_op_bool = List.map (fun (x, y) -> (y, Lazy.force x)) zop_table_bool
1180 }
1181
1182 let dump_qexpr =
1183 lazy
1184 { interp_typ = Lazy.force rocq_Q
1185 ; dump_cst = dump_q
1186 ; dump_add = Lazy.force rocq_Qplus
1187 ; dump_sub = Lazy.force rocq_Qminus
1188 ; dump_opp = Lazy.force rocq_Qopp
1189 ; dump_mul = Lazy.force rocq_Qmult
1190 ; dump_pow = Lazy.force rocq_Qpower
1191 ; dump_pow_arg = (fun n -> dump_z (CamlToCoq.z (CoqToCaml.n n)))
1192 ; dump_op_prop = List.map (fun (x, y) -> (y, Lazy.force x)) qop_table_prop
1193 ; dump_op_bool = List.map (fun (x, y) -> (y, Lazy.force x)) qop_table_bool
1194 }
1195
1196 let rec dump_Rcst_as_R cst =
1197 match cst with
1198 | Mc.C0 -> Lazy.force rocq_R0
1199 | Mc.C1 -> Lazy.force rocq_R1
1200 | Mc.CQ q -> EConstr.mkApp (Lazy.force rocq_IQR, [|dump_q q|])
1201 | Mc.CZ z -> EConstr.mkApp (Lazy.force rocq_IZR, [|dump_z z|])
1202 | Mc.CPlus (x, y) ->
1203 EConstr.mkApp (Lazy.force rocq_Rplus, [|dump_Rcst_as_R x; dump_Rcst_as_R y|])
1204 | Mc.CMinus (x, y) ->
1205 EConstr.mkApp (Lazy.force rocq_Rminus, [|dump_Rcst_as_R x; dump_Rcst_as_R y|])
1206 | Mc.CMult (x, y) ->
1207 EConstr.mkApp (Lazy.force rocq_Rmult, [|dump_Rcst_as_R x; dump_Rcst_as_R y|])
1208 | Mc.CPow (x, y) -> (
1209 match y with
1210 | Mc.Inl z ->
1211 EConstr.mkApp (Lazy.force rocq_powerZR, [|dump_Rcst_as_R x; dump_z z|])
1212 | Mc.Inr n ->
1213 EConstr.mkApp (Lazy.force rocq_Rpower, [|dump_Rcst_as_R x; dump_nat n|]) )
1214 | Mc.CInv t -> EConstr.mkApp (Lazy.force rocq_Rinv, [|dump_Rcst_as_R t|])
1215 | Mc.COpp t -> EConstr.mkApp (Lazy.force rocq_Ropp, [|dump_Rcst_as_R t|])
1216
1217 let dump_rexpr =
1218 lazy
1219 { interp_typ = Lazy.force rocq_R
1220 ; dump_cst = dump_Rcst_as_R
1221 ; dump_add = Lazy.force rocq_Rplus
1222 ; dump_sub = Lazy.force rocq_Rminus
1223 ; dump_opp = Lazy.force rocq_Ropp
1224 ; dump_mul = Lazy.force rocq_Rmult
1225 ; dump_pow = Lazy.force rocq_Rpower
1226 ; dump_pow_arg = (fun n -> dump_nat (CamlToCoq.nat (CoqToCaml.n n)))
1227 ; dump_op_prop = List.map (fun (x, y) -> (y, Lazy.force x)) rop_table_prop
1228 ; dump_op_bool = List.map (fun (x, y) -> (y, Lazy.force x)) rop_table_bool
1229 }
1230
1231 let prodn n env b =
1232 let rec prodrec = function
1233 | 0, env, b -> b
1234 | n, (v, t) :: l, b ->
1235 prodrec (n - 1, l, EConstr.mkProd (make_annot v ERelevance.relevant, t, b))
1236 | _ -> assert false
1237 in
1238 prodrec (n, env, b)
1239
1240 (** [make_goal_of_formula depxr vars props form] where
1241 - vars is an environment for the arithmetic variables occurring in form
1242 - props is an environment for the propositions occurring in form
1243 @return a goal where all the variables and propositions of the formula are quantified
1244
1245 *)
1246
1247 let eKind = function
1248 | Mc.IsProp -> EConstr.mkProp
1249 | Mc.IsBool -> Lazy.force rocq_bool
1250
1251 let make_goal_of_formula gl dexpr form =
1252 let vars_idx =
1253 List.mapi (fun i v -> (v, i + 1)) (ISet.elements (var_env_of_formula form))
1254 in
1255 (* List.iter (fun (v,i) -> Printf.fprintf stdout "var %i has index %i\n" v i) vars_idx ;*)
1256 let props = prop_env_of_formula gl form in
1257 let fresh_var str i = Names.Id.of_string (str ^ string_of_int i) in
1258 let fresh_prop str i = Names.Id.of_string (str ^ string_of_int i) in
1259 let vars_n =
1260 List.map (fun (_, i) -> (fresh_var "__x" i, dexpr.interp_typ)) vars_idx
1261 in
1262 let props_n =
1263 List.mapi
1264 (fun i (_, k) -> (fresh_prop "__p" (i + 1), eKind k))
1265 (Env.elements props)
1266 in
1267 let var_name_pos =
1268 List.fold_left2 (fun acc (idx, _) (id, _) -> (id, idx) :: acc) [] vars_idx vars_n
1269 in
1270 let dump_expr i e =
1271 let rec dump_expr = function
1272 | Mc.PEX n ->
1273 EConstr.mkRel (i + List.assoc (CoqToCaml.positive n) vars_idx)
1274 | Mc.PEc z -> dexpr.dump_cst z
1275 | Mc.PEadd (e1, e2) ->
1276 EConstr.mkApp (dexpr.dump_add, [|dump_expr e1; dump_expr e2|])
1277 | Mc.PEsub (e1, e2) ->
1278 EConstr.mkApp (dexpr.dump_sub, [|dump_expr e1; dump_expr e2|])
1279 | Mc.PEopp e -> EConstr.mkApp (dexpr.dump_opp, [|dump_expr e|])
1280 | Mc.PEmul (e1, e2) ->
1281 EConstr.mkApp (dexpr.dump_mul, [|dump_expr e1; dump_expr e2|])
1282 | Mc.PEpow (e, n) ->
1283 EConstr.mkApp (dexpr.dump_pow, [|dump_expr e; dexpr.dump_pow_arg n|])
1284 in
1285 dump_expr e
1286 in
1287 let mkop_prop op e1 e2 =
1288 try EConstr.mkApp (List.assoc op dexpr.dump_op_prop, [|e1; e2|])
1289 with Not_found ->
1290 EConstr.mkApp (Lazy.force rocq_eq, [|dexpr.interp_typ; e1; e2|])
1291 in
1292 let dump_cstr_prop i {Mc.flhs; Mc.fop; Mc.frhs} =
1293 mkop_prop fop (dump_expr i flhs) (dump_expr i frhs)
1294 in
1295 let mkop_bool op e1 e2 =
1296 try EConstr.mkApp (List.assoc op dexpr.dump_op_bool, [|e1; e2|])
1297 with Not_found ->
1298 EConstr.mkApp (Lazy.force rocq_eq, [|dexpr.interp_typ; e1; e2|])
1299 in
1300 let dump_cstr_bool i {Mc.flhs; Mc.fop; Mc.frhs} =
1301 mkop_bool fop (dump_expr i flhs) (dump_expr i frhs)
1302 in
1303 let rec xdump_prop pi xi f =
1304 match f with
1305 | Mc.TT _ -> Lazy.force rocq_True
1306 | Mc.FF _ -> Lazy.force rocq_False
1307 | Mc.AND (_, x, y) ->
1308 EConstr.mkApp
1309 (Lazy.force rocq_and, [|xdump_prop pi xi x; xdump_prop pi xi y|])
1310 | Mc.OR (_, x, y) ->
1311 EConstr.mkApp
1312 (Lazy.force rocq_or, [|xdump_prop pi xi x; xdump_prop pi xi y|])
1313 | Mc.IFF (_, x, y) ->
1314 EConstr.mkApp
1315 (Lazy.force rocq_iff, [|xdump_prop pi xi x; xdump_prop pi xi y|])
1316 | Mc.IMPL (_, x, _, y) ->
1317 EConstr.mkArrow (xdump_prop pi xi x) ERelevance.relevant
1318 (xdump_prop (pi + 1) (xi + 1) y)
1319 | Mc.NOT (_, x) ->
1320 EConstr.mkArrow (xdump_prop pi xi x) ERelevance.relevant (Lazy.force rocq_False)
1321 | Mc.EQ (x, y) ->
1322 EConstr.mkApp
1323 ( Lazy.force rocq_eq
1324 , [|Lazy.force rocq_bool; xdump_bool pi xi x; xdump_bool pi xi y|] )
1325 | Mc.A (_, x, _) -> dump_cstr_prop xi x
1326 | Mc.X (_, t) ->
1327 let idx = Env.get_rank props t in
1328 EConstr.mkRel (pi + idx)
1329 and xdump_bool pi xi f =
1330 match f with
1331 | Mc.TT _ -> Lazy.force rocq_true
1332 | Mc.FF _ -> Lazy.force rocq_false
1333 | Mc.AND (_, x, y) ->
1334 EConstr.mkApp
1335 (Lazy.force rocq_andb, [|xdump_bool pi xi x; xdump_bool pi xi y|])
1336 | Mc.OR (_, x, y) ->
1337 EConstr.mkApp
1338 (Lazy.force rocq_orb, [|xdump_bool pi xi x; xdump_bool pi xi y|])
1339 | Mc.IFF (_, x, y) ->
1340 EConstr.mkApp
1341 (Lazy.force rocq_eqb, [|xdump_bool pi xi x; xdump_bool pi xi y|])
1342 | Mc.IMPL (_, x, _, y) ->
1343 EConstr.mkApp
1344 (Lazy.force rocq_implb, [|xdump_bool pi xi x; xdump_bool pi xi y|])
1345 | Mc.NOT (_, x) ->
1346 EConstr.mkApp (Lazy.force rocq_negb, [|xdump_bool pi xi x|])
1347 | Mc.EQ (x, y) -> assert false
1348 | Mc.A (_, x, _) -> dump_cstr_bool xi x
1349 | Mc.X (_, t) ->
1350 let idx = Env.get_rank props t in
1351 EConstr.mkRel (pi + idx)
1352 in
1353 let nb_vars = List.length vars_n in
1354 let nb_props = List.length props_n in
1355 (* Printf.fprintf stdout "NBProps : %i\n" nb_props ;*)
1356 let subst_prop p =
1357 let idx = Env.get_rank props p in
1358 EConstr.mkVar (Names.Id.of_string (Printf.sprintf "__p%i" idx))
1359 in
1360 let form' = Mc.mapX (fun _ p -> subst_prop p) Mc.IsProp form in
1361 ( prodn nb_props
1362 (List.map (fun (x, y) -> (Name.Name x, y)) props_n)
1363 (prodn nb_vars
1364 (List.map (fun (x, y) -> (Name.Name x, y)) vars_n)
1365 (xdump_prop (List.length vars_n) 0 form))
1366 , List.rev props_n
1367 , var_name_pos
1368 , form' )
1369
1370 (**
1371 * Given a conclusion and a list of affectations, rebuild a term prefixed by
1372 * the appropriate letins.
1373 * TODO: reverse the list of bindings!
1374 *)
1375
1376 let set sigma l concl =
1377 let rec xset acc = function
1378 | [] -> acc
1379 | e :: l ->
1380 let name, expr, typ = e in
1381 xset
1382 (EConstr.mkNamedLetIn sigma
1383 (make_annot (Names.Id.of_string name) ERelevance.relevant)
1384 expr typ acc)
1385 l
1386 in
1387 xset concl l
1388
1389 let rocq_Branch = lazy (constr_of_ref "micromega.VarMap.Branch")
1390 let rocq_Elt = lazy (constr_of_ref "micromega.VarMap.Elt")
1391 let rocq_Empty = lazy (constr_of_ref "micromega.VarMap.Empty")
1392 let rocq_VarMap = lazy (constr_of_ref "micromega.VarMap.type")
1393
1394 let rec dump_varmap typ m =
1395 match m with
1396 | Mc.Empty -> EConstr.mkApp (Lazy.force rocq_Empty, [|typ|])
1397 | Mc.Elt v -> EConstr.mkApp (Lazy.force rocq_Elt, [|typ; v|])
1398 | Mc.Branch (l, o, r) ->
1399 EConstr.mkApp
1400 (Lazy.force rocq_Branch, [|typ; dump_varmap typ l; o; dump_varmap typ r|])
1401
1402 let vm_of_list env =
1403 match env with
1404 | [] -> Mc.Empty
1405 | (d, _) :: _ ->
1406 List.fold_left
1407 (fun vm (c, i) -> Mc.vm_add d (CamlToCoq.positive i) c vm)
1408 Mc.Empty env
1409
1410 let rec dump_proof_term = function
1411 | Micromega.DoneProof -> Lazy.force rocq_doneProof
1412 | Micromega.RatProof (cone, rst) ->
1413 EConstr.mkApp
1414 ( Lazy.force rocq_ratProof
1415 , [|dump_psatz rocq_Z dump_z cone; dump_proof_term rst|] )
1416 | Micromega.CutProof (cone, prf) ->
1417 EConstr.mkApp
1418 ( Lazy.force rocq_cutProof
1419 , [|dump_psatz rocq_Z dump_z cone; dump_proof_term prf|] )
1420 | Micromega.SplitProof (p, prf1, prf2) ->
1421 EConstr.mkApp
1422 ( Lazy.force rocq_splitProof
1423 , [| dump_pol (Lazy.force rocq_Z) dump_z p
1424 ; dump_proof_term prf1
1425 ; dump_proof_term prf2 |] )
1426 | Micromega.EnumProof (c1, c2, prfs) ->
1427 EConstr.mkApp
1428 ( Lazy.force rocq_enumProof
1429 , [| dump_psatz rocq_Z dump_z c1
1430 ; dump_psatz rocq_Z dump_z c2
1431 ; dump_list (Lazy.force rocq_proofTerm) dump_proof_term prfs |] )
1432 | Micromega.ExProof (p, prf) ->
1433 EConstr.mkApp
1434 (Lazy.force rocq_ExProof, [|dump_positive p; dump_proof_term prf|])
1435
1436 let rec size_of_psatz = function
1437 | Micromega.PsatzIn _ -> 1
1438 | Micromega.PsatzSquare _ -> 1
1439 | Micromega.PsatzMulC (_, p) -> 1 + size_of_psatz p
1440 | Micromega.PsatzLet (p1, p2)
1441 |Micromega.PsatzMulE (p1, p2)
1442 |Micromega.PsatzAdd (p1, p2) ->
1443 size_of_psatz p1 + size_of_psatz p2
1444 | Micromega.PsatzC _ -> 1
1445 | Micromega.PsatzZ -> 1
1446
1447 let rec size_of_pf = function
1448 | Micromega.DoneProof -> 1
1449 | Micromega.RatProof (p, a) -> size_of_pf a + size_of_psatz p
1450 | Micromega.CutProof (p, a) -> size_of_pf a + size_of_psatz p
1451 | Micromega.SplitProof (_, p1, p2) -> size_of_pf p1 + size_of_pf p2
1452 | Micromega.EnumProof (p1, p2, l) ->
1453 size_of_psatz p1 + size_of_psatz p2
1454 + List.fold_left (fun acc p -> size_of_pf p + acc) 0 l
1455 | Micromega.ExProof (_, a) -> size_of_pf a + 1
1456
1457 let dump_proof_term t =
1458 if debug then Printf.printf "dump_proof_term %i\n" (size_of_pf t);
1459 dump_proof_term t
1460
1461 let pp_q o q =
1462 Printf.fprintf o "%a/%a" pp_z q.Micromega.qnum pp_positive q.Micromega.qden
1463
1464
1465 let rec parse_hyps (genv, sigma) parse_arith env tg hyps =
1466 match hyps with
1467 | [] -> ([], env, tg)
1468 | decl :: l ->
1469 let i = NamedDecl.get_id decl in
1470 let t = NamedDecl.get_type decl in
1471 let lhyps, env, tg = parse_hyps (genv, sigma) parse_arith env tg l in
1472 if is_prop genv sigma t then
1473 try
1474 let c, env, tg = parse_formula (genv, sigma) parse_arith env tg t in
1475 ((i, c) :: lhyps, env, tg)
1476 with ParseError -> (lhyps, env, tg)
1477 else (lhyps, env, tg)
1478
1479 let parse_goal gl parse_arith (env : Env.t) hyps term =
1480 let f, env, tg = parse_formula gl parse_arith env (Tag.from 0) term in
1481 let lhyps, env, tg = parse_hyps gl parse_arith env tg hyps in
1482 (lhyps, f, env)
1483
1484 (**
1485 * The datastructures that aggregate theory-dependent proof values.
1486 *)
1487 type ('synt_c, 'prf) domain_spec =
1488 { typ : EConstr.constr
1489 ; (* is the type of the interpretation domain - Z, Q, R*)
1490 coeff : EConstr.constr
1491 ; (* is the type of the syntactic coeffs - Z , Q , Rcst *)
1492 dump_coeff : 'synt_c -> EConstr.constr
1493 ; undump_coeff : Evd.evar_map -> EConstr.constr -> 'synt_c
1494 ; proof_typ : EConstr.constr
1495 ; dump_proof : 'prf -> EConstr.constr
1496 ; coeff_eq : 'synt_c -> 'synt_c -> bool }
1497
1498 let zz_domain_spec =
1499 lazy
1500 { typ = Lazy.force rocq_Z
1501 ; coeff = Lazy.force rocq_Z
1502 ; dump_coeff = dump_z
1503 ; undump_coeff = parse_z
1504 ; proof_typ = Lazy.force rocq_proofTerm
1505 ; dump_proof = dump_proof_term
1506 ; coeff_eq = Mc.Z.eqb }
1507
1508 let qq_domain_spec =
1509 lazy
1510 { typ = Lazy.force rocq_Q
1511 ; coeff = Lazy.force rocq_Q
1512 ; dump_coeff = dump_q
1513 ; undump_coeff = parse_q
1514 ; proof_typ = Lazy.force rocq_QWitness
1515 ; dump_proof = dump_psatz rocq_Q dump_q
1516 ; coeff_eq = Mc.qeq_bool }
1517
1518 let max_tag f =
1519 1
1520 + Tag.to_int
1521 (Mc.foldA (fun t1 (t2, _) -> Tag.max t1 t2) Mc.IsProp f (Tag.from 0))
1522
1523 (** Naive topological sort of constr according to the subterm-ordering *)
1524
1525 (* An element is minimal x is minimal w.r.t y if
1526 x <= y or (x and y are incomparable) *)
1527
1528 (**
1529 * Instantiate the current Rocq goal with a Micromega formula, a varmap, and a
1530 * witness.
1531 *)
1532
1533 let micromega_order_change spec cert cert_typ env ff (*: unit Proofview.tactic*)
1534 =
1535 (* let ids = Util.List.map_i (fun i _ -> (Names.Id.of_string ("__v"^(string_of_int i)))) 0 env in *)
1536 let formula_typ = EConstr.mkApp (Lazy.force rocq_Cstr, [|spec.coeff|]) in
1537 let ff = dump_formula formula_typ (dump_cstr spec.coeff spec.dump_coeff) ff in
1538 let vm = dump_varmap spec.typ (vm_of_list env) in
1539 (* todo : directly generate the proof term - or generalize before conversion? *)
1540 Proofview.Goal.enter (fun gl ->
1541 let sigma = Proofview.Goal.sigma gl in
1542 let concl = Proofview.Goal.concl gl in
1543 Tacticals.tclTHENLIST
1544 [ Tactics.change_concl
1545 (set sigma
1546 [ ( "__ff"
1547 , ff
1548 , EConstr.mkApp
1549 ( Lazy.force rocq_Formula
1550 , [|formula_typ; Lazy.force rocq_IsProp|] ) )
1551 ; ( "__varmap"
1552 , vm
1553 , EConstr.mkApp (Lazy.force rocq_VarMap, [|spec.typ|]) )
1554 ; ("__wit", cert, cert_typ) ] concl) ])
1555
1556 (**
1557 * The datastructures that aggregate prover attributes.
1558 *)
1559
1560 open Certificate
1561
1562 type ('option, 'a, 'prf, 'model) prover =
1563 { name : string
1564 ; (* name of the prover *)
1565 get_option : unit -> 'option
1566 ; (* find the options of the prover *)
1567 prover : 'option * 'a list -> ('prf, 'model) Certificate.res
1568 ; (* the prover itself *)
1569 hyps : 'prf -> ISet.t
1570 ; (* extract the indexes of the hypotheses really used in the proof *)
1571 compact : 'prf -> (int -> int) -> 'prf
1572 ; (* remap the hyp indexes according to function *)
1573 pp_prf : out_channel -> 'prf -> unit
1574 ; (* pretting printing of proof *)
1575 pp_f : out_channel -> 'a -> unit
1576 (* pretty printing of the formulas (polynomials)*) }
1577
1578 (**
1579 * Given a prover and a disjunction of atoms, find a proof of any of
1580 * the atoms. Returns an (optional) pair of a proof and a prover
1581 * datastructure.
1582 *)
1583
1584 let find_witness p polys1 =
1585 let polys1 = List.map fst polys1 in
1586 p.prover (p.get_option (), polys1)
1587
1588 (**
1589 * Given a prover and a CNF, find a proof for each of the clauses.
1590 * Return the proofs as a list.
1591 *)
1592
1593 let witness_list prover l =
1594 let rec xwitness_list stack l =
1595 match stack with
1596 | [] -> Prf l
1597 | e :: stack ->
1598 match find_witness prover e with
1599 | Model m -> (Model (m, e))
1600 | Unknown -> Unknown
1601 | Prf w -> xwitness_list stack (w :: l)
1602 in
1603 xwitness_list (List.rev l) []
1604
1605 (* let t1 = System.get_time () in
1606 let res = witness_list p g in
1607 let t2 = System.get_time () in
1608 Feedback.msg_info Pp.(str "Witness generation "++int (List.length g) ++ str " "++System.fmt_time_difference t1 t2) ;
1609 res
1610 *)
1611
1612 (**
1613 * Prune the proof object, according to the 'diff' between two cnf formulas.
1614 *)
1615
1616 let compact_proofs prover (eq_cst : 'cst -> 'cst -> bool) (cnf_ff : 'cst cnf) res
1617 (cnf_ff' : 'cst cnf) =
1618 let eq_formula (p1, o1) (p2, o2) =
1619 let open Mutils.Hash in
1620 eq_pol eq_cst p1 p2 && eq_op1 o1 o2
1621 in
1622 let compact_proof (old_cl : 'cst clause) prf (new_cl : 'cst clause)
1623 =
1624 let new_cl = List.mapi (fun i (f, _) -> (f, i)) new_cl in
1625 let remap i =
1626 let formula =
1627 try fst (List.nth old_cl i) with Failure _ -> failwith "bad old index"
1628 in
1629 CList.assoc_f eq_formula formula new_cl
1630 in
1631 (* if debug then
1632 begin
1633 Printf.printf "\ncompact_proof : %a %a %a"
1634 (pp_ml_list prover.pp_f) (List.map fst old_cl)
1635 prover.pp_prf prf
1636 (pp_ml_list prover.pp_f) (List.map fst new_cl) ;
1637 flush stdout
1638 end ; *)
1639 let res =
1640 try prover.compact prf remap
1641 with x when CErrors.noncritical x -> (
1642 if debug then
1643 Printf.fprintf stdout "Proof compaction %s" (Printexc.to_string x);
1644 (* This should not happen -- this is the recovery plan... *)
1645 match prover.prover (prover.get_option (), List.map fst new_cl) with
1646 | Unknown | Model _ -> failwith "proof compaction error"
1647 | Prf p -> p )
1648 in
1649 if debug then begin
1650 Printf.printf " -> %a\n" prover.pp_prf res;
1651 flush stdout
1652 end;
1653 res
1654 in
1655 let is_proof_compatible (hyps, (old_cl : 'cst clause), prf) (new_cl : 'cst clause) =
1656 let eq (f1, (t1, e1)) (f2, (t2, e2)) =
1657 Int.equal (Tag.compare t1 t2) 0
1658 && eq_formula f1 f2
1659 (* We do not have to compare [e1] with [e2] because [t1 = t2] ensures
1660 by uid generation that they must be the same *)
1661 in
1662 is_sublist eq (Lazy.force hyps) new_cl
1663 in
1664 let map acc cl prf =
1665 let hyps = lazy (selecti (prover.hyps prf) cl) in
1666 (hyps, cl, prf) :: acc
1667 in
1668 let cnf_res = List.rev (List.fold_left2 map [] cnf_ff res) in
1669 (* we get pairs clause * proof *)
1670 if debug then begin
1671 Printf.printf "CNFRES\n";
1672 flush stdout;
1673 Printf.printf "CNFOLD %a\n" pp_cnf_tag cnf_ff;
1674 List.iter
1675 (fun (lazy hyps, cl, prf) ->
1676 Printf.printf "\nProver %a -> %a\n" pp_clause_tag cl pp_clause_tag hyps;
1677 flush stdout)
1678 cnf_res;
1679 Printf.printf "CNFNEW %a\n" pp_cnf_tag cnf_ff'
1680 end;
1681 List.map
1682 (fun x ->
1683 let _, o, p =
1684 try List.find (fun p -> is_proof_compatible p x) cnf_res
1685 with Not_found ->
1686 Printf.printf "ERROR: no compatible proof";
1687 flush stdout;
1688 failwith "Cannot find compatible proof"
1689 in
1690 compact_proof o p x)
1691 cnf_ff'
1692
1693 (**
1694 * "Hide out" tagged atoms of a formula by transforming them into generic
1695 * variables. See the Tag module in mutils.ml for more.
1696 *)
1697
1698 let abstract_formula : TagSet.t -> 'a formula -> 'a formula =
1699 fun hyps f ->
1700 let to_constr =
1701 Mc.
1702 { mkTT =
1703 (let rocq_True = Lazy.force rocq_True in
1704 let rocq_true = Lazy.force rocq_true in
1705 function Mc.IsProp -> rocq_True | Mc.IsBool -> rocq_true)
1706 ; mkFF =
1707 (let rocq_False = Lazy.force rocq_False in
1708 let rocq_false = Lazy.force rocq_false in
1709 function Mc.IsProp -> rocq_False | Mc.IsBool -> rocq_false)
1710 ; mkA = (fun k a (tg, t) -> t)
1711 ; mkAND =
1712 (let rocq_and = Lazy.force rocq_and in
1713 let rocq_andb = Lazy.force rocq_andb in
1714 fun k x y ->
1715 EConstr.mkApp
1716 ( (match k with Mc.IsProp -> rocq_and | Mc.IsBool -> rocq_andb)
1717 , [|x; y|] ))
1718 ; mkOR =
1719 (let rocq_or = Lazy.force rocq_or in
1720 let rocq_orb = Lazy.force rocq_orb in
1721 fun k x y ->
1722 EConstr.mkApp
1723 ( (match k with Mc.IsProp -> rocq_or | Mc.IsBool -> rocq_orb)
1724 , [|x; y|] ))
1725 ; mkIMPL =
1726 (fun k x y ->
1727 match k with
1728 | Mc.IsProp -> EConstr.mkArrow x ERelevance.relevant y
1729 | Mc.IsBool -> EConstr.mkApp (Lazy.force rocq_implb, [|x; y|]))
1730 ; mkIFF =
1731 (let rocq_iff = Lazy.force rocq_iff in
1732 let rocq_eqb = Lazy.force rocq_eqb in
1733 fun k x y ->
1734 EConstr.mkApp
1735 ( (match k with Mc.IsProp -> rocq_iff | Mc.IsBool -> rocq_eqb)
1736 , [|x; y|] ))
1737 ; mkNOT =
1738 (let rocq_not = Lazy.force rocq_not in
1739 let rocq_negb = Lazy.force rocq_negb in
1740 fun k x ->
1741 EConstr.mkApp
1742 ( (match k with Mc.IsProp -> rocq_not | Mc.IsBool -> rocq_negb)
1743 , [|x|] ))
1744 ; mkEQ =
1745 (let rocq_eq = Lazy.force rocq_eq in
1746 fun x y -> EConstr.mkApp (rocq_eq, [|Lazy.force rocq_bool; x; y|])) }
1747 in
1748 Mc.abst_form to_constr (fun (t, _) -> TagSet.mem t hyps) true Mc.IsProp f
1749
1750 (* [abstract_wrt_formula] is used in contexts whre f1 is already an abstraction of f2 *)
1751 let rec abstract_wrt_formula f1 f2 =
1752 Mc.(
1753 match (f1, f2) with
1754 | X (b, c), _ -> X (b, c)
1755 | A _, A _ -> f2
1756 | AND (b, f1, f2), AND (_, f1', f2') ->
1757 AND (b, abstract_wrt_formula f1 f1', abstract_wrt_formula f2 f2')
1758 | OR (b, f1, f2), OR (_, f1', f2') ->
1759 OR (b, abstract_wrt_formula f1 f1', abstract_wrt_formula f2 f2')
1760 | IMPL (b, f1, _, f2), IMPL (_, f1', x, f2') ->
1761 IMPL (b, abstract_wrt_formula f1 f1', x, abstract_wrt_formula f2 f2')
1762 | IFF (b, f1, f2), IFF (_, f1', f2') ->
1763 IFF (b, abstract_wrt_formula f1 f1', abstract_wrt_formula f2 f2')
1764 | EQ (f1, f2), EQ (f1', f2') ->
1765 EQ (abstract_wrt_formula f1 f1', abstract_wrt_formula f2 f2')
1766 | FF b, FF _ -> FF b
1767 | TT b, TT _ -> TT b
1768 | NOT (b, x), NOT (_, y) -> NOT (b, abstract_wrt_formula x y)
1769 | _ -> failwith "abstract_wrt_formula")
1770
1771 (**
1772 * This exception is raised by really_call_csdpcert if Rocq's configure didn't
1773 * find a CSDP executable.
1774 *)
1775
1776 exception CsdpNotFound
1777
1778 let fail_csdp_not_found () =
1779 flush stdout;
1780 let s =
1781 "Skipping the rest of this tactic: the complexity of the \
1782 goal requires the use of an external tool called CSDP. \n\n\
1783 However, the \"csdp\" binary is not present in the search path. \n\n\
1784 Some OS distributions include CSDP (package \"coinor-csdp\" on Debian \
1785 for instance). You can download binaries \
1786 and source code from <https://github.com/coin-or/csdp>." in
1787 Tacticals.tclFAIL (Pp.str s)
1788
1789 (**
1790 * This is the core of Micromega: apply the prover, analyze the result and
1791 * prune unused fomulas, and finally modify the proof state.
1792 *)
1793
1794 let formula_hyps_concl hyps concl =
1795 List.fold_right
1796 (fun (id, f) (cc, ids) ->
1797 match f with
1798 | Mc.X _ -> (cc, ids)
1799 | _ -> (Mc.IMPL (Mc.IsProp, f, Some id, cc), id :: ids))
1800 hyps (concl, [])
1801
1802 (* let time str f x =
1803 let t1 = System.get_time () in
1804 let res = f x in
1805 let t2 = System.get_time () in
1806 Feedback.msg_info (Pp.str str ++ Pp.str " " ++ System.fmt_time_difference t1 t2) ;
1807 res
1808 *)
1809
1810 let rec fold_trace f accu tr =
1811 let open Micromega in
1812 match tr with
1813 | Null -> accu
1814 | Push (x, t) -> fold_trace f (f accu x) t
1815 | Merge (Null, t2) -> fold_trace f accu t2
1816 | Merge (Push (x, t1), t2) -> fold_trace f (f accu x) (Merge (t1, t2))
1817 | Merge (Merge (t1, t2), t3) -> fold_trace f accu (Merge (t1, Merge (t2, t3)))
1818
1819 let micromega_tauto ?(abstract=true) pre_process cnf spec prover
1820 (polys1 : (Names.Id.t * 'cst formula) list) (polys2 : 'cst formula) =
1821 (* Express the goal as one big implication *)
1822 let ff, ids = formula_hyps_concl polys1 polys2 in
1823 let mt = CamlToCoq.positive (max_tag ff) in
1824 (* Construction of cnf *)
1825 let pre_ff = pre_process mt (ff : 'a formula) in
1826 let cnf_ff, cnf_ff_tags = cnf Mc.IsProp pre_ff in
1827 match witness_list prover cnf_ff with
1828 | Model m -> Model m
1829 | Unknown -> Unknown
1830 | Prf res ->
1831 (*Printf.printf "\nList %i" (List.length `res); *)
1832 let deps =
1833 List.fold_left2
1834 (fun s cl prf ->
1835 let tags =
1836 ISet.fold
1837 (fun i s ->
1838 let t = fst (snd (List.nth cl i)) in
1839 if debug then Printf.fprintf stdout "T : %i -> %a" i Tag.pp t;
1840 (*try*) TagSet.add t s
1841 (* with Invalid_argument _ -> s*))
1842 (prover.hyps prf) TagSet.empty
1843 in
1844 TagSet.union s tags)
1845 (fold_trace (fun s (i, _) -> TagSet.add i s) TagSet.empty cnf_ff_tags)
1846 cnf_ff res
1847 in
1848 let ff' = if abstract then abstract_formula deps ff else ff in
1849 let pre_ff' = pre_process mt ff' in
1850 let cnf_ff', _ = cnf Mc.IsProp pre_ff' in
1851 if debug then begin
1852 output_string stdout "\n";
1853 Printf.printf "TForm : %a\n" pp_formula ff;
1854 flush stdout;
1855 Printf.printf "CNF : %a\n" pp_cnf_tag cnf_ff;
1856 flush stdout;
1857 Printf.printf "TFormAbs : %a\n" pp_formula ff';
1858 flush stdout;
1859 Printf.printf "TFormPre : %a\n" pp_formula pre_ff;
1860 flush stdout;
1861 Printf.printf "TFormPreAbs : %a\n" pp_formula pre_ff';
1862 flush stdout;
1863 Printf.printf "CNF : %a\n" pp_cnf_tag cnf_ff';
1864 flush stdout
1865 end;
1866 (* Even if it does not work, this does not mean it is not provable
1867 -- the prover is REALLY incomplete *)
1868 (* if debug then
1869 begin
1870 (* recompute the proofs *)
1871 match witness_list_tags prover cnf_ff' with
1872 | None -> failwith "abstraction is wrong"
1873 | Some res -> ()
1874 end ; *)
1875 let res' = compact_proofs prover spec.coeff_eq cnf_ff res cnf_ff' in
1876 let ff', res', ids = (ff', res', Mc.ids_of_formula Mc.IsProp ff') in
1877 let res' = dump_list spec.proof_typ spec.dump_proof res' in
1878 if show_used_hyps ()
1879 then Feedback.msg_info Pp.(str "Micromega used hypotheses: "++pr_enum Names.Id.print ids);
1880 Prf (ids, ff', res')
1881
1882 let micromega_tauto ?abstract pre_process cnf spec prover
1883 (polys1 : (Names.Id.t * 'cst formula) list) (polys2 : 'cst formula) =
1884 try micromega_tauto ?abstract pre_process cnf spec prover polys1 polys2
1885 with Not_found ->
1886 Printexc.print_backtrace stdout;
1887 flush stdout;
1888 Unknown
1889
1890 (**
1891 * Parse the proof environment, and call micromega_tauto
1892 *)
1893 let fresh_id avoid id gl =
1894 Tactics.fresh_id_in_env avoid id (Proofview.Goal.env gl)
1895
1896 let clear_all_no_check =
1897 Proofview.Goal.enter (fun gl ->
1898 let env = Proofview.Goal.env gl in
1899 let concl = Proofview.Goal.concl gl in
1900 let env =
1901 Environ.reset_with_named_context Environ.empty_named_context_val env
1902 in
1903 Refine.refine_with_principal ~typecheck:false (fun sigma ->
1904 let sigma, ev = Evarutil.new_evar env sigma concl in
1905 sigma, ev, Some (fst (EConstr.destEvar sigma ev))))
1906
1907 let micromega_gen parse_arith pre_process cnf spec dumpexpr prover tac =
1908 Proofview.Goal.enter (fun gl ->
1909 let sigma = Proofview.Goal.sigma gl in
1910 let genv = Proofview.Goal.env gl in
1911 let concl = Proofview.Goal.concl gl in
1912 try
1913 let hyps, concl, env =
1914 parse_goal (genv, sigma) parse_arith
1915 (Env.empty (genv, sigma))
1916 (EConstr.named_context genv) concl
1917 in
1918 let env = Env.elements env in
1919 let spec = Lazy.force spec in
1920 let dumpexpr = Lazy.force dumpexpr in
1921 if debug then
1922 Feedback.msg_debug (Pp.str "Env " ++ Env.pp (genv, sigma) env);
1923 match
1924 micromega_tauto pre_process cnf spec prover hyps concl
1925 with
1926 | Unknown ->
1927 flush stdout;
1928 Tacticals.tclFAIL (Pp.str " Cannot find witness")
1929 | Model (m, e) ->
1930 Tacticals.tclFAIL (Pp.str " Cannot find witness")
1931 | Prf (ids, ff', res') ->
1932 let arith_goal, props, vars, ff_arith =
1933 make_goal_of_formula (genv, sigma) dumpexpr ff'
1934 in
1935 let intro (id, _) = Tactics.introduction id in
1936 let intro_vars = Tacticals.tclTHENLIST (List.map intro vars) in
1937 let intro_props = Tacticals.tclTHENLIST (List.map intro props) in
1938 (* let ipat_of_name id = Some (CAst.make @@ IntroNaming (Namegen.IntroIdentifier id)) in*)
1939 let goal_name =
1940 fresh_id Id.Set.empty (Names.Id.of_string "__arith") gl
1941 in
1942 let env' = List.map (fun (id, i) -> (EConstr.mkVar id, i)) vars in
1943 let tac_arith =
1944 Tacticals.tclTHENLIST
1945 [ clear_all_no_check
1946 ; intro_props
1947 ; intro_vars
1948 ; micromega_order_change spec res'
1949 (EConstr.mkApp (Lazy.force rocq_list, [|spec.proof_typ|]))
1950 env' ff_arith ]
1951 in
1952 let goal_props =
1953 List.rev
1954 (List.map fst
1955 (Env.elements (prop_env_of_formula (genv, sigma) ff')))
1956 in
1957 let goal_vars =
1958 List.map (fun (_, i) -> fst (List.nth env (i - 1))) vars
1959 in
1960 let arith_args = goal_props @ goal_vars in
1961 let kill_arith = Tacticals.tclTHEN tac_arith tac in
1962 (*
1963 (*tclABSTRACT fails in certain corner cases.*)
1964 Tacticals.tclTHEN
1965 clear_all_no_check
1966 (Abstract.tclABSTRACT ~opaque:false None (Tacticals.tclTHEN tac_arith tac)) in *)
1967 Tacticals.tclTHEN
1968 (Tactics.assert_by (Names.Name goal_name) arith_goal
1969 (*Proofview.tclTIME (Some "kill_arith")*) kill_arith)
1970 ((*Proofview.tclTIME (Some "apply_arith") *)
1971 Tactics.exact_check
1972 (EConstr.applist
1973 ( EConstr.mkVar goal_name
1974 , arith_args @ List.map EConstr.mkVar ids )))
1975 with
1976 | CsdpNotFound -> fail_csdp_not_found ())
1977
1978 let micromega_wit_gen pre_process cnf spec prover wit_id ff =
1979 Proofview.Goal.enter (fun gl ->
1980 let sigma = Proofview.Goal.sigma gl in
1981 try
1982 let spec = Lazy.force spec in
1983 let undump_cstr = undump_cstr spec.undump_coeff in
1984 let tg = Tag.from 0, Lazy.force rocq_tt in
1985 let ff = undump_formula undump_cstr tg sigma ff in
1986 match
1987 micromega_tauto ~abstract:false pre_process cnf spec prover [] ff
1988 with
1989 | Unknown ->
1990 flush stdout;
1991 Tacticals.tclFAIL (Pp.str " Cannot find witness")
1992 | Model (m, e) ->
1993 Tacticals.tclFAIL (Pp.str " Cannot find witness")
1994 | Prf (_ids, _ff', res') ->
1995 let tres' = EConstr.mkApp (Lazy.force rocq_list, [|spec.proof_typ|]) in
1996 Tactics.letin_tac
1997 None (Names.Name wit_id) res' (Some tres') Locusops.nowhere
1998 with CsdpNotFound -> fail_csdp_not_found ())
1999
2000 let micromega_order_changer cert env ff =
2001 (*let ids = Util.List.map_i (fun i _ -> (Names.Id.of_string ("__v"^(string_of_int i)))) 0 env in *)
2002 let coeff = Lazy.force rocq_Rcst in
2003 let dump_coeff = dump_Rcst in
2004 let typ = Lazy.force rocq_R in
2005 let cert_typ =
2006 EConstr.mkApp (Lazy.force rocq_list, [|Lazy.force rocq_QWitness|])
2007 in
2008 let formula_typ = EConstr.mkApp (Lazy.force rocq_Cstr, [|coeff|]) in
2009 let ff = dump_formula formula_typ (dump_cstr coeff dump_coeff) ff in
2010 let vm = dump_varmap typ (vm_of_list env) in
2011 Proofview.Goal.enter (fun gl ->
2012 let sigma = Proofview.Goal.sigma gl in
2013 let concl = Proofview.Goal.concl gl in
2014 Tacticals.tclTHENLIST
2015 [ Tactics.change_concl
2016 (set sigma
2017 [ ( "__ff"
2018 , ff
2019 , EConstr.mkApp
2020 ( Lazy.force rocq_Formula
2021 , [|formula_typ; Lazy.force rocq_IsProp|] ) )
2022 ; ("__varmap", vm, EConstr.mkApp (Lazy.force rocq_VarMap, [|typ|]))
2023 ; ("__wit", cert, cert_typ) ] concl)
2024 (* Tacticals.tclTHENLIST (List.map (fun id -> (Tactics.introduction id)) ids)*)
2025 ])
2026
2027 let micromega_genr prover tac =
2028 let parse_arith = parse_rarith in
2029 let spec =
2030 lazy
2031 { typ = Lazy.force rocq_R
2032 ; coeff = Lazy.force rocq_Rcst
2033 ; dump_coeff = dump_q
2034 ; undump_coeff = parse_q
2035 ; proof_typ = Lazy.force rocq_QWitness
2036 ; dump_proof = dump_psatz rocq_Q dump_q
2037 ; coeff_eq = Mc.qeq_bool }
2038 in
2039 Proofview.Goal.enter (fun gl ->
2040 let sigma = Proofview.Goal.sigma gl in
2041 let genv = Proofview.Goal.env gl in
2042 let concl = Proofview.Goal.concl gl in
2043 try
2044 let hyps, concl, env =
2045 parse_goal (genv, sigma) parse_arith
2046 (Env.empty (genv, sigma))
2047 (EConstr.named_context genv) concl
2048 in
2049 let env = Env.elements env in
2050 let spec = Lazy.force spec in
2051 let hyps' =
2052 List.map
2053 (fun (n, f) ->
2054 ( n
2055 , Mc.map_bformula Mc.IsProp
2056 (Micromega.map_Formula Micromega.q_of_Rcst)
2057 f ))
2058 hyps
2059 in
2060 let concl' =
2061 Mc.map_bformula Mc.IsProp
2062 (Micromega.map_Formula Micromega.q_of_Rcst)
2063 concl
2064 in
2065 match
2066 micromega_tauto
2067 (fun _ x -> x)
2068 Mc.cnfQ spec prover hyps' concl'
2069 with
2070 | Unknown | Model _ ->
2071 flush stdout;
2072 Tacticals.tclFAIL (Pp.str " Cannot find witness")
2073 | Prf (ids, ff', res') ->
2074 let ff, ids =
2075 formula_hyps_concl
2076 (List.filter (fun (n, _) -> CList.mem_f Id.equal n ids) hyps)
2077 concl
2078 in
2079 let ff' = abstract_wrt_formula ff' ff in
2080 let arith_goal, props, vars, ff_arith =
2081 make_goal_of_formula (genv, sigma) (Lazy.force dump_rexpr) ff'
2082 in
2083 let intro (id, _) = Tactics.introduction id in
2084 let intro_vars = Tacticals.tclTHENLIST (List.map intro vars) in
2085 let intro_props = Tacticals.tclTHENLIST (List.map intro props) in
2086 let ipat_of_name id =
2087 Some (CAst.make @@ IntroNaming (Namegen.IntroIdentifier id))
2088 in
2089 let goal_name =
2090 fresh_id Id.Set.empty (Names.Id.of_string "__arith") gl
2091 in
2092 let env' = List.map (fun (id, i) -> (EConstr.mkVar id, i)) vars in
2093 let tac_arith =
2094 Tacticals.tclTHENLIST
2095 [ clear_all_no_check
2096 ; intro_props
2097 ; intro_vars
2098 ; micromega_order_changer res' env' ff_arith ]
2099 in
2100 let goal_props =
2101 List.rev
2102 (List.map fst
2103 (Env.elements (prop_env_of_formula (genv, sigma) ff')))
2104 in
2105 let goal_vars =
2106 List.map (fun (_, i) -> fst (List.nth env (i - 1))) vars
2107 in
2108 let arith_args = goal_props @ goal_vars in
2109 let kill_arith = Tacticals.tclTHEN tac_arith tac in
2110 (* Tacticals.tclTHEN
2111 (Tactics.keep [])
2112 (Tactics.tclABSTRACT None*)
2113 Tacticals.tclTHENS
2114 (Tactics.forward true (Some None) (ipat_of_name goal_name)
2115 arith_goal)
2116 [ kill_arith
2117 ; Tacticals.tclTHENLIST
2118 [ Generalize.generalize (List.map EConstr.mkVar ids)
2119 ; Tactics.exact_check
2120 (EConstr.applist (EConstr.mkVar goal_name, arith_args)) ] ]
2121 with CsdpNotFound -> fail_csdp_not_found ())
2122
2123 let lift_ratproof prover l =
2124 match prover l with
2125 | Unknown | Model _ -> Unknown
2126 | Prf c -> Prf (Mc.RatProof (c, Mc.DoneProof))
2127
2128 type micromega_polys = (Micromega.q Mc.pol * Mc.op1) list
2129
2130 [@@@ocaml.warning "-37"]
2131
2132 type csdp_certificate = S of Sos_types.positivstellensatz option | F of string
2133
2134 (* Used to read the result of the execution of csdpcert *)
2135
2136 type provername = string * int option
2137
2138 (**
2139 * The caching mechanism.
2140 *)
2141
2142 module MakeCache (T : sig
2143 type prover_option
2144 type coeff
2145
2146 val hash_prover_option : int -> prover_option -> int
2147 val hash_coeff : int -> coeff -> int
2148 val eq_prover_option : prover_option -> prover_option -> bool
2149 val eq_coeff : coeff -> coeff -> bool
2150 end) : sig
2151 type key = T.prover_option * (T.coeff Mc.pol * Mc.op1) list
2152
2153 val memo_opt : (unit -> bool) -> string -> (key -> 'a) -> key -> 'a
2154 end = struct
2155 module E = struct
2156 type t = T.prover_option * (T.coeff Mc.pol * Mc.op1) list
2157
2158 let equal =
2159 Hash.(
2160 eq_pair T.eq_prover_option
2161 (CList.equal (eq_pair (eq_pol T.eq_coeff) Hash.eq_op1)))
2162
2163 let hash =
2164 let hash_cstr = Hash.(hash_pair (hash_pol T.hash_coeff) hash_op1) in
2165 Hash.((hash_pair T.hash_prover_option (List.fold_left hash_cstr)) 0)
2166 end
2167
2168 include Persistent_cache.PHashtable (E)
2169
2170 let memo_opt use_cache cache_file f =
2171 let memof = memo cache_file f in
2172 fun x -> if use_cache () then memof x else f x
2173 end
2174
2175 module CacheCsdp = MakeCache (struct
2176 type prover_option = provername
2177 type coeff = Mc.q
2178
2179 let hash_prover_option =
2180 Hash.(hash_pair hash_string (hash_elt (Option.hash (fun x -> x))))
2181
2182 let eq_prover_option = Hash.(eq_pair String.equal (Option.equal Int.equal))
2183 let hash_coeff = Hash.hash_q
2184 let eq_coeff = Hash.eq_q
2185 end)
2186
2187 (**
2188 * Build the command to call csdpcert, and launch it. This in turn will call
2189 * the sos driver to the csdp executable.
2190 * Throw CsdpNotFound if Rocq isn't aware of any csdp executable.
2191 *)
2192
2193 let require_csdp =
2194 lazy (if System.is_in_system_path "csdp" then () else raise CsdpNotFound)
2195
2196 let really_call_csdpcert :
2197 provername -> micromega_polys -> Sos_types.positivstellensatz option =
2198 fun provername poly ->
2199 Lazy.force require_csdp;
2200 let cmdname = "csdpcert" in
2201 match (command cmdname [|cmdname|] (provername, poly) : csdp_certificate) with
2202 | F str ->
2203 if debug then Printf.fprintf stdout "really_call_csdpcert : %s\n" str;
2204 raise (failwith str)
2205 | S res -> res
2206
2207 (**
2208 * Check the cache before calling the prover.
2209 *)
2210
2211 let xcall_csdpcert =
2212 CacheCsdp.memo_opt use_csdp_cache ".csdp.cache" (fun (prover, pb) ->
2213 really_call_csdpcert prover pb)
2214
2215 (**
2216 * Prover callback functions.
2217 *)
2218
2219 let call_csdpcert prover pb = xcall_csdpcert (prover, pb)
2220
2221 let rec z_to_q_pol e =
2222 match e with
2223 | Mc.Pc z -> Mc.Pc {Mc.qnum = z; Mc.qden = Mc.XH}
2224 | Mc.Pinj (p, pol) -> Mc.Pinj (p, z_to_q_pol pol)
2225 | Mc.PX (pol1, p, pol2) -> Mc.PX (z_to_q_pol pol1, p, z_to_q_pol pol2)
2226
2227 let call_csdpcert_q provername poly =
2228 match call_csdpcert provername poly with
2229 | None -> Unknown
2230 | Some cert ->
2231 let cert = Certificate.q_cert_of_pos cert in
2232 if Mc.qWeakChecker poly cert then Prf cert
2233 else (
2234 print_string "buggy certificate";
2235 Unknown )
2236
2237 let call_csdpcert_z provername poly =
2238 let l = List.map (fun (e, o) -> (z_to_q_pol e, o)) poly in
2239 match call_csdpcert provername l with
2240 | None -> Unknown
2241 | Some cert ->
2242 let cert = Certificate.z_cert_of_pos cert in
2243 if Mc.zWeakChecker poly cert then Prf cert
2244 else (
2245 print_string "buggy certificate";
2246 flush stdout;
2247 Unknown )
2248
2249 let rec xhyps_of_cone base acc prf =
2250 match prf with
2251 | Mc.PsatzC _ | Mc.PsatzZ | Mc.PsatzSquare _ -> acc
2252 | Mc.PsatzIn n ->
2253 let n = CoqToCaml.nat n in
2254 if n >= base then ISet.add (n - base) acc else acc
2255 | Mc.PsatzLet (e1, e2) ->
2256 xhyps_of_cone (base + 1) (xhyps_of_cone base acc e1) e2
2257 | Mc.PsatzMulC (_, c) -> xhyps_of_cone base acc c
2258 | Mc.PsatzAdd (e1, e2) | Mc.PsatzMulE (e1, e2) ->
2259 xhyps_of_cone base (xhyps_of_cone base acc e2) e1
2260
2261 let hyps_of_cone prf = xhyps_of_cone 0 ISet.empty prf
2262
2263 let hyps_of_pt pt =
2264 let rec xhyps base pt acc =
2265 match pt with
2266 | Mc.DoneProof -> acc
2267 | Mc.RatProof (c, pt) -> xhyps (base + 1) pt (xhyps_of_cone base acc c)
2268 | Mc.CutProof (c, pt) -> xhyps (base + 1) pt (xhyps_of_cone base acc c)
2269 | Mc.SplitProof (p, p1, p2) -> xhyps (base + 1) p1 (xhyps (base + 1) p2 acc)
2270 | Mc.EnumProof (c1, c2, l) ->
2271 let s = xhyps_of_cone base (xhyps_of_cone base acc c2) c1 in
2272 List.fold_left (fun s x -> xhyps (base + 1) x s) s l
2273 | Mc.ExProof (_, pt) -> xhyps (base + 3) pt acc
2274 in
2275 xhyps 0 pt ISet.empty
2276
2277 let compact_cone prf f =
2278 let translate ofset x = if x < ofset then x else f (x - ofset) + ofset in
2279 let np ofset n = CamlToCoq.nat (translate ofset (CoqToCaml.nat n)) in
2280 let rec xinterp ofset prf =
2281 match prf with
2282 | Mc.PsatzC _ | Mc.PsatzZ | Mc.PsatzSquare _ -> prf
2283 | Mc.PsatzIn n -> Mc.PsatzIn (np ofset n)
2284 | Mc.PsatzLet (e1, e2) ->
2285 Mc.PsatzLet (xinterp ofset e1, xinterp (ofset + 1) e2)
2286 | Mc.PsatzMulC (e, c) -> Mc.PsatzMulC (e, xinterp ofset c)
2287 | Mc.PsatzAdd (e1, e2) -> Mc.PsatzAdd (xinterp ofset e1, xinterp ofset e2)
2288 | Mc.PsatzMulE (e1, e2) -> Mc.PsatzMulE (xinterp ofset e1, xinterp ofset e2)
2289 in
2290 xinterp 0 prf
2291
2292 let compact_pt pt f =
2293 let translate ofset x = if x < ofset then x else f (x - ofset) + ofset in
2294 let rec compact_pt ofset pt =
2295 match pt with
2296 | Mc.DoneProof -> Mc.DoneProof
2297 | Mc.RatProof (c, pt) ->
2298 Mc.RatProof (compact_cone c (translate ofset), compact_pt (ofset + 1) pt)
2299 | Mc.CutProof (c, pt) ->
2300 Mc.CutProof (compact_cone c (translate ofset), compact_pt (ofset + 1) pt)
2301 | Mc.SplitProof (p, p1, p2) ->
2302 Mc.SplitProof (p, compact_pt (ofset + 1) p1, compact_pt (ofset + 1) p2)
2303 | Mc.EnumProof (c1, c2, l) ->
2304 Mc.EnumProof
2305 ( compact_cone c1 (translate ofset)
2306 , compact_cone c2 (translate ofset)
2307 , Mc.map (fun x -> compact_pt (ofset + 1) x) l )
2308 | Mc.ExProof (x, pt) -> Mc.ExProof (x, compact_pt (ofset + 3) pt)
2309 in
2310 compact_pt 0 pt
2311
2312 (**
2313 * Definition of provers.
2314 * Instantiates the type ('a,'prf) prover defined above.
2315 *)
2316
2317 let lift_pexpr_prover p l = p (List.map (fun (e, o) -> (Mc.denorm e, o)) l)
2318
2319 module CacheZ = MakeCache (struct
2320 type prover_option = int
2321 type coeff = Mc.z
2322
2323 let hash_prover_option : int -> prover_option -> int =
2324 Hash.hash_elt Hashtbl.hash
2325
2326 let eq_prover_option : prover_option -> prover_option -> bool = ( = )
2327 let eq_coeff = Hash.eq_z
2328 let hash_coeff = Hash.hash_z
2329 end)
2330
2331 module CacheQ = MakeCache (struct
2332 type prover_option = int
2333 type coeff = Mc.q
2334
2335 let hash_prover_option : int -> int -> int = Hash.hash_elt Hashtbl.hash
2336 let eq_prover_option = Int.equal
2337 let eq_coeff = Hash.eq_q
2338 let hash_coeff = Hash.hash_q
2339 end)
2340
2341 let memo_lia =
2342 CacheZ.memo_opt use_lia_cache ".lia.cache" (fun (b, s) ->
2343 lift_pexpr_prover (Certificate.lia b) s)
2344
2345 let memo_nlia =
2346 CacheZ.memo_opt use_nia_cache ".nia.cache" (fun (b, s) ->
2347 lift_pexpr_prover (Certificate.nlia b) s)
2348
2349 let memo_nra =
2350 CacheQ.memo_opt use_nra_cache ".nra.cache" (fun (o, s) ->
2351 lift_pexpr_prover (Certificate.nlinear_prover o) s)
2352
2353 let linear_prover_Q =
2354 { name = "linear prover"
2355 ; get_option = lra_proof_depth
2356 ; prover =
2357 (fun (o, l) ->
2358 lift_pexpr_prover (Certificate.linear_prover_with_cert o) l)
2359 ; hyps = hyps_of_cone
2360 ; compact = compact_cone
2361 ; pp_prf = pp_psatz pp_q
2362 ; pp_f = (fun o x -> pp_pol pp_q o (fst x)) }
2363
2364 let linear_prover_R =
2365 { name = "linear prover"
2366 ; get_option = lra_proof_depth
2367 ; prover =
2368 (fun (o, l) ->
2369 lift_pexpr_prover (Certificate.linear_prover_with_cert o) l)
2370 ; hyps = hyps_of_cone
2371 ; compact = compact_cone
2372 ; pp_prf = pp_psatz pp_q
2373 ; pp_f = (fun o x -> pp_pol pp_q o (fst x)) }
2374
2375 let nlinear_prover_R =
2376 { name = "nra"
2377 ; get_option = lra_proof_depth
2378 ; prover = memo_nra
2379 ; hyps = hyps_of_cone
2380 ; compact = compact_cone
2381 ; pp_prf = pp_psatz pp_q
2382 ; pp_f = (fun o x -> pp_pol pp_q o (fst x)) }
2383
2384 let non_linear_prover_Q str o =
2385 { name = "real nonlinear prover"
2386 ; get_option = (fun () -> (str, o))
2387 ; prover = (fun (o, l) -> call_csdpcert_q o l)
2388 ; hyps = hyps_of_cone
2389 ; compact = compact_cone
2390 ; pp_prf = pp_psatz pp_q
2391 ; pp_f = (fun o x -> pp_pol pp_q o (fst x)) }
2392
2393 let non_linear_prover_R str o =
2394 { name = "real nonlinear prover"
2395 ; get_option = (fun () -> (str, o))
2396 ; prover = (fun (o, l) -> call_csdpcert_q o l)
2397 ; hyps = hyps_of_cone
2398 ; compact = compact_cone
2399 ; pp_prf = pp_psatz pp_q
2400 ; pp_f = (fun o x -> pp_pol pp_q o (fst x)) }
2401
2402 let non_linear_prover_Z str o =
2403 { name = "real nonlinear prover"
2404 ; get_option = (fun () -> (str, o))
2405 ; prover = (fun (o, l) -> lift_ratproof (call_csdpcert_z o) l)
2406 ; hyps = hyps_of_pt
2407 ; compact = compact_pt
2408 ; pp_prf = pp_proof_term
2409 ; pp_f = (fun o x -> pp_pol pp_z o (fst x)) }
2410
2411 let linear_Z =
2412 { name = "lia"
2413 ; get_option = get_lia_option
2414 ; prover = memo_lia
2415 ; hyps = hyps_of_pt
2416 ; compact = compact_pt
2417 ; pp_prf = pp_proof_term
2418 ; pp_f = (fun o x -> pp_pol pp_z o (fst x)) }
2419
2420 let nlinear_Z =
2421 { name = "nlia"
2422 ; get_option = get_lia_option
2423 ; prover = memo_nlia
2424 ; hyps = hyps_of_pt
2425 ; compact = compact_pt
2426 ; pp_prf = pp_proof_term
2427 ; pp_f = (fun o x -> pp_pol pp_z o (fst x)) }
2428
2429 (**
2430 * Functions instantiating micromega_gen with the appropriate theories and
2431 * solvers
2432 *)
2433
2434 let exfalso_if_concl_not_Prop =
2435 Proofview.Goal.enter begin fun gl ->
2436 let sigma = Proofview.Goal.sigma gl in
2437 let env = Proofview.Goal.env gl in
2438 let concl = Proofview.Goal.concl gl in
2439 if is_prop env sigma concl then Tacticals.tclIDTAC
2440 else Tactics.exfalso
2441 end
2442
2443 let micromega_gen parse_arith pre_process cnf spec dumpexpr prover tac =
2444 Tacticals.tclTHEN exfalso_if_concl_not_Prop
2445 (micromega_gen parse_arith pre_process cnf spec dumpexpr prover tac)
2446
2447 let micromega_genr prover tac =
2448 Tacticals.tclTHEN exfalso_if_concl_not_Prop (micromega_genr prover tac)
2449
2450 let xlra_Q =
2451 micromega_gen parse_qarith
2452 (fun _ x -> x)
2453 Mc.cnfQ qq_domain_spec dump_qexpr linear_prover_Q
2454
2455 let wlra_Q =
2456 micromega_wit_gen
2457 (fun _ x -> x)
2458 Mc.cnfQ qq_domain_spec linear_prover_Q
2459
2460 let xlra_R = micromega_genr linear_prover_R
2461
2462 let xlia =
2463 micromega_gen parse_zarith
2464 (fun _ x -> x)
2465 Mc.cnfZ zz_domain_spec dump_zexpr linear_Z
2466
2467 let wlia =
2468 micromega_wit_gen
2469 (fun _ x -> x)
2470 Mc.cnfZ zz_domain_spec linear_Z
2471
2472 let xnra_Q =
2473 micromega_gen parse_qarith
2474 (fun _ x -> x)
2475 Mc.cnfQ qq_domain_spec dump_qexpr nlinear_prover_R
2476
2477 let wnra_Q =
2478 micromega_wit_gen
2479 (fun _ x -> x)
2480 Mc.cnfQ qq_domain_spec nlinear_prover_R
2481
2482 let xnra_R = micromega_genr nlinear_prover_R
2483
2484 let xnia =
2485 micromega_gen parse_zarith
2486 (fun _ x -> x)
2487 Mc.cnfZ zz_domain_spec dump_zexpr nlinear_Z
2488
2489 let wnia =
2490 micromega_wit_gen
2491 (fun _ x -> x)
2492 Mc.cnfZ zz_domain_spec nlinear_Z
2493
2494 let xsos_Q =
2495 micromega_gen parse_qarith
2496 (fun _ x -> x)
2497 Mc.cnfQ qq_domain_spec dump_qexpr
2498 (non_linear_prover_Q "pure_sos" None)
2499
2500 let wsos_Q =
2501 micromega_wit_gen
2502 (fun _ x -> x)
2503 Mc.cnfQ qq_domain_spec
2504 (non_linear_prover_Q "pure_sos" None)
2505
2506 let xsos_R = micromega_genr (non_linear_prover_R "pure_sos" None)
2507
2508 let xsos_Z =
2509 micromega_gen parse_zarith
2510 (fun _ x -> x)
2511 Mc.cnfZ zz_domain_spec dump_zexpr
2512 (non_linear_prover_Z "pure_sos" None)
2513
2514 let wsos_Z =
2515 micromega_wit_gen
2516 (fun _ x -> x)
2517 Mc.cnfZ zz_domain_spec
2518 (non_linear_prover_Z "pure_sos" None)
2519
2520 let xpsatz_Q i =
2521 micromega_gen parse_qarith
2522 (fun _ x -> x)
2523 Mc.cnfQ qq_domain_spec dump_qexpr
2524 (non_linear_prover_Q "real_nonlinear_prover" (Some i))
2525
2526 let wpsatz_Q i =
2527 micromega_wit_gen
2528 (fun _ x -> x)
2529 Mc.cnfQ qq_domain_spec
2530 (non_linear_prover_Q "real_nonlinear_prover" (Some i))
2531
2532 let xpsatz_R i =
2533 micromega_genr (non_linear_prover_R "real_nonlinear_prover" (Some i))
2534
2535 let xpsatz_Z i =
2536 micromega_gen parse_zarith
2537 (fun _ x -> x)
2538 Mc.cnfZ zz_domain_spec dump_zexpr
2539 (non_linear_prover_Z "real_nonlinear_prover" (Some i))
2540
2541 let wpsatz_Z i =
2542 micromega_wit_gen
2543 (fun _ x -> x)
2544 Mc.cnfZ zz_domain_spec
2545 (non_linear_prover_Z "real_nonlinear_prover" (Some i))
2546
2547 let print_lia_profile () =
2548 Simplex.(
2549 let { number_of_successes
2550 ; number_of_failures
2551 ; success_pivots
2552 ; failure_pivots
2553 ; average_pivots
2554 ; maximum_pivots } =
2555 Simplex.get_profile_info ()
2556 in
2557 Feedback.msg_notice
2558 Pp.(
2559 (* successes *)
2560 str "number of successes: "
2561 ++ int number_of_successes ++ fnl ()
2562 (* success pivots *)
2563 ++ str "number of success pivots: "
2564 ++ int success_pivots ++ fnl ()
2565 (* failure *)
2566 ++ str "number of failures: "
2567 ++ int number_of_failures ++ fnl ()
2568 (* failure pivots *)
2569 ++ str "number of failure pivots: "
2570 ++ int failure_pivots ++ fnl ()
2571 (* Other *)
2572 ++ str "average number of pivots: "
2573 ++ int average_pivots ++ fnl ()
2574 ++ str "maximum number of pivots: "
2575 ++ int maximum_pivots ++ fnl ()))
2576
2577 (* Local Variables: *)
2578 (* coding: utf-8 *)
2579 (* End: *)