]> matita.cs.unibo.it Git - helm.git/blob - components/lexicon/lexiconEngine.ml
tagged 0.5.0-rc1
[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 let out = ref ignore
29
30 let set_callback f = out := f
31
32 (* lexicon file name * ma file name *)
33 exception IncludedFileNotCompiled of string * string 
34 exception MetadataNotFound of string        (* file name *)
35
36 type status = {
37   aliases: DisambiguateTypes.environment;         (** disambiguation aliases *)
38   multi_aliases: DisambiguateTypes.multiple_environment;
39   lexicon_content_rev: LexiconMarshal.lexicon;
40   notation_ids: CicNotation.notation_id list;      (** in-scope notation ids *)
41 }
42
43 let initial_status = {
44   aliases = DisambiguateTypes.Environment.empty;
45   multi_aliases = DisambiguateTypes.Environment.empty;
46   lexicon_content_rev = [];
47   notation_ids = [];
48 }
49
50 let add_lexicon_content cmds status =
51   let content = status.lexicon_content_rev in
52   let content' =
53     List.fold_right
54      (fun cmd acc -> cmd :: (List.filter ((<>) cmd) acc))
55      cmds content
56   in
57 (*   prerr_endline ("new lexicon content: " ^ String.concat " " (List.map
58     LexiconAstPp.pp_command content')); *)
59   { status with lexicon_content_rev = content' }
60
61 let set_proof_aliases mode status new_aliases =
62  if mode = LexiconAst.WithoutPreferences then
63    status 
64  else
65    let commands_of_aliases =
66      List.map
67       (fun alias -> LexiconAst.Alias (HExtlib.dummy_floc, alias))
68    in
69    let aliases =
70     List.fold_left (fun acc (d,c) -> DisambiguateTypes.Environment.add d c acc)
71      status.aliases new_aliases in
72    let multi_aliases =
73     List.fold_left (fun acc (d,c) -> DisambiguateTypes.Environment.cons d c acc)
74      status.multi_aliases new_aliases in
75    let new_status =
76      { status with multi_aliases = multi_aliases ; aliases = aliases}
77    in
78    if new_aliases = [] then
79      new_status
80    else
81      let aliases = 
82        DisambiguatePp.aliases_of_domain_and_codomain_items_list new_aliases
83      in
84      let status = 
85        add_lexicon_content (commands_of_aliases aliases) new_status 
86      in
87      status
88
89
90 let rec eval_command ?(mode=LexiconAst.WithPreferences) status cmd =
91  !out cmd;
92  let notation_ids' = CicNotation.process_notation cmd in
93  let status =
94    { status with notation_ids = notation_ids' @ status.notation_ids } in
95   match cmd with
96   | LexiconAst.Include (loc, baseuri, mode, fullpath) ->
97      let lexiconpath_rw, lexiconpath_r = 
98        LibraryMisc.lexicon_file_of_baseuri 
99          ~must_exist:false ~writable:true ~baseuri,
100        LibraryMisc.lexicon_file_of_baseuri 
101          ~must_exist:false ~writable:false ~baseuri
102      in
103      let lexiconpath = 
104        if Sys.file_exists lexiconpath_rw then lexiconpath_rw else
105          if Sys.file_exists lexiconpath_r then lexiconpath_r else
106           raise (IncludedFileNotCompiled (lexiconpath_rw,fullpath))
107      in
108      let lexicon = LexiconMarshal.load_lexicon lexiconpath in
109      let status = List.fold_left (eval_command ~mode) status lexicon in
110      status
111   | LexiconAst.Alias (loc, spec) -> 
112      let diff =
113       (*CSC: Warning: this code should be factorized with the corresponding
114              code in DisambiguatePp *)
115       match spec with
116       | LexiconAst.Ident_alias (id,uri) -> 
117          [DisambiguateTypes.Id id,
118           (uri,(fun _ _ _-> CicUtil.term_of_uri(UriManager.uri_of_string uri)))]
119       | LexiconAst.Symbol_alias (symb, instance, desc) ->
120          [DisambiguateTypes.Symbol (symb,instance),
121           DisambiguateChoices.lookup_symbol_by_dsc symb desc]
122       | LexiconAst.Number_alias (instance,desc) ->
123          [DisambiguateTypes.Num instance,
124           DisambiguateChoices.lookup_num_by_dsc desc]
125      in
126       set_proof_aliases mode status diff
127   | LexiconAst.Interpretation (_, dsc, (symbol, _), cic_appl_pattern) as stm ->
128       let status = add_lexicon_content [stm] status in
129       let diff =
130        [DisambiguateTypes.Symbol (symbol, 0),
131          DisambiguateChoices.lookup_symbol_by_dsc symbol dsc]
132       in
133       let status = set_proof_aliases mode status diff in
134       status
135   | LexiconAst.Notation _ as stm -> add_lexicon_content [stm] status
136
137 let eval_command = eval_command ?mode:None
138
139 let set_proof_aliases  = set_proof_aliases LexiconAst.WithPreferences
140