]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/lexicon/lexiconEngine.ml
1) grafiteWalker removed
[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 module L = LexiconAst
29
30 let debug = ref false
31
32 (* lexicon file name * ma file name *)
33 exception IncludedFileNotCompiled of string * string 
34 exception MetadataNotFound of string        (* file name *)
35
36 type lexicon_status = {
37   aliases: L.alias_spec DisambiguateTypes.Environment.t;
38   multi_aliases: L.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 class status =
51  object
52   val lstatus = initial_status
53   method lstatus = lstatus
54   method set_lstatus v = {< lstatus = v >}
55  end
56
57 let dump_aliases out msg status =
58    out (if msg = "" then "aliases dump:" else msg ^ ": aliases dump:");
59    DisambiguateTypes.Environment.iter
60       (fun _ x -> out (LexiconAstPp.pp_alias x))
61       status#lstatus.aliases
62    
63 let add_lexicon_content cmds status =
64   let content = status#lstatus.lexicon_content_rev in
65   let content' =
66     List.fold_right
67      (fun cmd acc -> 
68         match cmd with
69         | L.Alias _ 
70         | L.Include _ 
71         | L.Notation _ -> cmd :: (List.filter ((<>) cmd) acc)
72         | L.Interpretation _ -> if List.exists ((=) cmd) acc then acc else cmd::acc)
73      cmds content
74   in
75 (*   
76   prerr_endline ("new lexicon content: " ^ 
77      String.concat "; " (List.map LexiconAstPp.pp_command content')
78   );
79 *)
80   status#set_lstatus
81    { status#lstatus with lexicon_content_rev = content' }
82
83 let set_proof_aliases mode status new_aliases =
84  if mode = L.WithoutPreferences then
85    status 
86  else
87    let commands_of_aliases =
88      List.map
89       (fun _,alias -> L.Alias (HExtlib.dummy_floc, alias))
90    in
91    let aliases =
92     List.fold_left (fun acc (d,c) -> DisambiguateTypes.Environment.add d c acc)
93      status#lstatus.aliases new_aliases in
94    let multi_aliases =
95     List.fold_left (fun acc (d,c) -> 
96       DisambiguateTypes.Environment.cons L.description_of_alias 
97          d c acc)
98      status#lstatus.multi_aliases new_aliases
99    in
100    let new_status =
101      { status#lstatus with
102         multi_aliases = multi_aliases ; aliases = aliases} in
103    let new_status = status#set_lstatus new_status in
104     if new_aliases = [] then
105       new_status
106     else
107       add_lexicon_content (commands_of_aliases new_aliases) new_status 
108
109 let rec eval_command ?(mode=L.WithPreferences) sstatus cmd =
110 (*
111  let bmode = match mode with L.WithPreferences -> true | _ -> false in
112  Printf.eprintf "Include preferences: %b\n" bmode;
113 *) 
114  let status = sstatus#lstatus in
115  let cmd =
116   match cmd with
117   | L.Interpretation (loc, dsc, (symbol, args), cic_appl_pattern) ->
118      let rec disambiguate =
119       function
120          CicNotationPt.ApplPattern l ->
121           CicNotationPt.ApplPattern (List.map disambiguate l)
122        | CicNotationPt.VarPattern id
123           when not
124            (List.exists
125             (function (CicNotationPt.IdentArg (_,id')) -> id'=id) args)
126           ->
127            let item = DisambiguateTypes.Id id in
128             begin try
129               match DisambiguateTypes.Environment.find item status.aliases with
130                  L.Ident_alias (_, uri) ->
131                   (try
132                     CicNotationPt.NRefPattern
133                      (NReference.reference_of_string uri)
134                    with
135                     NReference.IllFormedReference _ ->
136                      CicNotationPt.UriPattern (UriManager.uri_of_string uri))
137                | _ -> assert false
138              with Not_found -> 
139               prerr_endline ("LexiconEngine.eval_command: domain item not found: " ^ 
140                (DisambiguateTypes.string_of_domain_item item));
141               dump_aliases prerr_endline "" sstatus;
142               assert false
143             end
144        | p -> p
145      in
146       L.Interpretation
147        (loc, dsc, (symbol, args), disambiguate cic_appl_pattern)
148   | _-> cmd
149  in
150  let notation_ids' = CicNotation.process_notation cmd in
151  let status =
152    { status with notation_ids = notation_ids' @ status.notation_ids } in
153  let sstatus = sstatus#set_lstatus status in
154   match cmd with
155   | L.Include (loc, baseuri, mode, fullpath) ->
156      let lexiconpath_rw, lexiconpath_r = 
157        LibraryMisc.lexicon_file_of_baseuri 
158          ~must_exist:false ~writable:true ~baseuri,
159        LibraryMisc.lexicon_file_of_baseuri 
160          ~must_exist:false ~writable:false ~baseuri
161      in
162      let lexiconpath = 
163        if Sys.file_exists lexiconpath_rw then lexiconpath_rw else
164          if Sys.file_exists lexiconpath_r then lexiconpath_r else
165           raise (IncludedFileNotCompiled (lexiconpath_rw,fullpath))
166      in
167      let lexicon = LexiconMarshal.load_lexicon lexiconpath in
168       List.fold_left (eval_command ~mode) sstatus lexicon
169   | L.Alias (loc, spec) -> 
170      let diff =
171       (*CSC: Warning: this code should be factorized with the corresponding
172              code in DisambiguatePp *)
173       match spec with
174       | L.Ident_alias (id,uri) -> 
175          [DisambiguateTypes.Id id,spec]
176       | L.Symbol_alias (symb, instance, desc) ->
177          [DisambiguateTypes.Symbol (symb,instance),spec]
178       | L.Number_alias (instance,desc) ->
179          [DisambiguateTypes.Num instance,spec]
180      in
181       set_proof_aliases mode sstatus diff
182   | L.Interpretation (_, dsc, (symbol, _), _) as stm ->
183       let sstatus = add_lexicon_content [stm] sstatus in
184       let diff =
185        try
186         [DisambiguateTypes.Symbol (symbol, 0),
187           L.Symbol_alias (symbol,0,dsc)]
188        with
189         DisambiguateChoices.Choice_not_found msg ->
190           prerr_endline (Lazy.force msg);
191           assert false
192       in
193       let sstatus = set_proof_aliases mode sstatus diff in
194       sstatus
195   | L.Notation _ as stm ->
196       add_lexicon_content [stm] sstatus
197
198 let eval_command status cmd = 
199    if !debug then dump_aliases prerr_endline "before eval_command" status;
200    let status = eval_command ?mode:None status cmd in
201    if !debug then dump_aliases prerr_endline "after eval_command" status;
202    status
203
204 let set_proof_aliases status aliases =
205    if !debug then dump_aliases prerr_endline "before set_proof_aliases" status;
206    let status = set_proof_aliases L.WithPreferences status aliases in
207    if !debug then dump_aliases prerr_endline "after set_proof_aliases" status;
208    status