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