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 = 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
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 lexicon_status
62 grafite_status (text,prefix_len,ast)
64 let lexicon_status_ref = ref lexicon_status in
65 let baseuri = GrafiteTypes.get_baseuri grafite_status in
66 let changed_lexicon = ref false in
67 let wrap f x = changed_lexicon := true; f x in
69 match grafite_status.GrafiteTypes.ng_status with
70 | GrafiteTypes.CommandMode _ ->
71 { grafite_status with GrafiteTypes.ng_status =
72 GrafiteTypes.CommandMode lexicon_status }
73 | GrafiteTypes.ProofMode s ->
74 { grafite_status with GrafiteTypes.ng_status =
75 GrafiteTypes.ProofMode
76 { s with NTactics.istatus = { s.NTactics.istatus with NTactics.lstatus = lexicon_status }}}
78 let new_grafite_status,new_objs =
79 GrafiteEngine.eval_ast
80 ~disambiguate_tactic:(wrap (disambiguate_tactic text prefix_len lexicon_status_ref))
81 ~disambiguate_command:(wrap (disambiguate_command lexicon_status_ref))
82 ~disambiguate_macro:(wrap (disambiguate_macro lexicon_status_ref))
83 ?do_heavy_checks grafite_status (text,prefix_len,ast) in
85 if !changed_lexicon then
88 match new_grafite_status.GrafiteTypes.ng_status with
89 | GrafiteTypes.CommandMode l -> l
90 | GrafiteTypes.ProofMode s -> s.NTactics.istatus.NTactics.lstatus
92 let new_lexicon_status =
93 LexiconSync.add_aliases_for_objs lexicon_status new_objs in
95 LexiconSync.alias_diff ~from:lexicon_status new_lexicon_status in
96 let _,intermediate_states =
98 (fun (lexicon_status,acc) (k,value) ->
99 let v = LexiconAst.description_of_alias value in
102 (* this hack really sucks! *)
103 UriManager.buri_of_uri (UriManager.uri_of_string v) =
106 UriManager.IllFormedUri _ -> false (* v is a description, not a URI *)
112 let new_lexicon_status =
113 LexiconEngine.set_proof_aliases lexicon_status [k,value]
116 ((grafite_status,new_lexicon_status),Some (k,value))::acc
117 ) (lexicon_status,[]) new_aliases
119 ((new_grafite_status,new_lexicon_status),None)::intermediate_states
121 exception TryingToAdd of string
122 exception EnrichedWithLexiconStatus of exn * LexiconEngine.status
126 let set_callback f = out := f
128 let eval_from_stream ~first_statement_only ~include_paths
129 ?do_heavy_checks ?(enforce_no_new_aliases=true)
130 ?(watch_statuses=fun _ _ -> ()) lexicon_status grafite_status str cb
132 let matita_debug = Helm_registry.get_bool "matita.debug" in
133 let rec loop lexicon_status grafite_status statuses =
135 if first_statement_only then fun _ _ statuses -> statuses
142 Some (GrafiteParser.parse_statement ~include_paths str lexicon_status)
147 | None -> true, lexicon_status, grafite_status, statuses
148 | Some (lexicon_status,ast) ->
150 GrafiteParser.LNone _ ->
151 watch_statuses lexicon_status grafite_status ;
152 false, lexicon_status, grafite_status,
153 (((grafite_status,lexicon_status),None)::statuses)
154 | GrafiteParser.LSome ast ->
156 cb grafite_status ast;
158 eval_ast ?do_heavy_checks lexicon_status
159 grafite_status ("",0,ast) in
160 if enforce_no_new_aliases then
166 let newtxt = LexiconAstPp.pp_alias value in
167 raise (TryingToAdd newtxt)) new_statuses;
168 let grafite_status,lexicon_status =
169 match new_statuses with
173 watch_statuses lexicon_status grafite_status ;
174 false, lexicon_status, grafite_status, (new_statuses @ statuses))
175 with exn when not matita_debug ->
176 raise (EnrichedWithLexiconStatus (exn, lexicon_status))
178 if stop then s else loop l g s
180 loop lexicon_status grafite_status []