1 (* Copyright (C) 2005, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://helm.cs.unibo.it/
28 exception BaseUriNotSetYet
30 let singleton = function
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, _) =
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)
44 let lexicon_status = LexiconEngine.set_proof_aliases lexicon_status diff in
45 lexicon_status_ref := lexicon_status;
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) =
58 (GrafiteDisambiguator.disambiguate_term ~dbd:(LibraryDb.instance ())
59 ~initial_ugraph:ugraph ~aliases:lexicon_status.LexiconEngine.aliases
60 ~universe:(Some lexicon_status.LexiconEngine.multi_aliases)
63 let lexicon_status = LexiconEngine.set_proof_aliases lexicon_status diff in
64 lexicon_status_ref := lexicon_status;
65 cic, metasenv, ugraph)
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
75 let wanted = disambiguate_lazy_term lexicon_status_ref wanted in
78 (wanted, hyp_paths, goal_path)
80 let disambiguate_reduction_kind lexicon_status_ref = function
82 let t = disambiguate_lazy_term lexicon_status_ref t in
89 | `Whd as kind -> kind
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
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.Compare (loc,term) ->
116 let metasenv,term = disambiguate_term context metasenv term in
117 metasenv,GrafiteAst.Compare (loc,term)
118 | GrafiteAst.Constructor (loc,n) ->
119 metasenv,GrafiteAst.Constructor (loc,n)
120 | GrafiteAst.Contradiction loc ->
121 metasenv,GrafiteAst.Contradiction loc
122 | GrafiteAst.Cut (loc, ident, term) ->
123 let metasenv,cic = disambiguate_term context metasenv term in
124 metasenv,GrafiteAst.Cut (loc, ident, cic)
125 | GrafiteAst.DecideEquality loc ->
126 metasenv,GrafiteAst.DecideEquality loc
127 | GrafiteAst.Decompose (loc, types, what, names) ->
128 let disambiguate (metasenv,types) = function
129 | GrafiteAst.Type _ -> assert false
130 | GrafiteAst.Ident id ->
132 disambiguate_term context metasenv
133 (CicNotationPt.Ident(id, None))
135 | metasenv,Cic.MutInd (uri, tyno, _) ->
136 metasenv,(GrafiteAst.Type (uri, tyno) :: types)
138 raise (GrafiteDisambiguator.DisambiguationError
139 (0,[[None,lazy "Decompose works only on inductive types"]])))
142 List.fold_left disambiguate (metasenv,[]) types
144 metasenv,GrafiteAst.Decompose (loc, types, what, names)
145 | GrafiteAst.Discriminate (loc,term) ->
146 let metasenv,term = disambiguate_term context metasenv term in
147 metasenv,GrafiteAst.Discriminate(loc,term)
148 | GrafiteAst.Exact (loc, term) ->
149 let metasenv,cic = disambiguate_term context metasenv term in
150 metasenv,GrafiteAst.Exact (loc, cic)
151 | GrafiteAst.Elim (loc, what, Some using, depth, idents) ->
152 let metasenv,what = disambiguate_term context metasenv what in
153 let metasenv,using = disambiguate_term context metasenv using in
154 metasenv,GrafiteAst.Elim (loc, what, Some using, depth, idents)
155 | GrafiteAst.Elim (loc, what, None, depth, idents) ->
156 let metasenv,what = disambiguate_term context metasenv what in
157 metasenv,GrafiteAst.Elim (loc, what, None, depth, idents)
158 | GrafiteAst.ElimType (loc, what, Some using, depth, idents) ->
159 let metasenv,what = disambiguate_term context metasenv what in
160 let metasenv,using = disambiguate_term context metasenv using in
161 metasenv,GrafiteAst.ElimType (loc, what, Some using, depth, idents)
162 | GrafiteAst.ElimType (loc, what, None, depth, idents) ->
163 let metasenv,what = disambiguate_term context metasenv what in
164 metasenv,GrafiteAst.ElimType (loc, what, None, depth, idents)
165 | GrafiteAst.Exists loc ->
166 metasenv,GrafiteAst.Exists loc
167 | GrafiteAst.Fail loc ->
168 metasenv,GrafiteAst.Fail loc
169 | GrafiteAst.Fold (loc,red_kind, term, pattern) ->
170 let pattern = disambiguate_pattern pattern in
171 let term = disambiguate_lazy_term term in
172 let red_kind = disambiguate_reduction_kind red_kind in
173 metasenv,GrafiteAst.Fold (loc, red_kind, term, pattern)
174 | GrafiteAst.FwdSimpl (loc, hyp, names) ->
175 metasenv,GrafiteAst.FwdSimpl (loc, hyp, names)
176 | GrafiteAst.Fourier loc ->
177 metasenv,GrafiteAst.Fourier loc
178 | GrafiteAst.Generalize (loc,pattern,ident) ->
179 let pattern = disambiguate_pattern pattern in
180 metasenv,GrafiteAst.Generalize (loc,pattern,ident)
181 | GrafiteAst.Goal (loc, g) ->
182 metasenv,GrafiteAst.Goal (loc, g)
183 | GrafiteAst.IdTac loc ->
184 metasenv,GrafiteAst.IdTac loc
185 | GrafiteAst.Injection (loc, term) ->
186 let metasenv,term = disambiguate_term context metasenv term in
187 metasenv,GrafiteAst.Injection (loc,term)
188 | GrafiteAst.Intros (loc, num, names) ->
189 metasenv,GrafiteAst.Intros (loc, num, names)
190 | GrafiteAst.Inversion (loc, term) ->
191 let metasenv,term = disambiguate_term context metasenv term in
192 metasenv,GrafiteAst.Inversion (loc, term)
193 | GrafiteAst.LApply (loc, depth, to_what, what, ident) ->
195 let metasenv,term = disambiguate_term context metasenv term in
198 let to_what = List.fold_right f to_what [] in
199 let metasenv,what = disambiguate_term context metasenv what in
200 metasenv,GrafiteAst.LApply (loc, depth, to_what, what, ident)
201 | GrafiteAst.Left loc ->
202 metasenv,GrafiteAst.Left loc
203 | GrafiteAst.LetIn (loc, term, name) ->
204 let metasenv,term = disambiguate_term context metasenv term in
205 metasenv,GrafiteAst.LetIn (loc,term,name)
206 | GrafiteAst.Reduce (loc, red_kind, pattern) ->
207 let pattern = disambiguate_pattern pattern in
208 let red_kind = disambiguate_reduction_kind red_kind in
209 metasenv,GrafiteAst.Reduce(loc, red_kind, pattern)
210 | GrafiteAst.Reflexivity loc ->
211 metasenv,GrafiteAst.Reflexivity loc
212 | GrafiteAst.Replace (loc, pattern, with_what) ->
213 let pattern = disambiguate_pattern pattern in
214 let with_what = disambiguate_lazy_term with_what in
215 metasenv,GrafiteAst.Replace (loc, pattern, with_what)
216 | GrafiteAst.Rewrite (loc, dir, t, pattern) ->
217 let metasenv,term = disambiguate_term context metasenv t in
218 let pattern = disambiguate_pattern pattern in
219 metasenv,GrafiteAst.Rewrite (loc, dir, term, pattern)
220 | GrafiteAst.Right loc ->
221 metasenv,GrafiteAst.Right loc
222 | GrafiteAst.Ring loc ->
223 metasenv,GrafiteAst.Ring loc
224 | GrafiteAst.Split loc ->
225 metasenv,GrafiteAst.Split loc
226 | GrafiteAst.Symmetry loc ->
227 metasenv,GrafiteAst.Symmetry loc
228 | GrafiteAst.Transitivity (loc, term) ->
229 let metasenv,cic = disambiguate_term context metasenv term in
230 metasenv,GrafiteAst.Transitivity (loc, cic)
232 let disambiguate_obj lexicon_status ~baseuri metasenv obj =
235 | CicNotationPt.Inductive (_,(name,_,_,_)::_)
236 | CicNotationPt.Record (_,name,_,_) ->
239 Some (UriManager.uri_of_string (baseuri ^ "/" ^ name ^ ".ind"))
240 | None -> raise BaseUriNotSetYet)
241 | CicNotationPt.Inductive _ -> assert false
242 | CicNotationPt.Theorem _ -> None in
243 let (diff, metasenv, cic, _) =
245 (GrafiteDisambiguator.disambiguate_obj ~dbd:(LibraryDb.instance ())
246 ~aliases:lexicon_status.LexiconEngine.aliases
247 ~universe:(Some lexicon_status.LexiconEngine.multi_aliases) ~uri obj) in
248 let lexicon_status = LexiconEngine.set_proof_aliases lexicon_status diff in
249 lexicon_status, metasenv, cic
251 let disambiguate_command lexicon_status ~baseuri metasenv =
253 | GrafiteAst.Coercion _
254 | GrafiteAst.Default _
256 | GrafiteAst.Include _
258 | GrafiteAst.Set _ as cmd ->
259 lexicon_status,metasenv,cmd
260 | GrafiteAst.Obj (loc,obj) ->
261 let lexicon_status,metasenv,obj =
262 disambiguate_obj lexicon_status ~baseuri metasenv obj in
263 lexicon_status, metasenv, GrafiteAst.Obj (loc,obj)
265 let disambiguate_macro lexicon_status_ref metasenv context macro =
266 let disambiguate_term = disambiguate_term lexicon_status_ref in
268 | GrafiteAst.WMatch (loc,term) ->
269 let metasenv,term = disambiguate_term context metasenv term in
270 metasenv,GrafiteAst.WMatch (loc,term)
271 | GrafiteAst.WInstance (loc,term) ->
272 let metasenv,term = disambiguate_term context metasenv term in
273 metasenv,GrafiteAst.WInstance (loc,term)
274 | GrafiteAst.WElim (loc,term) ->
275 let metasenv,term = disambiguate_term context metasenv term in
276 metasenv,GrafiteAst.WElim (loc,term)
277 | GrafiteAst.WHint (loc,term) ->
278 let metasenv,term = disambiguate_term context metasenv term in
279 metasenv,GrafiteAst.WHint (loc,term)
280 | GrafiteAst.Check (loc,term) ->
281 let metasenv,term = disambiguate_term context metasenv term in
282 metasenv,GrafiteAst.Check (loc,term)
284 | GrafiteAst.WLocate _ as macro ->
288 | GrafiteAst.Search_pat _
289 | GrafiteAst.Search_term _ -> assert false