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