Debian Codegraph @

tactics/fixTactics.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 open Util
12 open Names
13 open Context
14 open Termops
15 open Environ
16 open EConstr
17 open Inductiveops
18 open Reductionops
19
20 let rec mk_holes env sigma = function
21 | [] -> (sigma, [])
22 | arg :: rem ->
23 let (sigma, arg) = Evarutil.new_evar env sigma arg in
24 let (sigma, rem) = mk_holes env sigma rem in
25 (sigma, arg :: rem)
26
27 let rec check_mutind env sigma k cl = match EConstr.kind sigma (strip_outer_cast sigma cl) with
28 | Prod (na, c1, b) ->
29 if Int.equal k 1 then
30 try ignore (find_inductive env sigma c1)
31 with Not_found -> TacticErrors.fixpoint_on_non_inductive_type ()
32 else
33 let open Context.Rel.Declaration in
34 check_mutind (push_rel (LocalAssum (na, c1)) env) sigma (pred k) b
35 | LetIn (na, c1, t, b) ->
36 let open Context.Rel.Declaration in
37 check_mutind (push_rel (LocalDef (na, c1, t)) env) sigma k b
38 | _ -> TacticErrors.not_enough_products ()
39
40 let mutual_fix f n others = Proofview.Goal.enter begin fun gl ->
41 let env = Proofview.Goal.env gl in
42 let sigma = Proofview.Goal.sigma gl in
43 let concl = Proofview.Goal.concl gl in
44 let () = check_mutind env sigma n concl in
45 let all = (f, n, concl) :: others in
46 let all = List.map (fun (f, n, ar) ->
47 let r = Retyping.relevance_of_type env sigma ar in
48 (f, r, n, ar))
49 all
50 in
51 let rec mk_sign sign = function
52 | [] -> sign
53 | (f, r, n, ar) :: oth ->
54 let open Context.Named.Declaration in
55 let () = check_mutind env sigma n ar in
56 if mem_named_context_val f sign then
57 TacticErrors.intro_already_declared f;
58 mk_sign (push_named_context_val (LocalAssum (make_annot f r, ar)) sign) oth
59 in
60 let nenv = reset_with_named_context (mk_sign (named_context_val env) all) env in
61 Refine.refine ~typecheck:false begin fun sigma ->
62 let (sigma, evs) = mk_holes nenv sigma (List.map (fun (_,_,_,ar) -> ar) all) in
63 let ids, rs, _, ars = List.split4 all in
64 let evs = List.map (Vars.subst_vars sigma (List.rev ids)) evs in
65 let indxs = Array.of_list (List.map (fun n -> n-1) (List.map (fun (_,_,n,_) -> n) all)) in
66 let funnames = Array.of_list (List.map2 (fun i r -> make_annot (Name i) r) ids rs) in
67 let bodies = Array.of_list evs in
68 let typarray = Array.of_list ars in
69 let oterm = mkFix ((indxs,0),(funnames,typarray,bodies)) in
70 (sigma, oterm)
71 end
72 end
73
74 let fix id n = mutual_fix id n []
75
76 let rec check_is_mutcoind env sigma cl =
77 let b = whd_all env sigma cl in
78 match EConstr.kind sigma b with
79 | Prod (na, c1, b) ->
80 let open Context.Rel.Declaration in
81 check_is_mutcoind (push_rel (LocalAssum (na,c1)) env) sigma b
82 | _ ->
83 try
84 let _ = find_coinductive env sigma b in ()
85 with Not_found ->
86 TacticErrors.all_methods_in_coinductive_type ()
87
88 let mutual_cofix f others = Proofview.Goal.enter begin fun gl ->
89 let env = Proofview.Goal.env gl in
90 let sigma = Proofview.Goal.sigma gl in
91 let concl = Proofview.Goal.concl gl in
92 let all = (f, concl) :: others in
93 List.iter (fun (_, c) -> check_is_mutcoind env sigma c) all;
94 let all = List.map (fun (id,t) -> (id, Retyping.relevance_of_type env sigma t, t)) all in
95 let rec mk_sign sign = function
96 | [] -> sign
97 | (f, r, ar) :: oth ->
98 let open Context.Named.Declaration in
99 if mem_named_context_val f sign then
100 TacticErrors.already_used f;
101 mk_sign (push_named_context_val (LocalAssum (make_annot f r, ar)) sign) oth
102 in
103 let nenv = reset_with_named_context (mk_sign (named_context_val env) all) env in
104 Refine.refine ~typecheck:false begin fun sigma ->
105 let (ids, rs, types) = List.split3 all in
106 let (sigma, evs) = mk_holes nenv sigma types in
107 let evs = List.map (Vars.subst_vars sigma (List.rev ids)) evs in
108 (* TODO relevance *)
109 let funnames = Array.of_list (List.map2 (fun i r -> make_annot (Name i) r) ids rs) in
110 let typarray = Array.of_list types in
111 let bodies = Array.of_list evs in
112 let oterm = mkCoFix (0, (funnames, typarray, bodies)) in
113 (sigma, oterm)
114 end
115 end
116
117 let cofix id = mutual_cofix id []