1 (* Copyright (C) 2004-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/
32 (* lexicon file name * ma file name *)
33 exception IncludedFileNotCompiled of string * string
34 exception MetadataNotFound of string (* file name *)
36 type lexicon_status = {
37 aliases: L.alias_spec DisambiguateTypes.Environment.t;
38 multi_aliases: L.alias_spec list DisambiguateTypes.Environment.t;
39 lexicon_content_rev: LexiconMarshal.lexicon;
40 notation_ids: CicNotation.notation_id list; (** in-scope notation ids *)
43 let initial_status = {
44 aliases = DisambiguateTypes.Environment.empty;
45 multi_aliases = DisambiguateTypes.Environment.empty;
46 lexicon_content_rev = [];
52 val lstatus = initial_status
53 method lstatus = lstatus
54 method set_lstatus v = {< lstatus = v >}
55 method set_lexicon_engine_status
56 : 'status. < lstatus: lexicon_status; .. > as 'status -> 'self
57 = fun o -> {< lstatus = o#lstatus >}
60 let dump_aliases out msg status =
61 out (if msg = "" then "aliases dump:" else msg ^ ": aliases dump:");
62 DisambiguateTypes.Environment.iter
63 (fun _ x -> out (LexiconAstPp.pp_alias x))
64 status#lstatus.aliases
66 let add_lexicon_content cmds status =
67 let content = status#lstatus.lexicon_content_rev in
74 | L.Notation _ -> cmd :: (List.filter ((<>) cmd) acc)
75 | L.Interpretation _ -> if List.exists ((=) cmd) acc then acc else cmd::acc)
79 prerr_endline ("new lexicon content: " ^
80 String.concat "; " (List.map LexiconAstPp.pp_command content')
84 { status#lstatus with lexicon_content_rev = content' }
86 let set_proof_aliases mode status new_aliases =
87 if mode = L.WithoutPreferences then
90 let commands_of_aliases =
92 (fun _,alias -> L.Alias (HExtlib.dummy_floc, alias))
95 List.fold_left (fun acc (d,c) -> DisambiguateTypes.Environment.add d c acc)
96 status#lstatus.aliases new_aliases in
98 List.fold_left (fun acc (d,c) ->
99 DisambiguateTypes.Environment.cons L.description_of_alias
101 status#lstatus.multi_aliases new_aliases
104 { status#lstatus with
105 multi_aliases = multi_aliases ; aliases = aliases} in
106 let new_status = status#set_lstatus new_status in
107 if new_aliases = [] then
110 add_lexicon_content (commands_of_aliases new_aliases) new_status
112 let rec eval_command ?(mode=L.WithPreferences) sstatus cmd =
114 let bmode = match mode with L.WithPreferences -> true | _ -> false in
115 Printf.eprintf "Include preferences: %b\n" bmode;
117 let status = sstatus#lstatus in
120 | L.Interpretation (loc, dsc, (symbol, args), cic_appl_pattern) ->
121 let rec disambiguate =
123 CicNotationPt.ApplPattern l ->
124 CicNotationPt.ApplPattern (List.map disambiguate l)
125 | CicNotationPt.VarPattern id
128 (function (CicNotationPt.IdentArg (_,id')) -> id'=id) args)
130 let item = DisambiguateTypes.Id id in
132 match DisambiguateTypes.Environment.find item status.aliases with
133 L.Ident_alias (_, uri) ->
135 CicNotationPt.NRefPattern
136 (NReference.reference_of_string uri)
138 NReference.IllFormedReference _ ->
139 CicNotationPt.UriPattern (UriManager.uri_of_string uri))
142 prerr_endline ("LexiconEngine.eval_command: domain item not found: " ^
143 (DisambiguateTypes.string_of_domain_item item));
144 dump_aliases prerr_endline "" sstatus;
150 (loc, dsc, (symbol, args), disambiguate cic_appl_pattern)
153 let notation_ids' = CicNotation.process_notation cmd in
155 { status with notation_ids = notation_ids' @ status.notation_ids } in
156 let sstatus = sstatus#set_lstatus status in
158 | L.Include (loc, baseuri, mode, fullpath) ->
159 let lexiconpath_rw, lexiconpath_r =
160 LibraryMisc.lexicon_file_of_baseuri
161 ~must_exist:false ~writable:true ~baseuri,
162 LibraryMisc.lexicon_file_of_baseuri
163 ~must_exist:false ~writable:false ~baseuri
166 if Sys.file_exists lexiconpath_rw then lexiconpath_rw else
167 if Sys.file_exists lexiconpath_r then lexiconpath_r else
168 raise (IncludedFileNotCompiled (lexiconpath_rw,fullpath))
170 let lexicon = LexiconMarshal.load_lexicon lexiconpath in
171 List.fold_left (eval_command ~mode) sstatus lexicon
172 | L.Alias (loc, spec) ->
174 (*CSC: Warning: this code should be factorized with the corresponding
175 code in DisambiguatePp *)
177 | L.Ident_alias (id,uri) ->
178 [DisambiguateTypes.Id id,spec]
179 | L.Symbol_alias (symb, instance, desc) ->
180 [DisambiguateTypes.Symbol (symb,instance),spec]
181 | L.Number_alias (instance,desc) ->
182 [DisambiguateTypes.Num instance,spec]
184 set_proof_aliases mode sstatus diff
185 | L.Interpretation (_, dsc, (symbol, _), _) as stm ->
186 let sstatus = add_lexicon_content [stm] sstatus in
189 [DisambiguateTypes.Symbol (symbol, 0),
190 L.Symbol_alias (symbol,0,dsc)]
192 DisambiguateChoices.Choice_not_found msg ->
193 prerr_endline (Lazy.force msg);
196 let sstatus = set_proof_aliases mode sstatus diff in
198 | L.Notation _ as stm ->
199 add_lexicon_content [stm] sstatus
201 let eval_command status cmd =
202 if !debug then dump_aliases prerr_endline "before eval_command" status;
203 let status = eval_command ?mode:None status cmd in
204 if !debug then dump_aliases prerr_endline "after eval_command" status;
207 let set_proof_aliases status aliases =
208 if !debug then dump_aliases prerr_endline "before set_proof_aliases" status;
209 let status = set_proof_aliases L.WithPreferences status aliases in
210 if !debug then dump_aliases prerr_endline "after set_proof_aliases" status;