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