]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/matitaEngine.ml
acic_procedural and tactics removed
[helm.git] / matita / matita / matitaEngine.ml
1 (* Copyright (C) 2005, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 (* $Id$ *)
27
28 module G = GrafiteAst
29
30 let debug = false ;;
31 let debug_print = if debug then prerr_endline else ignore ;;
32
33 let disambiguate_command lexicon_status_ref grafite_status cmd =
34  let baseuri = grafite_status#baseuri in
35  let lexicon_status,cmd =
36   GrafiteDisambiguate.disambiguate_command ~baseuri
37    !lexicon_status_ref cmd
38  in
39   lexicon_status_ref := lexicon_status;
40   grafite_status,cmd
41
42 let eval_macro_screenshot (status : GrafiteTypes.status) name =
43   let _,_,metasenv,subst,_ = status#obj in
44   let sequent = List.hd metasenv in
45   let mathml = 
46     ApplyTransformation.nmml_of_cic_sequent 
47       status metasenv subst sequent 
48   in
49   let domImpl = Gdome.domImplementation () in
50   ignore(domImpl#saveDocumentToFile 
51     ~name:(name^".xml") ~doc:mathml ());
52   ignore(Sys.command ("mathmlsvg --verbose=1 --font-size=20 --cut-filename=no " ^ 
53     Filename.quote (name^".xml")));
54   ignore(Sys.command ("convert " ^ 
55     Filename.quote (name^".svg") ^ " " ^ 
56     Filename.quote (name^".png")));
57   HLog.debug ("generated " ^ name ^ ".png");
58   status, `New []
59 ;;
60
61 let eval_ast ?do_heavy_checks status (text,prefix_len,ast) =
62  let dump = not (Helm_registry.get_bool "matita.moo") in
63  let lexicon_status_ref = ref (status :> LexiconEngine.status) in
64  let baseuri = status#baseuri in
65  let new_status,new_objs =
66   match ast with 
67      | G.Executable (_, G.Command (_, G.Coercion _)) when dump ->
68 (* FG: some commands can not be executed when mmas are parsed *************)
69 (* To be removed when mmas will be executed                               *)
70         status, `Old []
71      | ast -> 
72   GrafiteEngine.eval_ast
73    ~disambiguate_command:(disambiguate_command lexicon_status_ref)
74    ~disambiguate_macro:((* MATITA 1.0*) fun _ -> assert false)
75    ?do_heavy_checks status (text,prefix_len,ast)
76  in
77  let new_status =
78   if !lexicon_status_ref#lstatus != status#lstatus then
79    new_status#set_lstatus (!lexicon_status_ref#lstatus)
80   else
81    new_status in
82  let new_status = LexiconSync.add_aliases_for_objs new_status new_objs in
83  let new_aliases = LexiconSync.alias_diff ~from:status new_status in
84  let _,intermediate_states = 
85   List.fold_left
86    (fun (status,acc) (k,value) -> 
87      let v = LexiconAst.description_of_alias value in
88      let b =
89       try
90        (* this hack really sucks! *)
91        UriManager.buri_of_uri (UriManager.uri_of_string v) = baseuri
92       with
93        UriManager.IllFormedUri _ ->
94         try
95          (* this too! *)
96          let NReference.Ref (uri,_) = NReference.reference_of_string v in
97          let ouri = NCic2OCic.ouri_of_nuri uri in
98           UriManager.buri_of_uri ouri = baseuri
99         with
100          NReference.IllFormedReference _ ->
101           false (* v is a description, not a URI *)
102      in
103       if b then 
104        status,acc
105       else
106        let new_status =
107         LexiconEngine.set_proof_aliases status [k,value]
108        in
109         new_status, (new_status ,Some (k,value))::acc
110    ) (status,[]) new_aliases
111  in
112   ((new_status),None)::intermediate_states
113 ;;
114
115 exception TryingToAdd of string
116 exception EnrichedWithStatus of exn * GrafiteTypes.status
117
118 let eval_from_stream ~first_statement_only ~include_paths 
119  ?do_heavy_checks ?(enforce_no_new_aliases=true)
120  ?(watch_statuses=fun _ -> ()) status str cb 
121 =
122  let matita_debug = Helm_registry.get_bool "matita.debug" in
123  let rec loop status statuses =
124   let loop =
125    if first_statement_only then fun _ statuses -> statuses
126    else loop
127   in
128   let stop,g,s = 
129    try
130      let cont =
131        try Some (GrafiteParser.parse_statement ~include_paths str status)
132        with End_of_file -> None in
133      match cont with
134      | None -> true, status, statuses
135      | Some (status,ast) ->
136        (match ast with
137            GrafiteParser.LNone _ ->
138             watch_statuses status ;
139             false, status, ((status,None)::statuses)
140          | GrafiteParser.LSome ast ->
141             cb status ast;
142             let new_statuses = eval_ast ?do_heavy_checks status ("",0,ast) in
143             if enforce_no_new_aliases then
144              List.iter 
145               (fun (_,alias) ->
146                 match alias with
147                   None -> ()
148                 | Some (k,value) ->
149                    let newtxt = LexiconAstPp.pp_alias value in
150                     raise (TryingToAdd newtxt)) new_statuses;
151             let status =
152              match new_statuses with
153                 [] -> assert false
154               | (s,_)::_ -> s
155             in
156              watch_statuses status ;
157              false, status, (new_statuses @ statuses))
158    with exn when not matita_debug ->
159      raise (EnrichedWithStatus (exn, status))
160   in
161   if stop then s else loop g s
162  in
163   loop status []
164 ;;