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