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