Debian Codegraph @

plugins/ring/ring.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 module CVars = Vars
12 open Ltac_plugin
13 open Pp
14 open Util
15 open Names
16 open Constr
17 open EConstr
18 open Vars
19 open Environ
20 open Glob_term
21 open Locus
22 open Tacexpr
23 open Rocqlib
24 open Mod_subst
25 open Tacinterp
26 open Libobject
27 open Printer
28 open Declare
29 open Ring_ast
30 open Proofview.Notations
31
32 let error msg = CErrors.user_err Pp.(str msg)
33
34 (****************************************************************************)
35 (* controlled reduction *)
36
37 type protect_flag = Eval|Prot|Rec
38 type 'a reduction = Full | Arg of 'a
39
40 type protection = {
41 with_eq : bool;
42 arguments : ((unit -> GlobRef.t) * (int -> protect_flag) reduction) list;
43 }
44
45 type rprotection = {
46 r_with_eq : bool;
47 r_arguments : (GlobRef.t * (int -> protect_flag) reduction) list;
48 }
49
50 let interp_map env sigma l c = match EConstr.destRef sigma c with
51 | exception DestKO-> None
52 | (t, _) ->
53 let eq g1 g2 = QGlobRef.equal env g1 g2 in
54 match List.assoc_f eq t l.r_arguments with
55 | exception Not_found -> None
56 | Full -> Some Full
57 | Arg f -> Some (Arg f)
58
59 let fresh_rel (n, data) c =
60 let n = n + 1 in
61 (n, c :: data), mkRel n
62
63 let rec mk_clos_but env sigma f_map accu t =
64 let (f, args) = EConstr.decompose_app sigma t in
65 match interp_map env sigma f_map f with
66 | Some Full -> (accu, t)
67 | Some (Arg tag) ->
68 let fold i accu t = tag_arg env sigma f_map accu (tag i) t in
69 if Array.is_empty args then (accu, t)
70 else
71 let (accu, args) = Array.fold_left_map_i fold accu args in
72 accu, mkApp (f, args)
73 | None -> fresh_rel accu t
74
75 and tag_arg env sigma f_map accu tag c = match tag with
76 | Eval -> accu, c
77 | Prot -> fresh_rel accu c
78 | Rec -> mk_clos_but env sigma f_map accu c
79
80 let protect_maps : protection String.Map.t ref = ref String.Map.empty
81 let add_map s m = protect_maps := String.Map.add s m !protect_maps
82
83 let dest_rel sigma t =
84 match EConstr.kind sigma t with
85 | App(f,args) when Array.length args >= 2 ->
86 let rel = mkApp(f,Array.sub args 0 (Array.length args - 2)) in
87 if closed0 sigma rel then
88 (rel,args.(Array.length args - 2),args.(Array.length args - 1))
89 else error "ring: cannot find relation (not closed)"
90 | _ -> error "ring: cannot find relation"
91
92 let lookup_map map =
93 let map = match String.Map.find_opt map !protect_maps with
94 | Some map -> map
95 | None -> CErrors.user_err (str "Map " ++ qs map ++ str "not found.")
96 in
97 let r_arguments = List.map (fun (c, map) -> (c (), map)) map.arguments in
98 let r_with_eq = map.with_eq in
99 { r_with_eq; r_arguments }
100
101 let protect_red map env sigma c =
102 let map = lookup_map map in
103 let rec eval n c = match EConstr.kind sigma c with
104 | Prod (na, t, u) -> EConstr.mkProd (na, eval n t, eval (n + 1) u)
105 | _ ->
106 let rels = List.init n (fun i -> mkRel (n - i)) in
107 let norm c =
108 let (_, subterms), c = mk_clos_but env sigma map (n, rels) c in
109 let c = Reductionops.clos_norm_flags RedFlags.all env sigma c in
110 let subst = List.rev subterms in
111 EConstr.Vars.substl subst c
112 in
113 if map.r_with_eq then
114 let (rel, a1, a2) = dest_rel sigma c in
115 mkApp (rel, [|norm a1; norm a2|])
116 else
117 norm c
118 in
119 eval 0 c
120
121 let protect_tac map =
122 Tactics.reduct_option ~check:false (protect_red map,DEFAULTcast) None
123
124 let protect_tac_in map id =
125 Tactics.reduct_option ~check:false (protect_red map,DEFAULTcast) (Some(id, Locus.InHyp))
126
127
128 (****************************************************************************)
129
130 let rec closed_under sigma cset t =
131 try
132 let (gr, _) = destRef sigma t in
133 GlobRef.Set_env.mem gr cset
134 with DestKO ->
135 match EConstr.kind sigma t with
136 | Cast(c,_,_) -> closed_under sigma cset c
137 | App(f,l) -> closed_under sigma cset f && Array.for_all (closed_under sigma cset) l
138 | _ -> false
139
140 let closed_term args _ = match args with
141 | [t; l] ->
142 let t = Option.get (Value.to_constr t) in
143 let l = List.map (fun c -> Value.cast (Genarg.topwit Stdarg.wit_ref) c) (Option.get (Value.to_list l)) in
144 Proofview.tclEVARMAP >>= fun sigma ->
145 let cs = List.fold_right GlobRef.Set_env.add l GlobRef.Set_env.empty in
146 if closed_under sigma cs t then Proofview.tclUNIT () else Tacticals.tclFAIL (mt())
147 | _ -> assert false
148
149 let closed_term_ast =
150 let tacname = {
151 mltac_plugin = "rocq-runtime.plugins.ring";
152 mltac_tactic = "closed_term";
153 } in
154 let () = Tacenv.register_ml_tactic tacname [|closed_term|] in
155 let tacname = {
156 mltac_name = tacname;
157 mltac_index = 0;
158 } in
159 fun l ->
160 let l = List.map (fun gr -> ArgArg(Loc.tag gr)) l in
161 CAst.make (TacFun
162 ([Name(Id.of_string"t")],
163 CAst.make (TacML (tacname,
164 [TacGeneric (None, Genarg.in_gen (Genarg.glbwit Stdarg.wit_constr) (DAst.make @@ GVar(Id.of_string"t"),None));
165 TacGeneric (None, Genarg.in_gen (Genarg.glbwit (Genarg.wit_list Stdarg.wit_ref)) l)]))))
166 (*
167 let _ = add_tacdef false ((Loc.ghost,Id.of_string"ring_closed_term"
168 *)
169
170 (****************************************************************************)
171
172 let ic env sigma c =
173 let c, uctx = Constrintern.interp_constr env sigma c in
174 (Evd.from_ctx uctx, c)
175
176 let ic_unsafe env sigma c = (*FIXME remove *)
177 fst (Constrintern.interp_constr env sigma c)
178
179 let decl_constant name univs c =
180 let open Constr in
181 let vars = CVars.universes_of_constr c in
182 let univs = UState.restrict_universe_context univs vars in
183 let () = Global.push_context_set univs in
184 let types = (Typeops.infer (Global.env ()) c).uj_type in
185 let univs = UState.Monomorphic_entry Univ.ContextSet.empty, UnivNames.empty_binders in
186 (* UnsafeMonomorphic: we always do poly:false *)
187 UnsafeMonomorphic.mkConst
188 (declare_constant ~name
189 ~kind:Decls.(IsProof Lemma)
190 (DefinitionEntry (definition_entry ~opaque:true ~types ~univs c)))
191
192 let decl_constant na suff univs c =
193 let na = Namegen.next_global_ident_away (Global.safe_env ()) (Nameops.add_suffix na suff) Id.Set.empty in
194 decl_constant na univs c
195
196 (* Calling a global tactic *)
197 let ltac_call tac (args:glob_tactic_arg list) =
198 CAst.make @@ TacArg (TacCall (CAst.make (ArgArg(Loc.tag @@ Lazy.force tac),args)))
199
200 let constr_of sigma v = match Value.to_constr v with
201 | Some c -> EConstr.to_constr sigma c
202 | None -> failwith "Ring.exec_tactic: anomaly"
203
204 let tactic_res = ref [||]
205
206 let get_res =
207 let open Tacexpr in
208 let name = { mltac_plugin = "rocq-runtime.plugins.ring"; mltac_tactic = "get_res"; } in
209 let entry = { mltac_name = name; mltac_index = 0 } in
210 let tac args ist =
211 let n = Tacinterp.Value.cast (Genarg.topwit Stdarg.wit_int) (List.hd args) in
212 let init i = Id.Map.find (Id.of_string ("x" ^ string_of_int i)) ist.lfun in
213 tactic_res := Array.init n init;
214 Proofview.tclUNIT ()
215 in
216 Tacenv.register_ml_tactic name [| tac |];
217 entry
218
219 let exec_tactic env sigma n f args =
220 let fold arg (i, vars, lfun) =
221 let id = Id.of_string ("x" ^ string_of_int i) in
222 let x = Reference (ArgVar CAst.(make id)) in
223 (succ i, x :: vars, Id.Map.add id (Value.of_constr arg) lfun)
224 in
225 let (_, args, lfun) = List.fold_right fold args (0, [], Id.Map.empty) in
226 let ist = { (Tacinterp.default_ist ()) with Tacinterp.lfun = lfun; } in
227 (* Build the getter *)
228 let lid = List.init n (fun i -> Id.of_string("x"^string_of_int i)) in
229 let n = Genarg.in_gen (Genarg.glbwit Stdarg.wit_int) n in
230 let get_res = CAst.make (TacML (get_res, [TacGeneric (None, n)])) in
231 let getter = Tacexp (CAst.make (TacFun (List.map (fun n -> Name n) lid, get_res))) in
232 (* Evaluate the whole result *)
233 let _, pv = Proofview.init sigma [env, EConstr.mkProp] in
234 let tac = Tacinterp.eval_tactic_ist ist (ltac_call f (args@[getter])) in
235 let ((), pv, _, _, _) = Proofview.apply ~name:(Id.of_string "ring") ~poly:ist.Tacinterp.poly (Global.env ()) tac pv in
236 let sigma = Evd.minimize_universes (Proofview.return pv) in
237 let nf c = constr_of sigma c in
238 let uctx = UState.check_mono_univ_decl (Evd.ustate sigma) UState.default_univ_decl in
239 Array.map nf !tactic_res, uctx
240
241 let gen_constant n = (); fun () -> (EConstr.of_constr (UnivGen.constr_of_monomorphic_global (Global.env ()) (Rocqlib.lib_ref n)))
242 let gen_reference n = (); fun () -> (Rocqlib.lib_ref n)
243
244 let rocq_mk_Setoid = gen_constant "plugins.ring.Build_Setoid_Theory"
245 let rocq_None = gen_reference "core.option.None"
246 let rocq_Some = gen_reference "core.option.Some"
247 let rocq_eq = gen_reference "core.eq.type"
248
249 let rocq_cons = gen_reference "core.list.cons"
250 let rocq_nil = gen_reference "core.list.nil"
251
252 let lapp f args = mkApp (f (), args)
253
254 let plapp sigma f args =
255 let sigma, fc = Evd.fresh_global (Global.env ()) sigma (f ()) in
256 sigma, mkApp(fc,args)
257
258 (****************************************************************************)
259 (* Library linking *)
260
261 let plugin_dir = "setoid_ring"
262
263 let cdir = ["Stdlib";plugin_dir]
264
265 let znew_ring_path =
266 DirPath.make (List.map Id.of_string ["InitialRing";plugin_dir;"Stdlib"])
267 let zltac s =
268 lazy(KerName.make (ModPath.MPfile znew_ring_path) (Id.of_string s))
269
270 (* Ring theory *)
271
272 (* almost_ring defs *)
273 let rocq_almost_ring_theory = gen_reference "plugins.ring.almost_ring_theory"
274
275 (* setoid and morphism utilities *)
276 let rocq_eq_setoid = gen_reference "plugins.ring.Eqsth"
277 let rocq_eq_morph = gen_reference "plugins.ring.Eq_ext"
278 let rocq_eq_smorph = gen_reference "plugins.ring.Eq_s_ext"
279
280 (* ring -> almost_ring utilities *)
281 let rocq_ring_theory = gen_reference "plugins.ring.ring_theory"
282 let rocq_mk_reqe = gen_constant "plugins.ring.mk_reqe"
283
284 (* semi_ring -> almost_ring utilities *)
285 let rocq_semi_ring_theory = gen_reference "plugins.ring.semi_ring_theory"
286 let rocq_mk_seqe = gen_constant "plugins.ring.mk_seqe"
287
288 let rocq_abstract = gen_constant "plugins.ring.Abstract"
289 let rocq_comp = gen_constant "plugins.ring.Computational"
290 let rocq_morph = gen_constant "plugins.ring.Morphism"
291
292 (* power function *)
293 let ltac_inv_morph_nothing = zltac"inv_morph_nothing"
294
295 (* hypothesis *)
296 let rocq_mkhypo = gen_reference "plugins.ring.mkhypo"
297 let rocq_hypo = gen_reference "plugins.ring.hypo"
298
299 (* Equality: do not evaluate but make recursive call on both sides *)
300 let map_with_eq arg_map =
301 { with_eq = true; arguments = arg_map }
302
303 let map_without_eq arg_map =
304 { with_eq = false; arguments = arg_map }
305
306 let base_red = [
307 rocq_cons, Arg (function 2->Rec|_->Prot);
308 rocq_nil, Arg (function _ -> Prot);
309 gen_reference "plugins.ring.IDphi", Full;
310 gen_reference "plugins.ring.gen_phiZ", Full;
311 ]
312
313 let ring_red = [
314 (* Pphi_dev: evaluate polynomial and coef operations, protect
315 ring operations and make recursive call on the var map *)
316 gen_reference "plugins.ring.Pphi_dev", Arg (function 8|9|10|12|14->Eval|11|13->Rec|_->Prot);
317 gen_reference "plugins.ring.Pphi_pow",
318 Arg (function 8|9|10|13|15|17->Eval|11|16->Rec|_->Prot);
319 (* PEeval: evaluate polynomial, protect ring
320 operations and make recursive call on the var map *)
321 gen_reference "plugins.ring.eval", Arg (function 10|13->Eval|8|12->Rec|_->Prot)
322 ]
323
324 let _ = add_map "ring"
325 (map_with_eq
326 @@
327 base_red @
328 ring_red)
329
330 (****************************************************************************)
331 (* Ring database *)
332
333 module Cmap = Map.Make(Constr)
334
335 let from_carrier = Summary.ref Cmap.empty ~name:"ring-tac-carrier-table"
336
337 let print_rings () =
338 Feedback.msg_notice (strbrk "The following ring structures have been declared:");
339 Cmap.iter (fun _carrier ring ->
340 let env = Global.env () in
341 let sigma = Evd.from_env env in
342 Feedback.msg_notice
343 (hov 2
344 (Ppconstr.pr_id ring.ring_name ++ spc() ++
345 str"with carrier "++ pr_constr_env env sigma ring.ring_carrier++spc()++
346 str"and equivalence relation "++ pr_constr_env env sigma ring.ring_req))
347 ) !from_carrier
348
349 let ring_for_carrier r = Cmap.find r !from_carrier
350
351 let find_ring_structure env sigma l =
352 match l with
353 | t::cl' ->
354 let ty = Retyping.get_type_of env sigma t in
355 let check c =
356 let ty' = Retyping.get_type_of env sigma c in
357 if not (Reductionops.is_conv env sigma ty ty') then
358 CErrors.user_err
359 (str"Arguments of ring_simplify do not have all the same type.")
360 in
361 List.iter check cl';
362 (try ring_for_carrier (EConstr.to_constr ~abort_on_undefined_evars:false sigma ty)
363 with Not_found ->
364 CErrors.user_err
365 (str"Cannot find a declared ring structure over"++
366 spc() ++ str"\"" ++ pr_econstr_env env sigma ty ++ str"\"."))
367 | [] -> assert false
368
369 let add_entry e =
370 from_carrier := Cmap.add e.ring_carrier e !from_carrier
371
372 let subst_th (subst,th) =
373 let c' = subst_mps subst th.ring_carrier in
374 let eq' = subst_mps subst th.ring_req in
375 let set' = subst_mps subst th.ring_setoid in
376 let ext' = subst_mps subst th.ring_ext in
377 let morph' = subst_mps subst th.ring_morph in
378 let th' = subst_mps subst th.ring_th in
379 let thm1' = subst_mps subst th.ring_lemma1 in
380 let thm2' = subst_mps subst th.ring_lemma2 in
381 let tac'= Tacsubst.subst_tactic subst th.ring_cst_tac in
382 let pow_tac'= Tacsubst.subst_tactic subst th.ring_pow_tac in
383 let pretac'= Tacsubst.subst_tactic subst th.ring_pre_tac in
384 let posttac'= Tacsubst.subst_tactic subst th.ring_post_tac in
385 if c' == th.ring_carrier &&
386 eq' == th.ring_req &&
387 Constr.equal set' th.ring_setoid &&
388 ext' == th.ring_ext &&
389 morph' == th.ring_morph &&
390 th' == th.ring_th &&
391 thm1' == th.ring_lemma1 &&
392 thm2' == th.ring_lemma2 &&
393 tac' == th.ring_cst_tac &&
394 pow_tac' == th.ring_pow_tac &&
395 pretac' == th.ring_pre_tac &&
396 posttac' == th.ring_post_tac then th
397 else
398 { ring_name = th.ring_name;
399 ring_carrier = c';
400 ring_req = eq';
401 ring_setoid = set';
402 ring_ext = ext';
403 ring_morph = morph';
404 ring_th = th';
405 ring_cst_tac = tac';
406 ring_pow_tac = pow_tac';
407 ring_lemma1 = thm1';
408 ring_lemma2 = thm2';
409 ring_pre_tac = pretac';
410 ring_post_tac = posttac' }
411
412
413 let theory_to_obj : ring_info -> obj =
414 declare_object @@ global_object_nodischarge "tactic-new-ring-theory"
415 ~cache:add_entry
416 ~subst:(Some subst_th)
417
418 let setoid_of_relation env sigma a r =
419 try
420 let sigma, refl = Rewrite.get_reflexive_proof env sigma a r in
421 let sigma, sym = Rewrite.get_symmetric_proof env sigma a r in
422 let sigma, trans = Rewrite.get_transitive_proof env sigma a r in
423 sigma, lapp rocq_mk_Setoid [|a ; r ; refl; sym; trans |]
424 with Not_found ->
425 CErrors.user_err (str "Cannot find a setoid structure for relation " ++ pr_econstr_env env sigma r ++ str ".")
426
427 let op_morph r add mul opp req m1 m2 m3 =
428 lapp rocq_mk_reqe [| r; add; mul; opp; req; m1; m2; m3 |]
429
430 let op_smorph r add mul req m1 m2 =
431 lapp rocq_mk_seqe [| r; add; mul; req; m1; m2 |]
432
433 let ring_equality env sigma (r,add,mul,opp,req) =
434 match EConstr.kind sigma req with
435 | App (f, [| _ |]) when isRefX env sigma (rocq_eq ()) f ->
436 let sigma, setoid = plapp sigma rocq_eq_setoid [|r|] in
437 let sigma, op_morph =
438 match opp with
439 Some opp -> plapp sigma rocq_eq_morph [|r;add;mul;opp|]
440 | None -> plapp sigma rocq_eq_smorph [|r;add;mul|] in
441 let sigma, setoid = Typing.solve_evars env sigma setoid in
442 let sigma, op_morph = Typing.solve_evars env sigma op_morph in
443 (sigma,setoid,op_morph)
444 | _ ->
445 let sigma, setoid = setoid_of_relation env sigma r req in
446 let signature = [Some (r,Some req);Some (r,Some req)],Some(r,Some req) in
447 let sigma, add_m, add_m_lem =
448 try Rewrite.Internal.default_morphism env sigma signature add
449 with Not_found ->
450 CErrors.user_err (str "Ring addition " ++ pr_econstr_env env sigma add ++ str " should be declared as a morphism.") in
451 let sigma, mul_m, mul_m_lem =
452 try Rewrite.Internal.default_morphism env sigma signature mul
453 with Not_found ->
454 CErrors.user_err (str "Ring multiplication " ++ pr_econstr_env env sigma mul ++ str " should be declared as a morphism.") in
455 let sigma, op_morph =
456 match opp with
457 | Some opp ->
458 (let sigma, opp_m,opp_m_lem =
459 try Rewrite.Internal.default_morphism env sigma ([Some(r,Some req)],Some(r,Some req)) opp
460 with Not_found ->
461 CErrors.user_err (str "Ring opposite " ++ pr_econstr_env env sigma opp ++ str " should be declared as a morphism.") in
462 let op_morph =
463 op_morph r add mul opp req add_m_lem mul_m_lem opp_m_lem in
464 Flags.if_verbose
465 Feedback.msg_info
466 (str"Using setoid \""++ pr_econstr_env env sigma req++str"\""++spc()++
467 str"and morphisms \""++pr_econstr_env env sigma add_m ++
468 str"\","++spc()++ str"\""++pr_econstr_env env sigma mul_m++
469 str"\""++spc()++str"and \""++pr_econstr_env env sigma opp_m++
470 str"\"");
471 sigma, op_morph)
472 | None ->
473 (Flags.if_verbose
474 Feedback.msg_info
475 (str"Using setoid \""++pr_econstr_env env sigma req ++str"\"" ++ spc() ++
476 str"and morphisms \""++pr_econstr_env env sigma add_m ++
477 str"\""++spc()++str"and \""++
478 pr_econstr_env env sigma mul_m++str"\"");
479 sigma, op_smorph r add mul req add_m_lem mul_m_lem) in
480 (sigma,setoid,op_morph)
481
482 let build_setoid_params env sigma r add mul opp req eqth =
483 match eqth with
484 Some (a,b) -> sigma,a,b
485 | None -> ring_equality env sigma (r,add,mul,opp,req)
486
487 let dest_ring env sigma th_spec =
488 let th_typ = Retyping.get_type_of env sigma th_spec in
489 match EConstr.kind sigma th_typ with
490 App(f,[|r;zero;one;add;mul;sub;opp;req|])
491 when isRefX env sigma (rocq_almost_ring_theory ()) f ->
492 (None,r,zero,one,add,mul,Some sub,Some opp,req)
493 | App(f,[|r;zero;one;add;mul;req|])
494 when isRefX env sigma (rocq_semi_ring_theory ()) f ->
495 (Some true,r,zero,one,add,mul,None,None,req)
496 | App(f,[|r;zero;one;add;mul;sub;opp;req|])
497 when isRefX env sigma (rocq_ring_theory ()) f ->
498 (Some false,r,zero,one,add,mul,Some sub,Some opp,req)
499 | _ -> error "bad ring structure"
500
501
502 let reflect_coeff rkind =
503 (* We build an ill-typed terms on purpose... *)
504 match rkind with
505 Abstract -> rocq_abstract ()
506 | Computational c -> lapp rocq_comp [|c|]
507 | Morphism m -> lapp rocq_morph [|m|]
508
509 let interp_cst_tac env sigma rk kind (zero,one,add,mul,opp) cst_tac =
510 match cst_tac with
511 Some (CstTac t) -> Tacintern.glob_tactic t
512 | Some (Closed lc) ->
513 closed_term_ast (List.map Smartlocate.global_with_alias lc)
514 | None ->
515 let t = ArgArg(Loc.tag @@ Lazy.force ltac_inv_morph_nothing) in
516 CAst.make (TacArg (TacCall (CAst.make (t,[]))))
517
518 let make_hyp env sigma c =
519 let t = Retyping.get_type_of env sigma c in
520 plapp sigma rocq_mkhypo [|t;c|]
521
522 let make_hyp_list env sigma lH =
523 let sigma, carrier = Evd.fresh_global env sigma (rocq_hypo ()) in
524 let sigma, l =
525 List.fold_right
526 (fun c (sigma,l) ->
527 let sigma, c = make_hyp env sigma c in
528 plapp sigma rocq_cons [|carrier; c; l|]) lH
529 (plapp sigma rocq_nil [|carrier|])
530 in
531 let sigma, l' = Typing.solve_evars env sigma l in
532 sigma, l'
533
534 let interp_power env sigma pow =
535 let sigma, carrier = Evd.fresh_global env sigma (rocq_hypo ()) in
536 match pow with
537 | None ->
538 let t = ArgArg(Loc.tag (Lazy.force ltac_inv_morph_nothing)) in
539 let sigma, c = plapp sigma rocq_None [|carrier|] in
540 sigma, (CAst.make (TacArg (TacCall (CAst.make (t,[])))), c)
541 | Some (tac, spec) ->
542 let tac =
543 match tac with
544 | CstTac t -> Tacintern.glob_tactic t
545 | Closed lc ->
546 closed_term_ast (List.map Smartlocate.global_with_alias lc) in
547 let spec = ic_unsafe env sigma spec in
548 let sigma, spec = make_hyp env sigma spec in
549 let sigma, pow = plapp sigma rocq_Some [|carrier; spec|] in
550 sigma, (tac, pow)
551
552 let interp_sign env sigma sign =
553 let sigma, carrier = Evd.fresh_global env sigma (rocq_hypo ()) in
554 match sign with
555 | None -> plapp sigma rocq_None [|carrier|]
556 | Some spec ->
557 let sigma, spec = make_hyp env sigma (ic_unsafe env sigma spec) in
558 plapp sigma rocq_Some [|carrier;spec|]
559 (* Same remark on ill-typed terms ... *)
560
561 let interp_div env sigma div =
562 let sigma, carrier = Evd.fresh_global env sigma (rocq_hypo ()) in
563 match div with
564 | None -> plapp sigma rocq_None [|carrier|]
565 | Some spec ->
566 let sigma, spec = make_hyp env sigma (ic_unsafe env sigma spec) in
567 plapp sigma rocq_Some [|carrier;spec|]
568 (* Same remark on ill-typed terms ... *)
569
570 let add_theory0 env sigma name rth eqth morphth cst_tac (pre,post) power sign div =
571 check_required_library (cdir@["Ring_base"]);
572 let (kind,r,zero,one,add,mul,sub,opp,req) = dest_ring env sigma rth in
573 let (sigma, sth,ext) = build_setoid_params env sigma r add mul opp req eqth in
574 let sigma, (pow_tac, pspec) = interp_power env sigma power in
575 let sigma, sspec = interp_sign env sigma sign in
576 let sigma, dspec = interp_div env sigma div in
577 let rk = reflect_coeff morphth in
578 let params,ctx =
579 exec_tactic env sigma 5 (zltac "ring_lemmas")
580 [sth;ext;rth;pspec;sspec;dspec;rk] in
581 let lemma1 = params.(3) in
582 let lemma2 = params.(4) in
583
584 let lemma1 =
585 decl_constant name "_ring_lemma1" ctx lemma1 in
586 let lemma2 =
587 decl_constant name "_ring_lemma2" ctx lemma2 in
588 let cst_tac =
589 interp_cst_tac env sigma morphth kind (zero,one,add,mul,opp) cst_tac in
590 let pretac =
591 match pre with
592 Some t -> Tacintern.glob_tactic t
593 | _ -> CAst.make (TacId []) in
594 let posttac =
595 match post with
596 Some t -> Tacintern.glob_tactic t
597 | _ -> CAst.make (TacId []) in
598 let r = EConstr.to_constr sigma r in
599 let req = EConstr.to_constr sigma req in
600 let sth = EConstr.to_constr sigma sth in
601 let _ =
602 Lib.add_leaf
603 (theory_to_obj
604 { ring_name = name;
605 ring_carrier = r;
606 ring_req = req;
607 ring_setoid = sth;
608 ring_ext = params.(1);
609 ring_morph = params.(2);
610 ring_th = params.(0);
611 ring_cst_tac = cst_tac;
612 ring_pow_tac = pow_tac;
613 ring_lemma1 = lemma1;
614 ring_lemma2 = lemma2;
615 ring_pre_tac = pretac;
616 ring_post_tac = posttac }) in
617 ()
618
619 let ic_coeff_spec env sigma = function
620 | Computational t -> Computational (ic_unsafe env sigma t)
621 | Morphism t -> Morphism (ic_unsafe env sigma t)
622 | Abstract -> Abstract
623
624
625 let set_once s r v =
626 if Option.is_empty !r then r := Some v else error (s^" cannot be set twice")
627
628 let process_ring_mods env sigma l =
629 let kind = ref None in
630 let set = ref None in
631 let cst_tac = ref None in
632 let pre = ref None in
633 let post = ref None in
634 let sign = ref None in
635 let power = ref None in
636 let div = ref None in
637 List.iter(function
638 Ring_kind k -> set_once "ring kind" kind (ic_coeff_spec env sigma k)
639 | Const_tac t -> set_once "tactic recognizing constants" cst_tac t
640 | Pre_tac t -> set_once "preprocess tactic" pre t
641 | Post_tac t -> set_once "postprocess tactic" post t
642 | Setoid(sth,ext) -> set_once "setoid" set (ic_unsafe env sigma sth,ic_unsafe env sigma ext)
643 | Pow_spec(t,spec) -> set_once "power" power (t,spec)
644 | Sign_spec t -> set_once "sign" sign t
645 | Div_spec t -> set_once "div" div t) l;
646 let k = match !kind with Some k -> k | None -> Abstract in
647 (k, !set, !cst_tac, !pre, !post, !power, !sign, !div)
648
649 let add_theory id rth l =
650 let env = Global.env () in
651 let sigma = Evd.from_env env in
652 let sigma, rth = ic env sigma rth in
653 let (k,set,cst,pre,post,power,sign, div) = process_ring_mods env sigma l in
654 add_theory0 env sigma id rth set k cst (pre,post) power sign div
655
656 (*****************************************************************************)
657 (* The tactics consist then only in a lookup in the ring database and
658 call the appropriate ltac. *)
659
660 let make_args_list sigma rl t =
661 match rl with
662 | [] -> let (_,t1,t2) = dest_rel sigma t in [t1;t2]
663 | _ -> rl
664
665 let make_term_list env sigma carrier rl =
666 let sigma, l = List.fold_right
667 (fun x (sigma,l) -> plapp sigma rocq_cons [|carrier;x;l|]) rl
668 (plapp sigma rocq_nil [|carrier|])
669 in
670 Typing.solve_evars env sigma l
671
672 let carg c = Tacinterp.Value.of_constr (EConstr.of_constr c)
673 let tacarg expr =
674 Tacinterp.Value.of_closure (Tacinterp.default_ist ()) expr
675
676 let ltac_ring_structure e =
677 let req = carg e.ring_req in
678 let sth = carg e.ring_setoid in
679 let ext = carg e.ring_ext in
680 let morph = carg e.ring_morph in
681 let th = carg e.ring_th in
682 let cst_tac = tacarg e.ring_cst_tac in
683 let pow_tac = tacarg e.ring_pow_tac in
684 let lemma1 = carg e.ring_lemma1 in
685 let lemma2 = carg e.ring_lemma2 in
686 let pretac = tacarg (CAst.make (TacFun ([Anonymous],e.ring_pre_tac))) in
687 let posttac = tacarg (CAst.make (TacFun ([Anonymous],e.ring_post_tac))) in
688 [req;sth;ext;morph;th;cst_tac;pow_tac;
689 lemma1;lemma2;pretac;posttac]
690
691 let ring_lookup (f : Value.t) lH rl t =
692 Proofview.Goal.enter begin fun gl ->
693 let sigma = Proofview.Goal.sigma gl in
694 let env = Proofview.Goal.env gl in
695 let rl = make_args_list sigma rl t in
696 let e = find_ring_structure env sigma rl in
697 let sigma, l = make_term_list env sigma (EConstr.of_constr e.ring_carrier) rl in
698 let rl = Value.of_constr l in
699 let sigma, l = make_hyp_list env sigma lH in
700 let lH = Value.of_constr l in
701 let ring = ltac_ring_structure e in
702 Proofview.tclTHEN (Proofview.Unsafe.tclEVARS sigma) (Value.apply f (ring@[lH;rl]))
703 end
704
705 (***********************************************************************)
706
707 let new_field_path =
708 DirPath.make (List.map Id.of_string ["Field_tac";plugin_dir;"Stdlib"])
709
710 let field_ltac s =
711 lazy(KerName.make (ModPath.MPfile new_field_path) (Id.of_string s))
712
713
714 let _ = add_map "field"
715 (map_with_eq @@
716 base_red @
717 ring_red @ [
718 (* display_linear: evaluate polynomials and coef operations, protect
719 field operations and make recursive call on the var map *)
720 gen_reference "plugins.field.display_linear",
721 Arg (function 9|10|11|13|15|16->Eval|12|14->Rec|_->Prot);
722 gen_reference "plugins.field.display_pow_linear",
723 Arg (function 9|10|11|14|16|18|19->Eval|12|17->Rec|_->Prot);
724 (* FEeval: evaluate polynomial, protect field
725 operations and make recursive call on the var map *)
726 gen_reference "plugins.field.FEeval", Arg (function 12|15->Eval|10|14->Rec|_->Prot)]);;
727
728 let _ = add_map "field_cond"
729 (map_without_eq @@
730 base_red @ [
731 (* PCond: evaluate denum list, protect ring
732 operations and make recursive call on the var map *)
733 gen_reference "plugins.field.PCond", Arg (function 11|14->Eval|9|13->Rec|_->Prot)]);;
734
735
736 let _ = Redexpr.declare_reduction "simpl_field_expr"
737 (protect_red "field")
738
739
740
741 let afield_theory = gen_reference "plugins.field.almost_field_theory"
742 let field_theory = gen_reference "plugins.field.field_theory"
743 let sfield_theory = gen_reference "plugins.field.semi_field_theory"
744 let af_ar = gen_reference "plugins.field.AF_AR"
745 let f_r = gen_reference "plugins.field.F_R"
746 let sf_sr = gen_reference "plugins.field.SF_SR"
747 let dest_field env sigma th_spec =
748 let th_typ = Retyping.get_type_of env sigma th_spec in
749 match EConstr.kind sigma th_typ with
750 | App(f,[|r;zero;one;add;mul;sub;opp;div;inv;req|])
751 when isRefX env sigma (afield_theory ()) f ->
752 let sigma, rth = plapp sigma af_ar
753 [|r;zero;one;add;mul;sub;opp;div;inv;req;th_spec|] in
754 (None,r,zero,one,add,mul,Some sub,Some opp,div,inv,req,rth)
755 | App(f,[|r;zero;one;add;mul;sub;opp;div;inv;req|])
756 when isRefX env sigma (field_theory ()) f ->
757 let sigma, rth =
758 plapp sigma f_r
759 [|r;zero;one;add;mul;sub;opp;div;inv;req;th_spec|] in
760 (Some false,r,zero,one,add,mul,Some sub,Some opp,div,inv,req,rth)
761 | App(f,[|r;zero;one;add;mul;div;inv;req|])
762 when isRefX env sigma (sfield_theory ()) f ->
763 let sigma, rth = plapp sigma sf_sr
764 [|r;zero;one;add;mul;div;inv;req;th_spec|] in
765 (Some true,r,zero,one,add,mul,None,None,div,inv,req,rth)
766 | _ -> error "bad field structure"
767
768 let field_from_carrier = Summary.ref Cmap.empty ~name:"field-tac-carrier-table"
769
770 let print_fields () =
771 Feedback.msg_notice (strbrk "The following field structures have been declared:");
772 Cmap.iter (fun _carrier fi ->
773 let env = Global.env () in
774 let sigma = Evd.from_env env in
775 Feedback.msg_notice
776 (hov 2
777 (Id.print fi.field_name ++ spc() ++
778 str"with carrier "++ pr_constr_env env sigma fi.field_carrier++spc()++
779 str"and equivalence relation "++ pr_constr_env env sigma fi.field_req))
780 ) !field_from_carrier
781
782 let field_for_carrier r = Cmap.find r !field_from_carrier
783
784 let find_field_structure env sigma l =
785 check_required_library (cdir@["Field_tac"]);
786 match l with
787 | t::cl' ->
788 let ty = Retyping.get_type_of env sigma t in
789 let check c =
790 let ty' = Retyping.get_type_of env sigma c in
791 if not (Reductionops.is_conv env sigma ty ty') then
792 CErrors.user_err
793 (str"Arguments of field_simplify do not have all the same type.")
794 in
795 List.iter check cl';
796 (try field_for_carrier (EConstr.to_constr sigma ty)
797 with Not_found ->
798 CErrors.user_err
799 (str"Cannot find a declared field structure over"++
800 spc()++str"\""++pr_econstr_env env sigma ty++str"\"."))
801 | [] -> assert false
802
803 let add_field_entry e =
804 field_from_carrier := Cmap.add e.field_carrier e !field_from_carrier
805
806 let subst_th (subst,th) =
807 let c' = subst_mps subst th.field_carrier in
808 let eq' = subst_mps subst th.field_req in
809 let thm1' = subst_mps subst th.field_ok in
810 let thm2' = subst_mps subst th.field_simpl_eq_ok in
811 let thm3' = subst_mps subst th.field_simpl_ok in
812 let thm4' = subst_mps subst th.field_simpl_eq_in_ok in
813 let thm5' = subst_mps subst th.field_cond in
814 let tac'= Tacsubst.subst_tactic subst th.field_cst_tac in
815 let pow_tac' = Tacsubst.subst_tactic subst th.field_pow_tac in
816 let pretac'= Tacsubst.subst_tactic subst th.field_pre_tac in
817 let posttac'= Tacsubst.subst_tactic subst th.field_post_tac in
818 if c' == th.field_carrier &&
819 eq' == th.field_req &&
820 thm1' == th.field_ok &&
821 thm2' == th.field_simpl_eq_ok &&
822 thm3' == th.field_simpl_ok &&
823 thm4' == th.field_simpl_eq_in_ok &&
824 thm5' == th.field_cond &&
825 tac' == th.field_cst_tac &&
826 pow_tac' == th.field_pow_tac &&
827 pretac' == th.field_pre_tac &&
828 posttac' == th.field_post_tac then th
829 else
830 { field_name = th.field_name;
831 field_carrier = c';
832 field_req = eq';
833 field_cst_tac = tac';
834 field_pow_tac = pow_tac';
835 field_ok = thm1';
836 field_simpl_eq_ok = thm2';
837 field_simpl_ok = thm3';
838 field_simpl_eq_in_ok = thm4';
839 field_cond = thm5';
840 field_pre_tac = pretac';
841 field_post_tac = posttac' }
842
843 let ftheory_to_obj : field_info -> obj =
844 declare_object @@ global_object_nodischarge "tactic-new-field-theory"
845 ~cache:add_field_entry
846 ~subst:(Some subst_th)
847
848 let field_equality env sigma r inv req =
849 match EConstr.kind sigma req with
850 | App (f, [| _ |]) when isRefX env sigma (rocq_eq ()) f ->
851 let c = UnivGen.constr_of_monomorphic_global (Global.env ()) Rocqlib.(lib_ref "core.eq.congr") in
852 let c = EConstr.of_constr c in
853 sigma, mkApp(c,[|r;r;inv|])
854 | _ ->
855 let _setoid = setoid_of_relation env sigma r req in
856 let signature = [Some (r,Some req)],Some(r,Some req) in
857 let sigma, inv_m, inv_m_lem =
858 try Rewrite.Internal.default_morphism env sigma signature inv
859 with Not_found ->
860 error "field inverse should be declared as a morphism" in
861 sigma, inv_m_lem
862
863 let add_field_theory0 env sigma name fth eqth morphth cst_tac inj (pre,post) power sign odiv =
864 let open Constr in
865 check_required_library (cdir@["Field_tac"]);
866 let (sigma,fth) = ic env sigma fth in
867 let (kind,r,zero,one,add,mul,sub,opp,div,inv,req,rth) =
868 dest_field env sigma fth in
869 let (sigma,sth,ext) = build_setoid_params env sigma r add mul opp req eqth in
870 let eqth = Some(sth,ext) in
871 let _ = add_theory0 env sigma name rth eqth morphth cst_tac (pre,post) power sign odiv in
872 let sigma, (pow_tac, pspec) = interp_power env sigma power in
873 let sigma, sspec = interp_sign env sigma sign in
874 let sigma, dspec = interp_div env sigma odiv in
875 let sigma, inv_m = field_equality env sigma r inv req in
876 let rk = reflect_coeff morphth in
877 let params,ctx =
878 exec_tactic env sigma 9 (field_ltac"field_lemmas")
879 [sth;ext;inv_m;fth;pspec;sspec;dspec;rk] in
880 let lemma1 = params.(3) in
881 let lemma2 = params.(4) in
882 let lemma3 = params.(5) in
883 let lemma4 = params.(6) in
884 let cond_lemma =
885 match inj with
886 | Some thm -> mkApp(params.(8),[|EConstr.to_constr sigma thm|])
887 | None -> params.(7) in
888 let lemma1 = decl_constant name "_field_lemma1"
889 ctx lemma1 in
890 let lemma2 = decl_constant name "_field_lemma2"
891 ctx lemma2 in
892 let lemma3 = decl_constant name "_field_lemma3"
893 ctx lemma3 in
894 let lemma4 = decl_constant name "_field_lemma4"
895 ctx lemma4 in
896 let cond_lemma = decl_constant name "_lemma5"
897 ctx cond_lemma in
898 let cst_tac =
899 interp_cst_tac env sigma morphth kind (zero,one,add,mul,opp) cst_tac in
900 let pretac =
901 match pre with
902 Some t -> Tacintern.glob_tactic t
903 | _ -> CAst.make (TacId []) in
904 let posttac =
905 match post with
906 Some t -> Tacintern.glob_tactic t
907 | _ -> CAst.make (TacId []) in
908 let r = EConstr.to_constr sigma r in
909 let req = EConstr.to_constr sigma req in
910 let _ =
911 Lib.add_leaf
912 (ftheory_to_obj
913 { field_name = name;
914 field_carrier = r;
915 field_req = req;
916 field_cst_tac = cst_tac;
917 field_pow_tac = pow_tac;
918 field_ok = lemma1;
919 field_simpl_eq_ok = lemma2;
920 field_simpl_ok = lemma3;
921 field_simpl_eq_in_ok = lemma4;
922 field_cond = cond_lemma;
923 field_pre_tac = pretac;
924 field_post_tac = posttac }) in ()
925
926 let process_field_mods env sigma l =
927 let kind = ref None in
928 let set = ref None in
929 let cst_tac = ref None in
930 let pre = ref None in
931 let post = ref None in
932 let inj = ref None in
933 let sign = ref None in
934 let power = ref None in
935 let div = ref None in
936 List.iter(function
937 Ring_mod(Ring_kind k) -> set_once "field kind" kind (ic_coeff_spec env sigma k)
938 | Ring_mod(Const_tac t) ->
939 set_once "tactic recognizing constants" cst_tac t
940 | Ring_mod(Pre_tac t) -> set_once "preprocess tactic" pre t
941 | Ring_mod(Post_tac t) -> set_once "postprocess tactic" post t
942 | Ring_mod(Setoid(sth,ext)) -> set_once "setoid" set (ic_unsafe env sigma sth,ic_unsafe env sigma ext)
943 | Ring_mod(Pow_spec(t,spec)) -> set_once "power" power (t,spec)
944 | Ring_mod(Sign_spec t) -> set_once "sign" sign t
945 | Ring_mod(Div_spec t) -> set_once "div" div t
946 | Inject i -> set_once "infinite property" inj (ic_unsafe env sigma i)) l;
947 let k = match !kind with Some k -> k | None -> Abstract in
948 (env, sigma, k, !set, !inj, !cst_tac, !pre, !post, !power, !sign, !div)
949
950 let add_field_theory id t mods =
951 let env = Global.env () in
952 let sigma = Evd.from_env env in
953 let (env,sigma,k,set,inj,cst_tac,pre,post,power,sign,div) = process_field_mods env sigma mods in
954 add_field_theory0 env sigma id t set k cst_tac inj (pre,post) power sign div
955
956 let ltac_field_structure e =
957 let req = carg e.field_req in
958 let cst_tac = tacarg e.field_cst_tac in
959 let pow_tac = tacarg e.field_pow_tac in
960 let field_ok = carg e.field_ok in
961 let field_simpl_ok = carg e.field_simpl_ok in
962 let field_simpl_eq_ok = carg e.field_simpl_eq_ok in
963 let field_simpl_eq_in_ok = carg e.field_simpl_eq_in_ok in
964 let cond_ok = carg e.field_cond in
965 let pretac = tacarg (CAst.make (TacFun ([Anonymous],e.field_pre_tac))) in
966 let posttac = tacarg (CAst.make (TacFun ([Anonymous],e.field_post_tac))) in
967 [req;cst_tac;pow_tac;field_ok;field_simpl_ok;field_simpl_eq_ok;
968 field_simpl_eq_in_ok;cond_ok;pretac;posttac]
969
970 let field_lookup (f : Value.t) lH rl t =
971 Proofview.Goal.enter begin fun gl ->
972 let sigma = Proofview.Goal.sigma gl in
973 let env = Proofview.Goal.env gl in
974 let rl = make_args_list sigma rl t in
975 let e = find_field_structure env sigma rl in
976 let sigma, c = make_term_list env sigma (EConstr.of_constr e.field_carrier) rl in
977 let rl = Value.of_constr c in
978 let sigma, l = make_hyp_list env sigma lH in
979 let lH = Value.of_constr l in
980 let field = ltac_field_structure e in
981 Proofview.tclTHEN (Proofview.Unsafe.tclEVARS sigma) (Value.apply f (field@[lH;rl]))
982 end