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