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/
30 exception Elim_failure of string Lazy.t
31 exception Can_t_eliminate
33 let debug_print = fun _ -> ()
34 (*let debug_print s = prerr_endline (Lazy.force s) *)
36 let counter = ref ~-1 ;;
38 let fresh_binder () = Cic.Name "matita_dummy"
41 Cic.Name ("e" ^ string_of_int !counter) *)
43 (** verifies if a given inductive type occurs in a term in target position *)
44 let rec recursive uri typeno = function
45 | Cic.Prod (_, _, target) -> recursive uri typeno target
46 | Cic.MutInd (uri', typeno', [])
47 | Cic.Appl (Cic.MutInd (uri', typeno', []) :: _) ->
48 UriManager.eq uri uri' && typeno = typeno'
51 (** given a list of constructor types, return true if at least one of them is
52 * recursive, false otherwise *)
53 let recursive_type uri typeno constructors =
54 let rec aux = function
55 | Cic.Prod (_, src, tgt) -> recursive uri typeno src || aux tgt
58 List.exists (fun (_, ty) -> aux ty) constructors
60 let unfold_appl = function
61 | Cic.Appl ((Cic.Appl args) :: tl) -> Cic.Appl (args @ tl)
67 | (he::tl, n) -> let (l1,l2) = split tl (n-1) in (he::l1,l2)
68 | (_,_) -> assert false
70 (** build elimination principle part related to a single constructor
71 * @param paramsno number of Prod to ignore in this constructor (i.e. number of
72 * inductive parameters)
73 * @param dependent true if we are in the dependent case (i.e. sort <> Prop) *)
74 let rec delta (uri, typeno) dependent paramsno consno t p args =
76 | Cic.MutInd (uri', typeno', []) when
77 UriManager.eq uri uri' && typeno = typeno' ->
81 | [arg] -> unfold_appl (Cic.Appl [p; arg])
82 | _ -> unfold_appl (Cic.Appl [p; unfold_appl (Cic.Appl args)]))
85 | Cic.Appl (Cic.MutInd (uri', typeno', []) :: tl) when
86 UriManager.eq uri uri' && typeno = typeno' ->
87 let (lparams, rparams) = split tl paramsno in
91 | [arg] -> unfold_appl (Cic.Appl (p :: rparams @ [arg]))
93 unfold_appl (Cic.Appl (p ::
94 rparams @ [unfold_appl (Cic.Appl args)])))
95 else (* non dependent *)
98 | _ -> Cic.Appl (p :: rparams))
99 | Cic.Prod (binder, src, tgt) ->
100 if recursive uri typeno src then
101 let args = List.map (CicSubstitution.lift 2) args in
103 let src = CicSubstitution.lift 1 src in
104 delta (uri, typeno) dependent paramsno consno src
105 (CicSubstitution.lift 1 p) [Cic.Rel 1]
107 let tgt = CicSubstitution.lift 1 tgt in
108 Cic.Prod (fresh_binder (), src,
109 Cic.Prod (Cic.Anonymous, phi,
110 delta (uri, typeno) dependent paramsno consno tgt
111 (CicSubstitution.lift 2 p) (args @ [Cic.Rel 2])))
112 else (* non recursive *)
113 let args = List.map (CicSubstitution.lift 1) args in
114 Cic.Prod (fresh_binder (), src,
115 delta (uri, typeno) dependent paramsno consno tgt
116 (CicSubstitution.lift 1 p) (args @ [Cic.Rel 1]))
119 let rec strip_left_params consno leftno = function
120 | t when leftno = 0 -> t (* no need to lift, the term is (hopefully) closed *)
121 | Cic.Prod (_, _, tgt) (* when leftno > 0 *) ->
122 (* after stripping the parameters we lift of consno. consno is 1 based so,
123 * the first constructor will be lifted by 1 (for P), the second by 2 (1
124 * for P and 1 for the 1st constructor), and so on *)
126 CicSubstitution.lift consno tgt
128 strip_left_params consno (leftno - 1) tgt
131 let delta (ury, typeno) dependent paramsno consno t p args =
132 let t = strip_left_params consno paramsno t in
133 delta (ury, typeno) dependent paramsno consno t p args
135 let rec add_params binder indno ty eliminator =
140 | Cic.Prod (name, src, tgt) ->
144 | Cic.Anonymous -> fresh_binder ()
146 binder name src (add_params binder (indno - 1) tgt eliminator)
149 let rec mk_rels consno = function
151 | n -> Cic.Rel (n+consno) :: mk_rels consno (n-1)
153 let rec strip_pi = function
154 | Cic.Prod (_, _, tgt) -> strip_pi tgt
157 let rec count_pi = function
158 | Cic.Prod (_, _, tgt) -> count_pi tgt + 1
161 let rec type_of_p sort dependent leftno indty = function
162 | Cic.Prod (n, src, tgt) when leftno = 0 ->
167 | Cic.Anonymous -> fresh_binder ()
171 Cic.Prod (n, src, type_of_p sort dependent leftno indty tgt)
172 | Cic.Prod (_, _, tgt) -> type_of_p sort dependent (leftno - 1) indty tgt
175 Cic.Prod (Cic.Anonymous, indty, Cic.Sort sort)
179 let rec add_right_pi dependent strip liftno liftfrom rightno indty = function
180 | Cic.Prod (_, src, tgt) when strip = 0 ->
181 Cic.Prod (fresh_binder (),
182 CicSubstitution.lift_from liftfrom liftno src,
183 add_right_pi dependent strip liftno (liftfrom + 1) rightno indty tgt)
184 | Cic.Prod (_, _, tgt) ->
185 add_right_pi dependent (strip - 1) liftno liftfrom rightno indty tgt
188 Cic.Prod (fresh_binder (),
189 CicSubstitution.lift_from (rightno + 1) liftno indty,
190 Cic.Appl (Cic.Rel (1 + liftno + rightno) :: mk_rels 0 (rightno + 1)))
192 Cic.Prod (Cic.Anonymous,
193 CicSubstitution.lift_from (rightno + 1) liftno indty,
195 Cic.Rel (1 + liftno + rightno)
197 Cic.Appl (Cic.Rel (1 + liftno + rightno) :: mk_rels 1 rightno))
199 let rec add_right_lambda dependent strip liftno liftfrom rightno indty case =
201 | Cic.Prod (_, src, tgt) when strip = 0 ->
202 Cic.Lambda (fresh_binder (),
203 CicSubstitution.lift_from liftfrom liftno src,
204 add_right_lambda dependent strip liftno (liftfrom + 1) rightno indty
206 | Cic.Prod (_, _, tgt) ->
207 add_right_lambda true (strip - 1) liftno liftfrom rightno indty
210 Cic.Lambda (fresh_binder (),
211 CicSubstitution.lift_from (rightno + 1) liftno indty, case)
213 let rec branch (uri, typeno) insource paramsno t fix head args =
215 | Cic.MutInd (uri', typeno', []) when
216 UriManager.eq uri uri' && typeno = typeno' ->
219 | [arg] -> Cic.Appl (fix :: args)
220 | _ -> Cic.Appl (fix :: [Cic.Appl args]))
224 | _ -> Cic.Appl (head :: args))
225 | Cic.Appl (Cic.MutInd (uri', typeno', []) :: tl) when
226 UriManager.eq uri uri' && typeno = typeno' ->
228 let (lparams, rparams) = split tl paramsno in
230 | [arg] -> Cic.Appl (fix :: rparams @ args)
231 | _ -> Cic.Appl (fix :: rparams @ [Cic.Appl args])
235 | _ -> Cic.Appl (head :: args))
236 | Cic.Prod (binder, src, tgt) ->
237 if recursive uri typeno src then
238 let args = List.map (CicSubstitution.lift 1) args in
240 let fix = CicSubstitution.lift 1 fix in
241 let src = CicSubstitution.lift 1 src in
242 branch (uri, typeno) true paramsno src fix head [Cic.Rel 1]
244 Cic.Lambda (fresh_binder (), src,
245 branch (uri, typeno) insource paramsno tgt
246 (CicSubstitution.lift 1 fix) (CicSubstitution.lift 1 head)
247 (args @ [Cic.Rel 1; phi]))
248 else (* non recursive *)
249 let args = List.map (CicSubstitution.lift 1) args in
250 Cic.Lambda (fresh_binder (), src,
251 branch (uri, typeno) insource paramsno tgt
252 (CicSubstitution.lift 1 fix) (CicSubstitution.lift 1 head)
253 (args @ [Cic.Rel 1]))
256 let branch (uri, typeno) insource liftno paramsno t fix head args =
257 let t = strip_left_params liftno paramsno t in
258 branch (uri, typeno) insource paramsno t fix head args
260 let elim_of ~sort uri typeno =
262 let (obj, univ) = (CicEnvironment.get_obj CicUniv.oblivion_ugraph uri) in
264 | Cic.InductiveDefinition (indTypes, params, leftno, _) ->
265 let (name, inductive, ty, constructors) =
267 List.nth indTypes typeno
268 with Failure _ -> assert false
270 let ty = Unshare.unshare ~fresh_univs:true ty in
272 List.map (fun (name,c)-> name,Unshare.unshare ~fresh_univs:true c) constructors
274 let paramsno = count_pi ty in (* number of (left or right) parameters *)
275 let rightno = paramsno - leftno in
276 let dependent = (strip_pi ty <> Cic.Sort Cic.Prop) in
278 match strip_pi ty with
282 let conslen = List.length constructors in
283 let consno = ref (conslen + 1) in
286 (CicTypeChecker.check_allowed_sort_elimination uri typeno head sort)
288 raise Can_t_eliminate;
290 let indty = Cic.MutInd (uri, typeno, []) in
294 Cic.Appl (indty :: mk_rels 0 paramsno)
296 let mk_constructor consno =
297 let constructor = Cic.MutConstruct (uri, typeno, consno, []) in
301 Cic.Appl (constructor :: mk_rels consno leftno)
303 let p_ty = type_of_p sort dependent leftno indty ty in
305 add_right_pi dependent leftno (conslen + 1) 1 rightno indty ty
307 let eliminator_type =
309 Cic.Prod (Cic.Name "P", p_ty,
311 (fun (_, constructor) acc ->
313 let p = Cic.Rel !consno in
314 Cic.Prod (Cic.Anonymous,
315 (delta (uri, typeno) dependent leftno !consno
316 constructor p [mk_constructor !consno]),
318 constructors final_ty))
320 add_params (fun b s t -> Cic.Prod (b, s, t)) leftno ty cic
322 let consno = ref (conslen + 1) in
323 let eliminator_body =
324 let fix = Cic.Rel (rightno + 2) in
325 let is_recursive = recursive_type uri typeno constructors in
326 let recshift = if is_recursive then 1 else 0 in
329 (fun (_, ty) (shift, branches) ->
330 let head = Cic.Rel (rightno + shift + 1 + recshift) in
332 branch (uri, typeno) false
333 (rightno + conslen + 2 + recshift) leftno ty fix head []
335 (shift + 1, b :: branches))
338 let shiftno = conslen + rightno + 2 + recshift in
345 CicSubstitution.lift 1 (Cic.Rel shiftno)
348 ((CicSubstitution.lift (rightno + 1) (Cic.Rel shiftno)) ::
351 add_right_lambda true leftno shiftno 1 rightno indty head ty
354 Cic.MutCase (uri, typeno, outtype, Cic.Rel 1, branches)
359 add_right_lambda dependent leftno (conslen + 2) 1 rightno
362 (* rightno is the decreasing argument, i.e. the argument of
364 Cic.Fix (0, ["aux", rightno, final_ty, fixfun])
366 add_right_lambda dependent leftno (conslen + 1) 1 rightno indty
370 Cic.Lambda (Cic.Name "P", p_ty,
372 (fun (_, constructor) acc ->
374 let p = Cic.Rel !consno in
375 Cic.Lambda (fresh_binder (),
376 (delta (uri, typeno) dependent leftno !consno
377 constructor p [mk_constructor !consno]),
381 add_params (fun b s t -> Cic.Lambda (b, s, t)) leftno ty cic
384 debug_print (lazy (CicPp.ppterm eliminator_type));
385 debug_print (lazy (CicPp.ppterm eliminator_body));
387 let eliminator_type =
388 FreshNamesGenerator.mk_fresh_names [] [] [] eliminator_type in
389 let eliminator_body =
390 FreshNamesGenerator.mk_fresh_names [] [] [] eliminator_body in
392 debug_print (lazy (CicPp.ppterm eliminator_type));
393 debug_print (lazy (CicPp.ppterm eliminator_body));
395 let (computed_type, ugraph) =
397 CicTypeChecker.type_of_aux' [] [] eliminator_body
398 CicUniv.oblivion_ugraph
399 with CicTypeChecker.TypeCheckerFailure msg ->
400 raise (Elim_failure (lazy (sprintf
401 "type checker failure while type checking:\n%s\nerror:\n%s"
402 (CicPp.ppterm eliminator_body) (Lazy.force msg))))
404 if not (fst (CicReduction.are_convertible []
405 eliminator_type computed_type ugraph))
407 raise (Failure (sprintf
408 "internal error: type mismatch on eliminator type\n%s\n%s"
409 (CicPp.ppterm eliminator_type) (CicPp.ppterm computed_type)));
414 | Cic.Type _ -> "_rect"
417 (* let name = UriManager.name_of_uri uri ^ suffix in *)
418 let name = name ^ suffix in
419 let buri = UriManager.buri_of_uri uri in
420 let uri = UriManager.uri_of_string (buri ^ "/" ^ name ^ ".con") in
421 let obj_attrs = [`Class (`Elim sort); `Generated] in
423 Cic.Constant (name, Some eliminator_body, eliminator_type, [], obj_attrs)
425 failwith (sprintf "not an inductive definition (%s)"
426 (UriManager.string_of_uri uri))