]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitaInterpreter.ml
renamed Http_client to Http_user_agent to avoid clashes with Gerd's
[helm.git] / helm / matita / matitaInterpreter.ml
1 (* Copyright (C) 2004, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 (** Interpreter for textual phrases coming from matita's console (textual entry
27 * window at the bottom of the main window).
28 *
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.
33 *)
34
35 open Printf
36
37   (** None means: "same state as before" *)
38 type state_tag = [ `Command | `Proof ] option
39
40 exception Command_error of string
41
42 let uri name =
43   UriManager.uri_of_string (sprintf "%s/%s" BuildTimeConf.base_uri name)
44
45 let canonical_context metano metasenv =
46   try
47     let (_, context, _) = List.find (fun (m, _, _) -> m = metano) metasenv in
48     context
49   with Not_found ->
50     failwith (sprintf "Can't find canonical context for %d" metano)
51
52 let get_context_and_metasenv (proof_handler:MatitaTypes.proof_handler) =
53   if proof_handler.MatitaTypes.has_proof () then
54     let proof = proof_handler.MatitaTypes.get_proof () in
55     let metasenv = proof#metasenv in
56     let goal = proof#goal in
57     (canonical_context goal metasenv, metasenv)
58   else
59     ([], [])
60
61   (** term AST -> Cic.term. Uses disambiguator and change imperatively the
62   * metasenv as needed *)
63 let disambiguate ~(disambiguator:MatitaTypes.disambiguator) ~proof_handler ast =
64   if proof_handler.MatitaTypes.has_proof () then begin
65     let proof = proof_handler.MatitaTypes.get_proof () in
66     let metasenv = proof#metasenv in
67     let goal = proof#goal in
68     let context = canonical_context goal metasenv in
69     let (_, metasenv, term) as retval =
70       disambiguator#disambiguateTermAst ~context ~metasenv ast
71     in
72     proof#set_metasenv metasenv;
73     retval
74   end else
75     disambiguator#disambiguateTermAst ast
76
77 class virtual interpreterState ~(console: MatitaConsole.console) =
78   object (self)
79       (** eval a toplevel phrase in the current state and return the new state
80       *)
81     method parsePhrase s = CicTextualParser2.parse_tactical (Stream.of_string s)
82
83     method virtual evalTactical:
84       (CicAst.term, string) TacticAst.tactical -> state_tag
85
86     method evalPhrase s = self#evalTactical (self#parsePhrase s)
87
88   end
89
90 let check_widget: MatitaTypes.sequent_viewer lazy_t = lazy
91   (let gui = MatitaGui.instance () in
92   MatitaMathView.sequent_viewer ~show:true ~packing:gui#check#scrolledCheck#add
93     ())
94
95   (** Implements phrases that should be accepted in all states *)
96 class sharedState
97   ~(disambiguator: MatitaTypes.disambiguator)
98   ~(proof_handler: MatitaTypes.proof_handler)
99   ~(console: MatitaConsole.console)
100   ()
101 =
102   object (self)
103     inherit interpreterState ~console
104     method evalTactical = function
105       | TacticAst.Command TacticAst.Quit ->
106           proof_handler.MatitaTypes.quit ();
107           Some `Command (* dummy answer, useless *)
108       | TacticAst.Command TacticAst.Proof ->
109             (* do nothing, just for compatibility with coq syntax *)
110           Some `Command
111       | TacticAst.Command (TacticAst.Check term) ->
112           let (_, _, term) = disambiguate ~disambiguator ~proof_handler term in
113           let (context, metasenv) = get_context_and_metasenv proof_handler in
114           let dummyno = CicMkImplicit.new_meta metasenv [] in
115           let ty = CicTypeChecker.type_of_aux' metasenv context term in
116           let expr = Cic.Cast (term, ty) in
117           let sequent = (dummyno, context, expr) in
118           let widget = Lazy.force check_widget in
119           let gui = MatitaGui.instance () in
120           gui#check#checkWin#show ();
121           gui#main#showCheckMenuItem#set_active true;
122           widget#load_sequent (sequent::metasenv) dummyno;
123           None
124       | tactical ->
125           raise (Command_error (TacticAstPp.pp_tactical tactical))
126   end
127
128   (** Implements phrases that should be accepted only in `Command state *)
129 class commandState
130   ~(disambiguator: MatitaTypes.disambiguator)
131   ~(proof_handler: MatitaTypes.proof_handler)
132   ~(console: MatitaConsole.console)
133   ()
134 =
135   let shared = new sharedState ~disambiguator ~proof_handler ~console () in
136   object (self)
137     inherit interpreterState ~console
138
139     method evalTactical = function
140       | TacticAst.LocatedTactical (_, tactical) -> self#evalTactical tactical
141       | TacticAst.Command (TacticAst.Theorem (_, name_opt, ast, None)) ->
142           let (_, metasenv, expr) = disambiguator#disambiguateTermAst ast in
143           let uri =
144             match name_opt with
145             | None -> None
146             | Some name -> Some (uri name)
147           in
148           let proof = MatitaProof.proof ~typ:expr ?uri ~metasenv () in
149           proof_handler.MatitaTypes.new_proof proof;
150           Some `Proof
151       | TacticAst.Command TacticAst.Quit ->
152           proof_handler.MatitaTypes.quit ();
153           Some `Command (* dummy answer, useless *)
154       | TacticAst.Command TacticAst.Proof ->
155             (* do nothing, just for compatibility with coq syntax *)
156           Some `Command
157       | tactical -> shared#evalTactical tactical
158   end
159
160   (** create a ProofEngineTypes.mk_fresh_name_type function which uses given
161   * names as long as they are available, then it fallbacks to name generation
162   * using FreshNamesGenerator module *)
163 let namer_of names =
164   let len = List.length names in
165   let count = ref 0 in
166   fun metasenv context name ~typ ->
167     if !count < len then begin
168       let name = Cic.Name (List.nth names !count) in
169       incr count;
170       name
171     end else
172       FreshNamesGenerator.mk_fresh_name metasenv context name ~typ
173
174   (** Implements phrases that should be accepted only in `Proof state, basically
175   * tacticals *)
176 class proofState
177   ~(disambiguator: MatitaTypes.disambiguator)
178   ~(proof_handler: MatitaTypes.proof_handler)
179   ~(console: MatitaConsole.console)
180   ()
181 =
182   let disambiguate ast =
183     let (_, _, term) = disambiguate ~disambiguator ~proof_handler ast in
184     term
185   in
186     (** tactic AST -> ProofEngineTypes.tactic *)
187   let rec lookup_tactic = function
188     | TacticAst.LocatedTactic (_, tactic) -> lookup_tactic tactic
189     | TacticAst.Intros (_, names) ->  (* TODO Zack implement intros length *)
190         PrimitiveTactics.intros_tac ~mk_fresh_name_callback:(namer_of names) ()
191     | TacticAst.Reflexivity -> EqualityTactics.reflexivity_tac
192     | TacticAst.Assumption -> VariousTactics.assumption_tac
193     | TacticAst.Contradiction -> NegationTactics.contradiction_tac
194     | TacticAst.Exists -> IntroductionTactics.exists_tac
195     | TacticAst.Fourier -> FourierR.fourier_tac
196     | TacticAst.Left -> IntroductionTactics.left_tac
197     | TacticAst.Right -> IntroductionTactics.right_tac
198     | TacticAst.Ring -> Ring.ring_tac
199     | TacticAst.Split -> IntroductionTactics.split_tac
200     | TacticAst.Symmetry -> EqualityTactics.symmetry_tac
201     | TacticAst.Transitivity term ->
202         EqualityTactics.transitivity_tac (disambiguate term)
203     | TacticAst.Apply term -> PrimitiveTactics.apply_tac (disambiguate term)
204     | TacticAst.Absurd term -> NegationTactics.absurd_tac (disambiguate term)
205     | TacticAst.Exact term -> PrimitiveTactics.exact_tac (disambiguate term)
206     | TacticAst.Cut term -> PrimitiveTactics.cut_tac (disambiguate term)
207     | TacticAst.Elim (term, _) -> (* TODO Zack implement "using" argument *)
208         PrimitiveTactics.elim_intros_simpl_tac (disambiguate term)
209     | TacticAst.ElimType term ->
210         EliminationTactics.elim_type_tac (disambiguate term)
211     | TacticAst.Replace (what, with_what) ->
212         EqualityTactics.replace_tac ~what:(disambiguate what)
213           ~with_what:(disambiguate with_what)
214   (*
215     (* TODO Zack a lot more of tactics to be implemented here ... *)
216     | TacticAst.Change of 'term * 'term * 'ident option
217     | TacticAst.Change_pattern of 'term pattern * 'term * 'ident option
218     | TacticAst.Decompose of 'ident * 'ident list
219     | TacticAst.Discriminate of 'ident
220     | TacticAst.Fold of reduction_kind * 'term
221     | TacticAst.Injection of 'ident
222     | TacticAst.LetIn of 'term * 'ident
223     | TacticAst.Reduce of reduction_kind * 'term pattern * 'ident option
224     | TacticAst.Replace_pattern of 'term pattern * 'term
225     | TacticAst.Rewrite of direction * 'term * 'ident option
226   *)
227     | _ ->
228         MatitaTypes.not_implemented "some tactic"
229   in
230   let shared = new sharedState ~disambiguator ~proof_handler ~console () in
231   object (self)
232     inherit interpreterState ~console
233
234     method evalTactical = function
235       | TacticAst.LocatedTactical (_, tactical) -> self#evalTactical tactical
236       | TacticAst.Command TacticAst.Abort ->
237           proof_handler.MatitaTypes.abort_proof ();
238           Some `Command
239       | TacticAst.Command (TacticAst.Undo steps) ->
240           (proof_handler.MatitaTypes.get_proof ())#undo ?steps ();
241           Some `Proof
242       | TacticAst.Command (TacticAst.Redo steps) ->
243           (proof_handler.MatitaTypes.get_proof ())#redo ?steps ();
244           Some `Proof
245       | TacticAst.Command (TacticAst.Qed name_opt) ->
246           (* TODO Zack this function probably should not simply fail with
247           * Failure, but rather raise some more meaningful exception *)
248           if not (proof_handler.MatitaTypes.has_proof ()) then assert false;
249           let proof = proof_handler.MatitaTypes.get_proof () in
250           (match name_opt with
251           | None -> ()
252           | Some name -> proof#set_uri (uri name));
253           let (uri, metasenv, bo, ty) = proof#proof in
254           let uri = MatitaTypes.unopt_uri uri in
255           if metasenv <> [] then failwith "Proof not completed";
256           let proved_ty = CicTypeChecker.type_of_aux' [] [] bo in
257           if not (CicReduction.are_convertible [] proved_ty ty) then
258             failwith "Wrong proof";
259           (* TODO Zack [] probably wrong *)
260           CicEnvironment.add_type_checked_term uri
261             (Cic.Constant ((UriManager.name_of_uri uri),(Some bo),ty,[]));
262           proof_handler.MatitaTypes.set_proof None;
263           (* TODO Zack a lot more to be done here:
264             * - save object to disk in xml format
265             * - collect metadata
266             * - register uri to the getter *)
267           Some `Command
268       | TacticAst.Seq tacticals ->
269           (* TODO Zack check for proof completed at each step? *)
270           List.iter (fun t -> ignore (self#evalTactical t)) tacticals;
271           Some `Proof
272       | TacticAst.Tactic tactic_phrase ->
273           let tactic = lookup_tactic tactic_phrase in
274           (proof_handler.MatitaTypes.get_proof ())#apply_tactic tactic;
275           Some `Proof
276       | tactical -> shared#evalTactical tactical
277   end
278
279 class interpreter
280   ~(disambiguator: MatitaTypes.disambiguator)
281   ~(proof_handler: MatitaTypes.proof_handler)
282   ~(console: MatitaConsole.console)
283   ()
284 =
285   let commandState =
286     new commandState ~disambiguator ~proof_handler ~console ()
287   in
288   let proofState = new proofState ~disambiguator ~proof_handler ~console () in
289   object (self)
290     val mutable state = commandState
291
292     method reset = state <- commandState
293
294     method private updateState = function
295       | Some `Command -> state <- commandState
296       | Some `Proof -> state <- proofState
297       | None -> ()
298
299     method evalPhrase s =
300       try
301         self#updateState (state#evalPhrase s)
302       with exn ->
303         console#echo_error (sprintf "Uncaught exception: %s"
304           (Printexc.to_string exn))
305
306 (*
307     method evalAll s =
308       let get_end_pos = function
309         | TacticAst.LocatedTactical ((_, end_pos), _) -> end_pos.Lexing.pos_cnum
310         | _ -> assert false
311       in
312       let str_len = String.length s in
313       let rec aux offset =
314         let tactical =
315           self#parsePhrase (String.sub s offset (str_len - offset))
316         in
317         self#updateState (state#evalTactical tactical);
318         let next_offset = get_end_pos tactical + offset in
319         if next_offset = str_len - 1 then
320       in
321 *)
322
323   end
324