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/
26 exception BaseUriNotSetYet
28 let singleton = function
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, _) =
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)
42 let lexicon_status = LexiconEngine.set_proof_aliases lexicon_status diff in
43 lexicon_status_ref := lexicon_status;
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) =
56 (GrafiteDisambiguator.disambiguate_term ~dbd:(LibraryDb.instance ())
57 ~initial_ugraph:ugraph ~aliases:lexicon_status.LexiconEngine.aliases
58 ~universe:(Some lexicon_status.LexiconEngine.multi_aliases)
61 let lexicon_status = LexiconEngine.set_proof_aliases lexicon_status diff in
62 lexicon_status_ref := lexicon_status;
63 cic, metasenv, ugraph)
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
73 let wanted = disambiguate_lazy_term lexicon_status_ref wanted in
76 (wanted, hyp_paths, goal_path)
78 let disambiguate_reduction_kind lexicon_status_ref = function
80 let t = disambiguate_lazy_term lexicon_status_ref t in
86 | `Whd as kind -> kind
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
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 ->
129 disambiguate_term context metasenv
130 (CicNotationPt.Ident(id, None))
132 | metasenv,Cic.MutInd (uri, tyno, _) ->
133 metasenv,(GrafiteAst.Type (uri, tyno) :: types)
135 raise (GrafiteDisambiguator.DisambiguationError
136 (0,[[None,lazy "Decompose works only on inductive types"]])))
139 List.fold_left disambiguate (metasenv,[]) types
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) ->
192 let metasenv,term = disambiguate_term context metasenv term 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)
229 let disambiguate_obj lexicon_status ~baseuri metasenv obj =
232 | CicNotationPt.Inductive (_,(name,_,_,_)::_)
233 | CicNotationPt.Record (_,name,_,_) ->
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, _) =
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
248 let disambiguate_command lexicon_status ~baseuri metasenv =
250 | GrafiteAst.Coercion _
251 | GrafiteAst.Default _
253 | GrafiteAst.Include _
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)