]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitaEngine.ml
first matitadep snapshot
[helm.git] / helm / matita / matitaEngine.ml
1
2 open Printf
3 open MatitaTypes
4
5 exception Drop;;
6
7 let debug = false ;;
8 let debug_print = if debug then prerr_endline else ignore ;;
9
10 (** create a ProofEngineTypes.mk_fresh_name_type function which uses given
11   * names as long as they are available, then it fallbacks to name generation
12   * using FreshNamesGenerator module *)
13 let namer_of names =
14   let len = List.length names in
15   let count = ref 0 in
16   fun metasenv context name ~typ ->
17     if !count < len then begin
18       let name = Cic.Name (List.nth names !count) in
19       incr count;
20       name
21     end else
22       FreshNamesGenerator.mk_fresh_name ~subst:[] metasenv context name ~typ
23
24 let tactic_of_ast = function
25   | TacticAst.Absurd (_, term) -> Tactics.absurd term
26   | TacticAst.Apply (_, term) -> Tactics.apply term
27   | TacticAst.Assumption _ -> Tactics.assumption
28   | TacticAst.Auto (_,depth,width) -> 
29       AutoTactic.auto_tac ?depth ?width ~dbd:(MatitaDb.instance ()) ()
30   | TacticAst.Change (_, pattern, with_what) ->
31      Tactics.change ~pattern with_what
32   | TacticAst.Clear (_,id) -> Tactics.clear id
33   | TacticAst.ClearBody (_,id) -> Tactics.clearbody id
34   | TacticAst.Contradiction _ -> Tactics.contradiction
35   | TacticAst.Compare (_, term) -> Tactics.compare term
36   | TacticAst.Constructor (_, n) -> Tactics.constructor n
37   | TacticAst.Cut (_, ident, term) ->
38      let names = match ident with None -> [] | Some id -> [id] in
39      Tactics.cut ~mk_fresh_name_callback:(namer_of names) term
40   | TacticAst.DecideEquality _ -> Tactics.decide_equality
41   | TacticAst.Decompose (_,term) -> Tactics.decompose term
42   | TacticAst.Discriminate (_,term) -> Tactics.discriminate term
43   | TacticAst.Elim (_, term, _) ->
44       Tactics.elim_intros term
45   | TacticAst.ElimType (_, term) -> Tactics.elim_type term
46   | TacticAst.Exact (_, term) -> Tactics.exact term
47   | TacticAst.Exists _ -> Tactics.exists
48   | TacticAst.Fail _ -> Tactics.fail
49   | TacticAst.Fold (_, reduction_kind, pattern) ->
50      let reduction =
51       match reduction_kind with
52        | `Normalize -> CicReduction.normalize ~delta:false ~subst:[]
53        | `Reduce -> ProofEngineReduction.reduce
54        | `Simpl -> ProofEngineReduction.simpl
55        | `Whd -> CicReduction.whd ~delta:false ~subst:[]
56      in
57       Tactics.fold ~reduction ~pattern
58   | TacticAst.Fourier _ -> Tactics.fourier
59   | TacticAst.FwdSimpl (_, term) -> 
60      Tactics.fwd_simpl ~what:term ~dbd:(MatitaDb.instance ())
61   | TacticAst.Generalize (_,pattern,ident) ->
62      let names = match ident with None -> [] | Some id -> [id] in
63      Tactics.generalize ~mk_fresh_name_callback:(namer_of names) pattern 
64   | TacticAst.Goal (_, n) -> Tactics.set_goal n
65   | TacticAst.IdTac _ -> Tactics.id
66   | TacticAst.Injection (_,term) -> Tactics.injection term
67   | TacticAst.Intros (_, None, names) ->
68       PrimitiveTactics.intros_tac ~mk_fresh_name_callback:(namer_of names) ()
69   | TacticAst.Intros (_, Some num, names) ->
70       PrimitiveTactics.intros_tac ~howmany:num
71         ~mk_fresh_name_callback:(namer_of names) ()
72   | TacticAst.LApply (_, to_what, what, ident) ->
73      let names = match ident with None -> [] | Some id -> [id] in
74      Tactics.lapply ~mk_fresh_name_callback:(namer_of names) ?to_what what
75   | TacticAst.Left _ -> Tactics.left
76   | TacticAst.LetIn (loc,term,name) ->
77       Tactics.letin term ~mk_fresh_name_callback:(namer_of [name])
78   | TacticAst.Reduce (_, reduction_kind, pattern) ->
79       (match reduction_kind with
80       | `Normalize -> Tactics.normalize ~pattern
81       | `Reduce -> Tactics.reduce ~pattern  
82       | `Simpl -> Tactics.simpl ~pattern 
83       | `Whd -> Tactics.whd ~pattern)
84   | TacticAst.Reflexivity _ -> Tactics.reflexivity
85   | TacticAst.Replace (_, pattern, with_what) ->
86      Tactics.replace ~pattern ~with_what
87   | TacticAst.Rewrite (_, dir, t, pattern) ->
88       if dir = `Left then
89         EqualityTactics.rewrite_tac ~where:pattern ~term:t ()
90       else
91         EqualityTactics.rewrite_back_tac ~where:pattern ~term:t ()
92   | TacticAst.Right _ -> Tactics.right
93   | TacticAst.Ring _ -> Tactics.ring
94   | TacticAst.Split _ -> Tactics.split
95   | TacticAst.Symmetry _ -> Tactics.symmetry
96   | TacticAst.Transitivity (_, term) -> Tactics.transitivity term
97
98 let eval_tactical status tac =
99   let apply_tactic tactic =
100     let (proof, goals) =
101       ProofEngineTypes.apply_tactic tactic (MatitaMisc.get_proof_status status)
102     in
103     let new_status =
104       match goals with
105       | [] -> 
106           let (_,metasenv,_,_) = proof in
107           (match metasenv with
108           | [] -> Proof proof
109           | (ng,_,_)::_ -> Incomplete_proof (proof,ng))
110       | ng::_ -> Incomplete_proof (proof, ng)
111     in
112     { status with proof_status = new_status }
113   in
114   let rec tactical_of_ast = function
115     | TacticAst.Tactic (loc, tactic) -> tactic_of_ast tactic
116     | TacticAst.Do (loc, num, tactical) ->
117         Tacticals.do_tactic num (tactical_of_ast tactical)
118     | TacticAst.Repeat (loc, tactical) ->
119         Tacticals.repeat_tactic (tactical_of_ast tactical)
120     | TacticAst.Seq (loc, tacticals) ->  (* tac1; tac2; ... *)
121         Tacticals.seq (List.map tactical_of_ast tacticals)
122     | TacticAst.Then (loc, tactical, tacticals) ->  (* tac; [ tac1 | ... ] *)
123         Tacticals.thens (tactical_of_ast tactical)
124           (List.map tactical_of_ast tacticals)
125     | TacticAst.Tries (loc, tacticals) ->
126         Tacticals.try_tactics
127           (List.map (fun t -> "", tactical_of_ast t) tacticals)
128     | TacticAst.Try (loc, tactical) ->
129         Tacticals.try_tactic (tactical_of_ast tactical)
130   in
131   apply_tactic (tactical_of_ast tac)
132
133 let eval_coercion status coercion = 
134   let coer_uri,coer_ty =
135     match coercion with 
136     | Cic.Const (uri,_)
137     | Cic.Var (uri,_) ->
138         let o,_ = 
139           CicEnvironment.get_obj CicUniv.empty_ugraph uri 
140         in
141         (match o with
142         | Cic.Constant (_,_,ty,_,_)
143         | Cic.Variable (_,_,ty,_,_) ->
144             uri,ty
145         | _ -> assert false)
146     | Cic.MutConstruct (uri,t,c,_) ->
147         let o,_ = 
148           CicEnvironment.get_obj CicUniv.empty_ugraph uri 
149         in
150         (match o with
151         | Cic.InductiveDefinition (l,_,_,_) ->
152             let (_,_,_,cl) = List.nth l t in
153             let (_,cty) = List.nth cl c in
154               uri,cty
155         | _ -> assert false)
156     | _ -> assert false 
157   in
158   (* we have to get the source and the tgt type uri 
159    * in Coq syntax we have already their names, but 
160    * since we don't support Funclass and similar I think
161    * all the coercion should be of the form
162    * (A:?)(B:?)T1->T2
163    * So we should be able to extract them from the coercion type
164    *)
165   let extract_last_two_p ty =
166     let rec aux = function
167       | Cic.Prod( _, src, Cic.Prod (n,t1,t2)) -> aux (Cic.Prod(n,t1,t2))   
168       | Cic.Prod( _, src, tgt) -> src, tgt
169       | _ -> assert false
170     in  
171     aux ty
172   in
173   let ty_src,ty_tgt = extract_last_two_p coer_ty in
174   let context = [] in 
175   let src_uri = 
176     let ty_src = CicReduction.whd context ty_src in
177      CicUtil.uri_of_term ty_src
178   in
179   let tgt_uri = 
180     let ty_tgt = CicReduction.whd context ty_tgt in
181      CicUtil.uri_of_term ty_tgt
182   in
183   let new_coercions =
184     (* also adds them to the Db *)
185     CoercGraph.close_coercion_graph src_uri tgt_uri coer_uri in
186   let status =
187    List.fold_left (fun s (uri,o,ugraph) -> MatitaSync.add_obj uri o status)
188     status new_coercions in
189   {status with proof_status = No_proof}
190
191 let generate_elimination_principles uri status =
192  let elim sort status =
193    try
194     let uri,obj = CicElim.elim_of ~sort uri 0 in
195      MatitaSync.add_obj uri obj status
196    with CicElim.Can_t_eliminate -> status
197  in
198  List.fold_left (fun status sort -> elim sort status) status
199   [ Cic.Prop; Cic.Set; (Cic.Type (CicUniv.fresh ())) ]
200
201 let generate_projections uri fields status =
202  let projections = CicRecord.projections_of uri fields in
203   List.fold_left
204    (fun status (uri, name, bo) -> 
205      try 
206       let ty, ugraph = 
207         CicTypeChecker.type_of_aux' [] [] bo CicUniv.empty_ugraph in
208       let bo = Unshare.unshare bo in
209       let ty = Unshare.unshare ty in
210       let attrs = [`Class `Projection; `Generated] in
211       let obj = Cic.Constant (name,Some bo,ty,[],attrs) in
212        MatitaSync.add_obj uri obj status
213      with
214         CicTypeChecker.TypeCheckerFailure s ->
215          MatitaLog.message 
216           ("Unable to create projection " ^ name ^ " cause: " ^ s);
217          status
218       | CicEnvironment.Object_not_found uri ->
219          let depend = UriManager.name_of_uri uri in
220           MatitaLog.message 
221            ("Unable to create projection " ^ name ^ " because it requires " ^ depend);
222          status
223   ) status projections
224  
225 let eval_command status cmd =
226   match cmd with
227   | TacticAst.Set (loc, name, value) -> 
228       let value = 
229         if name = "baseuri" then
230           MatitaMisc.strip_trailing_slash value
231         else
232           value
233       in
234       set_option status name value
235   | TacticAst.Drop loc -> raise Drop
236   | TacticAst.Qed loc ->
237       let uri, metasenv, bo, ty = 
238         match status.proof_status with
239         | Proof (Some uri, metasenv, body, ty) ->
240             uri, metasenv, body, ty
241         | Proof (None, metasenv, body, ty) -> 
242             command_error 
243               ("Someone allows to start a thm without giving the "^
244                "name/uri. This should be fixed!")
245         | _-> command_error "You can't qed an uncomplete theorem"
246       in
247       let suri = UriManager.string_of_uri uri in
248       if metasenv <> [] then 
249         command_error "Proof not completed! metasenv is not empty!";
250       let name = UriManager.name_of_uri uri in
251       let obj = Cic.Constant (name,Some bo,ty,[],[]) in
252       MatitaSync.add_obj uri obj status
253   | TacticAst.Coercion (loc, coercion) -> 
254       eval_coercion status coercion
255   | TacticAst.Alias (loc, spec) -> 
256      (match spec with
257       | TacticAst.Ident_alias (id,uri) -> 
258           {status with aliases = 
259             DisambiguateTypes.Environment.add 
260               (DisambiguateTypes.Id id) 
261               ("boh?",(fun _ _ _ -> CicUtil.term_of_uri (UriManager.uri_of_string uri)))
262               status.aliases }
263       | TacticAst.Symbol_alias (symb, instance, desc) ->
264           {status with aliases = 
265             DisambiguateTypes.Environment.add 
266               (DisambiguateTypes.Symbol (symb,instance))
267               (DisambiguateChoices.lookup_symbol_by_dsc symb desc) 
268               status.aliases }
269       | TacticAst.Number_alias (instance,desc) ->
270           {status with aliases = 
271             DisambiguateTypes.Environment.add 
272               (DisambiguateTypes.Num instance) 
273               (DisambiguateChoices.lookup_num_by_dsc desc) status.aliases })
274   | TacticAst.Obj (loc,obj) ->
275      let ext,name =
276       match obj with
277          Cic.Constant (name,_,_,_,_)
278        | Cic.CurrentProof (name,_,_,_,_,_) -> ".con",name
279        | Cic.InductiveDefinition (types,_,_,_) ->
280           ".ind",
281           (match types with (name,_,_,_)::_ -> name | _ -> assert false)
282        | _ -> assert false in
283      let uri = 
284        UriManager.uri_of_string (MatitaMisc.qualify status name ^ ext) 
285      in
286      let metasenv = MatitaMisc.get_proof_metasenv status in
287      match obj with
288         Cic.CurrentProof (_,metasenv',bo,ty,_,_) ->
289          assert (metasenv = metasenv');
290          let goalno =
291           match metasenv' with (goalno,_,_)::_ -> goalno | _ -> assert false in
292          let initial_proof = (Some uri, metasenv, bo, ty) in
293           { status with proof_status = Incomplete_proof (initial_proof,goalno)}
294       | _ ->
295         if metasenv <> [] then
296          command_error (
297            "metasenv not empty while giving a definition with body: " ^
298            CicMetaSubst.ppmetasenv metasenv []);
299         let status = MatitaSync.add_obj uri obj status in
300          match obj with
301             Cic.Constant _ -> status
302           | Cic.InductiveDefinition (_,_,_,attrs) ->
303              let status = generate_elimination_principles uri status in
304              let rec get_record_attrs =
305               function
306                  [] -> None
307                | (`Class (`Record fields))::_ -> Some fields
308                | _::tl -> get_record_attrs tl
309              in
310               (match get_record_attrs attrs with
311                   None -> status (* not a record *)
312                 | Some fields -> generate_projections uri fields status)
313           | Cic.CurrentProof _
314           | Cic.Variable _ -> assert false
315
316 let eval_executable status ex =
317   match ex with
318   | TacticAst.Tactical (_, tac) -> eval_tactical status tac
319   | TacticAst.Command (_, cmd) -> eval_command status cmd
320   | TacticAst.Macro (_, mac) -> 
321       command_error (sprintf "The macro %s can't be in a script" 
322         (TacticAstPp.pp_macro_cic mac))
323
324 let eval_comment status c = status
325             
326 let eval status st =
327   match st with
328   | TacticAst.Executable (_,ex) -> eval_executable status ex
329   | TacticAst.Comment (_,c) -> eval_comment status c
330
331 let disambiguate_term status term =
332   let (aliases, metasenv, cic, _) =
333     match
334       MatitaDisambiguator.disambiguate_term ~dbd:(MatitaDb.instance ())
335         ~aliases:(status.aliases) ~context:(MatitaMisc.get_proof_context status)
336         ~metasenv:(MatitaMisc.get_proof_metasenv status) term
337     with
338     | [x] -> x
339     | _ -> assert false
340   in
341   let proof_status =
342     match status.proof_status with
343     | No_proof -> Intermediate metasenv
344     | Incomplete_proof ((uri, _, proof, ty), goal) ->
345         Incomplete_proof ((uri, metasenv, proof, ty), goal)
346     | Intermediate _ -> Intermediate metasenv 
347     | Proof _ -> assert false
348   in
349   let status =
350     { status with
351         aliases = aliases;
352         proof_status = proof_status }
353   in
354   status, cic
355   
356 let disambiguate_obj status obj =
357   let uri =
358    match obj with
359       TacticAst.Inductive (_,(name,_,_,_)::_)
360     | TacticAst.Record (_,name,_,_) ->
361        Some (UriManager.uri_of_string (MatitaMisc.qualify status name ^ ".ind"))
362     | TacticAst.Inductive _ -> assert false
363     | _ -> None in
364   let (aliases, metasenv, cic, _) =
365     match
366       MatitaDisambiguator.disambiguate_obj ~dbd:(MatitaDb.instance ())
367         ~aliases:(status.aliases) ~uri obj
368     with
369     | [x] -> x
370     | _ -> assert false
371   in
372   let proof_status =
373     match status.proof_status with
374     | No_proof -> Intermediate metasenv
375     | Incomplete_proof _
376     | Intermediate _
377     | Proof _ -> assert false
378   in
379   let status =
380     { status with
381         aliases = aliases;
382         proof_status = proof_status }
383   in
384   status, cic
385   
386 let disambiguate_pattern status (wanted, hyp_paths, goal_path) =
387   let interp path = Disambiguate.interpretate_path [] status.aliases path in
388   let goal_path = interp goal_path in
389   let hyp_paths = List.map (fun (name, path) -> name, interp path) hyp_paths in
390   let status,wanted =
391    match wanted with
392       None -> status,None
393     | Some wanted ->
394        let status,wanted = disambiguate_term status wanted in
395         status, Some wanted
396   in
397    status, (wanted, hyp_paths ,goal_path)
398   
399 let disambiguate_tactic status = function
400   | TacticAst.Apply (loc, term) ->
401       let status, cic = disambiguate_term status term in
402       status, TacticAst.Apply (loc, cic)
403   | TacticAst.Absurd (loc, term) -> 
404       let status, cic = disambiguate_term status term in
405       status, TacticAst.Absurd (loc, cic)
406   | TacticAst.Assumption loc -> status, TacticAst.Assumption loc
407   | TacticAst.Auto (loc,depth,width) -> status, TacticAst.Auto (loc,depth,width)
408   | TacticAst.Change (loc, pattern, with_what) -> 
409       let status, with_what = disambiguate_term status with_what in
410       let status, pattern = disambiguate_pattern status pattern in
411       status, TacticAst.Change (loc, pattern, with_what)
412   | TacticAst.Clear (loc,id) -> status,TacticAst.Clear (loc,id)
413   | TacticAst.ClearBody (loc,id) -> status,TacticAst.ClearBody (loc,id)
414   | TacticAst.Compare (loc,term) ->
415       let status, term = disambiguate_term status term in
416       status, TacticAst.Compare (loc,term)
417   | TacticAst.Constructor (loc,n) ->
418       status, TacticAst.Constructor (loc,n)
419   | TacticAst.Contradiction loc ->
420       status, TacticAst.Contradiction loc
421   | TacticAst.Cut (loc, ident, term) -> 
422       let status, cic = disambiguate_term status term in
423       status, TacticAst.Cut (loc, ident, cic)
424   | TacticAst.DecideEquality loc ->
425       status, TacticAst.DecideEquality loc
426   | TacticAst.Decompose (loc,term) ->
427       let status,term = disambiguate_term status term in
428       status, TacticAst.Decompose(loc,term)
429   | TacticAst.Discriminate (loc,term) ->
430       let status,term = disambiguate_term status term in
431       status, TacticAst.Discriminate(loc,term)
432   | TacticAst.Exact (loc, term) -> 
433       let status, cic = disambiguate_term status term in
434       status, TacticAst.Exact (loc, cic)
435   | TacticAst.Elim (loc, term, Some term') ->
436       let status, cic1 = disambiguate_term status term in
437       let status, cic2 = disambiguate_term status term' in
438       status, TacticAst.Elim (loc, cic1, Some cic2)
439   | TacticAst.Elim (loc, term, None) ->
440       let status, cic = disambiguate_term status term in
441       status, TacticAst.Elim (loc, cic, None)
442   | TacticAst.ElimType (loc, term) -> 
443       let status, cic = disambiguate_term status term in
444       status, TacticAst.ElimType (loc, cic)
445   | TacticAst.Exists loc -> status, TacticAst.Exists loc 
446   | TacticAst.Fail loc -> status,TacticAst.Fail loc
447   | TacticAst.Fold (loc,reduction_kind, pattern) ->
448      let status, pattern = disambiguate_pattern status pattern in
449      status, TacticAst.Fold (loc,reduction_kind, pattern)
450   | TacticAst.FwdSimpl (loc, term) ->
451      let status, term = disambiguate_term status term in
452      status, TacticAst.FwdSimpl (loc, term)  
453   | TacticAst.Fourier loc -> status, TacticAst.Fourier loc
454   | TacticAst.Generalize (loc,pattern,ident) ->
455       let status, pattern = disambiguate_pattern status pattern in
456       status, TacticAst.Generalize(loc,pattern,ident)
457   | TacticAst.Goal (loc, g) -> status, TacticAst.Goal (loc, g)
458   | TacticAst.IdTac loc -> status,TacticAst.IdTac loc
459   | TacticAst.Injection (loc,term) ->
460       let status, term = disambiguate_term status term in
461       status, TacticAst.Injection (loc,term)
462   | TacticAst.Intros (loc, num, names) ->
463       status, TacticAst.Intros (loc, num, names)
464   | TacticAst.LApply (loc, to_what, what, ident) ->
465      let status, to_what =
466       match to_what with
467          None -> status,None
468        | Some to_what -> 
469            let status, to_what = disambiguate_term status to_what in
470            status, Some to_what
471      in
472      let status, what = disambiguate_term status what in
473      status, TacticAst.LApply (loc, to_what, what, ident)
474   | TacticAst.Left loc -> status, TacticAst.Left loc
475   | TacticAst.LetIn (loc, term, name) ->
476       let status, term = disambiguate_term status term in
477       status, TacticAst.LetIn (loc,term,name)
478   | TacticAst.Reduce (loc, reduction_kind, pattern) ->
479       let status, pattern = disambiguate_pattern status pattern in
480       status, TacticAst.Reduce(loc, reduction_kind, pattern)
481   | TacticAst.Reflexivity loc -> status, TacticAst.Reflexivity loc
482   | TacticAst.Replace (loc, pattern, with_what) -> 
483       let status, pattern = disambiguate_pattern status pattern in
484       let status, with_what = disambiguate_term status with_what in
485       status, TacticAst.Replace (loc, pattern, with_what)
486   | TacticAst.Rewrite (loc, dir, t, pattern) ->
487       let status, term = disambiguate_term status t in
488       let status, pattern = disambiguate_pattern status pattern in
489       status, TacticAst.Rewrite (loc, dir, term, pattern)
490   | TacticAst.Right loc -> status, TacticAst.Right loc
491   | TacticAst.Ring loc -> status, TacticAst.Ring loc
492   | TacticAst.Split loc -> status, TacticAst.Split loc
493   | TacticAst.Symmetry loc -> status, TacticAst.Symmetry loc
494   | TacticAst.Transitivity (loc, term) -> 
495       let status, cic = disambiguate_term status term in
496       status, TacticAst.Transitivity (loc, cic)
497
498 let rec disambiguate_tactical status = function 
499   | TacticAst.Tactic (loc, tactic) -> 
500       let status, tac = disambiguate_tactic status tactic in
501       status, TacticAst.Tactic (loc, tac)
502   | TacticAst.Do (loc, num, tactical) ->
503       let status, tac = disambiguate_tactical status tactical in
504       status, TacticAst.Do (loc, num, tac)
505   | TacticAst.Repeat (loc, tactical) -> 
506       let status, tac = disambiguate_tactical status tactical in
507       status, TacticAst.Repeat (loc, tac)
508   | TacticAst.Seq (loc, tacticals) ->  (* tac1; tac2; ... *)
509       let status, tacticals = disambiguate_tacticals status tacticals in
510       let tacticals = List.rev tacticals in
511       status, TacticAst.Seq (loc, tacticals)
512   | TacticAst.Then (loc, tactical, tacticals) ->  (* tac; [ tac1 | ... ] *)
513       let status, tactical = disambiguate_tactical status tactical in
514       let status, tacticals = disambiguate_tacticals status tacticals in
515       status, TacticAst.Then (loc, tactical, tacticals)
516   | TacticAst.Tries (loc, tacticals) ->
517       let status, tacticals = disambiguate_tacticals status tacticals in
518       status, TacticAst.Tries (loc, tacticals)
519   | TacticAst.Try (loc, tactical) ->
520       let status, tactical = disambiguate_tactical status tactical in
521       status, TacticAst.Try (loc, tactical)
522
523 and disambiguate_tacticals status tacticals =
524   let status, tacticals =
525     List.fold_left
526       (fun (status, tacticals) tactical ->
527         let status, tac = disambiguate_tactical status tactical in
528         status, tac :: tacticals)
529       (status, [])
530       tacticals
531   in
532   let tacticals = List.rev tacticals in
533   status, tacticals
534   
535 let disambiguate_command status = function
536   | TacticAst.Coercion (loc, term) ->
537       let status, term = disambiguate_term status term in
538       status, TacticAst.Coercion (loc,term)
539   | (TacticAst.Set _ | TacticAst.Qed _ | TacticAst.Drop _ ) as cmd ->
540       status, cmd
541   | TacticAst.Alias _ as x -> status, x
542   | TacticAst.Obj (loc,obj) ->
543       let status,obj = disambiguate_obj status obj in
544        status, TacticAst.Obj (loc,obj)
545
546 let disambiguate_executable status ex =
547   match ex with
548   | TacticAst.Tactical (loc, tac) ->
549       let status, tac = disambiguate_tactical status tac in
550       status, (TacticAst.Tactical (loc, tac))
551   | TacticAst.Command (loc, cmd) ->
552       let status, cmd = disambiguate_command status cmd in
553       status, (TacticAst.Command (loc, cmd))
554   | TacticAst.Macro (_, mac) -> 
555       command_error (sprintf "The macro %s can't be in a script" 
556         (TacticAstPp.pp_macro_ast mac))
557
558 let disambiguate_comment status c = 
559   match c with
560   | TacticAst.Note (loc,n) -> status, TacticAst.Note (loc,n)
561   | TacticAst.Code (loc,ex) -> 
562         let status, ex = disambiguate_executable status ex in
563         status, TacticAst.Code (loc,ex)
564         
565 let disambiguate_statement status statement =
566   match statement with
567   | TacticAst.Comment (loc,c) -> 
568         let status, c = disambiguate_comment status c in
569         status, TacticAst.Comment (loc,c)
570   | TacticAst.Executable (loc,ex) -> 
571         let status, ex = disambiguate_executable status ex in
572         status, TacticAst.Executable (loc,ex)
573   
574 let eval_ast status ast =
575   let status,st = disambiguate_statement status ast in
576   (* this disambiguation step should be deferred to support tacticals *)
577   eval status st
578
579 let eval_from_stream status str cb =
580   let stl = CicTextualParser2.parse_statements str in
581   List.iter (fun ast -> cb !status ast;status := eval_ast !status ast) stl
582
583 let eval_from_stream_greedy status str cb =
584   while true do
585     print_string "matita> ";
586     flush stdout;
587     let ast = CicTextualParser2.parse_statement str in
588     cb !status ast;
589     status := eval_ast !status ast 
590   done
591   
592 let eval_string status str =
593   eval_from_stream status (Stream.of_string str) (fun _ _ -> ())
594
595 let default_options () =
596 (*
597   let options =
598     StringMap.add "baseuri"
599       (String
600         (Helm_registry.get "matita.baseuri" ^ Helm_registry.get "matita.owner"))
601       no_options
602   in
603 *)
604   let options =
605     StringMap.add "basedir"
606       (String (Helm_registry.get "matita.basedir" ))
607       no_options
608   in
609   options
610
611 let initial_status =
612   lazy {
613     aliases = DisambiguateTypes.empty_environment;
614     proof_status = No_proof;
615     options = default_options ();
616     objects = [];
617   }
618
619