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