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