]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_proof_checking/cicElim.ml
alim of now resets the counter
[helm.git] / helm / ocaml / cic_proof_checking / cicElim.ml
1 (* Copyright (C) 2004, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 open Printf
27
28 exception Elim_failure of string
29 exception Can_t_eliminate
30
31 let debug_print = fun _ -> ()
32
33 let counter = ref ~-1 ;;
34
35 let fresh_binder =
36   function
37     | true ->
38         incr counter;
39         Cic.Name ("e" ^ string_of_int !counter)
40     | _ -> Cic.Anonymous
41
42   (** verifies if a given inductive type occurs in a term in target position *)
43 let rec recursive uri typeno = function
44   | Cic.Prod (_, _, target) -> recursive uri typeno target
45   | Cic.MutInd (uri', typeno', [])
46   | Cic.Appl (Cic.MutInd  (uri', typeno', []) :: _) ->
47       UriManager.eq uri uri' && typeno = typeno'
48   | _ -> false
49
50   (** given a list of constructor types, return true if at least one of them is
51   * recursive, false otherwise *)
52 let recursive_type uri typeno constructors =
53   let rec aux = function
54     | Cic.Prod (_, src, tgt) -> recursive uri typeno src || aux tgt
55     | _ -> false
56   in
57   List.exists (fun (_, ty) -> aux ty) constructors
58
59 let unfold_appl = function
60   | Cic.Appl ((Cic.Appl args) :: tl) -> Cic.Appl (args @ tl)
61   | t -> t
62
63 let rec split l n =
64  match (l,n) with
65     (l,0) -> ([], l)
66   | (he::tl, n) -> let (l1,l2) = split tl (n-1) in (he::l1,l2)
67   | (_,_) -> assert false
68
69   (** build elimination principle part related to a single constructor
70   * @param paramsno number of Prod to ignore in this constructor (i.e. number of
71   * inductive parameters)
72   * @param dependent true if we are in the dependent case (i.e. sort <> Prop) *)
73 let rec delta (uri, typeno) dependent paramsno consno t p args =
74   match t with
75   | Cic.MutInd (uri', typeno', []) when
76     UriManager.eq uri uri' && typeno = typeno' ->
77       if dependent then
78         (match args with
79         | [] -> assert false
80         | [arg] -> unfold_appl (Cic.Appl [p; arg])
81         | _ -> unfold_appl (Cic.Appl [p; unfold_appl (Cic.Appl args)]))
82       else
83         p
84   | Cic.Appl (Cic.MutInd (uri', typeno', []) :: tl) when
85     UriManager.eq uri uri' && typeno = typeno' ->
86       let (lparams, rparams) = split tl paramsno in
87       if dependent then
88         (match args with
89         | [] -> assert false
90         | [arg] -> unfold_appl (Cic.Appl (p :: rparams @ [arg]))
91         | _ ->
92             unfold_appl (Cic.Appl (p ::
93               rparams @ [unfold_appl (Cic.Appl args)])))
94       else  (* non dependent *)
95         (match rparams with
96         | [] -> p
97         | _ -> Cic.Appl (p :: rparams))
98   | Cic.Prod (binder, src, tgt) ->
99       if recursive uri typeno src then
100         let args = List.map (CicSubstitution.lift 2) args in
101         let phi =
102           let src = CicSubstitution.lift 1 src in
103           delta (uri, typeno) dependent paramsno consno src
104             (CicSubstitution.lift 1 p) [Cic.Rel 1]
105         in
106         let tgt = CicSubstitution.lift 1 tgt in
107         Cic.Prod (fresh_binder dependent, src,
108           Cic.Prod (Cic.Anonymous, phi,
109             delta (uri, typeno) dependent paramsno consno tgt
110               (CicSubstitution.lift 2 p) (args @ [Cic.Rel 2])))
111       else  (* non recursive *)
112         let args = List.map (CicSubstitution.lift 1) args in
113         Cic.Prod (fresh_binder dependent, src,
114           delta (uri, typeno) dependent paramsno consno tgt
115             (CicSubstitution.lift 1 p) (args @ [Cic.Rel 1]))
116   | _ -> assert false
117
118 let rec strip_left_params consno leftno = function
119   | t when leftno = 0 -> t (* no need to lift, the term is (hopefully) closed *)
120   | Cic.Prod (_, _, tgt) (* when leftno > 0 *) ->
121       (* after stripping the parameters we lift of consno. consno is 1 based so,
122       * the first constructor will be lifted by 1 (for P), the second by 2 (1
123       * for P and 1 for the 1st constructor), and so on *)
124       if leftno = 1 then
125         CicSubstitution.lift consno tgt
126       else
127         strip_left_params consno (leftno - 1) tgt
128   | _ -> assert false
129
130 let delta (ury, typeno) dependent paramsno consno t p args =
131   let t = strip_left_params consno paramsno t in
132   delta (ury, typeno) dependent paramsno consno t p args
133
134 let rec add_params binder indno ty eliminator =
135   if indno = 0 then
136     eliminator
137   else
138     match ty with
139     | Cic.Prod (name, src, tgt) ->
140         binder name src (add_params binder (indno - 1) tgt eliminator)
141     | _ -> assert false
142
143 let rec mk_rels consno = function
144   | 0 -> []
145   | n -> Cic.Rel (n+consno) :: mk_rels consno (n-1)
146
147 let rec strip_pi = function
148   | Cic.Prod (_, _, tgt) -> strip_pi tgt
149   | t -> t
150
151 let rec count_pi = function
152   | Cic.Prod (_, _, tgt) -> count_pi tgt + 1
153   | t -> 0
154
155 let rec type_of_p sort dependent leftno indty = function
156   | Cic.Prod (n, src, tgt) when leftno = 0 ->
157       Cic.Prod (n, src, type_of_p sort dependent leftno indty tgt)
158   | Cic.Prod (_, _, tgt) -> type_of_p sort dependent (leftno - 1) indty tgt
159   | t ->
160       if dependent then
161         Cic.Prod (Cic.Anonymous, indty, Cic.Sort sort)
162       else
163         Cic.Sort sort
164
165 let rec add_right_pi dependent strip liftno liftfrom rightno indty = function
166   | Cic.Prod (_, src, tgt) when strip = 0 ->
167       Cic.Prod (fresh_binder true,
168         CicSubstitution.lift_from liftfrom liftno src,
169         add_right_pi dependent strip liftno (liftfrom + 1) rightno indty tgt)
170   | Cic.Prod (_, _, tgt) ->
171       add_right_pi dependent (strip - 1) liftno liftfrom rightno indty tgt
172   | t ->
173       if dependent then
174         Cic.Prod (fresh_binder dependent,
175           CicSubstitution.lift_from (rightno + 1) liftno indty,
176           Cic.Appl (Cic.Rel (1 + liftno + rightno) :: mk_rels 0 (rightno + 1)))
177       else
178         Cic.Prod (Cic.Anonymous,
179           CicSubstitution.lift_from (rightno + 1) liftno indty,
180           if rightno = 0 then
181             Cic.Rel (1 + liftno + rightno)
182           else
183             Cic.Appl (Cic.Rel (1 + liftno + rightno) :: mk_rels 1 rightno))
184
185 let rec add_right_lambda dependent strip liftno liftfrom rightno indty case =
186 function
187   | Cic.Prod (_, src, tgt) when strip = 0 ->
188       Cic.Lambda (fresh_binder true,
189         CicSubstitution.lift_from liftfrom liftno src,
190         add_right_lambda dependent strip liftno (liftfrom + 1) rightno indty
191           case tgt)
192   | Cic.Prod (_, _, tgt) ->
193       add_right_lambda dependent (strip - 1) liftno liftfrom rightno indty
194         case tgt
195   | t ->
196       Cic.Lambda (fresh_binder true,
197         CicSubstitution.lift_from (rightno + 1) liftno indty, case)
198
199 let rec branch (uri, typeno) insource paramsno t fix head args =
200   match t with
201   | Cic.MutInd (uri', typeno', []) when
202     UriManager.eq uri uri' && typeno = typeno' ->
203       if insource then
204         (match args with
205         | [arg] -> Cic.Appl (fix :: args)
206         | _ -> Cic.Appl (head :: [Cic.Appl args]))
207       else
208         (match args with
209         | [] -> head
210         | _ -> Cic.Appl (head :: args))
211   | Cic.Appl (Cic.MutInd (uri', typeno', []) :: tl) when
212     UriManager.eq uri uri' && typeno = typeno' ->
213       if insource then
214         let (lparams, rparams) = split tl paramsno in
215         match args with
216         | [arg] -> Cic.Appl (fix :: rparams @ args)
217         | _ -> Cic.Appl (fix :: rparams @ [Cic.Appl args])
218       else
219         (match args with
220         | [] -> head
221         | _ -> Cic.Appl (head :: args))
222   | Cic.Prod (binder, src, tgt) ->
223       if recursive uri typeno src then
224         let args = List.map (CicSubstitution.lift 1) args in
225         let phi =
226           let fix = CicSubstitution.lift 1 fix in
227           let src = CicSubstitution.lift 1 src in
228           branch (uri, typeno) true paramsno src fix head [Cic.Rel 1]
229         in
230         Cic.Lambda (fresh_binder true, src,
231           branch (uri, typeno) 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) insource paramsno tgt
238           (CicSubstitution.lift 1 fix) (CicSubstitution.lift 1 head)
239             (args @ [Cic.Rel 1]))
240   | _ -> assert false
241
242 let branch (uri, typeno) insource liftno paramsno t fix head args =
243   let t = strip_left_params liftno paramsno t in
244   branch (uri, typeno) insource paramsno t fix head args
245
246 let elim_of ?(sort = Cic.Type (CicUniv.fresh ())) uri typeno =
247   counter := ~-1;
248   let (obj, univ) = (CicEnvironment.get_obj CicUniv.empty_ugraph uri) in
249   match obj with
250   | Cic.InductiveDefinition (indTypes, params, leftno, _) ->
251       let (name, inductive, ty, constructors) =
252         try
253           List.nth indTypes typeno
254         with Failure _ -> assert false
255       in
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;
263       let indty =
264         let indty = Cic.MutInd (uri, typeno, []) in
265         if paramsno = 0 then
266           indty
267         else
268           Cic.Appl (indty :: mk_rels 0 paramsno)
269       in
270       let mk_constructor consno =
271         let constructor = Cic.MutConstruct (uri, typeno, consno, []) in
272         if leftno = 0 then
273           constructor
274         else
275           Cic.Appl (constructor :: mk_rels consno leftno)
276       in
277       let p_ty = type_of_p sort dependent leftno indty ty in
278       let final_ty =
279         add_right_pi dependent leftno (conslen + 1) 1 rightno indty ty
280       in
281       let eliminator_type =
282         let cic =
283           Cic.Prod (Cic.Name "P", p_ty,
284             (List.fold_right
285               (fun (_, constructor) acc ->
286                 decr consno;
287                 let p = Cic.Rel !consno in
288                 Cic.Prod (Cic.Anonymous,
289                   (delta (uri, typeno) dependent leftno !consno
290                     constructor p [mk_constructor !consno]),
291                   acc))
292               constructors final_ty))
293         in
294         add_params (fun b s t -> Cic.Prod (b, s, t)) leftno ty cic
295       in
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 constructors in
300         let recshift = if is_recursive then 1 else 0 in
301         let (_, branches) =
302           List.fold_right
303             (fun (_, ty) (shift, branches) ->
304               let head = Cic.Rel (rightno + shift + 1 + recshift) in
305               let b =
306                 branch (uri, typeno) false
307                   (rightno + conslen + 2 + recshift) leftno ty fix head []
308               in
309               (shift + 1,  b :: branches))
310             constructors (1, [])
311         in
312         let mutcase =
313           Cic.MutCase (uri, typeno, Cic.Rel (conslen + rightno + 2 + recshift),
314             Cic.Rel 1, branches)
315         in
316         let body =
317           if is_recursive then
318             let fixfun =
319               add_right_lambda dependent leftno (conslen + 2) 1 rightno
320                 indty mutcase ty
321             in
322             (* rightno is the decreasing argument, i.e. the argument of
323              * inductive type *)
324             Cic.Fix (0, ["f", rightno, final_ty, fixfun])
325           else
326             add_right_lambda dependent leftno (conslen + 1) 1 rightno indty
327               mutcase ty
328         in
329         let cic =
330           Cic.Lambda (Cic.Name "P", p_ty,
331             (List.fold_right
332               (fun (_, constructor) acc ->
333                 decr consno;
334                 let p = Cic.Rel !consno in
335                 Cic.Lambda (fresh_binder true,
336                   (delta (uri, typeno) dependent leftno !consno
337                     constructor p [mk_constructor !consno]),
338                   acc))
339               constructors body))
340         in
341         add_params (fun b s t -> Cic.Lambda (b, s, t)) leftno ty cic
342       in
343 (*
344 debug_print (CicPp.ppterm eliminator_type);
345 debug_print (CicPp.ppterm eliminator_body);
346 *)
347       let (computed_type, ugraph) =
348         try
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))
354       in
355       if not (fst (CicReduction.are_convertible []
356         eliminator_type computed_type ugraph))
357       then
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)));
361       let suffix =
362         match sort with
363         | Cic.Prop -> "_ind"
364         | Cic.Set -> "_rec"
365         | Cic.Type _ -> "_rect"
366         | _ -> assert false
367       in
368       let name = UriManager.name_of_uri uri ^ suffix in
369       let buri = UriManager.buri_of_uri uri in
370       let uri = UriManager.uri_of_string (buri ^ "/" ^ name ^ ".con") in
371       let obj_attrs = [`Class (`Elim sort); `Generated] in
372        uri,
373        Cic.Constant (name, Some eliminator_body, eliminator_type, [], obj_attrs)
374   | _ ->
375       failwith (sprintf "not an inductive definition (%s)"
376         (UriManager.string_of_uri uri))
377