]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/ocaml/cic_proof_checking/cicElim.ml
ocaml 3.09 transition
[helm.git] / helm / ocaml / cic_proof_checking / cicElim.ml
index eecb0fae6a97826e037e8c150b14d3493a0d7c67..c668d1c9be33420a1b9222a8646c8c3408b7a436 100644 (file)
 
 open Printf
 
-exception Elim_failure of string
+exception Elim_failure of string Lazy.t
 exception Can_t_eliminate
 
 let debug_print = fun _ -> ()
+(*let debug_print s = prerr_endline (Lazy.force s) *)
 
 let counter = ref ~-1 ;;
 
-let fresh_binder () =
+let fresh_binder () =  Cic.Name "matita_dummy"
+(*
  incr counter;
- Cic.Name ("e" ^ string_of_int !counter)
+ Cic.Name ("e" ^ string_of_int !counter) *)
 
   (** verifies if a given inductive type occurs in a term in target position *)
 let rec recursive uri typeno = function
@@ -253,7 +255,7 @@ let branch (uri, typeno) insource liftno paramsno t fix head args =
   let t = strip_left_params liftno paramsno t in
   branch (uri, typeno) insource paramsno t fix head args
 
-let elim_of ?(sort = Cic.Type (CicUniv.fresh ())) uri typeno =
+let elim_of ~sort uri typeno =
   counter := ~-1;
   let (obj, univ) = (CicEnvironment.get_obj CicUniv.empty_ugraph uri) in
   match obj with
@@ -266,10 +268,14 @@ let elim_of ?(sort = Cic.Type (CicUniv.fresh ())) uri typeno =
       let paramsno = count_pi ty in (* number of (left or right) parameters *)
       let rightno = paramsno - leftno in
       let dependent = (strip_pi ty <> Cic.Sort Cic.Prop) in
+let head = match strip_pi ty with Cic.Sort s -> s in
       let conslen = List.length constructors in
       let consno = ref (conslen + 1) in
-      if (not dependent) && (sort <> Cic.Prop) && (conslen > 1) then
-        raise Can_t_eliminate;
+      if
+       not
+        (CicTypeChecker.check_allowed_sort_elimination uri typeno head sort)
+      then
+       raise Can_t_eliminate;
       let indty =
         let indty = Cic.MutInd (uri, typeno, []) in
         if paramsno = 0 then
@@ -319,9 +325,23 @@ let elim_of ?(sort = Cic.Type (CicUniv.fresh ())) uri typeno =
               (shift + 1,  b :: branches))
             constructors (1, [])
         in
+        let shiftno  = conslen + rightno + 2 + recshift in
+        let outtype =
+         if dependent then
+          Cic.Rel shiftno
+         else
+          let head =
+           if rightno = 0 then
+            CicSubstitution.lift 1 (Cic.Rel shiftno)
+           else
+            Cic.Appl
+             ((CicSubstitution.lift (rightno + 1) (Cic.Rel shiftno)) ::
+              mk_rels 1 rightno)
+          in
+           add_right_lambda true leftno shiftno 1 rightno indty head ty
+        in
         let mutcase =
-          Cic.MutCase (uri, typeno, Cic.Rel (conslen + rightno + 2 + recshift),
-            Cic.Rel 1, branches)
+          Cic.MutCase (uri, typeno, outtype, Cic.Rel 1, branches)
         in
         let body =
           if is_recursive then
@@ -351,16 +371,24 @@ let elim_of ?(sort = Cic.Type (CicUniv.fresh ())) uri typeno =
         add_params (fun b s t -> Cic.Lambda (b, s, t)) leftno ty cic
       in
 (*
-debug_print (CicPp.ppterm eliminator_type);
-debug_print (CicPp.ppterm eliminator_body);
+debug_print (lazy (CicPp.ppterm eliminator_type));
+debug_print (lazy (CicPp.ppterm eliminator_body));
+*)
+      let eliminator_type = 
+       FreshNamesGenerator.mk_fresh_names [] [] [] eliminator_type in
+      let eliminator_body = 
+       FreshNamesGenerator.mk_fresh_names [] [] [] eliminator_body in
+(*
+debug_print (lazy (CicPp.ppterm eliminator_type));
+debug_print (lazy (CicPp.ppterm eliminator_body));
 *)
       let (computed_type, ugraph) =
         try
           CicTypeChecker.type_of_aux' [] [] eliminator_body CicUniv.empty_ugraph
         with CicTypeChecker.TypeCheckerFailure msg ->
-          raise (Elim_failure (sprintf 
+          raise (Elim_failure (lazy (sprintf 
             "type checker failure while type checking:\n%s\nerror:\n%s"
-            (CicPp.ppterm eliminator_body) msg))
+            (CicPp.ppterm eliminator_body) (Lazy.force msg))))
       in
       if not (fst (CicReduction.are_convertible []
         eliminator_type computed_type ugraph))