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