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