]> matita.cs.unibo.it Git - helm.git/blob - components/lexicon/lexiconEngine.ml
a0792d2281fd09bfc4ecf2fa0d05beb7ddf23327
[helm.git] / components / lexicon / lexiconEngine.ml
1 (* Copyright (C) 2004-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 let out = ref ignore
29
30 let set_callback f = out := f
31
32 (* lexicon file name * ma file name *)
33 exception IncludedFileNotCompiled of string * string 
34 exception MetadataNotFound of string        (* file name *)
35
36 type status = {
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 *)
41   metadata: LibraryNoDb.metadata list;
42 }
43
44 let initial_status = {
45   aliases = DisambiguateTypes.Environment.empty;
46   multi_aliases = DisambiguateTypes.Environment.empty;
47   lexicon_content_rev = [];
48   notation_ids = [];
49   metadata = [];
50 }
51
52 let add_lexicon_content cmds status =
53   let content = status.lexicon_content_rev in
54   let content' =
55     List.fold_right
56      (fun cmd acc -> cmd :: (List.filter ((<>) cmd) acc))
57      cmds content
58   in
59 (*   prerr_endline ("new lexicon content: " ^ String.concat " " (List.map
60     LexiconAstPp.pp_command content')); *)
61   { status with lexicon_content_rev = content' }
62
63 let add_metadata new_metadata status =
64   if Helm_registry.get_bool "db.nodb" then
65     let metadata = status.metadata in
66     let metadata' =
67       List.fold_left
68         (fun acc m ->
69           match m with
70           | LibraryNoDb.Dependency buri ->
71               if List.exists (LibraryNoDb.eq_metadata m) metadata
72               then acc
73               else m :: acc)
74         metadata new_metadata
75     in
76     { status with metadata = metadata' }
77   else
78     status
79
80 let set_proof_aliases mode status new_aliases =
81  if mode = LexiconAst.WithoutPreferences then
82    status 
83  else
84    let commands_of_aliases =
85      List.map
86       (fun alias -> LexiconAst.Alias (HExtlib.dummy_floc, alias))
87    in
88    let deps_of_aliases =
89      HExtlib.filter_map
90       (function
91       | LexiconAst.Ident_alias (_, suri) ->
92           let buri = UriManager.buri_of_uri (UriManager.uri_of_string suri) in
93           Some (LibraryNoDb.Dependency buri)
94       | _ -> None)
95    in
96    let aliases =
97     List.fold_left (fun acc (d,c) -> DisambiguateTypes.Environment.add d c acc)
98      status.aliases new_aliases in
99    let multi_aliases =
100     List.fold_left (fun acc (d,c) -> DisambiguateTypes.Environment.cons d c acc)
101      status.multi_aliases new_aliases in
102    let new_status =
103      { status with multi_aliases = multi_aliases ; aliases = aliases}
104    in
105    if new_aliases = [] then
106      new_status
107    else
108      let aliases = 
109        DisambiguatePp.aliases_of_domain_and_codomain_items_list new_aliases
110      in
111      let status = 
112        add_lexicon_content (commands_of_aliases aliases) new_status 
113      in
114      let status = add_metadata (deps_of_aliases aliases) status in
115      status
116
117
118 let rec eval_command ?(mode=LexiconAst.WithPreferences) status cmd =
119  !out cmd;
120  let notation_ids' = CicNotation.process_notation cmd in
121  let status =
122    { status with notation_ids = notation_ids' @ status.notation_ids } in
123   match cmd with
124   | LexiconAst.Include (loc, baseuri, mode, fullpath) ->
125      let lexiconpath_rw, lexiconpath_r = 
126        LibraryMisc.lexicon_file_of_baseuri 
127          ~must_exist:false ~writable:true ~baseuri,
128        LibraryMisc.lexicon_file_of_baseuri 
129          ~must_exist:false ~writable:false ~baseuri
130      in
131      let lexiconpath = 
132        if Sys.file_exists lexiconpath_rw then lexiconpath_rw else
133          if Sys.file_exists lexiconpath_r then lexiconpath_r else
134           raise (IncludedFileNotCompiled (lexiconpath_rw,fullpath))
135      in
136      let lexicon = LexiconMarshal.load_lexicon lexiconpath in
137      let status = List.fold_left (eval_command ~mode) status lexicon in
138       if Helm_registry.get_bool "db.nodb" then
139        let metadatapath_rw, metadatapath_r = 
140          LibraryMisc.metadata_file_of_baseuri 
141            ~must_exist:false ~baseuri ~writable:true,
142          LibraryMisc.metadata_file_of_baseuri 
143            ~must_exist:false ~baseuri ~writable:false
144        in
145        let metadatapath =
146         if Sys.file_exists metadatapath_rw then metadatapath_rw else
147          if Sys.file_exists metadatapath_r then metadatapath_r else
148            raise (MetadataNotFound metadatapath_rw)
149        in
150          add_metadata (LibraryNoDb.load_metadata ~fname:metadatapath) status
151       else
152          status
153   | LexiconAst.Alias (loc, spec) -> 
154      let diff =
155       (*CSC: Warning: this code should be factorized with the corresponding
156              code in DisambiguatePp *)
157       match spec with
158       | LexiconAst.Ident_alias (id,uri) -> 
159          [DisambiguateTypes.Id id,
160           (uri,(fun _ _ _-> CicUtil.term_of_uri(UriManager.uri_of_string uri)))]
161       | LexiconAst.Symbol_alias (symb, instance, desc) ->
162          [DisambiguateTypes.Symbol (symb,instance),
163           DisambiguateChoices.lookup_symbol_by_dsc symb desc]
164       | LexiconAst.Number_alias (instance,desc) ->
165          [DisambiguateTypes.Num instance,
166           DisambiguateChoices.lookup_num_by_dsc desc]
167      in
168       set_proof_aliases mode status diff
169   | LexiconAst.Interpretation (_, dsc, (symbol, _), cic_appl_pattern) as stm ->
170       let status = add_lexicon_content [stm] status in
171       let uris =
172         List.map
173           (fun uri -> LibraryNoDb.Dependency (UriManager.buri_of_uri uri))
174           (CicNotationUtil.find_appl_pattern_uris cic_appl_pattern)
175       in
176       let diff =
177        [DisambiguateTypes.Symbol (symbol, 0),
178          DisambiguateChoices.lookup_symbol_by_dsc symbol dsc]
179       in
180       let status = set_proof_aliases mode status diff in
181       let status = add_metadata uris status in
182       status
183   | LexiconAst.Notation _ as stm -> add_lexicon_content [stm] status
184
185 let eval_command = eval_command ?mode:None
186
187 let set_proof_aliases  = set_proof_aliases LexiconAst.WithPreferences
188