1 (* Copyright (C) 2005, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
31 let debug_print = if debug then prerr_endline else ignore ;;
33 let disambiguate_tactic text prefix_len lexicon_status_ref grafite_status goal tac =
35 GrafiteDisambiguate.disambiguate_tactic
37 (GrafiteTypes.get_proof_context grafite_status goal)
38 (GrafiteTypes.get_proof_metasenv grafite_status) (Some goal)
41 GrafiteTypes.set_metasenv metasenv grafite_status,tac
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
49 lexicon_status_ref := lexicon_status;
50 GrafiteTypes.set_metasenv metasenv grafite_status,cmd
52 let disambiguate_macro lexicon_status_ref grafite_status macro context =
54 GrafiteDisambiguate.disambiguate_macro
56 (GrafiteTypes.get_proof_metasenv grafite_status)
59 GrafiteTypes.set_metasenv metasenv grafite_status,macro
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 =
67 | G.Executable (_, G.Command (_, G.Coercion _)) when dump ->
68 (* FG: some commands can not be executed when mmas are parsed *************)
69 (* To be removed when mmas will be executed *)
72 GrafiteEngine.eval_ast
73 ~disambiguate_tactic:(disambiguate_tactic text prefix_len lexicon_status_ref)
74 ~disambiguate_command:(disambiguate_command lexicon_status_ref)
75 ~disambiguate_macro:(disambiguate_macro lexicon_status_ref)
76 ?do_heavy_checks status (text,prefix_len,ast)
79 if !lexicon_status_ref#lstatus != status#lstatus then
80 new_status#set_lstatus (!lexicon_status_ref#lstatus)
83 let new_status = LexiconSync.add_aliases_for_objs new_status new_objs in
84 let new_aliases = LexiconSync.alias_diff ~from:status new_status in
85 let _,intermediate_states =
87 (fun (status,acc) (k,value) ->
88 let v = LexiconAst.description_of_alias value in
91 (* this hack really sucks! *)
92 UriManager.buri_of_uri (UriManager.uri_of_string v) = baseuri
94 UriManager.IllFormedUri _ ->
97 let NReference.Ref (uri,_) = NReference.reference_of_string v in
98 let ouri = NCic2OCic.ouri_of_nuri uri in
99 UriManager.buri_of_uri ouri = baseuri
101 NReference.IllFormedReference _ ->
102 false (* v is a description, not a URI *)
108 LexiconEngine.set_proof_aliases status [k,value]
110 new_status, (new_status ,Some (k,value))::acc
111 ) (status,[]) new_aliases
113 ((new_status),None)::intermediate_states
116 exception TryingToAdd of string
117 exception EnrichedWithStatus of exn * GrafiteTypes.status
119 let eval_from_stream ~first_statement_only ~include_paths
120 ?do_heavy_checks ?(enforce_no_new_aliases=true)
121 ?(watch_statuses=fun _ -> ()) status str cb
123 let matita_debug = Helm_registry.get_bool "matita.debug" in
124 let rec loop status statuses =
126 if first_statement_only then fun _ statuses -> statuses
132 try Some (GrafiteParser.parse_statement ~include_paths str status)
133 with End_of_file -> None in
135 | None -> true, status, statuses
136 | Some (status,ast) ->
138 GrafiteParser.LNone _ ->
139 watch_statuses status ;
140 false, status, ((status,None)::statuses)
141 | GrafiteParser.LSome ast ->
143 let new_statuses = eval_ast ?do_heavy_checks status ("",0,ast) in
144 if enforce_no_new_aliases then
150 let newtxt = LexiconAstPp.pp_alias value in
151 raise (TryingToAdd newtxt)) new_statuses;
153 match new_statuses with
157 watch_statuses status ;
158 false, status, (new_statuses @ statuses))
159 with exn when not matita_debug ->
160 raise (EnrichedWithStatus (exn, status))
162 if stop then s else loop g s