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/
29 let counter = ref ~-1 in
33 Cic.Name ("elim" ^ string_of_int !counter)
36 (** verifies if a given uri occurs in a term in target position *)
37 let rec recursive uri = function
38 | Cic.Prod (_, _, target) -> recursive uri target
39 | Cic.MutInd (uri', _, _) -> UriManager.eq uri uri'
40 | Cic.Appl args -> List.exists (recursive uri) args
43 let unfold_appl = function
44 | Cic.Appl ((Cic.Appl args) :: tl) -> Cic.Appl (args @ tl)
50 | (he::tl, n) -> let (l1,l2) = split tl (n-1) in (he::l1,l2)
51 | (_,_) -> assert false
53 (** build elimination principle part related to a single constructor
54 * @param paramsno number of Prod to ignore in this constructor (i.e. number of
55 * inductive parameters)
56 * @param dependent true if we are in the dependent case (i.e. sort <> Prop) *)
57 let rec delta (uri, typeno, subst) dependent paramsno consno t p args =
60 | Cic.MutInd (uri', typeno', subst') ->
64 | [arg] -> unfold_appl (Cic.Appl [p; arg])
65 | _ -> unfold_appl (Cic.Appl [p; unfold_appl (Cic.Appl args)]))
68 | Cic.Appl (Cic.MutInd (uri', typeno', subst') :: tl) when
69 UriManager.eq uri uri' && typeno = typeno' && subst = subst' ->
70 let (lparams, rparams) = split tl paramsno in
74 | [arg] -> unfold_appl (Cic.Appl (p :: rparams @ [arg]))
76 unfold_appl (Cic.Appl (p ::
77 rparams @ [unfold_appl (Cic.Appl args)])))
78 else (* non dependent *)
81 | _ -> Cic.Appl (p :: rparams))
82 | Cic.Prod (binder, src, tgt) ->
83 if recursive uri src then
84 let args = List.map (CicSubstitution.lift 2) args in
86 let src = CicSubstitution.lift 1 src in
87 delta (uri, typeno, subst) dependent paramsno consno src
88 (CicSubstitution.lift 1 p) [Cic.Rel 1]
90 let tgt = CicSubstitution.lift 1 tgt in
91 Cic.Prod (fresh_binder dependent, src,
92 Cic.Prod (Cic.Anonymous, phi,
93 delta (uri, typeno, subst) dependent paramsno consno tgt
94 (CicSubstitution.lift 2 p) (args @ [Cic.Rel 2])))
95 else (* non recursive *)
96 let args = List.map (CicSubstitution.lift 1) args in
97 Cic.Prod (fresh_binder dependent, src,
98 delta (uri, typeno, subst) dependent paramsno consno tgt
99 (CicSubstitution.lift 1 p) (args @ [Cic.Rel 1]))
102 let rec strip_left_params consno leftno = function
103 | t when leftno = 0 -> t (* no need to lift, the term is (hopefully) closed *)
104 | Cic.Prod (_, _, tgt) (* when leftno > 0 *) ->
105 (* after stripping the parameters we lift of consno. consno is 1 based so,
106 * the first constructor will be lifted by 1 (for P), the second by 2 (1
107 * for P and 1 for the 1st constructor), and so on *)
109 CicSubstitution.lift consno tgt
111 strip_left_params consno (leftno - 1) tgt
114 let delta (ury, typeno, subst) dependent paramsno consno t p args =
115 let t = strip_left_params consno paramsno t in
116 delta (ury, typeno, subst) dependent paramsno consno t p args
118 let rec add_params indno ty eliminator =
123 | Cic.Prod (binder, src, tgt) ->
124 Cic.Prod (binder, src, add_params (indno - 1) tgt eliminator)
127 let rec mk_rels consno = function
129 | n -> Cic.Rel (n+consno) :: mk_rels consno (n-1)
131 let rec strip_pi = function
132 | Cic.Prod (_, _, tgt) -> strip_pi tgt
135 let rec count_pi = function
136 | Cic.Prod (_, _, tgt) -> count_pi tgt + 1
139 let rec type_of_p sort dependent leftno indty = function
140 | Cic.Prod (n, src, tgt) when leftno = 0 ->
141 Cic.Prod (n, src, type_of_p sort dependent leftno indty tgt)
142 | Cic.Prod (_, _, tgt) -> type_of_p sort dependent (leftno - 1) indty tgt
145 Cic.Prod (Cic.Anonymous, indty, Cic.Sort sort)
149 let rec add_right_pi dependent strip liftno liftfrom rightno indty = function
150 | Cic.Prod (_, src, tgt) when strip = 0 ->
151 Cic.Prod (fresh_binder true,
152 CicSubstitution.lift_from (liftfrom + 1) liftno src,
153 add_right_pi dependent strip liftno (liftfrom + 1) rightno indty tgt)
154 | Cic.Prod (_, _, tgt) ->
155 add_right_pi dependent (strip - 1) liftno liftfrom rightno indty tgt
158 Cic.Prod (fresh_binder dependent,
159 CicSubstitution.lift_from (rightno + 1) liftno indty,
160 Cic.Appl (Cic.Rel (1 + liftno + rightno) :: mk_rels 0 (rightno + 1)))
162 Cic.Prod (Cic.Anonymous,
163 CicSubstitution.lift_from (rightno + 1) liftno indty,
165 Cic.Rel (1 + liftno + rightno)
167 Cic.Appl (Cic.Rel (1 + liftno + rightno) :: mk_rels 1 rightno))
169 exception Failure of string
171 let string_of_sort = function
173 | Cic.CProp -> "CProp"
175 | Cic.Type _ -> "Type"
177 let elim_of ?(sort = Cic.Type (CicUniv.fresh ())) uri typeno =
178 let (obj, univ) = (CicEnvironment.get_obj uri CicUniv.empty_ugraph) in
181 | Cic.InductiveDefinition (indTypes, params, leftno) ->
182 let (name, inductive, ty, constructors) =
184 List.nth indTypes typeno
185 with Failure _ -> assert false
187 let paramsno = count_pi ty in (* number of (left or right) parameters *)
188 let dependent = (strip_pi ty <> Cic.Sort Cic.Prop) in
189 let conslen = List.length constructors in
190 let consno = ref (conslen + 1) in
191 if (not dependent) && (sort <> Cic.Prop) && (conslen > 1) then
192 raise (Failure (sprintf "can't eliminate from Prop to %s"
193 (string_of_sort sort)));
195 let indty = Cic.MutInd (uri, typeno, subst) in
199 Cic.Appl (indty :: mk_rels 0 paramsno)
201 let mk_constructor consno =
202 let constructor = Cic.MutConstruct (uri, typeno, consno, subst) in
206 Cic.Appl (constructor :: mk_rels consno leftno)
209 let p_ty = type_of_p sort dependent leftno indty ty in
211 add_right_pi dependent leftno (conslen + 1) 1 (paramsno - leftno)
214 Cic.Prod (Cic.Name "P", p_ty,
216 (fun (_, constructor) acc ->
218 let p = Cic.Rel !consno in
219 Cic.Prod (Cic.Anonymous,
220 (delta (uri, typeno, subst) dependent leftno !consno
221 constructor p [mk_constructor !consno]),
226 add_params leftno ty eliminator