1 (* Copyright (C) 2004, 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 Elim_failure of string
29 exception Can_t_eliminate
32 let counter = ref ~-1 in
36 Cic.Name ("e" ^ string_of_int !counter)
39 (** verifies if a given inductive type occurs in a term in target position *)
40 let rec recursive uri typeno = function
41 | Cic.Prod (_, _, target) -> recursive uri typeno target
42 | Cic.MutInd (uri', typeno', [])
43 | Cic.Appl (Cic.MutInd (uri', typeno', []) :: _) ->
44 UriManager.eq uri uri' && typeno = typeno'
47 (** given a list of constructor types, return true if at least one of them is
48 * recursive, false otherwise *)
49 let recursive_type uri typeno constructors =
50 let rec aux = function
51 | Cic.Prod (_, src, tgt) -> recursive uri typeno src || aux tgt
54 List.exists (fun (_, ty) -> aux ty) constructors
56 let unfold_appl = function
57 | Cic.Appl ((Cic.Appl args) :: tl) -> Cic.Appl (args @ tl)
63 | (he::tl, n) -> let (l1,l2) = split tl (n-1) in (he::l1,l2)
64 | (_,_) -> assert false
66 (** build elimination principle part related to a single constructor
67 * @param paramsno number of Prod to ignore in this constructor (i.e. number of
68 * inductive parameters)
69 * @param dependent true if we are in the dependent case (i.e. sort <> Prop) *)
70 let rec delta (uri, typeno) dependent paramsno consno t p args =
72 | Cic.MutInd (uri', typeno', []) when
73 UriManager.eq uri uri' && typeno = typeno' ->
77 | [arg] -> unfold_appl (Cic.Appl [p; arg])
78 | _ -> unfold_appl (Cic.Appl [p; unfold_appl (Cic.Appl args)]))
81 | Cic.Appl (Cic.MutInd (uri', typeno', []) :: tl) when
82 UriManager.eq uri uri' && typeno = typeno' ->
83 let (lparams, rparams) = split tl paramsno in
87 | [arg] -> unfold_appl (Cic.Appl (p :: rparams @ [arg]))
89 unfold_appl (Cic.Appl (p ::
90 rparams @ [unfold_appl (Cic.Appl args)])))
91 else (* non dependent *)
94 | _ -> Cic.Appl (p :: rparams))
95 | Cic.Prod (binder, src, tgt) ->
96 if recursive uri typeno src then
97 let args = List.map (CicSubstitution.lift 2) args in
99 let src = CicSubstitution.lift 1 src in
100 delta (uri, typeno) dependent paramsno consno src
101 (CicSubstitution.lift 1 p) [Cic.Rel 1]
103 let tgt = CicSubstitution.lift 1 tgt in
104 Cic.Prod (fresh_binder dependent, src,
105 Cic.Prod (Cic.Anonymous, phi,
106 delta (uri, typeno) dependent paramsno consno tgt
107 (CicSubstitution.lift 2 p) (args @ [Cic.Rel 2])))
108 else (* non recursive *)
109 let args = List.map (CicSubstitution.lift 1) args in
110 Cic.Prod (fresh_binder dependent, src,
111 delta (uri, typeno) dependent paramsno consno tgt
112 (CicSubstitution.lift 1 p) (args @ [Cic.Rel 1]))
115 let rec strip_left_params consno leftno = function
116 | t when leftno = 0 -> t (* no need to lift, the term is (hopefully) closed *)
117 | Cic.Prod (_, _, tgt) (* when leftno > 0 *) ->
118 (* after stripping the parameters we lift of consno. consno is 1 based so,
119 * the first constructor will be lifted by 1 (for P), the second by 2 (1
120 * for P and 1 for the 1st constructor), and so on *)
122 CicSubstitution.lift consno tgt
124 strip_left_params consno (leftno - 1) tgt
127 let delta (ury, typeno) dependent paramsno consno t p args =
128 let t = strip_left_params consno paramsno t in
129 delta (ury, typeno) dependent paramsno consno t p args
131 let rec add_params binder indno ty eliminator =
136 | Cic.Prod (name, src, tgt) ->
137 binder name src (add_params binder (indno - 1) tgt eliminator)
140 let rec mk_rels consno = function
142 | n -> Cic.Rel (n+consno) :: mk_rels consno (n-1)
144 let rec strip_pi = function
145 | Cic.Prod (_, _, tgt) -> strip_pi tgt
148 let rec count_pi = function
149 | Cic.Prod (_, _, tgt) -> count_pi tgt + 1
152 let rec type_of_p sort dependent leftno indty = function
153 | Cic.Prod (n, src, tgt) when leftno = 0 ->
154 Cic.Prod (n, src, type_of_p sort dependent leftno indty tgt)
155 | Cic.Prod (_, _, tgt) -> type_of_p sort dependent (leftno - 1) indty tgt
158 Cic.Prod (Cic.Anonymous, indty, Cic.Sort sort)
162 let rec add_right_pi dependent strip liftno liftfrom rightno indty = function
163 | Cic.Prod (_, src, tgt) when strip = 0 ->
164 Cic.Prod (fresh_binder true,
165 CicSubstitution.lift_from liftfrom liftno src,
166 add_right_pi dependent strip liftno (liftfrom + 1) rightno indty tgt)
167 | Cic.Prod (_, _, tgt) ->
168 add_right_pi dependent (strip - 1) liftno liftfrom rightno indty tgt
171 Cic.Prod (fresh_binder dependent,
172 CicSubstitution.lift_from (rightno + 1) liftno indty,
173 Cic.Appl (Cic.Rel (1 + liftno + rightno) :: mk_rels 0 (rightno + 1)))
175 Cic.Prod (Cic.Anonymous,
176 CicSubstitution.lift_from (rightno + 1) liftno indty,
178 Cic.Rel (1 + liftno + rightno)
180 Cic.Appl (Cic.Rel (1 + liftno + rightno) :: mk_rels 1 rightno))
182 let rec add_right_lambda dependent strip liftno liftfrom rightno indty case =
184 | Cic.Prod (_, src, tgt) when strip = 0 ->
185 Cic.Lambda (fresh_binder true,
186 CicSubstitution.lift_from liftfrom liftno src,
187 add_right_lambda dependent strip liftno (liftfrom + 1) rightno indty
189 | Cic.Prod (_, _, tgt) ->
190 add_right_lambda dependent (strip - 1) liftno liftfrom rightno indty
193 Cic.Lambda (fresh_binder true,
194 CicSubstitution.lift_from (rightno + 1) liftno indty, case)
196 let rec branch (uri, typeno) insource paramsno t fix head args =
198 | Cic.MutInd (uri', typeno', []) when
199 UriManager.eq uri uri' && typeno = typeno' ->
202 | [arg] -> Cic.Appl (fix :: args)
203 | _ -> Cic.Appl (head :: [Cic.Appl args]))
207 | _ -> Cic.Appl (head :: args))
208 | Cic.Appl (Cic.MutInd (uri', typeno', []) :: tl) when
209 UriManager.eq uri uri' && typeno = typeno' ->
211 let (lparams, rparams) = split tl paramsno in
213 | [arg] -> Cic.Appl (fix :: rparams @ args)
214 | _ -> Cic.Appl (fix :: rparams @ [Cic.Appl args])
218 | _ -> Cic.Appl (head :: args))
219 | Cic.Prod (binder, src, tgt) ->
220 if recursive uri typeno src then
221 let args = List.map (CicSubstitution.lift 1) args in
223 let fix = CicSubstitution.lift 1 fix in
224 let src = CicSubstitution.lift 1 src in
225 branch (uri, typeno) true paramsno src fix head [Cic.Rel 1]
227 Cic.Lambda (fresh_binder true, src,
228 branch (uri, typeno) insource paramsno tgt
229 (CicSubstitution.lift 1 fix) (CicSubstitution.lift 1 head)
230 (args @ [Cic.Rel 1; phi]))
231 else (* non recursive *)
232 let args = List.map (CicSubstitution.lift 1) args in
233 Cic.Lambda (fresh_binder true, src,
234 branch (uri, typeno) insource paramsno tgt
235 (CicSubstitution.lift 1 fix) (CicSubstitution.lift 1 head)
236 (args @ [Cic.Rel 1]))
239 let branch (uri, typeno) insource liftno paramsno t fix head args =
240 let t = strip_left_params liftno paramsno t in
241 branch (uri, typeno) insource paramsno t fix head args
243 let elim_of ?(sort = Cic.Type (CicUniv.fresh ())) uri typeno =
244 let (obj, univ) = (CicEnvironment.get_obj CicUniv.empty_ugraph uri) in
246 | Cic.InductiveDefinition (indTypes, params, leftno, _) ->
247 let (name, inductive, ty, constructors) =
249 List.nth indTypes typeno
250 with Failure _ -> assert false
252 let paramsno = count_pi ty in (* number of (left or right) parameters *)
253 let rightno = paramsno - leftno in
254 let dependent = (strip_pi ty <> Cic.Sort Cic.Prop) in
255 let conslen = List.length constructors in
256 let consno = ref (conslen + 1) in
257 if (not dependent) && (sort <> Cic.Prop) && (conslen > 1) then
258 raise Can_t_eliminate;
260 let indty = Cic.MutInd (uri, typeno, []) in
264 Cic.Appl (indty :: mk_rels 0 paramsno)
266 let mk_constructor consno =
267 let constructor = Cic.MutConstruct (uri, typeno, consno, []) in
271 Cic.Appl (constructor :: mk_rels consno leftno)
273 let p_ty = type_of_p sort dependent leftno indty ty in
275 add_right_pi dependent leftno (conslen + 1) 1 rightno indty ty
277 let eliminator_type =
279 Cic.Prod (Cic.Name "P", p_ty,
281 (fun (_, constructor) acc ->
283 let p = Cic.Rel !consno in
284 Cic.Prod (Cic.Anonymous,
285 (delta (uri, typeno) dependent leftno !consno
286 constructor p [mk_constructor !consno]),
288 constructors final_ty))
290 add_params (fun b s t -> Cic.Prod (b, s, t)) leftno ty cic
292 let consno = ref (conslen + 1) in
293 let eliminator_body =
294 let fix = Cic.Rel (rightno + 2) in
295 let is_recursive = recursive_type uri typeno constructors in
296 let recshift = if is_recursive then 1 else 0 in
299 (fun (_, ty) (shift, branches) ->
300 let head = Cic.Rel (rightno + shift + 1 + recshift) in
302 branch (uri, typeno) false
303 (rightno + conslen + 2 + recshift) leftno ty fix head []
305 (shift + 1, b :: branches))
309 Cic.MutCase (uri, typeno, Cic.Rel (conslen + rightno + 2 + recshift),
315 add_right_lambda dependent leftno (conslen + 2) 1 rightno
318 (* rightno is the decreasing argument, i.e. the argument of
320 Cic.Fix (0, ["f", rightno, final_ty, fixfun])
322 add_right_lambda dependent leftno (conslen + 1) 1 rightno indty
326 Cic.Lambda (Cic.Name "P", p_ty,
328 (fun (_, constructor) acc ->
330 let p = Cic.Rel !consno in
331 Cic.Lambda (fresh_binder true,
332 (delta (uri, typeno) dependent leftno !consno
333 constructor p [mk_constructor !consno]),
337 add_params (fun b s t -> Cic.Lambda (b, s, t)) leftno ty cic
340 prerr_endline (CicPp.ppterm eliminator_type);
341 prerr_endline (CicPp.ppterm eliminator_body);
343 let (computed_type, ugraph) =
345 CicTypeChecker.type_of_aux' [] [] eliminator_body CicUniv.empty_ugraph
346 with CicTypeChecker.TypeCheckerFailure msg ->
347 raise (Elim_failure (sprintf
348 "type checker failure while type checking:\n%s\nerror:\n%s"
349 (CicPp.ppterm eliminator_body) msg))
351 if not (fst (CicReduction.are_convertible []
352 eliminator_type computed_type ugraph))
354 raise (Failure (sprintf
355 "internal error: type mismatch on eliminator type\n%s\n%s"
356 (CicPp.ppterm eliminator_type) (CicPp.ppterm computed_type)));
361 | Cic.Type _ -> "_rect"
364 let name = UriManager.name_of_uri uri ^ suffix in
365 let obj_attrs = [`Class (`Elim sort); `Generated] in
366 Cic.Constant (name, Some eliminator_body, eliminator_type, [], obj_attrs)
368 failwith (sprintf "not an inductive definition (%s)"
369 (UriManager.string_of_uri uri))