1 (* Copyright (C) 2004, HELM Team.
3 * This file is part of HELM, an Hypertextual, Electronic
4 * Library of Mathematics, developed at the Computer Science
5 * Department, University of Bologna, Italy.
7 * HELM is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * HELM is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with HELM; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
26 (** Interpreter for textual phrases coming from matita's console (textual entry
27 * window at the bottom of the main window).
29 * Interpreter is either in `Command state or in `Proof state (see state_tag type
30 * below). In `Command state commands for starting proofs are accepted, but
31 * tactic and tactical applications are not. In `Proof state both
32 * tactic/tacticals and commands are accepted.
39 (** None means: "same state as before" *)
40 type state_tag = [ `Command | `Proof ] option
42 exception Command_error of string
46 UriManager.uri_of_string (sprintf "%s/%s" BuildTimeConf.base_uri name)
49 let baseuri = ref "cic:/matita"
51 let baseuri = !baseuri in
52 if baseuri.[String.length baseuri - 1] = '/' then
55 String.concat "/" [baseuri; name]
57 let canonical_context metano metasenv =
59 let (_, context, _) = List.find (fun (m, _, _) -> m = metano) metasenv in
62 failwith (sprintf "Can't find canonical context for %d" metano)
64 let get_context_and_metasenv (proof_handler:MatitaTypes.proof_handler) =
65 if proof_handler.MatitaTypes.has_proof () then
66 let proof = proof_handler.MatitaTypes.get_proof () in
67 let metasenv = proof#metasenv in
68 let goal = proof#goal in
69 (canonical_context goal metasenv, metasenv)
73 (** term AST -> Cic.term. Uses disambiguator and change imperatively the
74 * metasenv as needed *)
75 let disambiguate ~(disambiguator:MatitaTypes.disambiguator) ~proof_handler ast =
76 if proof_handler.MatitaTypes.has_proof () then begin
77 let proof = proof_handler.MatitaTypes.get_proof () in
78 let metasenv = proof#metasenv in
79 let goal = proof#goal in
80 let context = canonical_context goal metasenv in
81 let (_, metasenv, term,ugraph) as retval =
82 disambiguator#disambiguateTermAst ~context ~metasenv ast
84 proof#set_metasenv metasenv;
87 disambiguator#disambiguateTermAst ast
89 class virtual interpreterState =
90 (* static values, shared by all states inheriting this class *)
92 let history = ref [] in
93 fun ~(console: MatitaConsole.console) ->
96 (** eval a toplevel phrase in the current state and return the new state
98 method parsePhrase s =
99 match CicTextualParser2.parse_tactical (Stream.of_string s) with
100 | (TacticAst.LocatedTactical (loc', tac)) as tactical ->
102 (match tac with (* update interpreter history *)
103 | TacticAst.Command (TacticAst.Qed None) ->
104 history := `Qed :: !history
105 | TacticAst.Command (TacticAst.Theorem (_, Some name, _, None)) ->
106 history := `Theorem name :: !history
107 | TacticAst.Command (TacticAst.Qed _)
108 | TacticAst.Command (TacticAst.Theorem _) -> assert false
109 | _ -> history := `Tactic :: !history);
113 method virtual evalTactical:
114 (CicAst.term, string) TacticAst.tactical -> state_tag
116 method evalPhrase s =
117 debug_print (sprintf "evaluating '%s'" s);
118 self#evalTactical (self#parsePhrase s)
120 method evalAst ast = self#evalTactical ast
124 | Some (start_pos, end_pos) -> end_pos.Lexing.pos_cnum
125 | None -> failwith "MatitaInterpreter: no offset recorded"
129 let check_widget: MatitaTypes.sequent_viewer lazy_t = lazy
130 (let gui = MatitaGui.instance () in
131 MatitaMathView.sequent_viewer ~show:true ~packing:gui#check#scrolledCheck#add
134 (** Implements phrases that should be accepted in all states *)
136 ~(disambiguator: MatitaTypes.disambiguator)
137 ~(proof_handler: MatitaTypes.proof_handler)
138 ~(console: MatitaConsole.console)
142 inherit interpreterState ~console
143 method evalTactical = function
144 | TacticAst.Command TacticAst.Quit ->
145 proof_handler.MatitaTypes.quit ();
146 Some `Command (* dummy answer, useless *)
147 | TacticAst.Command TacticAst.Proof ->
148 (* do nothing, just for compatibility with coq syntax *)
150 | TacticAst.Command (TacticAst.Baseuri (Some uri)) ->
152 console#echo_message (sprintf "base uri set to \"%s\"" uri);
154 | TacticAst.Command (TacticAst.Baseuri None) ->
155 console#echo_message (sprintf "base uri is \"%s\"" !baseuri);
157 | TacticAst.Command (TacticAst.Check term) ->
158 let (_, _, term,ugraph) =
159 disambiguate ~disambiguator ~proof_handler term
161 let (context, metasenv) = get_context_and_metasenv proof_handler in
162 let dummyno = CicMkImplicit.new_meta metasenv [] in
164 CicTypeChecker.type_of_aux' metasenv context term ugraph
166 (* TASSI: here ugraph1 is unused.... FIXME *)
167 let expr = Cic.Cast (term, ty) in
168 let sequent = (dummyno, context, expr) in
169 let widget = Lazy.force check_widget in
170 let gui = MatitaGui.instance () in
171 gui#check#checkWin#show ();
172 gui#main#showCheckMenuItem#set_active true;
173 widget#load_sequent (sequent::metasenv) dummyno;
176 raise (Command_error (TacticAstPp.pp_tactical tactical))
179 (** Implements phrases that should be accepted only in `Command state *)
181 ~(disambiguator: MatitaTypes.disambiguator)
182 ~(proof_handler: MatitaTypes.proof_handler)
183 ~(console: MatitaConsole.console)
186 let shared = new sharedState ~disambiguator ~proof_handler ~console () in
188 inherit interpreterState ~console
190 method evalTactical = function
191 | TacticAst.LocatedTactical (_, tactical) -> self#evalTactical tactical
192 | TacticAst.Command (TacticAst.Theorem (_, Some name, ast, None)) ->
193 let (_, metasenv, expr,ugraph) =
194 disambiguator#disambiguateTermAst ast
196 let uri = UriManager.uri_of_string (qualify name) in
197 let proof = MatitaProof.proof ~typ:expr ~uri ~metasenv () in
198 proof_handler.MatitaTypes.new_proof proof;
200 | TacticAst.Command TacticAst.Quit ->
201 proof_handler.MatitaTypes.quit ();
202 Some `Command (* dummy answer, useless *)
203 | TacticAst.Command TacticAst.Proof ->
204 (* do nothing, just for compatibility with coq syntax *)
206 | tactical -> shared#evalTactical tactical
209 (** create a ProofEngineTypes.mk_fresh_name_type function which uses given
210 * names as long as they are available, then it fallbacks to name generation
211 * using FreshNamesGenerator module *)
213 let len = List.length names in
215 fun metasenv context name ~typ ->
216 if !count < len then begin
217 let name = Cic.Name (List.nth names !count) in
221 FreshNamesGenerator.mk_fresh_name ~subst:[] metasenv context name ~typ
223 (** Implements phrases that should be accepted only in `Proof state, basically
226 ~(disambiguator: MatitaTypes.disambiguator)
227 ~(proof_handler: MatitaTypes.proof_handler)
228 ~(console: MatitaConsole.console)
232 let disambiguate ast =
233 let (_, _, term,ugraph) =
234 disambiguate ~disambiguator ~proof_handler ast
238 (** tactic AST -> ProofEngineTypes.tactic *)
239 let rec lookup_tactic = function
240 | TacticAst.LocatedTactic (_, tactic) -> lookup_tactic tactic
241 | TacticAst.Intros (_, names) -> (* TODO Zack implement intros length *)
242 PrimitiveTactics.intros_tac ~mk_fresh_name_callback:(namer_of names) ()
243 | TacticAst.Reflexivity -> Tactics.reflexivity
244 | TacticAst.Assumption -> Tactics.assumption
245 | TacticAst.Contradiction -> Tactics.contradiction
246 | TacticAst.Exists -> Tactics.exists
247 | TacticAst.Fourier -> Tactics.fourier
248 | TacticAst.Left -> Tactics.left
249 | TacticAst.Right -> Tactics.right
250 | TacticAst.Ring -> Tactics.ring
251 | TacticAst.Split -> Tactics.split
252 | TacticAst.Symmetry -> Tactics.symmetry
253 | TacticAst.Transitivity term -> Tactics.transitivity (disambiguate term)
254 | TacticAst.Apply term -> Tactics.apply (disambiguate term)
255 | TacticAst.Absurd term -> Tactics.absurd (disambiguate term)
256 | TacticAst.Exact term -> Tactics.exact (disambiguate term)
257 | TacticAst.Cut term -> Tactics.cut (disambiguate term)
258 | TacticAst.Elim (term, _) -> (* TODO Zack implement "using" argument *)
259 Tactics.elim_intros_simpl (disambiguate term)
260 | TacticAst.ElimType term -> Tactics.elim_type (disambiguate term)
261 | TacticAst.Replace (what, with_what) ->
262 Tactics.replace ~what:(disambiguate what)
263 ~with_what:(disambiguate with_what)
264 | TacticAst.Auto -> Tactics.auto_new ~dbd
266 (* TODO Zack a lot more of tactics to be implemented here ... *)
267 | TacticAst.Change of 'term * 'term * 'ident option
268 | TacticAst.Change_pattern of 'term pattern * 'term * 'ident option
269 | TacticAst.Decompose of 'ident * 'ident list
270 | TacticAst.Discriminate of 'ident
271 | TacticAst.Fold of reduction_kind * 'term
272 | TacticAst.Injection of 'ident
273 | TacticAst.LetIn of 'term * 'ident
274 | TacticAst.Reduce of reduction_kind * 'term pattern * 'ident option
275 | TacticAst.Replace_pattern of 'term pattern * 'term
276 | TacticAst.Rewrite of direction * 'term * 'ident option
279 MatitaTypes.not_implemented "some tactic"
281 let shared = new sharedState ~disambiguator ~proof_handler ~console () in
283 inherit interpreterState ~console
285 method evalTactical = function
286 | TacticAst.LocatedTactical (_, tactical) -> self#evalTactical tactical
287 | TacticAst.Command TacticAst.Abort ->
288 proof_handler.MatitaTypes.abort_proof ();
290 | TacticAst.Command (TacticAst.Undo steps) ->
291 (proof_handler.MatitaTypes.get_proof ())#undo ?steps ();
293 | TacticAst.Command (TacticAst.Redo steps) ->
294 (proof_handler.MatitaTypes.get_proof ())#redo ?steps ();
296 | TacticAst.Command (TacticAst.Qed None) ->
297 (* TODO Zack this function probably should not simply fail with
298 * Failure, but rather raise some more meaningful exception *)
299 if not (proof_handler.MatitaTypes.has_proof ()) then assert false;
300 let proof = proof_handler.MatitaTypes.get_proof () in
301 let (uri, metasenv, bo, ty) = proof#proof in
302 let uri = MatitaTypes.unopt_uri uri in
303 if metasenv <> [] then failwith "Proof not completed";
304 let proved_ty,ugraph =
305 CicTypeChecker.type_of_aux' [] [] bo CicUniv.empty_ugraph
308 CicReduction.are_convertible [] proved_ty ty ugraph
311 failwith "Wrong proof";
312 (* TODO Zack [] probably wrong *)
313 let ugraph2 = CicUniv.fill_empty_nodes_with_uri ugraph1 uri in
314 CicEnvironment.add_type_checked_term uri
315 ((Cic.Constant ((UriManager.name_of_uri uri),
318 proof_handler.MatitaTypes.set_proof None;
319 (MatitaMathView.proof_viewer_instance ())#unload;
320 (* TODO Zack a lot more to be done here:
321 * - save object to disk in xml format
323 * - register uri to the getter
324 * - save universe file *)
326 | TacticAst.Seq tacticals ->
327 (* TODO Zack check for proof completed at each step? *)
328 List.iter (fun t -> ignore (self#evalTactical t)) tacticals;
330 | TacticAst.Tactic tactic_phrase ->
331 let tactic = lookup_tactic tactic_phrase in
332 (proof_handler.MatitaTypes.get_proof ())#apply_tactic tactic;
334 | tactical -> shared#evalTactical tactical
338 ~(disambiguator: MatitaTypes.disambiguator)
339 ~(proof_handler: MatitaTypes.proof_handler)
340 ~(console: MatitaConsole.console)
345 new commandState ~disambiguator ~proof_handler ~console ()
348 new proofState ~disambiguator ~proof_handler ~console ~dbd ()
351 val mutable state = commandState
353 method reset = state <- commandState
355 method endOffset = state#endOffset
357 method private updateState = function
358 | Some `Command -> state <- commandState
359 | Some `Proof -> state <- proofState
362 method evalPhrase s =
364 console#wrap_exn (fun () -> self#updateState (state#evalPhrase s))
366 if success then console#clear ();
371 console#wrap_exn (fun () -> self#updateState (state#evalAst ast))
373 if success then console#clear ();