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