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