]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/matitaEngine.ml
a8dcf98092e29a8d68009d012a6469ed3542f18f
[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 = GrafiteTypes.get_baseuri grafite_status 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_ast ?do_heavy_checks lexicon_status
62  grafite_status (text,prefix_len,ast)
63 =
64  let dump = not (Helm_registry.get_bool "matita.moo") in
65  let lexicon_status_ref = ref lexicon_status in
66  let baseuri = GrafiteTypes.get_baseuri grafite_status in
67  let changed_lexicon = ref false in
68  let wrap f x = changed_lexicon := true; f x in
69  let grafite_status = 
70    match grafite_status.GrafiteTypes.ng_status with
71    | GrafiteTypes.CommandMode _ -> 
72       { grafite_status with GrafiteTypes.ng_status = 
73          GrafiteTypes.CommandMode lexicon_status }
74    | GrafiteTypes.ProofMode s -> 
75       { grafite_status with GrafiteTypes.ng_status = 
76          GrafiteTypes.ProofMode 
77           { s with NTacStatus.istatus = { s.NTacStatus.istatus with NTacStatus.lstatus =  lexicon_status }}}
78  in
79  let new_grafite_status,new_objs =
80   match ast with 
81      | G.Executable (_, G.Command (_, G.Coercion _)) when dump ->
82 (* FG: some commands can not be executed when mmas are parsed *************)
83 (* To be removed when mmas will be executed                               *)
84         grafite_status, `Old []
85      | ast -> 
86   GrafiteEngine.eval_ast
87    ~disambiguate_tactic:(wrap (disambiguate_tactic text prefix_len lexicon_status_ref))
88    ~disambiguate_command:(wrap (disambiguate_command lexicon_status_ref))
89    ~disambiguate_macro:(wrap (disambiguate_macro lexicon_status_ref))
90    ?do_heavy_checks grafite_status (text,prefix_len,ast)
91  in
92  let new_lexicon_status =
93   if !changed_lexicon then
94    !lexicon_status_ref
95   else
96    match new_grafite_status.GrafiteTypes.ng_status with
97    | GrafiteTypes.CommandMode l -> l
98    | GrafiteTypes.ProofMode s -> s.NTacStatus.istatus.NTacStatus.lstatus
99  in
100  let new_lexicon_status =
101   LexiconSync.add_aliases_for_objs new_lexicon_status new_objs in
102  let new_aliases =
103   LexiconSync.alias_diff ~from:lexicon_status new_lexicon_status in
104  let _,intermediate_states = 
105   List.fold_left
106    (fun (lexicon_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) = 
112        baseuri
113       with
114        UriManager.IllFormedUri _ ->
115         try
116          (* this too! *)
117          let NReference.Ref (uri,_) = NReference.reference_of_string v in
118          let ouri = NCic2OCic.ouri_of_nuri uri in
119           UriManager.buri_of_uri ouri = baseuri
120         with
121          NReference.IllFormedReference _ ->
122           false (* v is a description, not a URI *)
123      in
124       if b then 
125        lexicon_status,acc
126       else
127
128        let new_lexicon_status =
129         LexiconEngine.set_proof_aliases lexicon_status [k,value]
130        in
131         new_lexicon_status,
132          ((grafite_status,new_lexicon_status),Some (k,value))::acc
133    ) (lexicon_status,[]) new_aliases
134  in
135   ((new_grafite_status,new_lexicon_status),None)::intermediate_states
136
137 exception TryingToAdd of string
138 exception EnrichedWithStatus of exn * LexiconEngine.status * GrafiteTypes.status
139
140 let eval_from_stream ~first_statement_only ~include_paths 
141  ?do_heavy_checks ?(enforce_no_new_aliases=true)
142  ?(watch_statuses=fun _ _ -> ()) lexicon_status grafite_status str cb 
143 =
144  let matita_debug = Helm_registry.get_bool "matita.debug" in
145  let rec loop lexicon_status grafite_status statuses =
146   let loop =
147    if first_statement_only then fun _ _ statuses -> statuses
148    else loop
149   in
150   let stop,l,g,s = 
151    try
152      let cont =
153        try
154          Some (GrafiteParser.parse_statement ~include_paths str lexicon_status)
155        with
156          End_of_file -> None
157      in
158      match cont with
159      | None -> true, lexicon_status, grafite_status, statuses
160      | Some (lexicon_status,ast) ->
161        (match ast with
162            GrafiteParser.LNone _ ->
163             watch_statuses lexicon_status grafite_status ;
164             false, lexicon_status, grafite_status,
165              (((grafite_status,lexicon_status),None)::statuses)
166          | GrafiteParser.LSome ast ->
167             cb grafite_status ast;
168             let new_statuses =
169              eval_ast ?do_heavy_checks lexicon_status
170               grafite_status ("",0,ast) in
171             if enforce_no_new_aliases then
172              List.iter 
173               (fun (_,alias) ->
174                 match alias with
175                   None -> ()
176                 | Some (k,value) ->
177                    let newtxt = LexiconAstPp.pp_alias value in
178                     raise (TryingToAdd newtxt)) new_statuses;
179             let grafite_status,lexicon_status =
180              match new_statuses with
181                 [] -> assert false
182               | (s,_)::_ -> s
183             in
184              watch_statuses lexicon_status grafite_status ;
185              false, lexicon_status, grafite_status, (new_statuses @ statuses))
186    with exn when not matita_debug ->
187      raise (EnrichedWithStatus (exn, lexicon_status, grafite_status))
188   in
189   if stop then s else loop l g s
190  in
191   loop lexicon_status grafite_status []
192 ;;