]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/matitaEngine.ml
Preparing for 0.5.9 release.
[helm.git] / helm / software / 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_tactic text prefix_len lexicon_status_ref grafite_status goal tac =
34  let metasenv,tac =
35   GrafiteDisambiguate.disambiguate_tactic
36    lexicon_status_ref
37    (GrafiteTypes.get_proof_context grafite_status goal)
38    (GrafiteTypes.get_proof_metasenv grafite_status) (Some goal)
39    tac
40  in
41   GrafiteTypes.set_metasenv metasenv grafite_status,tac
42
43 let disambiguate_command lexicon_status_ref grafite_status cmd =
44  let baseuri = grafite_status#baseuri in
45  let lexicon_status,metasenv,cmd =
46   GrafiteDisambiguate.disambiguate_command ~baseuri
47    !lexicon_status_ref (GrafiteTypes.get_proof_metasenv grafite_status) cmd
48  in
49   lexicon_status_ref := lexicon_status;
50   GrafiteTypes.set_metasenv metasenv grafite_status,cmd
51
52 let disambiguate_macro lexicon_status_ref grafite_status macro context =
53  let metasenv,macro =
54   GrafiteDisambiguate.disambiguate_macro
55    lexicon_status_ref
56    (GrafiteTypes.get_proof_metasenv grafite_status)
57    context macro
58  in
59   GrafiteTypes.set_metasenv metasenv grafite_status,macro
60
61 let eval_macro_screenshot (status : GrafiteTypes.status) name =
62   let _,_,metasenv,subst,_ = status#obj in
63   let sequent = List.hd metasenv in
64   let mathml = 
65     ApplyTransformation.nmml_of_cic_sequent 
66       status metasenv subst sequent 
67   in
68   let domImpl = Gdome.domImplementation () in
69   ignore(domImpl#saveDocumentToFile 
70     ~name:(name^".xml") ~doc:mathml ());
71   ignore(Sys.command ("mathmlsvg --verbose=1 --font-size=20 --cut-filename=no " ^ 
72     Filename.quote (name^".xml")));
73   ignore(Sys.command ("convert " ^ 
74     Filename.quote (name^".svg") ^ " " ^ 
75     Filename.quote (name^".png")));
76   HLog.debug ("generated " ^ name ^ ".png");
77   status, `New []
78 ;;
79
80 let eval_ast ?do_heavy_checks status (text,prefix_len,ast) =
81  let dump = not (Helm_registry.get_bool "matita.moo") in
82  let lexicon_status_ref = ref (status :> LexiconEngine.status) in
83  let baseuri = status#baseuri in
84  let new_status,new_objs =
85   match ast with 
86      | G.Executable (_, G.Command (_, G.Coercion _)) when dump ->
87 (* FG: some commands can not be executed when mmas are parsed *************)
88 (* To be removed when mmas will be executed                               *)
89         status, `Old []
90      | ast -> 
91   GrafiteEngine.eval_ast
92    ~disambiguate_tactic:(disambiguate_tactic text prefix_len lexicon_status_ref)
93    ~disambiguate_command:(disambiguate_command lexicon_status_ref)
94    ~disambiguate_macro:(disambiguate_macro lexicon_status_ref)
95    ?do_heavy_checks status (text,prefix_len,ast)
96  in
97  let new_status =
98   if !lexicon_status_ref#lstatus != status#lstatus then
99    new_status#set_lstatus (!lexicon_status_ref#lstatus)
100   else
101    new_status in
102  let new_status = LexiconSync.add_aliases_for_objs new_status new_objs in
103  let new_aliases = LexiconSync.alias_diff ~from:status new_status in
104  let _,intermediate_states = 
105   List.fold_left
106    (fun (status,acc) (k,value) -> 
107      let v = LexiconAst.description_of_alias value in
108      let b =
109       try
110        (* this hack really sucks! *)
111        UriManager.buri_of_uri (UriManager.uri_of_string v) = baseuri
112       with
113        UriManager.IllFormedUri _ ->
114         try
115          (* this too! *)
116          let NReference.Ref (uri,_) = NReference.reference_of_string v in
117          let ouri = NCic2OCic.ouri_of_nuri uri in
118           UriManager.buri_of_uri ouri = baseuri
119         with
120          NReference.IllFormedReference _ ->
121           false (* v is a description, not a URI *)
122      in
123       if b then 
124        status,acc
125       else
126        let new_status =
127         LexiconEngine.set_proof_aliases status [k,value]
128        in
129         new_status, (new_status ,Some (k,value))::acc
130    ) (status,[]) new_aliases
131  in
132   ((new_status),None)::intermediate_states
133 ;;
134
135 exception TryingToAdd of string
136 exception EnrichedWithStatus of exn * GrafiteTypes.status
137
138 let eval_from_stream ~first_statement_only ~include_paths 
139  ?do_heavy_checks ?(enforce_no_new_aliases=true)
140  ?(watch_statuses=fun _ -> ()) status str cb 
141 =
142  let matita_debug = Helm_registry.get_bool "matita.debug" in
143  let rec loop status statuses =
144   let loop =
145    if first_statement_only then fun _ statuses -> statuses
146    else loop
147   in
148   let stop,g,s = 
149    try
150      let cont =
151        try Some (GrafiteParser.parse_statement ~include_paths str status)
152        with End_of_file -> None in
153      match cont with
154      | None -> true, status, statuses
155      | Some (status,ast) ->
156        (match ast with
157            GrafiteParser.LNone _ ->
158             watch_statuses status ;
159             false, status, ((status,None)::statuses)
160          | GrafiteParser.LSome ast ->
161             cb status ast;
162             let new_statuses = eval_ast ?do_heavy_checks status ("",0,ast) in
163             if enforce_no_new_aliases then
164              List.iter 
165               (fun (_,alias) ->
166                 match alias with
167                   None -> ()
168                 | Some (k,value) ->
169                    let newtxt = LexiconAstPp.pp_alias value in
170                     raise (TryingToAdd newtxt)) new_statuses;
171             let status =
172              match new_statuses with
173                 [] -> assert false
174               | (s,_)::_ -> s
175             in
176              watch_statuses status ;
177              false, status, (new_statuses @ statuses))
178    with exn when not matita_debug ->
179      raise (EnrichedWithStatus (exn, status))
180   in
181   if stop then s else loop g s
182  in
183   loop status []
184 ;;