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/
27 let counter = ref ~-1 in
31 Cic.Name ("elim" ^ string_of_int !counter)
34 (** verifies if a given uri occurs in a term in target position *)
35 let rec recursive uri = function
36 | Cic.Prod (_, _, target) -> recursive uri target
37 | Cic.MutInd (uri', _, _) -> UriManager.eq uri uri'
38 | Cic.Appl args -> List.exists (recursive uri) args
41 let unfold_appl = function
42 | Cic.Appl ((Cic.Appl args) :: tl) -> Cic.Appl (args @ tl)
48 | (he::tl, n) -> let (l1,l2) = split tl (n-1) in (he::l1,l2)
49 | (_,_) -> assert false
51 (** build elimination principle part related to a single constructor
52 * @param paramsno number of Prod to ignore in this constructor (i.e. number of
53 * inductive parameters)
54 * @param dependent true if we are in the dependent case (i.e. sort <> Prop) *)
55 let rec delta (uri, typeno, subst) dependent paramsno consno t p args =
58 | Cic.MutInd (uri', typeno', subst') ->
62 | [arg] -> unfold_appl (Cic.Appl [p; arg])
63 | _ -> unfold_appl (Cic.Appl [p; unfold_appl (Cic.Appl args)]))
66 | Cic.Appl (Cic.MutInd (uri', typeno', subst') :: tl) when
67 UriManager.eq uri uri' && typeno = typeno' && subst = subst' ->
68 let (lparams, rparams) = split tl paramsno in
72 | [arg] -> unfold_appl (Cic.Appl (p :: rparams @ [arg]))
74 unfold_appl (Cic.Appl (p ::
75 rparams @ [unfold_appl (Cic.Appl args)])))
76 else (* non dependent *)
79 | _ -> Cic.Appl (p :: rparams))
80 | Cic.Prod (binder, src, tgt) ->
81 if recursive uri src then
82 let args = List.map (CicSubstitution.lift 2) args in
84 (delta (uri, typeno, subst) dependent paramsno consno src
85 (CicSubstitution.lift 1 p) [Cic.Rel 1])
87 Cic.Prod (fresh_binder dependent, src,
88 Cic.Prod (Cic.Anonymous, phi,
89 delta (uri, typeno, subst) dependent paramsno consno tgt
90 (CicSubstitution.lift 2 p) (args @ [Cic.Rel 2])))
91 else (* non recursive *)
92 let args = List.map (CicSubstitution.lift 1) args in
93 Cic.Prod (fresh_binder dependent, src,
94 delta (uri, typeno, subst) dependent paramsno consno tgt
95 (CicSubstitution.lift 1 p) (args @ [Cic.Rel 1]))
98 let rec strip_left_params consno leftno = function
99 | t when leftno = 0 -> t (* no need to lift, the term is (hopefully) closed *)
100 | Cic.Prod (_, _, tgt) (* when leftno > 0 *) ->
101 (* after stripping the parameters we lift of consno. consno is 1 based so,
102 * the first constructor will be lifted by 1 (for P), the second by 2 (1
103 * for P and 1 for the 1st constructor), and so on *)
105 CicSubstitution.lift consno tgt
107 strip_left_params consno (leftno - 1) tgt
110 let delta (ury, typeno, subst) dependent paramsno consno t p args =
111 let t = strip_left_params consno paramsno t in
112 delta (ury, typeno, subst) dependent paramsno consno t p args
114 let rec add_params indno ty eliminator =
119 | Cic.Prod (binder, src, tgt) ->
120 Cic.Prod (binder, src, add_params (indno - 1) tgt eliminator)
123 let rec mk_rels consno = function
125 | n -> Cic.Rel (n+consno) :: mk_rels consno (n-1)
127 let rec strip_pi = function
128 | Cic.Prod (_, _, tgt) -> strip_pi tgt
131 let rec count_pi = function
132 | Cic.Prod (_, _, tgt) -> count_pi tgt + 1
135 let rec type_of_p dependent leftno indty = function
136 | Cic.Prod (n, src, tgt) when leftno = 0 ->
137 Cic.Prod (n, src, type_of_p dependent leftno indty tgt)
138 | Cic.Prod (_, _, tgt) -> type_of_p dependent (leftno - 1) indty tgt
141 Cic.Prod (Cic.Anonymous, indty,
142 Cic.Sort (Cic.Type (CicUniv.fresh ())))
144 Cic.Sort (Cic.Type (CicUniv.fresh ()))
146 let rec add_right_pi dependent strip liftno rightno indty = function
147 | Cic.Prod (_, src, tgt) when strip = 0 ->
148 Cic.Prod (fresh_binder true,
149 CicSubstitution.lift liftno src,
150 add_right_pi dependent strip liftno rightno indty tgt)
151 | Cic.Prod (_, _, tgt) ->
152 add_right_pi dependent (strip - 1) liftno rightno indty tgt
155 Cic.Prod (fresh_binder dependent,
156 CicSubstitution.lift_from (rightno + 1) liftno indty,
157 Cic.Appl (Cic.Rel (1 + liftno + rightno) :: mk_rels 0 (rightno + 1)))
159 Cic.Prod (Cic.Anonymous,
160 CicSubstitution.lift_from (rightno + 1) liftno indty,
162 Cic.Rel (1 + liftno + rightno)
164 Cic.Appl (Cic.Rel (1 + liftno + rightno) :: mk_rels 1 rightno))
166 let elim_of uri typeno =
167 let (obj, univ) = (CicEnvironment.get_obj uri CicUniv.empty_ugraph) in
170 | Cic.InductiveDefinition (indTypes, params, leftno) ->
171 let (name, inductive, ty, constructors) =
173 List.nth indTypes typeno
174 with Failure _ -> assert false
176 let paramsno = count_pi ty in (* number of (left or right) parameters *)
177 let dependent = (strip_pi ty <> Cic.Sort Cic.Prop) in
178 let conslen = List.length constructors in
179 let consno = ref (conslen + 1) in
181 let indty = Cic.MutInd (uri, typeno, subst) in
185 Cic.Appl (indty :: mk_rels 0 paramsno)
187 let mk_constructor consno =
188 let constructor = Cic.MutConstruct (uri, typeno, consno, subst) in
192 Cic.Appl (constructor :: mk_rels consno leftno)
195 let p_ty = type_of_p dependent leftno indty ty in
197 add_right_pi dependent leftno (conslen + 1) (paramsno - leftno)
200 Cic.Prod (Cic.Name "P", p_ty,
202 (fun (_, constructor) acc ->
204 let p = Cic.Rel !consno in
205 Cic.Prod (Cic.Anonymous,
206 (delta (uri, typeno, subst) dependent leftno !consno
207 constructor p [mk_constructor !consno]),
212 add_params leftno ty eliminator