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