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