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