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