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