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