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