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/
30 let set_callback f = out := f
32 (* lexicon file name * ma file name *)
33 exception IncludedFileNotCompiled of string * string
34 exception MetadataNotFound of string (* file name *)
37 aliases: DisambiguateTypes.environment; (** disambiguation aliases *)
38 multi_aliases: DisambiguateTypes.multiple_environment;
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 = [];
50 let add_lexicon_content cmds status =
51 let content = status.lexicon_content_rev in
57 | LexiconAst.Include _
58 | LexiconAst.Notation _ -> cmd :: (List.filter ((<>) cmd) acc)
59 | LexiconAst.Interpretation _ -> if List.exists ((=) cmd) acc then acc else cmd::acc)
62 (* prerr_endline ("new lexicon content: " ^ String.concat " " (List.map
63 LexiconAstPp.pp_command content')); *)
64 { status with lexicon_content_rev = content' }
66 let set_proof_aliases mode status new_aliases =
67 if mode = LexiconAst.WithoutPreferences then
70 let commands_of_aliases =
72 (fun alias -> LexiconAst.Alias (HExtlib.dummy_floc, alias))
75 List.fold_left (fun acc (d,c) -> DisambiguateTypes.Environment.add d c acc)
76 status.aliases new_aliases in
78 List.fold_left (fun acc (d,c) -> DisambiguateTypes.Environment.cons d c acc)
79 status.multi_aliases new_aliases in
81 { status with multi_aliases = multi_aliases ; aliases = aliases}
83 if new_aliases = [] then
87 DisambiguatePp.aliases_of_domain_and_codomain_items_list new_aliases
90 add_lexicon_content (commands_of_aliases aliases) new_status
95 let rec eval_command ?(mode=LexiconAst.WithPreferences) status cmd =
98 | LexiconAst.Interpretation (loc, dsc, (symbol, args), cic_appl_pattern) ->
99 let rec disambiguate =
101 CicNotationPt.ApplPattern l ->
102 CicNotationPt.ApplPattern (List.map disambiguate l)
103 | CicNotationPt.VarPattern id
106 (function (CicNotationPt.IdentArg (_,id')) -> id'=id) args)
108 let item = DisambiguateTypes.Id id in
111 snd (DisambiguateTypes.Environment.find item status.aliases)
112 status.aliases "" [] in
113 let uri = CicUtil.uri_of_term t in
114 CicNotationPt.UriPattern uri
116 prerr_endline ("Domain item not found: " ^
117 (DisambiguateTypes.string_of_domain_item item));
121 LexiconAst.Interpretation
122 (loc, dsc, (symbol, args), disambiguate cic_appl_pattern)
126 let notation_ids' = CicNotation.process_notation cmd in
128 { status with notation_ids = notation_ids' @ status.notation_ids } in
130 | LexiconAst.Include (loc, baseuri, mode, fullpath) ->
131 let lexiconpath_rw, lexiconpath_r =
132 LibraryMisc.lexicon_file_of_baseuri
133 ~must_exist:false ~writable:true ~baseuri,
134 LibraryMisc.lexicon_file_of_baseuri
135 ~must_exist:false ~writable:false ~baseuri
138 if Sys.file_exists lexiconpath_rw then lexiconpath_rw else
139 if Sys.file_exists lexiconpath_r then lexiconpath_r else
140 raise (IncludedFileNotCompiled (lexiconpath_rw,fullpath))
142 let lexicon = LexiconMarshal.load_lexicon lexiconpath in
143 let status = List.fold_left (eval_command ~mode) status lexicon in
145 | LexiconAst.Alias (loc, spec) ->
147 (*CSC: Warning: this code should be factorized with the corresponding
148 code in DisambiguatePp *)
150 | LexiconAst.Ident_alias (id,uri) ->
151 [DisambiguateTypes.Id id,
152 (uri,(fun _ _ _-> CicUtil.term_of_uri(UriManager.uri_of_string uri)))]
153 | LexiconAst.Symbol_alias (symb, instance, desc) ->
154 [DisambiguateTypes.Symbol (symb,instance),
155 DisambiguateChoices.lookup_symbol_by_dsc symb desc]
156 | LexiconAst.Number_alias (instance,desc) ->
157 [DisambiguateTypes.Num instance,
158 DisambiguateChoices.lookup_num_by_dsc desc]
160 set_proof_aliases mode status diff
161 | LexiconAst.Interpretation (_, dsc, (symbol, _), _) as stm ->
162 let status = add_lexicon_content [stm] status in
165 [DisambiguateTypes.Symbol (symbol, 0),
166 DisambiguateChoices.lookup_symbol_by_dsc symbol dsc]
168 DisambiguateChoices.Choice_not_found msg ->
169 prerr_endline (Lazy.force msg);
172 let status = set_proof_aliases mode status diff in
174 | LexiconAst.Notation _ as stm ->
175 add_lexicon_content [stm] status
177 let eval_command = eval_command ?mode:None
179 let set_proof_aliases = set_proof_aliases LexiconAst.WithPreferences