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