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