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