]> matita.cs.unibo.it Git - helm.git/blob - components/grafite_parser/grafiteDisambiguate.ml
types2006 patch
[helm.git] / components / grafite_parser / grafiteDisambiguate.ml
1 (* Copyright (C) 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 exception BaseUriNotSetYet
29
30 type tactic = 
31  (CicNotationPt.term, CicNotationPt.term, 
32   CicNotationPt.term GrafiteAst.reduction, string) 
33    GrafiteAst.tactic
34    
35 type lazy_tactic = 
36   (Cic.term, Cic.lazy_term, Cic.lazy_term GrafiteAst.reduction, string) 
37     GrafiteAst.tactic
38
39 let singleton = function
40   | [x], _ -> x
41   | _ -> assert false
42 ;;
43
44   (** @param term not meaningful when context is given *)
45 let disambiguate_term text prefix_len lexicon_status_ref context metasenv term =
46   let lexicon_status = !lexicon_status_ref in
47   let (diff, metasenv, cic, _) =
48     singleton
49       (GrafiteDisambiguator.disambiguate_term ~dbd:(LibraryDb.instance ())
50         ~aliases:lexicon_status.LexiconEngine.aliases
51         ~universe:(Some lexicon_status.LexiconEngine.multi_aliases)
52         ~context ~metasenv (text,prefix_len,term))
53   in
54   let lexicon_status = LexiconEngine.set_proof_aliases lexicon_status diff in
55   lexicon_status_ref := lexicon_status;
56   metasenv,cic
57 ;;
58
59   (** disambiguate_lazy_term (circa): term -> (unit -> status) * lazy_term
60    * rationale: lazy_term will be invoked in different context to obtain a term,
61    * each invocation will disambiguate the term and can add aliases. Once all
62    * disambiguations have been performed, the first returned function can be
63    * used to obtain the resulting aliases *)
64 let disambiguate_lazy_term text prefix_len lexicon_status_ref term =
65   (fun context metasenv ugraph ->
66     let lexicon_status = !lexicon_status_ref in
67     let (diff, metasenv, cic, ugraph) =
68       singleton
69         (GrafiteDisambiguator.disambiguate_term ~dbd:(LibraryDb.instance ())
70           ~initial_ugraph:ugraph ~aliases:lexicon_status.LexiconEngine.aliases
71           ~universe:(Some lexicon_status.LexiconEngine.multi_aliases)
72           ~context ~metasenv
73           (text,prefix_len,term)) in
74     let lexicon_status = LexiconEngine.set_proof_aliases lexicon_status diff in
75     lexicon_status_ref := lexicon_status;
76     cic, metasenv, ugraph)
77 ;;
78
79 let disambiguate_pattern 
80   text prefix_len lexicon_status_ref (wanted, hyp_paths, goal_path) 
81 =
82   let interp path = Disambiguate.interpretate_path [] path in
83   let goal_path = HExtlib.map_option interp goal_path in
84   let hyp_paths = List.map (fun (name, path) -> name, interp path) hyp_paths in
85   let wanted =
86    match wanted with
87       None -> None
88     | Some wanted ->
89        let wanted = 
90          disambiguate_lazy_term text prefix_len lexicon_status_ref wanted 
91        in
92        Some wanted
93   in
94   (wanted, hyp_paths, goal_path)
95 ;;
96
97 let disambiguate_reduction_kind text prefix_len lexicon_status_ref = function
98   | `Unfold (Some t) ->
99       let t = disambiguate_lazy_term text prefix_len lexicon_status_ref t in
100       `Unfold (Some t)
101   | `Demodulate
102   | `Normalize
103   | `Reduce
104   | `Simpl
105   | `Unfold None
106   | `Whd as kind -> kind
107 ;;
108
109 let disambiguate_tactic 
110   lexicon_status_ref context metasenv (text,prefix_len,tactic) 
111 =
112   let disambiguate_term = 
113     disambiguate_term text prefix_len lexicon_status_ref in
114   let disambiguate_pattern = 
115     disambiguate_pattern text prefix_len lexicon_status_ref in
116   let disambiguate_reduction_kind = 
117     disambiguate_reduction_kind text prefix_len lexicon_status_ref in
118   let disambiguate_lazy_term = 
119     disambiguate_lazy_term text prefix_len lexicon_status_ref in
120    match tactic with
121     | GrafiteAst.Absurd (loc, term) -> 
122         let metasenv,cic = disambiguate_term context metasenv term in
123         metasenv,GrafiteAst.Absurd (loc, cic)
124     | GrafiteAst.Apply (loc, term) ->
125         let metasenv,cic = disambiguate_term context metasenv term in
126         metasenv,GrafiteAst.Apply (loc, cic)
127     | GrafiteAst.Assumption loc ->
128         metasenv,GrafiteAst.Assumption loc
129     | GrafiteAst.Auto (loc,depth,width,paramodulation,full) ->
130         metasenv,GrafiteAst.Auto (loc,depth,width,paramodulation,full)
131     | GrafiteAst.Change (loc, pattern, with_what) -> 
132         let with_what = disambiguate_lazy_term with_what in
133         let pattern = disambiguate_pattern pattern in
134         metasenv,GrafiteAst.Change (loc, pattern, with_what)
135     | GrafiteAst.Clear (loc,id) ->
136         metasenv,GrafiteAst.Clear (loc,id)
137     | GrafiteAst.ClearBody (loc,id) ->
138        metasenv,GrafiteAst.ClearBody (loc,id)
139     | GrafiteAst.Constructor (loc,n) ->
140         metasenv,GrafiteAst.Constructor (loc,n)
141     | GrafiteAst.Contradiction loc ->
142         metasenv,GrafiteAst.Contradiction loc
143     | GrafiteAst.Cut (loc, ident, term) -> 
144         let metasenv,cic = disambiguate_term context metasenv term in
145         metasenv,GrafiteAst.Cut (loc, ident, cic)
146     | GrafiteAst.Decompose (loc, types, what, names) ->
147         let disambiguate (metasenv,types) = function
148            | GrafiteAst.Type _   -> assert false
149            | GrafiteAst.Ident id ->
150               (match
151                 disambiguate_term context metasenv
152                  (CicNotationPt.Ident(id, None))
153                with
154                 | metasenv,Cic.MutInd (uri, tyno, _) ->
155                     metasenv,(GrafiteAst.Type (uri, tyno) :: types)
156                 | _ ->
157                   raise (GrafiteDisambiguator.DisambiguationError
158                    (0,[[None,lazy "Decompose works only on inductive types"]])))
159         in
160         let metasenv,types =
161          List.fold_left disambiguate (metasenv,[]) types
162         in
163          metasenv,GrafiteAst.Decompose (loc, types, what, names)
164     | GrafiteAst.Discriminate (loc,term) ->
165         let metasenv,term = disambiguate_term context metasenv term in
166         metasenv,GrafiteAst.Discriminate(loc,term)
167     | GrafiteAst.Exact (loc, term) -> 
168         let metasenv,cic = disambiguate_term context metasenv term in
169         metasenv,GrafiteAst.Exact (loc, cic)
170     | GrafiteAst.Elim (loc, what, Some using, depth, idents) ->
171         let metasenv,what = disambiguate_term context metasenv what in
172         let metasenv,using = disambiguate_term context metasenv using in
173         metasenv,GrafiteAst.Elim (loc, what, Some using, depth, idents)
174     | GrafiteAst.Elim (loc, what, None, depth, idents) ->
175         let metasenv,what = disambiguate_term context metasenv what in
176         metasenv,GrafiteAst.Elim (loc, what, None, depth, idents)
177     | GrafiteAst.ElimType (loc, what, Some using, depth, idents) ->
178         let metasenv,what = disambiguate_term context metasenv what in
179         let metasenv,using = disambiguate_term context metasenv using in
180         metasenv,GrafiteAst.ElimType (loc, what, Some using, depth, idents)
181     | GrafiteAst.ElimType (loc, what, None, depth, idents) ->
182         let metasenv,what = disambiguate_term context metasenv what in
183         metasenv,GrafiteAst.ElimType (loc, what, None, depth, idents)
184     | GrafiteAst.Exists loc ->
185         metasenv,GrafiteAst.Exists loc 
186     | GrafiteAst.Fail loc ->
187         metasenv,GrafiteAst.Fail loc
188     | GrafiteAst.Fold (loc,red_kind, term, pattern) ->
189         let pattern = disambiguate_pattern pattern in
190         let term = disambiguate_lazy_term term in
191         let red_kind = disambiguate_reduction_kind red_kind in
192         metasenv,GrafiteAst.Fold (loc, red_kind, term, pattern)
193     | GrafiteAst.FwdSimpl (loc, hyp, names) ->
194        metasenv,GrafiteAst.FwdSimpl (loc, hyp, names)  
195     | GrafiteAst.Fourier loc ->
196        metasenv,GrafiteAst.Fourier loc
197     | GrafiteAst.Generalize (loc,pattern,ident) ->
198         let pattern = disambiguate_pattern pattern in
199         metasenv,GrafiteAst.Generalize (loc,pattern,ident)
200     | GrafiteAst.Goal (loc, g) ->
201         metasenv,GrafiteAst.Goal (loc, g)
202     | GrafiteAst.IdTac loc ->
203         metasenv,GrafiteAst.IdTac loc
204     | GrafiteAst.Injection (loc, term) ->
205         let metasenv,term = disambiguate_term context metasenv term in
206         metasenv,GrafiteAst.Injection (loc,term)
207     | GrafiteAst.Intros (loc, num, names) ->
208         metasenv,GrafiteAst.Intros (loc, num, names)
209     | GrafiteAst.Inversion (loc, term) ->
210        let metasenv,term = disambiguate_term context metasenv term in
211         metasenv,GrafiteAst.Inversion (loc, term)
212     | GrafiteAst.LApply (loc, depth, to_what, what, ident) ->
213        let f term to_what =
214           let metasenv,term = disambiguate_term context metasenv term in
215           term :: to_what
216        in
217        let to_what = List.fold_right f to_what [] in 
218        let metasenv,what = disambiguate_term context metasenv what in
219        metasenv,GrafiteAst.LApply (loc, depth, to_what, what, ident)
220     | GrafiteAst.Left loc ->
221        metasenv,GrafiteAst.Left loc
222     | GrafiteAst.LetIn (loc, term, name) ->
223         let metasenv,term = disambiguate_term context metasenv term in
224         metasenv,GrafiteAst.LetIn (loc,term,name)
225     | GrafiteAst.Reduce (loc, red_kind, pattern) ->
226         let pattern = disambiguate_pattern pattern in
227         let red_kind = disambiguate_reduction_kind red_kind in
228         metasenv,GrafiteAst.Reduce(loc, red_kind, pattern)
229     | GrafiteAst.Reflexivity loc ->
230         metasenv,GrafiteAst.Reflexivity loc
231     | GrafiteAst.Replace (loc, pattern, with_what) -> 
232         let pattern = disambiguate_pattern pattern in
233         let with_what = disambiguate_lazy_term with_what in
234         metasenv,GrafiteAst.Replace (loc, pattern, with_what)
235     | GrafiteAst.Rewrite (loc, dir, t, pattern) ->
236         let metasenv,term = disambiguate_term context metasenv t in
237         let pattern = disambiguate_pattern pattern in
238         metasenv,GrafiteAst.Rewrite (loc, dir, term, pattern)
239     | GrafiteAst.Right loc ->
240         metasenv,GrafiteAst.Right loc
241     | GrafiteAst.Ring loc ->
242         metasenv,GrafiteAst.Ring loc
243     | GrafiteAst.Split loc ->
244         metasenv,GrafiteAst.Split loc
245     | GrafiteAst.Symmetry loc ->
246         metasenv,GrafiteAst.Symmetry loc
247     | GrafiteAst.Transitivity (loc, term) -> 
248         let metasenv,cic = disambiguate_term context metasenv term in
249         metasenv,GrafiteAst.Transitivity (loc, cic)
250
251 let disambiguate_obj lexicon_status ~baseuri metasenv (text,prefix_len,obj) =
252   let uri =
253    match obj with
254     | CicNotationPt.Inductive (_,(name,_,_,_)::_)
255     | CicNotationPt.Record (_,name,_,_) ->
256        (match baseuri with
257          | Some baseuri ->
258             Some (UriManager.uri_of_string (baseuri ^ "/" ^ name ^ ".ind"))
259          | None -> raise BaseUriNotSetYet)
260     | CicNotationPt.Inductive _ -> assert false
261     | CicNotationPt.Theorem _ -> None in
262   let (diff, metasenv, cic, _) =
263     singleton
264       (GrafiteDisambiguator.disambiguate_obj ~dbd:(LibraryDb.instance ())
265         ~aliases:lexicon_status.LexiconEngine.aliases
266         ~universe:(Some lexicon_status.LexiconEngine.multi_aliases) ~uri 
267         (text,prefix_len,obj)) in
268   let lexicon_status = LexiconEngine.set_proof_aliases lexicon_status diff in
269   lexicon_status, metasenv, cic
270   
271 let disambiguate_command lexicon_status ~baseuri metasenv (text,prefix_len,cmd)=
272  match cmd with
273   | GrafiteAst.Coercion _
274   | GrafiteAst.Default _
275   | GrafiteAst.Drop _
276   | GrafiteAst.Include _
277   | GrafiteAst.Qed _
278   | GrafiteAst.Set _ as cmd ->
279       lexicon_status,metasenv,cmd
280   | GrafiteAst.Obj (loc,obj) ->
281       let lexicon_status,metasenv,obj =
282        disambiguate_obj lexicon_status ~baseuri metasenv (text,prefix_len,obj)in
283       lexicon_status, metasenv, GrafiteAst.Obj (loc,obj)
284
285 let disambiguate_macro 
286   lexicon_status_ref metasenv context (text,prefix_len, macro) 
287 =
288  let disambiguate_term = disambiguate_term text prefix_len lexicon_status_ref in
289   match macro with
290    | GrafiteAst.WMatch (loc,term) ->
291       let metasenv,term = disambiguate_term context metasenv term in
292        metasenv,GrafiteAst.WMatch (loc,term)
293    | GrafiteAst.WInstance (loc,term) ->
294       let metasenv,term = disambiguate_term context metasenv term in
295        metasenv,GrafiteAst.WInstance (loc,term)
296    | GrafiteAst.WElim (loc,term) ->
297       let metasenv,term = disambiguate_term context metasenv term in
298        metasenv,GrafiteAst.WElim (loc,term)
299    | GrafiteAst.WHint (loc,term) ->
300       let metasenv,term = disambiguate_term context metasenv term in
301        metasenv,GrafiteAst.WHint (loc,term)
302    | GrafiteAst.Check (loc,term) ->
303       let metasenv,term = disambiguate_term context metasenv term in
304        metasenv,GrafiteAst.Check (loc,term)
305    | GrafiteAst.Hint _
306    | GrafiteAst.WLocate _ as macro ->
307       metasenv,macro
308    | GrafiteAst.Quit _
309    | GrafiteAst.Print _
310    | GrafiteAst.Search_pat _
311    | GrafiteAst.Search_term _ -> assert false