]> matita.cs.unibo.it Git - helm.git/blob - components/lexicon/lexiconEngine.ml
4adfe624b2039c4042e925a6ed2641e1c0b37c98
[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 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 status new_aliases =
76  let commands_of_aliases =
77    List.map
78     (fun alias -> LexiconAst.Alias (HExtlib.dummy_floc, alias))
79  in
80  let deps_of_aliases =
81    HExtlib.filter_map
82     (function
83     | LexiconAst.Ident_alias (_, suri) ->
84         let buri = UriManager.buri_of_uri (UriManager.uri_of_string suri) in
85         Some (LibraryNoDb.Dependency buri)
86     | _ -> None)
87  in
88  let aliases =
89   List.fold_left (fun acc (d,c) -> DisambiguateTypes.Environment.add d c acc)
90    status.aliases new_aliases in
91  let multi_aliases =
92   List.fold_left (fun acc (d,c) -> DisambiguateTypes.Environment.cons d c acc)
93    status.multi_aliases new_aliases in
94  let new_status =
95    { status with multi_aliases = multi_aliases ; aliases = aliases}
96  in
97  if new_aliases = [] then
98    new_status
99  else
100    let aliases = 
101      DisambiguatePp.aliases_of_domain_and_codomain_items_list new_aliases
102    in
103    let status = add_lexicon_content (commands_of_aliases aliases) new_status in
104    let status = add_metadata (deps_of_aliases aliases) status in
105    status
106
107 let rec eval_command status cmd =
108  let notation_ids' = CicNotation.process_notation cmd in
109  let status =
110    { status with notation_ids = notation_ids' @ status.notation_ids } in
111  let basedir = Helm_registry.get "matita.basedir" in
112   match cmd with
113   | LexiconAst.Include (loc, baseuri) ->
114      let lexiconpath = LibraryMisc.lexicon_file_of_baseuri ~basedir ~baseuri in
115      if not (Sys.file_exists lexiconpath) then
116        raise (IncludedFileNotCompiled lexiconpath);
117      let lexicon = LexiconMarshal.load_lexicon lexiconpath in
118      let status = List.fold_left eval_command status lexicon in
119       if Helm_registry.get_bool "db.nodb" then
120        let metadatapath = LibraryMisc.metadata_file_of_baseuri ~basedir ~baseuri in
121         if not (Sys.file_exists metadatapath) then
122           raise (MetadataNotFound metadatapath)
123         else
124           add_metadata (LibraryNoDb.load_metadata ~fname:metadatapath) status
125       else
126          status
127   | LexiconAst.Alias (loc, spec) -> 
128      let diff =
129       (*CSC: Warning: this code should be factorized with the corresponding
130              code in DisambiguatePp *)
131       match spec with
132       | LexiconAst.Ident_alias (id,uri) -> 
133          [DisambiguateTypes.Id id,
134           (uri,(fun _ _ _-> CicUtil.term_of_uri(UriManager.uri_of_string uri)))]
135       | LexiconAst.Symbol_alias (symb, instance, desc) ->
136          [DisambiguateTypes.Symbol (symb,instance),
137           DisambiguateChoices.lookup_symbol_by_dsc symb desc]
138       | LexiconAst.Number_alias (instance,desc) ->
139          [DisambiguateTypes.Num instance,
140           DisambiguateChoices.lookup_num_by_dsc desc]
141      in
142       set_proof_aliases status diff
143   | LexiconAst.Interpretation (_, dsc, (symbol, _), cic_appl_pattern) as stm ->
144       let status = add_lexicon_content [stm] status in
145       let uris =
146         List.map
147           (fun uri -> LibraryNoDb.Dependency (UriManager.buri_of_uri uri))
148           (CicNotationUtil.find_appl_pattern_uris cic_appl_pattern)
149       in
150       let diff =
151        [DisambiguateTypes.Symbol (symbol, 0),
152          DisambiguateChoices.lookup_symbol_by_dsc symbol dsc]
153       in
154       let status = set_proof_aliases status diff in
155       let status = add_metadata uris status in
156       status
157   | LexiconAst.Notation _ as stm -> add_lexicon_content [stm] status
158