Debian Codegraph @

vernac/vernacinterp.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 Vernacexpr
12 open Synterp
13
14 let vernac_pperr_endline = CDebug.create ~name:"vernacinterp" ()
15
16 let real_error_loc ~cmdloc ~eloc =
17 if Loc.finer eloc cmdloc then eloc
18 else cmdloc
19
20 let locate_if_not_already ?loc (e, info) =
21 (e, Option.cata (Loc.add_loc info) info (real_error_loc ~cmdloc:loc ~eloc:(Loc.get_loc info)))
22
23 let with_interp_state ~unfreeze_transient st =
24 let with_local_state synterp_st f =
25 unfreeze_transient synterp_st;
26 let v = f () in
27 Vernacstate.Interp.invalidate_cache ();
28 Vernacstate.unfreeze_full_state st;
29 (), v
30 in
31 { VernacControl.with_local_state }
32
33 let interp_control_gen ~loc ~st ~unfreeze_transient control f =
34 let noop = st.Vernacstate.interp.lemmas, st.Vernacstate.interp.program in
35 let control, res =
36 VernacControl.under_control ~loc
37 ~with_local_state:(with_interp_state ~unfreeze_transient st)
38 control
39 ~noop
40 (Flags.with_modified_ref Flags.in_synterp_phase (fun _ -> Some false) f)
41 in
42 if VernacControl.after_last_phase ~loc control
43 then noop
44 else res
45
46 (* [loc] is the [Loc.t] of the vernacular command being interpreted. *)
47 let rec interp_expr ?loc ~st cmd =
48 let before_univs = Global.universes () in
49 let pstack, pm = with_generic_atts ~check:false cmd.attrs (fun ~atts ->
50 interp_expr_core ?loc ~atts ~st cmd.expr)
51 in
52 let after_univs = Global.universes () in
53 if before_univs == after_univs then pstack, pm
54 else
55 let f = Declare.Proof.update_sigma_univs after_univs in
56 Option.map (Vernacstate.LemmaStack.map ~f) pstack, pm
57
58 and interp_expr_core ?loc ~atts ~st c =
59 match c with
60
61 (* The STM should handle that, but LOAD bypasses the STM... *)
62 | VernacSynPure VernacAbortAll -> CErrors.user_err (Pp.str "AbortAll cannot be used through the Load command")
63 | VernacSynPure VernacRestart -> CErrors.user_err (Pp.str "Restart cannot be used through the Load command")
64 | VernacSynPure VernacUndo _ -> CErrors.user_err (Pp.str "Undo cannot be used through the Load command")
65 | VernacSynPure VernacUndoTo _ -> CErrors.user_err (Pp.str "UndoTo cannot be used through the Load command")
66
67 (* Resetting *)
68 | VernacSynPure VernacResetName _ -> CErrors.anomaly (Pp.str "VernacResetName not handled by Stm.")
69 | VernacSynPure VernacResetInitial -> CErrors.anomaly (Pp.str "VernacResetInitial not handled by Stm.")
70 | VernacSynPure VernacBack _ -> CErrors.anomaly (Pp.str "VernacBack not handled by Stm.")
71
72 | VernacSynterp EVernacLoad (verbosely, fname) ->
73 Attributes.unsupported_attributes atts;
74 vernac_load ~verbosely fname
75
76 | v ->
77 let fv = Vernacentries.translate_vernac ?loc ~atts v in
78 let stack = st.Vernacstate.interp.lemmas in
79 let program = st.Vernacstate.interp.program in
80 let {Vernactypes.prog; proof; opaque_access=(); }, () = Vernactypes.run fv {
81 prog=program;
82 proof=stack;
83 opaque_access=();
84 }
85 in
86 proof, prog
87
88 and vernac_load ~verbosely entries =
89 (* Note that no proof should be open here, so the state here is just token for now *)
90 let st = Vernacstate.freeze_full_state () in
91 let v_mod = if verbosely then Flags.verbosely else Flags.silently in
92 let interp_entry (stack, pm) (CAst.{ loc; v = cmd }, synterp_st) =
93 Vernacstate.Synterp.unfreeze synterp_st;
94 let st = Vernacstate.{ synterp = synterp_st; interp = { st.interp with Interp.lemmas = stack; program = pm }} in
95 v_mod (interp_control ~st) (CAst.make ?loc cmd)
96 in
97 let pm = st.Vernacstate.interp.program in
98 let stack = st.Vernacstate.interp.lemmas in
99 let stack, pm =
100 Dumpglob.with_glob_output Dumpglob.NoGlob
101 (fun () -> List.fold_left interp_entry (stack, pm) entries) ()
102 in
103 (* If Load left a proof open, we fail too. *)
104 if Option.has_some stack then
105 CErrors.user_err Pp.(str "Files processed by Load cannot leave open proofs.");
106 stack, pm
107
108 and interp_control ~st ({ CAst.v = cmd; loc }) =
109 Util.try_finally (fun () ->
110 Loc.set_current_command_loc loc;
111 interp_control_gen ~loc ~st cmd.control
112 ~unfreeze_transient:Vernacstate.Synterp.unfreeze
113 (fun () -> interp_expr ?loc ~st cmd))
114 ()
115 (fun () -> Loc.set_current_command_loc None)
116 ()
117
118 (* XXX: This won't properly set the proof mode, as of today, it is
119 controlled by the STM. Thus, we would need access information from
120 the classifier. The proper fix is to move it to the STM, however,
121 the way the proof mode is set there makes the task non trivial
122 without a considerable amount of refactoring.
123 *)
124
125 (* Interpreting a possibly delayed proof *)
126 let interp_qed_delayed ~proof ~st pe =
127 let stack = st.Vernacstate.interp.lemmas in
128 let pm = st.Vernacstate.interp.program in
129 let stack = Option.cata (fun stack -> snd @@ Vernacstate.LemmaStack.pop stack) None stack in
130 let pm = NeList.map_head (fun pm -> match pe with
131 | Admitted ->
132 Declare.Proof.save_lemma_admitted_delayed ~pm ~proof
133 | Proved (_,idopt) ->
134 let pm = Declare.Proof.save_lemma_proved_delayed ~pm ~proof ~idopt in
135 pm)
136 pm
137 in
138 stack, pm
139
140 let interp_qed_delayed_control ~proof ~st ~control { CAst.loc; v=pe } =
141 interp_control_gen ~loc ~st control
142 ~unfreeze_transient:(fun () -> ())
143 (fun () -> interp_qed_delayed ~proof ~st pe)
144
145 (* General interp with management of state *)
146
147 (* Be careful with the cache here in case of an exception. *)
148 let interp_gen ~verbosely ~st ~interp_fn cmd =
149 try
150 let v_mod = if verbosely then Flags.verbosely else Flags.silently in
151 let ontop = v_mod (interp_fn ~st) cmd in
152 Vernacstate.Declare.set ontop [@ocaml.warning "-3"];
153 Vernacstate.Interp.freeze_interp_state ()
154 with exn ->
155 let exn = Exninfo.capture exn in
156 let exn = locate_if_not_already ?loc:cmd.CAst.loc exn in
157 Vernacstate.Interp.invalidate_cache ();
158 Exninfo.iraise exn
159
160 (* Regular interp *)
161 let interp ~intern ?(verbosely=true) ~st cmd =
162 Vernacstate.unfreeze_full_state st;
163 vernac_pperr_endline Pp.(fun () -> str "interpreting: " ++ Ppvernac.pr_vernac_expr cmd.CAst.v.expr);
164 let entry = NewProfile.profile "synterp" (fun () -> Synterp.synterp_control ~intern cmd) () in
165 let interp = NewProfile.profile "interp" (fun () -> interp_gen ~verbosely ~st ~interp_fn:interp_control entry) () in
166 Vernacstate.{ synterp = Vernacstate.Synterp.freeze (); interp }
167
168 let interp_entry ?(verbosely=true) ~st entry =
169 Vernacstate.unfreeze_full_state st;
170 interp_gen ~verbosely ~st ~interp_fn:interp_control entry
171
172 module Intern = struct
173
174 let fs_intern dp =
175 match Loadpath.locate_absolute_library dp with
176 | Ok file ->
177 Feedback.feedback @@ Feedback.FileDependency (Some file, Names.DirPath.to_string dp);
178 let res, provenance = Library.intern_from_file file in
179 Result.iter (fun _ ->
180 Feedback.feedback @@ Feedback.FileLoaded (Names.DirPath.to_string dp, file)) res;
181 res, provenance
182 | Error e ->
183 Loadpath.Error.raise dp e
184 end
185
186 let fs_intern = Intern.fs_intern
187
188 let interp_qed_delayed_proof ~proof ~st ~control (CAst.{loc; v = pe } as e) : Vernacstate.Interp.t =
189 NewProfile.profile "interp-delayed-qed" (fun () ->
190 interp_gen ~verbosely:false ~st
191 ~interp_fn:(interp_qed_delayed_control ~proof ~control) e)
192 ()