]> matita.cs.unibo.it Git - helm.git/blob - components/grafite_parser/grafiteDisambiguate.ml
Demodulate used to be a reduction_kind and it used to take a ~pattern.
[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   | `Normalize
102   | `Reduce
103   | `Simpl
104   | `Unfold None
105   | `Whd as kind -> kind
106 ;;
107
108 let disambiguate_tactic 
109   lexicon_status_ref context metasenv (text,prefix_len,tactic) 
110 =
111   let disambiguate_term = 
112     disambiguate_term text prefix_len lexicon_status_ref in
113   let disambiguate_pattern = 
114     disambiguate_pattern text prefix_len lexicon_status_ref in
115   let disambiguate_reduction_kind = 
116     disambiguate_reduction_kind text prefix_len lexicon_status_ref in
117   let disambiguate_lazy_term = 
118     disambiguate_lazy_term text prefix_len lexicon_status_ref in
119    match tactic with
120     | GrafiteAst.Absurd (loc, term) -> 
121         let metasenv,cic = disambiguate_term context metasenv term in
122         metasenv,GrafiteAst.Absurd (loc, cic)
123     | GrafiteAst.Apply (loc, term) ->
124         let metasenv,cic = disambiguate_term context metasenv term in
125         metasenv,GrafiteAst.Apply (loc, cic)
126     | GrafiteAst.ApplyS (loc, term) ->
127         let metasenv,cic = disambiguate_term context metasenv term in
128         metasenv,GrafiteAst.ApplyS (loc, cic)
129     | GrafiteAst.Assumption loc ->
130         metasenv,GrafiteAst.Assumption loc
131     | GrafiteAst.Auto (loc,params) ->
132         metasenv,GrafiteAst.Auto (loc,params)
133     | GrafiteAst.Change (loc, pattern, with_what) -> 
134         let with_what = disambiguate_lazy_term with_what in
135         let pattern = disambiguate_pattern pattern in
136         metasenv,GrafiteAst.Change (loc, pattern, with_what)
137     | GrafiteAst.Clear (loc,id) ->
138         metasenv,GrafiteAst.Clear (loc,id)
139     | GrafiteAst.ClearBody (loc,id) ->
140        metasenv,GrafiteAst.ClearBody (loc,id)
141     | GrafiteAst.Constructor (loc,n) ->
142         metasenv,GrafiteAst.Constructor (loc,n)
143     | GrafiteAst.Contradiction loc ->
144         metasenv,GrafiteAst.Contradiction loc
145     | GrafiteAst.Cut (loc, ident, term) -> 
146         let metasenv,cic = disambiguate_term context metasenv term in
147         metasenv,GrafiteAst.Cut (loc, ident, cic)
148     | GrafiteAst.Decompose (loc, types, what, names) ->
149         let disambiguate (metasenv,types) = function
150            | GrafiteAst.Type _   -> assert false
151            | GrafiteAst.Ident id ->
152               (match
153                 disambiguate_term context metasenv
154                  (CicNotationPt.Ident(id, None))
155                with
156                 | metasenv,Cic.MutInd (uri, tyno, _) ->
157                     metasenv,(GrafiteAst.Type (uri, tyno) :: types)
158                 | _ ->
159                   raise (GrafiteDisambiguator.DisambiguationError
160                    (0,[[None,lazy "Decompose works only on inductive types"]])))
161         in
162         let metasenv,types =
163          List.fold_left disambiguate (metasenv,[]) types
164         in
165          metasenv,GrafiteAst.Decompose (loc, types, what, names)
166     | GrafiteAst.Demodulate loc ->
167         metasenv,GrafiteAst.Demodulate loc
168     | GrafiteAst.Discriminate (loc,term) ->
169         let metasenv,term = disambiguate_term context metasenv term in
170         metasenv,GrafiteAst.Discriminate(loc,term)
171     | GrafiteAst.Exact (loc, term) -> 
172         let metasenv,cic = disambiguate_term context metasenv term in
173         metasenv,GrafiteAst.Exact (loc, cic)
174     | GrafiteAst.Elim (loc, what, Some using, depth, idents) ->
175         let metasenv,what = disambiguate_term context metasenv what in
176         let metasenv,using = disambiguate_term context metasenv using in
177         metasenv,GrafiteAst.Elim (loc, what, Some using, depth, idents)
178     | GrafiteAst.Elim (loc, what, None, depth, idents) ->
179         let metasenv,what = disambiguate_term context metasenv what in
180         metasenv,GrafiteAst.Elim (loc, what, None, depth, idents)
181     | GrafiteAst.ElimType (loc, what, Some using, depth, idents) ->
182         let metasenv,what = disambiguate_term context metasenv what in
183         let metasenv,using = disambiguate_term context metasenv using in
184         metasenv,GrafiteAst.ElimType (loc, what, Some using, depth, idents)
185     | GrafiteAst.ElimType (loc, what, None, depth, idents) ->
186         let metasenv,what = disambiguate_term context metasenv what in
187         metasenv,GrafiteAst.ElimType (loc, what, None, depth, idents)
188     | GrafiteAst.Exists loc ->
189         metasenv,GrafiteAst.Exists loc 
190     | GrafiteAst.Fail loc ->
191         metasenv,GrafiteAst.Fail loc
192     | GrafiteAst.Fold (loc,red_kind, term, pattern) ->
193         let pattern = disambiguate_pattern pattern in
194         let term = disambiguate_lazy_term term in
195         let red_kind = disambiguate_reduction_kind red_kind in
196         metasenv,GrafiteAst.Fold (loc, red_kind, term, pattern)
197     | GrafiteAst.FwdSimpl (loc, hyp, names) ->
198        metasenv,GrafiteAst.FwdSimpl (loc, hyp, names)  
199     | GrafiteAst.Fourier loc ->
200        metasenv,GrafiteAst.Fourier loc
201     | GrafiteAst.Generalize (loc,pattern,ident) ->
202         let pattern = disambiguate_pattern pattern in
203         metasenv,GrafiteAst.Generalize (loc,pattern,ident)
204     | GrafiteAst.Goal (loc, g) ->
205         metasenv,GrafiteAst.Goal (loc, g)
206     | GrafiteAst.IdTac loc ->
207         metasenv,GrafiteAst.IdTac loc
208     | GrafiteAst.Injection (loc, term) ->
209         let metasenv,term = disambiguate_term context metasenv term in
210         metasenv,GrafiteAst.Injection (loc,term)
211     | GrafiteAst.Intros (loc, num, names) ->
212         metasenv,GrafiteAst.Intros (loc, num, names)
213     | GrafiteAst.Inversion (loc, term) ->
214        let metasenv,term = disambiguate_term context metasenv term in
215         metasenv,GrafiteAst.Inversion (loc, term)
216     | GrafiteAst.LApply (loc, linear, depth, to_what, what, ident) ->
217        let f term to_what =
218           let metasenv,term = disambiguate_term context metasenv term in
219           term :: to_what
220        in
221        let to_what = List.fold_right f to_what [] in 
222        let metasenv,what = disambiguate_term context metasenv what in
223        metasenv,GrafiteAst.LApply (loc, linear, depth, to_what, what, ident)
224     | GrafiteAst.Left loc ->
225        metasenv,GrafiteAst.Left loc
226     | GrafiteAst.LetIn (loc, term, name) ->
227         let metasenv,term = disambiguate_term context metasenv term in
228         metasenv,GrafiteAst.LetIn (loc,term,name)
229     | GrafiteAst.Reduce (loc, red_kind, pattern) ->
230         let pattern = disambiguate_pattern pattern in
231         let red_kind = disambiguate_reduction_kind red_kind in
232         metasenv,GrafiteAst.Reduce(loc, red_kind, pattern)
233     | GrafiteAst.Reflexivity loc ->
234         metasenv,GrafiteAst.Reflexivity loc
235     | GrafiteAst.Replace (loc, pattern, with_what) -> 
236         let pattern = disambiguate_pattern pattern in
237         let with_what = disambiguate_lazy_term with_what in
238         metasenv,GrafiteAst.Replace (loc, pattern, with_what)
239     | GrafiteAst.Rewrite (loc, dir, t, pattern) ->
240         let metasenv,term = disambiguate_term context metasenv t in
241         let pattern = disambiguate_pattern pattern in
242         metasenv,GrafiteAst.Rewrite (loc, dir, term, pattern)
243     | GrafiteAst.Right loc ->
244         metasenv,GrafiteAst.Right loc
245     | GrafiteAst.Ring loc ->
246         metasenv,GrafiteAst.Ring loc
247     | GrafiteAst.Split loc ->
248         metasenv,GrafiteAst.Split loc
249     | GrafiteAst.Symmetry loc ->
250         metasenv,GrafiteAst.Symmetry loc
251     | GrafiteAst.Transitivity (loc, term) -> 
252         let metasenv,cic = disambiguate_term context metasenv term in
253         metasenv,GrafiteAst.Transitivity (loc, cic)
254
255 let disambiguate_obj lexicon_status ~baseuri metasenv (text,prefix_len,obj) =
256   let uri =
257    match obj with
258     | CicNotationPt.Inductive (_,(name,_,_,_)::_)
259     | CicNotationPt.Record (_,name,_,_) ->
260        (match baseuri with
261          | Some baseuri ->
262             Some (UriManager.uri_of_string (baseuri ^ "/" ^ name ^ ".ind"))
263          | None -> raise BaseUriNotSetYet)
264     | CicNotationPt.Inductive _ -> assert false
265     | CicNotationPt.Theorem _ -> None in
266   let (diff, metasenv, cic, _) =
267     singleton
268       (GrafiteDisambiguator.disambiguate_obj ~dbd:(LibraryDb.instance ())
269         ~aliases:lexicon_status.LexiconEngine.aliases
270         ~universe:(Some lexicon_status.LexiconEngine.multi_aliases) ~uri 
271         (text,prefix_len,obj)) in
272   let lexicon_status = LexiconEngine.set_proof_aliases lexicon_status diff in
273   lexicon_status, metasenv, cic
274   
275 let disambiguate_command lexicon_status ~baseuri metasenv (text,prefix_len,cmd)=
276  match cmd with
277   | GrafiteAst.Coercion _
278   | GrafiteAst.Default _
279   | GrafiteAst.Drop _
280   | GrafiteAst.Include _
281   | GrafiteAst.Qed _
282   | GrafiteAst.Set _ as cmd ->
283       lexicon_status,metasenv,cmd
284   | GrafiteAst.Obj (loc,obj) ->
285       let lexicon_status,metasenv,obj =
286        disambiguate_obj lexicon_status ~baseuri metasenv (text,prefix_len,obj)in
287       lexicon_status, metasenv, GrafiteAst.Obj (loc,obj)
288
289 let disambiguate_macro 
290   lexicon_status_ref metasenv context (text,prefix_len, macro) 
291 =
292  let disambiguate_term = disambiguate_term text prefix_len lexicon_status_ref in
293   match macro with
294    | GrafiteAst.WMatch (loc,term) ->
295       let metasenv,term = disambiguate_term context metasenv term in
296        metasenv,GrafiteAst.WMatch (loc,term)
297    | GrafiteAst.WInstance (loc,term) ->
298       let metasenv,term = disambiguate_term context metasenv term in
299        metasenv,GrafiteAst.WInstance (loc,term)
300    | GrafiteAst.WElim (loc,term) ->
301       let metasenv,term = disambiguate_term context metasenv term in
302        metasenv,GrafiteAst.WElim (loc,term)
303    | GrafiteAst.WHint (loc,term) ->
304       let metasenv,term = disambiguate_term context metasenv term in
305        metasenv,GrafiteAst.WHint (loc,term)
306    | GrafiteAst.Check (loc,term) ->
307       let metasenv,term = disambiguate_term context metasenv term in
308        metasenv,GrafiteAst.Check (loc,term)
309    | GrafiteAst.Hint _
310    | GrafiteAst.WLocate _ as macro ->
311       metasenv,macro
312    | GrafiteAst.Quit _
313    | GrafiteAst.Print _
314    | GrafiteAst.Search_pat _
315    | GrafiteAst.Search_term _ -> assert false