]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/matitaEngine.ml
- bug fixed (introduced by last commit from Andrea in MatitaEngine):
[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 lexicon_status,cmd =
35   GrafiteDisambiguate.disambiguate_command !lexicon_status_ref cmd
36  in
37   lexicon_status_ref := lexicon_status;
38   grafite_status,cmd
39
40 let eval_macro_screenshot (status : GrafiteTypes.status) name =
41   assert false (* MATITA 1.0
42   let _,_,metasenv,subst,_ = status#obj in
43   let sequent = List.hd metasenv in
44   let mathml = 
45     ApplyTransformation.nmml_of_cic_sequent 
46       status metasenv subst sequent 
47   in
48   let domImpl = Gdome.domImplementation () in
49   ignore(domImpl#saveDocumentToFile 
50     ~name:(name^".xml") ~doc:mathml ());
51   ignore(Sys.command ("mathmlsvg --verbose=1 --font-size=20 --cut-filename=no " ^ 
52     Filename.quote (name^".xml")));
53   ignore(Sys.command ("convert " ^ 
54     Filename.quote (name^".svg") ^ " " ^ 
55     Filename.quote (name^".png")));
56   HLog.debug ("generated " ^ name ^ ".png");
57   status, `New []
58   *)
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   GrafiteEngine.eval_ast
67    ~disambiguate_command:(disambiguate_command lexicon_status_ref)
68    ?do_heavy_checks status (text,prefix_len,ast)
69  in
70  let new_status =
71   if !lexicon_status_ref#lstatus != status#lstatus then
72    new_status#set_lstatus (!lexicon_status_ref#lstatus)
73   else
74    new_status in
75  let new_status = LexiconSync.add_aliases_for_objs new_status new_objs in
76  let new_aliases = LexiconSync.alias_diff ~from:status new_status in
77  let _,intermediate_states = 
78   List.fold_left
79    (fun (status,acc) (k,value) -> 
80      let v = LexiconAst.description_of_alias value in
81      let b =
82       try
83        let NReference.Ref (uri,_) = NReference.reference_of_string v in
84         NUri.baseuri_of_uri uri = baseuri
85       with
86        NReference.IllFormedReference _ ->
87         false (* v is a description, not a URI *)
88      in
89       if b then 
90        status,acc
91       else
92        let new_status =
93         LexiconEngine.set_proof_aliases status [k,value]
94        in
95         new_status, (new_status ,Some (k,value))::acc
96    ) (status,[]) new_aliases
97  in
98   ((new_status),None)::intermediate_states
99 ;;
100
101 exception TryingToAdd of string
102 exception EnrichedWithStatus of exn * GrafiteTypes.status
103
104 let eval_from_stream ~first_statement_only ~include_paths 
105  ?do_heavy_checks ?(enforce_no_new_aliases=true)
106  ?(watch_statuses=fun _ -> ()) status str cb 
107 =
108  let matita_debug = Helm_registry.get_bool "matita.debug" in
109  let rec loop status statuses =
110   let loop =
111    if first_statement_only then fun _ statuses -> statuses
112    else loop
113   in
114   let stop,g,s = 
115    try
116      let cont =
117        try Some (GrafiteParser.parse_statement ~include_paths str status)
118        with End_of_file -> None in
119      match cont with
120      | None -> true, status, statuses
121      | Some (status,ast) ->
122        (match ast with
123            GrafiteParser.LNone _ ->
124             watch_statuses status ;
125             false, status, ((status,None)::statuses)
126          | GrafiteParser.LSome ast ->
127             cb status ast;
128             let new_statuses = eval_ast ?do_heavy_checks status ("",0,ast) in
129             if enforce_no_new_aliases then
130              List.iter 
131               (fun (_,alias) ->
132                 match alias with
133                   None -> ()
134                 | Some (k,value) ->
135                    let newtxt = LexiconAstPp.pp_alias value in
136                     raise (TryingToAdd newtxt)) new_statuses;
137             let status =
138              match new_statuses with
139                 [] -> assert false
140               | (s,_)::_ -> s
141             in
142              watch_statuses status ;
143              false, status, (new_statuses @ statuses))
144    with exn when not matita_debug ->
145      raise (EnrichedWithStatus (exn, status))
146   in
147   if stop then s else loop g s
148  in
149   loop status []
150 ;;