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 subst = function
41 | Cic.Prod (_, _, target) -> recursive uri typeno subst target
42 | Cic.MutInd (uri', typeno', subst')
43 | Cic.Appl (Cic.MutInd (uri', typeno', subst') :: _) ->
44 UriManager.eq uri uri' && typeno = typeno' && subst = subst'
45 (* | Cic.Appl args -> List.exists (recursive uri typeno subst) args *)
48 (** given a list of constructor types, return true if at least one of them is
49 * recursive, false otherwise *)
50 let recursive_type uri typeno subst constructors =
51 let rec aux = function
52 | Cic.Prod (_, src, tgt) -> recursive uri typeno subst src || aux tgt
55 List.exists (fun (_, ty) -> aux ty) constructors
57 let unfold_appl = function
58 | Cic.Appl ((Cic.Appl args) :: tl) -> Cic.Appl (args @ tl)
64 | (he::tl, n) -> let (l1,l2) = split tl (n-1) in (he::l1,l2)
65 | (_,_) -> assert false
67 (** build elimination principle part related to a single constructor
68 * @param paramsno number of Prod to ignore in this constructor (i.e. number of
69 * inductive parameters)
70 * @param dependent true if we are in the dependent case (i.e. sort <> Prop) *)
71 let rec delta (uri, typeno, subst) dependent paramsno consno t p args =
74 | Cic.MutInd (uri', typeno', subst') when
75 UriManager.eq uri uri' && typeno = typeno' && subst = subst' ->
79 | [arg] -> unfold_appl (Cic.Appl [p; arg])
80 | _ -> unfold_appl (Cic.Appl [p; unfold_appl (Cic.Appl args)]))
83 | Cic.Appl (Cic.MutInd (uri', typeno', subst') :: tl) when
84 UriManager.eq uri uri' && typeno = typeno' && subst = subst' ->
85 let (lparams, rparams) = split tl paramsno in
89 | [arg] -> unfold_appl (Cic.Appl (p :: rparams @ [arg]))
91 unfold_appl (Cic.Appl (p ::
92 rparams @ [unfold_appl (Cic.Appl args)])))
93 else (* non dependent *)
96 | _ -> Cic.Appl (p :: rparams))
97 | Cic.Prod (binder, src, tgt) ->
98 if recursive uri typeno subst src then
99 let args = List.map (CicSubstitution.lift 2) args in
101 let src = CicSubstitution.lift 1 src in
102 delta (uri, typeno, subst) dependent paramsno consno src
103 (CicSubstitution.lift 1 p) [Cic.Rel 1]
105 let tgt = CicSubstitution.lift 1 tgt in
106 Cic.Prod (fresh_binder dependent, src,
107 Cic.Prod (Cic.Anonymous, phi,
108 delta (uri, typeno, subst) dependent paramsno consno tgt
109 (CicSubstitution.lift 2 p) (args @ [Cic.Rel 2])))
110 else (* non recursive *)
111 let args = List.map (CicSubstitution.lift 1) args in
112 Cic.Prod (fresh_binder dependent, src,
113 delta (uri, typeno, subst) dependent paramsno consno tgt
114 (CicSubstitution.lift 1 p) (args @ [Cic.Rel 1]))
117 let rec strip_left_params consno leftno = function
118 | t when leftno = 0 -> t (* no need to lift, the term is (hopefully) closed *)
119 | Cic.Prod (_, _, tgt) (* when leftno > 0 *) ->
120 (* after stripping the parameters we lift of consno. consno is 1 based so,
121 * the first constructor will be lifted by 1 (for P), the second by 2 (1
122 * for P and 1 for the 1st constructor), and so on *)
124 CicSubstitution.lift consno tgt
126 strip_left_params consno (leftno - 1) tgt
129 let delta (ury, typeno, subst) dependent paramsno consno t p args =
130 let t = strip_left_params consno paramsno t in
131 delta (ury, typeno, subst) dependent paramsno consno t p args
133 let rec add_params binder indno ty eliminator =
138 | Cic.Prod (name, src, tgt) ->
139 binder name src (add_params binder (indno - 1) tgt eliminator)
142 let rec mk_rels consno = function
144 | n -> Cic.Rel (n+consno) :: mk_rels consno (n-1)
146 let rec strip_pi = function
147 | Cic.Prod (_, _, tgt) -> strip_pi tgt
150 let rec count_pi = function
151 | Cic.Prod (_, _, tgt) -> count_pi tgt + 1
154 let rec type_of_p sort dependent leftno indty = function
155 | Cic.Prod (n, src, tgt) when leftno = 0 ->
156 Cic.Prod (n, src, type_of_p sort dependent leftno indty tgt)
157 | Cic.Prod (_, _, tgt) -> type_of_p sort dependent (leftno - 1) indty tgt
160 Cic.Prod (Cic.Anonymous, indty, Cic.Sort sort)
164 let rec add_right_pi dependent strip liftno liftfrom rightno indty = function
165 | Cic.Prod (_, src, tgt) when strip = 0 ->
166 Cic.Prod (fresh_binder true,
167 CicSubstitution.lift_from (liftfrom + 1) liftno src,
168 add_right_pi dependent strip liftno (liftfrom + 1) rightno indty tgt)
169 | Cic.Prod (_, _, tgt) ->
170 add_right_pi dependent (strip - 1) liftno liftfrom rightno indty tgt
173 Cic.Prod (fresh_binder dependent,
174 CicSubstitution.lift_from (rightno + 1) liftno indty,
175 Cic.Appl (Cic.Rel (1 + liftno + rightno) :: mk_rels 0 (rightno + 1)))
177 Cic.Prod (Cic.Anonymous,
178 CicSubstitution.lift_from (rightno + 1) liftno indty,
180 Cic.Rel (1 + liftno + rightno)
182 Cic.Appl (Cic.Rel (1 + liftno + rightno) :: mk_rels 1 rightno))
184 let rec add_right_lambda dependent strip liftno liftfrom rightno indty case =
186 | Cic.Prod (_, src, tgt) when strip = 0 ->
187 Cic.Lambda (fresh_binder true,
188 CicSubstitution.lift_from (liftfrom + 1) liftno src,
189 add_right_lambda dependent strip liftno (liftfrom + 1) rightno indty
191 | Cic.Prod (_, _, tgt) ->
192 add_right_lambda dependent (strip - 1) liftno liftfrom rightno indty
195 Cic.Lambda (fresh_binder true,
196 CicSubstitution.lift_from (rightno + 1) liftno indty, case)
198 let string_of_sort = function
200 | Cic.CProp -> "CProp"
202 | Cic.Type _ -> "Type"
204 let rec branch (uri, typeno, subst) insource paramsno t fix head args =
207 | Cic.MutInd (uri', typeno', subst') when
208 UriManager.eq uri uri' && typeno = typeno' && subst = subst' ->
209 let head = if insource then fix else head in
212 | _ -> Cic.Appl (head :: args))
213 | Cic.Appl (Cic.MutInd (uri', typeno', subst') :: tl) when
214 UriManager.eq uri uri' && typeno = typeno' && subst = subst' ->
216 let (lparams, rparams) = split tl paramsno in
217 Cic.Appl (fix :: rparams @ args)
221 | _ -> Cic.Appl (head :: args))
222 | Cic.Prod (binder, src, tgt) ->
223 if recursive uri typeno subst src then
224 let args = List.map (CicSubstitution.lift 1) args in
226 let fix = CicSubstitution.lift 1 fix in
227 let src = CicSubstitution.lift 1 src in
228 branch (uri, typeno, subst) true paramsno src fix head [Cic.Rel 1]
230 Cic.Lambda (fresh_binder true, src,
231 branch (uri, typeno, subst) insource paramsno tgt
232 (CicSubstitution.lift 1 fix) (CicSubstitution.lift 1 head)
233 (args @ [Cic.Rel 1; phi]))
234 else (* non recursive *)
235 let args = List.map (CicSubstitution.lift 1) args in
236 Cic.Lambda (fresh_binder true, src,
237 branch (uri, typeno, subst) insource paramsno tgt
238 (CicSubstitution.lift 1 fix) (CicSubstitution.lift 1 head)
239 (args @ [Cic.Rel 1]))
242 let branch (uri, typeno, subst) insource liftno paramsno t fix head args =
243 let t = strip_left_params liftno paramsno t in
244 branch (uri, typeno, subst) insource paramsno t fix head args
246 let elim_of ?(sort = Cic.Type (CicUniv.fresh ())) uri typeno =
247 let (obj, univ) = (CicEnvironment.get_obj uri CicUniv.empty_ugraph) in
250 | Cic.InductiveDefinition (indTypes, params, leftno) ->
251 let (name, inductive, ty, constructors) =
253 List.nth indTypes typeno
254 with Failure _ -> assert false
256 let paramsno = count_pi ty in (* number of (left or right) parameters *)
257 let rightno = paramsno - leftno in
258 let dependent = (strip_pi ty <> Cic.Sort Cic.Prop) in
259 let conslen = List.length constructors in
260 let consno = ref (conslen + 1) in
261 if (not dependent) && (sort <> Cic.Prop) && (conslen > 1) then
262 raise Can_t_eliminate;
264 let indty = Cic.MutInd (uri, typeno, subst) in
268 Cic.Appl (indty :: mk_rels 0 paramsno)
270 let mk_constructor consno =
271 let constructor = Cic.MutConstruct (uri, typeno, consno, subst) in
275 Cic.Appl (constructor :: mk_rels consno leftno)
277 let p_ty = type_of_p sort dependent leftno indty ty in
279 add_right_pi dependent leftno (conslen + 1) 1 rightno indty ty
281 let eliminator_type =
283 Cic.Prod (Cic.Name "P", p_ty,
285 (fun (_, constructor) acc ->
287 let p = Cic.Rel !consno in
288 Cic.Prod (Cic.Anonymous,
289 (delta (uri, typeno, subst) dependent leftno !consno
290 constructor p [mk_constructor !consno]),
292 constructors final_ty))
294 add_params (fun b s t -> Cic.Prod (b, s, t)) leftno ty cic
296 let consno = ref (conslen + 1) in
297 let eliminator_body =
298 let fix = Cic.Rel (rightno + 2) in
299 let is_recursive = recursive_type uri typeno subst constructors in
300 let recshift = if is_recursive then 1 else 0 in
303 (fun (_, ty) (shift, branches) ->
304 let head = Cic.Rel (rightno + shift + 1 + recshift) in
306 branch (uri, typeno, subst) false
307 (rightno + conslen + 2 + recshift) leftno ty fix head []
309 (shift + 1, b :: branches))
313 Cic.MutCase (uri, typeno, Cic.Rel (conslen + rightno + 2 + recshift),
319 add_right_lambda dependent leftno (conslen + 2) 2 rightno
322 (* rightno is the decreasing argument, i.e. the argument of
324 Cic.Fix (0, ["f", rightno, final_ty, fixfun])
326 add_right_lambda dependent leftno (conslen + 1) 1 rightno indty
330 Cic.Lambda (Cic.Name "P", p_ty,
332 (fun (_, constructor) acc ->
334 let p = Cic.Rel !consno in
335 Cic.Lambda (fresh_binder true,
336 (delta (uri, typeno, subst) dependent leftno !consno
337 constructor p [mk_constructor !consno]),
341 add_params (fun b s t -> Cic.Lambda (b, s, t)) leftno ty cic
344 prerr_endline (CicPp.ppterm eliminator_type);
345 prerr_endline (CicPp.ppterm eliminator_body);
347 let (computed_type, ugraph) =
349 CicTypeChecker.type_of_aux' [] [] eliminator_body CicUniv.empty_ugraph
350 with CicTypeChecker.TypeCheckerFailure msg ->
351 raise (Elim_failure (sprintf
352 "type checker failure while type checking:\n%s\nerror:\n%s"
353 (CicPp.ppterm eliminator_body) msg))
355 if not (fst (CicReduction.are_convertible []
356 eliminator_type computed_type ugraph))
358 raise (Failure (sprintf
359 "internal error: type mismatch on eliminator type\n%s\n%s"
360 (CicPp.ppterm eliminator_type) (CicPp.ppterm computed_type)));
365 | Cic.Type _ -> "_rect"
368 let name = UriManager.name_of_uri uri ^ suffix in
369 Cic.Constant (name, Some eliminator_body, eliminator_type, [])