]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitaInterpreter.ml
- added code for Coercion command, Print Env command.
[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 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 open MatitaTypes
38
39 type state = Command | Proof
40 type outcome = New_state of state | Quiet | Echo of string
41
42 exception Command_error of string
43
44 (*
45 let uri name =
46   UriManager.uri_of_string (sprintf "%s/%s" BuildTimeConf.base_uri name)
47 *)
48
49 let baseuri = lazy (ref ("cic:/matita/" ^ Helm_registry.get "matita.owner"))
50 let qualify name =
51   let baseuri = !(Lazy.force baseuri) in
52   if baseuri.[String.length baseuri - 1] = '/' then
53     baseuri ^ name
54   else
55     String.concat "/" [baseuri; name]
56 let split_obj = function
57   | Cic.Constant (name, body, ty, _, attrs)
58   | Cic.Variable (name, body, ty, _, attrs) -> (name, body, ty, attrs)
59   | _ -> assert false
60
61 let canonical_context metano metasenv =
62   try
63     let (_, context, _) = List.find (fun (m, _, _) -> m = metano) metasenv in
64     context
65   with Not_found ->
66     failwith (sprintf "Can't find canonical context for %d" metano)
67
68 let get_context_and_metasenv (currentProof:MatitaTypes.currentProof) =
69   if currentProof#onGoing () then
70     let proof = currentProof#proof in
71     let metasenv = proof#metasenv in
72     let goal = proof#goal in
73     (canonical_context goal metasenv, metasenv)
74   else
75     ([], [])
76
77   (** term AST -> Cic.term. Uses disambiguator and change imperatively the
78   * metasenv as needed *)
79 let disambiguate ~(disambiguator:MatitaTypes.disambiguator) ~currentProof ast =
80   if currentProof#onGoing () then begin
81     let proof = currentProof#proof in
82     let metasenv = proof#metasenv in
83     let goal = proof#goal in
84     let context = canonical_context goal metasenv in
85     let (_, metasenv, term,ugraph) as retval =
86       disambiguator#disambiguateTermAst ~context ~metasenv ast
87     in
88     proof#set_metasenv metasenv;
89     retval
90   end else
91     disambiguator#disambiguateTermAst ast
92
93 class virtual interpreterState = 
94     (* static values, shared by all states inheriting this class *)
95   let loc = ref None in
96   let history = ref [] in
97   fun ~(console: MatitaTypes.console) ->
98   object (self)
99
100       (** eval a toplevel phrase in the current state and return the new state
101       *)
102     method parsePhrase s =
103       match CicTextualParser2.parse_tactical s with
104       | (TacticAst.LocatedTactical (loc', tac)) as tactical ->
105           loc := Some loc';
106           (match tac with (* update interpreter history *)
107           | TacticAst.Command (TacticAst.Qed None) ->
108               history := `Qed :: !history
109           | TacticAst.Command (TacticAst.Theorem (_, Some name, _, None)) ->
110               history := `Theorem name :: !history
111           | TacticAst.Command (TacticAst.Qed _)
112           | TacticAst.Command (TacticAst.Theorem _) -> assert false
113           | _ -> history := `Tactic :: !history);
114           tactical
115       | _ -> assert false
116
117     method virtual evalTactical:
118       (CicAst.term, string) TacticAst.tactical -> outcome
119
120     method evalPhrase s =
121       debug_print (sprintf "evaluating '%s'" s);
122       self#evalTactical (self#parsePhrase (Stream.of_string s))
123
124     method evalAst ast = self#evalTactical ast
125
126     method endOffset =
127       match !loc with
128       | Some (start_pos, end_pos) -> end_pos.Lexing.pos_cnum
129       | None -> failwith "MatitaInterpreter: no offset recorded"
130
131   end
132
133   (** Implements phrases that should be accepted in all states *)
134 class sharedState
135   ~(disambiguator: MatitaTypes.disambiguator)
136   ~(currentProof: MatitaTypes.currentProof)
137   ~(console: MatitaTypes.console)
138   ?(mathViewer: MatitaTypes.mathViewer option)
139   ~(dbd: Mysql.dbd)
140   ()
141 =
142   object (self)
143     inherit interpreterState ~console
144     method evalTactical = function
145       | TacticAst.Command TacticAst.Quit ->
146           currentProof#quit ();
147           assert false  (* dummy answer, useless *)
148       | TacticAst.Command TacticAst.Proof ->
149             (* do nothing, just for compatibility with coq syntax *)
150           New_state Command
151       | TacticAst.Command (TacticAst.Baseuri (Some uri)) ->
152           Lazy.force baseuri := uri;
153           console#echo_message (sprintf "base uri set to \"%s\"" uri);
154           Quiet
155       | TacticAst.Command (TacticAst.Baseuri None) ->
156           console#echo_message (sprintf "base uri is \"%s\""
157             !(Lazy.force baseuri));
158           Quiet
159       | TacticAst.Command (TacticAst.Check term) ->
160           let (_, _, term,ugraph) = 
161             disambiguate ~disambiguator ~currentProof term 
162           in
163           let (context, metasenv) = get_context_and_metasenv currentProof in
164           let dummyno = CicMkImplicit.new_meta metasenv [] in
165           let ty,ugraph1 = 
166             CicTypeChecker.type_of_aux' metasenv context term ugraph 
167           in
168             (* TASSI: here ugraph1 is unused.... FIXME *)
169           let expr = Cic.Cast (term, ty) in
170           let sequent = (dummyno, context, expr) in
171           (match mathViewer with
172           | None -> ()
173           | Some v -> v#checkTerm sequent metasenv);
174           Quiet
175       | TacticAst.Command (TacticAst.Search_pat (search_kind, pat)) ->
176           let uris =
177             match search_kind with
178             | `Locate -> MetadataQuery.locate ~dbd pat
179             | `Elim -> MetadataQuery.elim ~dbd pat
180             | _ -> assert false
181           in
182           (* TODO ZACK: show URIs to the user *)
183           Quiet
184       | TacticAst.Command (TacticAst.Print `Env) ->
185           let uris = CicEnvironment.list_uri () in
186           List.iter (fun u ->
187             console#echo_message (UriManager.string_of_uri u);
188             prerr_endline "x"
189           ) uris;
190           Quiet
191       | tactical ->
192           raise (Command_error (TacticAstPp.pp_tactical tactical))
193   end
194
195 open Printf
196
197 let pp_indtypes indTypes =
198   List.iter
199     (fun (name, _, typ, constructors) ->
200       printf "%s: %s\n" name (CicPp.ppterm typ);
201       List.iter
202         (fun (name, term) -> printf "\t%s: %s\n" name (CicPp.ppterm term))
203         constructors)
204     indTypes;
205   flush stdout
206
207 let inddef_of_ast params indTypes (disambiguator:MatitaTypes.disambiguator) =
208   let add_pi binders t =
209     List.fold_right
210       (fun (name, ast) acc ->
211         CicAst.Binder (`Forall, (Cic.Name name, Some ast), acc))
212       binders t
213   in
214   let ind_binders =
215     List.map (fun (name, _, typ, _) -> (name, add_pi params typ)) indTypes
216   in
217   let binders = ind_binders @ params in
218   let asts = ref [] in
219   let add_ast ast = asts := ast :: !asts in
220   let paramsno = List.length params in
221   let indbindersno = List.length ind_binders in
222   List.iter
223     (fun (name, _, typ, constructors) ->
224       add_ast (add_pi params typ);
225       List.iter (fun (_, ast) -> add_ast (add_pi binders ast)) constructors)
226     indTypes;
227   let (_, metasenv, terms, ugraph) =
228     disambiguator#disambiguateTermAsts ~metasenv:[] !asts
229   in
230   let terms = ref (List.rev terms) in
231   let get_term () =
232     match !terms with [] -> assert false | hd :: tl -> terms := tl; hd
233   in
234   let uri =
235     match indTypes with
236     | (name, _, _, _) :: _ -> qualify name ^ ".ind"
237     | _ -> assert false
238   in
239   let mutinds =
240     let counter = ref 0 in
241     List.map
242       (fun _ ->
243         incr counter;
244         CicUtil.term_of_uri (sprintf "%s#xpointer(1/%d)" uri !counter))
245       indTypes
246   in
247   let subst_mutinds = List.fold_right CicSubstitution.subst mutinds in
248   let cicIndTypes =
249     List.fold_left
250       (fun acc (name, inductive, typ, constructors) ->
251         let cicTyp = get_term () in
252         let cicConstructors =
253           List.fold_left
254             (fun acc (name, _) ->
255               let typ =
256                 subst_mutinds (CicUtil.strip_prods indbindersno (get_term ()))
257               in
258               (name, typ) :: acc)
259             [] constructors
260         in
261         (name, inductive, cicTyp, List.rev cicConstructors) :: acc)
262       [] indTypes
263   in
264   let cicIndTypes = List.rev cicIndTypes in
265   (UriManager.uri_of_string uri, (cicIndTypes, [], paramsno))
266
267   (* TODO Zack a lot more to be done here:
268     * - save object to disk in xml format
269     * - register uri to the getter 
270     * - save universe file *)
271 let add_constant_to_world ~(console:MatitaTypes.console)
272   ~dbd ~uri ?body ~ty ?(params = []) ?(attrs = []) ~ugraph ()
273 =
274   let suri = UriManager.string_of_uri uri in
275   if CicEnvironment.in_library uri then
276     error (sprintf "%s constant already defined" suri)
277   else begin
278     let name = UriManager.name_of_uri uri in
279     let obj = Cic.Constant (name, body, ty, params, attrs) in
280     let ugraph = CicUnivUtils.clean_and_fill uri obj ugraph in
281     CicEnvironment.add_type_checked_term uri (obj, ugraph);
282     MetadataDb.index_constant ~dbd
283       ~owner:(Helm_registry.get "matita.owner") ~uri ~body ~ty;
284     console#echo_message (sprintf "%s constant defined" suri)
285   end
286
287 let add_inductive_def_to_world ~(console:MatitaTypes.console)
288   ~dbd ~uri ~indTypes ?(params = []) ?(leftno = 0) ?(attrs = []) ~ugraph ()
289 =
290   let suri = UriManager.string_of_uri uri in
291   if CicEnvironment.in_library uri then
292     error (sprintf "%s inductive type already defined" suri)
293   else begin
294     let name = UriManager.name_of_uri uri in
295     let obj = Cic.InductiveDefinition (indTypes, params, leftno, attrs) in
296     let ugraph = CicUnivUtils.clean_and_fill uri obj ugraph in
297     CicEnvironment.put_inductive_definition uri (obj, ugraph);
298     MetadataDb.index_inductive_def ~dbd
299       ~owner:(Helm_registry.get "matita.owner") ~uri ~types:indTypes;
300     console#echo_message (sprintf "%s inductive type defined" suri);
301     let elim sort =
302       try
303         let obj = CicElim.elim_of ~sort uri 0 in
304         let (name, body, ty, attrs) = split_obj obj in
305         let suri = qualify name ^ ".con" in
306         let uri = UriManager.uri_of_string suri in
307           (* TODO Zack: make CicElim returns a universe *)
308         let ugraph = CicUniv.empty_ugraph in
309         add_constant_to_world ~console ~dbd ~uri ?body ~ty ~attrs ~ugraph ();
310 (*
311         console#echo_message
312           (sprintf "%s eliminator (automatically) defined" suri)
313 *)
314       with CicElim.Can_t_eliminate -> ()
315     in
316     List.iter elim [ Cic.Prop; Cic.Set; (Cic.Type (CicUniv.fresh ())) ];
317   end
318
319   (** Implements phrases that should be accepted only in Command state *)
320 class commandState
321   ~(disambiguator: MatitaTypes.disambiguator)
322   ~(currentProof: MatitaTypes.currentProof)
323   ~(console: MatitaTypes.console)
324   ?mathViewer
325   ~(dbd: Mysql.dbd)
326   ()
327 =
328   let shared =
329     new sharedState ~disambiguator ~currentProof ~console ?mathViewer ~dbd ()
330   in
331   object (self)
332     inherit interpreterState ~console
333
334     method evalTactical = function
335       | TacticAst.LocatedTactical (_, tactical) -> self#evalTactical tactical
336       | TacticAst.Command (TacticAst.Theorem (_, Some name, ast, None)) ->
337           let (_, metasenv, expr,ugraph) = 
338             disambiguator#disambiguateTermAst ast 
339           in
340           let uri = UriManager.uri_of_string (qualify name ^ ".con") in
341           let proof = MatitaProof.proof ~typ:expr ~uri ~metasenv () in
342           currentProof#start proof;
343           New_state Proof
344       | TacticAst.Command
345         (TacticAst.Theorem (_, Some name, type_ast, Some body_ast)) ->
346           let (_, metasenv, type_cic, ugraph) = 
347             disambiguator#disambiguateTermAst type_ast
348           in
349           let (_, metasenv, body_cic, ugraph) = 
350             disambiguator#disambiguateTermAst ~metasenv body_ast
351           in
352           let (body_type, ugraph) =
353             CicTypeChecker.type_of_aux' metasenv [] body_cic ugraph
354           in
355           let uri = UriManager.uri_of_string (qualify name ^ ".con") in
356           let (subst, metasenv, ugraph) =
357             CicUnification.fo_unif metasenv [] body_type type_cic ugraph
358           in
359           let body = CicMetaSubst.apply_subst subst body_cic in
360           let ty = CicMetaSubst.apply_subst subst type_cic in
361           add_constant_to_world ~console ~dbd ~uri ~body ~ty ~ugraph ();
362           Quiet
363       | TacticAst.Command (TacticAst.Inductive (params, indTypes)) ->
364           
365           let (uri, (indTypes, params, leftno)) =
366             inddef_of_ast params indTypes disambiguator
367           in
368           let obj = Cic.InductiveDefinition (indTypes, params, leftno, []) in
369           let ugraph =
370             CicTypeChecker.typecheck_mutual_inductive_defs uri
371               (indTypes, params, leftno) CicUniv.empty_ugraph
372           in
373           add_inductive_def_to_world ~console
374             ~dbd ~uri ~indTypes ~params ~leftno ~ugraph ();
375           Quiet
376       | TacticAst.Command TacticAst.Quit ->
377           currentProof#quit ();
378           New_state Command (* dummy answer, useless *)
379       | TacticAst.Command TacticAst.Proof ->
380             (* do nothing, just for compatibility with coq syntax *)
381           New_state Command
382       | TacticAst.Command (TacticAst.Coercion c_ast) ->
383           prerr_endline ("beccata la coercion " ^ (CicAstPp.pp_term c_ast));
384
385           let env, metasenv, coercion, ugraph = 
386             disambiguator#disambiguateTermAst c_ast 
387           in
388           let coer_uri,coer_ty =
389             match coercion with 
390             | Cic.Const (uri,_)
391             | Cic.Var (uri,_) ->
392                 let o,_ = 
393                   CicEnvironment.get_obj CicUniv.empty_ugraph uri 
394                 in
395                 (match o with
396                 | Cic.Constant (_,_,ty,_,_)
397                 | Cic.Variable (_,_,ty,_,_) ->
398                     uri,ty
399                 | _ -> assert false)
400             | Cic.MutConstruct (uri,t,c,_) ->
401                 let o,_ = 
402                   CicEnvironment.get_obj CicUniv.empty_ugraph uri 
403                 in
404                 (match o with
405                 | Cic.InductiveDefinition (l,_,_,_) ->
406                     let (_,_,_,cl) = List.nth l t in
407                     let (_,cty) = List.nth cl c in
408                       uri,cty
409                 | _ -> assert false)
410             | _ -> assert false 
411           in
412           (* we have to get the source and the tgt type uri 
413            * in Coq syntax we have already their names, but 
414            * since we don't support Funclass and similar I think
415            * all the coercion should be of the form
416            * (A:?)(B:?)T1->T2
417            * So we should be able to extract them from the coercion type
418            *)
419           let extract_last_two_p ty =
420             let rec aux = function
421               | Cic.Prod( _, src, Cic.Prod (n,t1,t2)) -> aux (Cic.Prod(n,t1,t2))   
422               | Cic.Prod( _, src, tgt) -> src, tgt
423               | _ -> assert false
424             in  
425             aux ty
426           in
427           let uri_of_term = function
428             | Cic.Const(u,_) -> u
429             | Cic.MutInd (u, i , _) ->
430                 (* we have to build by hand the #xpointer *)
431                 let base = UriManager.string_of_uri u in
432                 let xp = "#xpointer(1/" ^ (string_of_int (i+1)) ^ ")" in
433                   UriManager.uri_of_string (base ^ xp)
434             | _ -> assert false 
435           in
436           let ty_src,ty_tgt = extract_last_two_p coer_ty in
437           let src_uri = uri_of_term ty_src in
438           let tgt_uri = uri_of_term ty_tgt in
439           let coercions_to_add = 
440             CoercGraph.close_coercion_graph src_uri tgt_uri coer_uri
441           in
442           (* FIXME: we should chek it this object can be a coercion 
443            * maybe add the check to extract_last_two_p
444            *)
445           List.iter (fun (uri,obj,ugraph) -> 
446             (*
447             prerr_endline (Printf.sprintf 
448              "Aggiungo la coercion %s\n%s\n\n"
449              (UriManager.string_of_uri uri) (CicPp.ppobj obj));
450             *)
451             let (name, body, ty, attrs) = split_obj obj in
452             add_constant_to_world ~console 
453               ~dbd ~uri ?body ~ty ~attrs ~ugraph ();
454           ) coercions_to_add;
455           Quiet
456       | tactical -> shared#evalTactical tactical
457   end
458
459   (** create a ProofEngineTypes.mk_fresh_name_type function which uses given
460   * names as long as they are available, then it fallbacks to name generation
461   * using FreshNamesGenerator module *)
462 let namer_of names =
463   let len = List.length names in
464   let count = ref 0 in
465   fun metasenv context name ~typ ->
466     if !count < len then begin
467       let name = Cic.Name (List.nth names !count) in
468       incr count;
469       name
470     end else
471       FreshNamesGenerator.mk_fresh_name ~subst:[] metasenv context name ~typ
472
473   (** Implements phrases that should be accepted only in Proof state, basically
474   * tacticals *)
475 class proofState
476   ~(disambiguator: MatitaTypes.disambiguator)
477   ~(currentProof: MatitaTypes.currentProof)
478   ~(console: MatitaTypes.console)
479   ?mathViewer
480   ~(dbd: Mysql.dbd)
481   ()
482 =
483   let disambiguate ast =
484     let (_, _, term, _) = disambiguate ~disambiguator ~currentProof ast in
485     term
486   in
487     (** tactic AST -> ProofEngineTypes.tactic *)
488   let rec lookup_tactic = function
489     | TacticAst.LocatedTactic (_, tactic) -> lookup_tactic tactic
490     | TacticAst.Intros (_, names) ->  (* TODO Zack implement intros length *)
491         PrimitiveTactics.intros_tac ~mk_fresh_name_callback:(namer_of names) ()
492     | TacticAst.Reflexivity -> Tactics.reflexivity
493     | TacticAst.Assumption -> Tactics.assumption
494     | TacticAst.Contradiction -> Tactics.contradiction
495     | TacticAst.Exists -> Tactics.exists
496     | TacticAst.Fourier -> Tactics.fourier
497     | TacticAst.Left -> Tactics.left
498     | TacticAst.Right -> Tactics.right
499     | TacticAst.Ring -> Tactics.ring
500     | TacticAst.Split -> Tactics.split
501     | TacticAst.Symmetry -> Tactics.symmetry
502     | TacticAst.Transitivity term -> Tactics.transitivity (disambiguate term)
503     | TacticAst.Apply term -> Tactics.apply (disambiguate term)
504     | TacticAst.Absurd term -> Tactics.absurd (disambiguate term)
505     | TacticAst.Exact term -> Tactics.exact (disambiguate term)
506     | TacticAst.Cut term -> Tactics.cut (disambiguate term)
507     | TacticAst.Elim (term, _) -> (* TODO Zack implement "using" argument *)
508         Tactics.elim_intros_simpl (disambiguate term)
509     | TacticAst.ElimType term -> Tactics.elim_type (disambiguate term)
510     | TacticAst.Replace (what, with_what) ->
511         Tactics.replace ~what:(disambiguate what)
512           ~with_what:(disambiguate with_what)
513     | TacticAst.Auto -> Tactics.auto_new ~dbd
514   (*
515     (* TODO Zack a lot more of tactics to be implemented here ... *)
516     | TacticAst.Change of 'term * 'term * 'ident option
517     | TacticAst.Change_pattern of 'term pattern * 'term * 'ident option
518     | TacticAst.Decompose of 'ident * 'ident list
519     | TacticAst.Discriminate of 'ident
520     | TacticAst.Fold of reduction_kind * 'term
521     | TacticAst.Injection of 'ident
522     | TacticAst.LetIn of 'term * 'ident
523     | TacticAst.Reduce of reduction_kind * 'term pattern * 'ident option
524     | TacticAst.Replace_pattern of 'term pattern * 'term
525     | TacticAst.Rewrite of direction * 'term * 'ident option
526   *)
527     | _ ->
528         MatitaTypes.not_implemented "some tactic"
529   in
530   let shared =
531     new sharedState ~disambiguator ~currentProof ~console ?mathViewer ~dbd ()
532   in
533   object (self)
534     inherit interpreterState ~console
535
536     method evalTactical = function
537       | TacticAst.LocatedTactical (_, tactical) -> self#evalTactical tactical
538       | TacticAst.Command TacticAst.Abort ->
539           currentProof#abort ();
540           New_state Command
541       | TacticAst.Command (TacticAst.Undo steps) ->
542           currentProof#proof#undo ?steps ();
543           New_state Proof
544       | TacticAst.Command (TacticAst.Redo steps) ->
545           currentProof#proof#redo ?steps ();
546           New_state Proof
547       | TacticAst.Command (TacticAst.Qed None) ->
548           if not (currentProof#onGoing ()) then assert false;
549           let proof = currentProof#proof in
550           let (uri, metasenv, bo, ty) = proof#proof in
551           let uri = MatitaTypes.unopt_uri uri in
552           let suri = UriManager.string_of_uri uri in
553             (* TODO Zack this function probably should not simply fail with
554             * Failure, but rather raise some more meaningful exception *)
555           if metasenv <> [] then failwith "Proof not completed";
556           let proved_ty,ugraph = 
557             CicTypeChecker.type_of_aux' [] [] bo CicUniv.empty_ugraph
558           in
559           let b,ugraph = 
560             CicReduction.are_convertible [] proved_ty ty ugraph 
561           in
562           if not b then failwith "Wrong proof";
563           add_constant_to_world ~console ~dbd ~uri ~body:bo ~ty ~ugraph ();
564           currentProof#abort ();
565           (match mathViewer with None -> () | Some v -> v#unload ());
566           console#echo_message (sprintf "%s defined" suri);
567           New_state Command
568       | TacticAst.Seq tacticals ->
569           (* TODO Zack check for proof completed at each step? *)
570           List.iter (fun t -> ignore (self#evalTactical t)) tacticals;
571           New_state Proof
572       | TacticAst.Tactic tactic_phrase ->
573           let tactic = lookup_tactic tactic_phrase in
574           currentProof#proof#apply_tactic tactic;
575           New_state Proof
576       | tactical -> shared#evalTactical tactical
577   end
578
579 class interpreter
580   ~(disambiguator: MatitaTypes.disambiguator)
581   ~(currentProof: MatitaTypes.currentProof)
582   ~(console: MatitaTypes.console)
583   ?mathViewer
584   ~(dbd: Mysql.dbd)
585   ()
586 =
587   let commandState =
588     new commandState ~disambiguator ~currentProof ~console ?mathViewer ~dbd ()
589   in
590   let proofState =
591     new proofState ~disambiguator ~currentProof ~console ?mathViewer ~dbd ()
592   in
593   object (self)
594     val mutable state = commandState
595
596     method reset = state <- commandState
597
598     method endOffset = state#endOffset
599
600     method private updateState = function
601       | New_state Command -> (state <- commandState)
602       | New_state Proof -> (state <- proofState)
603       | _ -> ()
604
605     method private eval f =
606       let ok () = (* console#clear (); *) (true, true) in
607       match console#wrap_exn f with
608       | Some (New_state Command) -> (state <- commandState); ok ()
609       | Some (New_state Proof) -> (state <- proofState); ok ()
610       | Some (Echo msg) -> console#echo_message msg; (true, false)
611       | Some Quiet -> ok ()
612       | None -> (false, false)
613
614     method evalPhrase s = self#eval (fun () -> state#evalPhrase s)
615     method evalAst ast = self#eval (fun () -> state#evalAst ast)
616   end
617