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