Debian Codegraph @

plugins/ssr/ssrbwd.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 (* This file is (C) Copyright 2006-2015 Microsoft Corporation and Inria. *)
12
13 open Printer
14 open Pretyping
15 open Glob_term
16
17 open Ssrmatching_plugin
18 open Ssrmatching
19
20 open Ssrast
21 open Ssrcommon
22
23 (** Backward chaining tactics: apply, exact, congr. *)
24
25 (** The "apply" tactic *)
26
27 let interp_agen ist env sigma ((goclr, _), (k, gc as c)) (clr, rcs) =
28 (* ppdebug(lazy(str"sigma@interp_agen=" ++ pr_evar_map None (project gl))); *)
29 let rc = intern_term ist env c in
30 let rcs' = rc :: rcs in
31 match goclr with
32 | None -> clr, rcs'
33 | Some ghyps ->
34 let clr' = interp_hyps ist env sigma ghyps @ clr in
35 if k <> NoFlag then clr', rcs' else
36 let loc = rc.CAst.loc in
37 match DAst.get rc with
38 | GVar id when not_section_id id -> SsrHyp (Loc.tag ?loc id) :: clr', rcs'
39 | GRef (Names.GlobRef.VarRef id, _) when not_section_id id ->
40 SsrHyp (Loc.tag ?loc id) :: clr', rcs'
41 | _ -> clr', rcs'
42
43 let interp_nbargs ist env sigma rc =
44 try
45 let rc6 = mkRApp rc (mkRHoles 6) in
46 let t = interp_open_constr env sigma ist (rc6, None) in
47 6 + Ssrcommon.nbargs_open_constr env t
48 with e when CErrors.noncritical e -> 5
49
50 let interp_view_nbimps ist env sigma rc =
51 try
52 let t = interp_open_constr env sigma ist (rc, None) in
53 let pl, c = splay_open_constr env t in
54 if Ssrcommon.isAppInd env sigma c then List.length pl else (-(List.length pl))
55 with e when CErrors.noncritical e -> 0
56
57 let interp_agens ist env sigma ~concl gagens =
58 match List.fold_right (interp_agen ist env sigma) gagens ([], []) with
59 | clr, rlemma :: args ->
60 let n = interp_nbargs ist env sigma rlemma - List.length args in
61 let rec loop i =
62 if i > n then
63 errorstrm Pp.(str "Cannot apply lemma " ++ pr_glob_constr_env env sigma rlemma)
64 else
65 try interp_refine env sigma ist ~concl (mkRApp rlemma (mkRHoles i @ args))
66 with e when CErrors.noncritical e -> loop (i + 1) in
67 clr, loop 0
68 | _ -> assert false
69
70 let apply_rconstr ?ist t =
71 Proofview.Goal.enter begin fun gl ->
72 let env = Proofview.Goal.env gl in
73 let sigma = Proofview.Goal.sigma gl in
74 let cl = Proofview.Goal.concl gl in
75 (* ppdebug(lazy(str"sigma@apply_rconstr=" ++ pr_evar_map None (project gl))); *)
76 let n = match ist, DAst.get t with
77 | None, (GVar id | GRef (Names.GlobRef.VarRef id,_)) -> pf_nbargs env sigma (EConstr.mkVar id)
78 | Some ist, _ -> interp_nbargs ist env sigma t
79 | _ -> anomaly "apply_rconstr without ist and not RVar" in
80 let mkRlemma i = mkRApp t (mkRHoles i) in
81 let rec loop i =
82 if i > n then
83 errorstrm Pp.(str"Cannot apply lemma "++pr_glob_constr_env env sigma t)
84 else try understand_tcc env sigma ~expected_type:(OfType cl) (mkRlemma i)
85 with e when CErrors.noncritical e -> loop (i + 1) in
86 refine_with (loop 0)
87 end
88
89 let mkRAppView ist env sigma rv gv =
90 let nb_view_imps = interp_view_nbimps ist env sigma rv in
91 mkRApp rv (mkRHoles (abs nb_view_imps))
92
93 let refine_interp_apply_view dbl ist gv =
94 Proofview.Goal.enter begin fun gl ->
95 let sigma = Proofview.Goal.sigma gl in
96 let env = Proofview.Goal.env gl in
97 let concl = Proofview.Goal.concl gl in
98 let pair i = List.map (fun x -> i, x) in
99 let rv = intern_term ist env gv in
100 let v = mkRAppView ist env sigma rv gv in
101 let interp_with (dbl, hint) =
102 let i = if dbl = Ssrview.AdaptorDb.Equivalence then 2 else 1 in
103 interp_refine env sigma ist ~concl (mkRApp hint (v :: mkRHoles i)) in
104 let rec loop = function
105 | [] -> Proofview.tclORELSE (apply_rconstr ~ist rv) (fun _ -> view_error "apply" gv)
106 | h :: hs ->
107 match interp_with h with
108 | t -> Proofview.tclORELSE (refine_with t) (fun _ -> loop hs)
109 | exception e when CErrors.noncritical e -> loop hs
110 in
111 loop (pair dbl (Ssrview.AdaptorDb.get dbl) @
112 if dbl = Ssrview.AdaptorDb.Equivalence
113 then pair Ssrview.AdaptorDb.Backward (Ssrview.AdaptorDb.(get Backward))
114 else [])
115 end
116
117 let apply_top_tac =
118 Proofview.Goal.enter begin fun _ ->
119 Tacticals.tclTHENLIST [
120 introid top_id;
121 apply_rconstr (mkRVar top_id);
122 cleartac [SsrHyp(None,top_id)]
123 ]
124 end
125
126 let inner_ssrapplytac gviews (ggenl, gclr) ist =
127 Proofview.Goal.enter begin fun gl ->
128 let sigma = Proofview.Goal.sigma gl in
129 let env = Proofview.Goal.env gl in
130 let clr = interp_hyps ist env sigma gclr in
131 let vtac gv i = refine_interp_apply_view i ist gv in
132 let ggenl, tclGENTAC =
133 if gviews <> [] && ggenl <> [] then
134 let ggenl= List.map (fun (x,(k,p)) -> x, {kind=k; pattern=p; interpretation= Some ist}) (List.hd ggenl) in
135 [], Tacticals.tclTHEN (genstac (ggenl,[]))
136 else ggenl, (fun tac -> tac) in
137 tclGENTAC (Proofview.Goal.enter (fun gl ->
138 match gviews, ggenl with
139 | v :: tl, [] ->
140 let dbl =
141 if List.length tl = 1
142 then Ssrview.AdaptorDb.Equivalence
143 else Ssrview.AdaptorDb.Backward in
144 Tacticals.tclTHEN
145 (List.fold_left (fun acc v ->
146 Tacticals.tclTHENLAST acc (vtac v dbl))
147 (vtac v Ssrview.AdaptorDb.Backward) tl)
148 (cleartac clr)
149 | [], [agens] ->
150 let sigma = Proofview.Goal.sigma gl in
151 let env = Proofview.Goal.env gl in
152 let concl = Proofview.Goal.concl gl in
153 let clr', lemma = interp_agens ist env sigma ~concl agens in
154 let sigma = Evd.merge_universe_context sigma (Evd.ustate (fst lemma)) in
155 Tacticals.tclTHENLIST [Proofview.Unsafe.tclEVARS sigma; cleartac clr; refine_with ~beta:true lemma; cleartac clr']
156 | _, _ ->
157 Tacticals.tclTHENLIST [apply_top_tac; cleartac clr]))
158
159 end