]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/lexicon/lexiconEngine.ml
22d9b2f33a703ce7357f598cd16af11a62626220
[helm.git] / helm / software / 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: LexiconAst.alias_spec DisambiguateTypes.Environment.t;
38   multi_aliases: LexiconAst.alias_spec list DisambiguateTypes.Environment.t;
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 -> 
55         match cmd with
56         | LexiconAst.Alias _ 
57         | LexiconAst.Include _ 
58         | LexiconAst.Notation _ -> cmd :: (List.filter ((<>) cmd) acc)
59         | LexiconAst.Interpretation _ -> if List.exists ((=) cmd) acc then acc else cmd::acc)
60      cmds content
61   in
62 (*   prerr_endline ("new lexicon content: " ^ String.concat " " (List.map
63     LexiconAstPp.pp_command content')); *)
64   { status with lexicon_content_rev = content' }
65
66 let set_proof_aliases mode status new_aliases =
67  if mode = LexiconAst.WithoutPreferences then
68    status 
69  else
70    let commands_of_aliases =
71      List.map
72       (fun _,alias -> LexiconAst.Alias (HExtlib.dummy_floc, alias))
73    in
74    let aliases =
75     List.fold_left (fun acc (d,c) -> DisambiguateTypes.Environment.add d c acc)
76      status.aliases new_aliases in
77    let multi_aliases =
78     List.fold_left (fun acc (d,c) -> 
79       DisambiguateTypes.Environment.cons LexiconAst.description_of_alias 
80          d c acc)
81      status.multi_aliases new_aliases
82    in
83    let new_status =
84      { status with multi_aliases = multi_aliases ; aliases = aliases}
85    in
86    if new_aliases = [] then
87      new_status
88    else
89      let status = 
90        add_lexicon_content (commands_of_aliases new_aliases) new_status 
91      in
92      status
93
94
95 let rec eval_command ?(mode=LexiconAst.WithPreferences) status cmd =
96  let cmd =
97   match cmd with
98   | LexiconAst.Interpretation (loc, dsc, (symbol, args), cic_appl_pattern) ->
99      let rec disambiguate =
100       function
101          CicNotationPt.ApplPattern l ->
102           CicNotationPt.ApplPattern (List.map disambiguate l)
103        | CicNotationPt.VarPattern id
104           when not
105            (List.exists
106             (function (CicNotationPt.IdentArg (_,id')) -> id'=id) args)
107           ->
108            let item = DisambiguateTypes.Id id in
109             (try
110               let uri =
111                match DisambiguateTypes.Environment.find item status.aliases with
112                   LexiconAst.Ident_alias (_, uri)-> UriManager.uri_of_string uri
113                 | _ -> assert false
114               in
115                CicNotationPt.UriPattern uri
116              with Not_found -> 
117               prerr_endline ("Domain item not found: " ^ 
118                (DisambiguateTypes.string_of_domain_item item));
119               assert false)
120        | p -> p
121      in
122       LexiconAst.Interpretation
123        (loc, dsc, (symbol, args), disambiguate cic_appl_pattern)
124   | _-> cmd
125  in
126  !out cmd;
127  let notation_ids' = CicNotation.process_notation cmd in
128  let status =
129    { status with notation_ids = notation_ids' @ status.notation_ids } in
130   match cmd with
131   | LexiconAst.Include (loc, baseuri, mode, fullpath) ->
132      let lexiconpath_rw, lexiconpath_r = 
133        LibraryMisc.lexicon_file_of_baseuri 
134          ~must_exist:false ~writable:true ~baseuri,
135        LibraryMisc.lexicon_file_of_baseuri 
136          ~must_exist:false ~writable:false ~baseuri
137      in
138      let lexiconpath = 
139        if Sys.file_exists lexiconpath_rw then lexiconpath_rw else
140          if Sys.file_exists lexiconpath_r then lexiconpath_r else
141           raise (IncludedFileNotCompiled (lexiconpath_rw,fullpath))
142      in
143      let lexicon = LexiconMarshal.load_lexicon lexiconpath in
144      let status = List.fold_left (eval_command ~mode) status lexicon in
145      status
146   | LexiconAst.Alias (loc, spec) -> 
147      let diff =
148       (*CSC: Warning: this code should be factorized with the corresponding
149              code in DisambiguatePp *)
150       match spec with
151       | LexiconAst.Ident_alias (id,uri) -> 
152          [DisambiguateTypes.Id id,spec]
153       | LexiconAst.Symbol_alias (symb, instance, desc) ->
154          [DisambiguateTypes.Symbol (symb,instance),spec]
155       | LexiconAst.Number_alias (instance,desc) ->
156          [DisambiguateTypes.Num instance,spec]
157      in
158       set_proof_aliases mode status diff
159   | LexiconAst.Interpretation (_, dsc, (symbol, _), _) as stm ->
160       let status = add_lexicon_content [stm] status in
161       let diff =
162        try
163         [DisambiguateTypes.Symbol (symbol, 0),
164           LexiconAst.Symbol_alias (symbol,0,dsc)]
165        with
166         DisambiguateChoices.Choice_not_found msg ->
167           prerr_endline (Lazy.force msg);
168           assert false
169       in
170       let status = set_proof_aliases mode status diff in
171       status
172   | LexiconAst.Notation _ as stm ->
173       add_lexicon_content [stm] status
174
175 let eval_command = eval_command ?mode:None
176
177 let set_proof_aliases  = set_proof_aliases LexiconAst.WithPreferences
178