]> 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 b46aa0b226516bcc8abab2bb9be679ef3583c435..c668d1c9be33420a1b9222a8646c8c3408b7a436 100644 (file)
 
 open Printf
 
-exception Elim_failure of string
+exception Elim_failure of string Lazy.t
 exception Can_t_eliminate
 
-let fresh_binder =
-  let counter = ref ~-1 in
-  function
-    | true ->
-        incr counter;
-        Cic.Name ("e" ^ string_of_int !counter)
-    | _ -> Cic.Anonymous
+let debug_print = fun _ -> ()
+(*let debug_print s = prerr_endline (Lazy.force s) *)
+
+let counter = ref ~-1 ;;
+
+let fresh_binder () =  Cic.Name "matita_dummy"
+(*
+ incr 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
@@ -101,13 +103,13 @@ let rec delta (uri, typeno) dependent paramsno consno t p args =
             (CicSubstitution.lift 1 p) [Cic.Rel 1]
         in
         let tgt = CicSubstitution.lift 1 tgt in
-        Cic.Prod (fresh_binder dependent, src,
+        Cic.Prod (fresh_binder (), src,
           Cic.Prod (Cic.Anonymous, phi,
             delta (uri, typeno) dependent paramsno consno tgt
               (CicSubstitution.lift 2 p) (args @ [Cic.Rel 2])))
       else  (* non recursive *)
         let args = List.map (CicSubstitution.lift 1) args in
-        Cic.Prod (fresh_binder dependent, src,
+        Cic.Prod (fresh_binder (), src,
           delta (uri, typeno) dependent paramsno consno tgt
             (CicSubstitution.lift 1 p) (args @ [Cic.Rel 1]))
   | _ -> assert false
@@ -134,6 +136,11 @@ let rec add_params binder indno ty eliminator =
   else
     match ty with
     | Cic.Prod (name, src, tgt) ->
+       let name =
+        match name with
+           Cic.Name _ -> name
+         | Cic.Anonymous -> fresh_binder ()
+       in
         binder name src (add_params binder (indno - 1) tgt eliminator)
     | _ -> assert false
 
@@ -151,7 +158,15 @@ let rec count_pi = function
 
 let rec type_of_p sort dependent leftno indty = function
   | Cic.Prod (n, src, tgt) when leftno = 0 ->
-      Cic.Prod (n, src, type_of_p sort dependent leftno indty tgt)
+      let n =
+       if dependent then 
+        match n with
+           Cic.Name _ -> n
+         | Cic.Anonymous -> fresh_binder ()
+       else
+        n
+      in
+       Cic.Prod (n, src, type_of_p sort dependent leftno indty tgt)
   | Cic.Prod (_, _, tgt) -> type_of_p sort dependent (leftno - 1) indty tgt
   | t ->
       if dependent then
@@ -161,14 +176,14 @@ let rec type_of_p sort dependent leftno indty = function
 
 let rec add_right_pi dependent strip liftno liftfrom rightno indty = function
   | Cic.Prod (_, src, tgt) when strip = 0 ->
-      Cic.Prod (fresh_binder true,
+      Cic.Prod (fresh_binder (),
         CicSubstitution.lift_from liftfrom liftno src,
         add_right_pi dependent strip liftno (liftfrom + 1) rightno indty tgt)
   | Cic.Prod (_, _, tgt) ->
       add_right_pi dependent (strip - 1) liftno liftfrom rightno indty tgt
   | t ->
       if dependent then
-        Cic.Prod (fresh_binder dependent,
+        Cic.Prod (fresh_binder (),
           CicSubstitution.lift_from (rightno + 1) liftno indty,
           Cic.Appl (Cic.Rel (1 + liftno + rightno) :: mk_rels 0 (rightno + 1)))
       else
@@ -182,23 +197,17 @@ let rec add_right_pi dependent strip liftno liftfrom rightno indty = function
 let rec add_right_lambda dependent strip liftno liftfrom rightno indty case =
 function
   | Cic.Prod (_, src, tgt) when strip = 0 ->
-      Cic.Lambda (fresh_binder true,
+      Cic.Lambda (fresh_binder (),
         CicSubstitution.lift_from liftfrom liftno src,
         add_right_lambda dependent strip liftno (liftfrom + 1) rightno indty
           case tgt)
   | Cic.Prod (_, _, tgt) ->
-      add_right_lambda dependent (strip - 1) liftno liftfrom rightno indty
+      add_right_lambda true (strip - 1) liftno liftfrom rightno indty
         case tgt
   | t ->
-      Cic.Lambda (fresh_binder true,
+      Cic.Lambda (fresh_binder (),
         CicSubstitution.lift_from (rightno + 1) liftno indty, case)
 
-let string_of_sort = function
-  | Cic.Prop -> "Prop"
-  | Cic.CProp -> "CProp"
-  | Cic.Set -> "Set"
-  | Cic.Type _ -> "Type"
-
 let rec branch (uri, typeno) insource paramsno t fix head args =
   match t with
   | Cic.MutInd (uri', typeno', []) when
@@ -230,13 +239,13 @@ let rec branch (uri, typeno) insource paramsno t fix head args =
           let src = CicSubstitution.lift 1 src in
           branch (uri, typeno) true paramsno src fix head [Cic.Rel 1]
         in
-        Cic.Lambda (fresh_binder true, src,
+        Cic.Lambda (fresh_binder (), src,
           branch (uri, typeno) insource paramsno tgt
             (CicSubstitution.lift 1 fix) (CicSubstitution.lift 1 head)
             (args @ [Cic.Rel 1; phi]))
       else  (* non recursive *)
         let args = List.map (CicSubstitution.lift 1) args in
-        Cic.Lambda (fresh_binder true, src,
+        Cic.Lambda (fresh_binder (), src,
           branch (uri, typeno) insource paramsno tgt
           (CicSubstitution.lift 1 fix) (CicSubstitution.lift 1 head)
             (args @ [Cic.Rel 1]))
@@ -246,10 +255,11 @@ 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 (obj, univ) = (CicEnvironment.get_obj uri CicUniv.empty_ugraph) in
+let elim_of ~sort uri typeno =
+  counter := ~-1;
+  let (obj, univ) = (CicEnvironment.get_obj CicUniv.empty_ugraph uri) in
   match obj with
-  | Cic.InductiveDefinition (indTypes, params, leftno) ->
+  | Cic.InductiveDefinition (indTypes, params, leftno, _) ->
       let (name, inductive, ty, constructors) =
         try
           List.nth indTypes typeno
@@ -258,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
@@ -311,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
@@ -334,7 +362,7 @@ let elim_of ?(sort = Cic.Type (CicUniv.fresh ())) uri typeno =
               (fun (_, constructor) acc ->
                 decr consno;
                 let p = Cic.Rel !consno in
-                Cic.Lambda (fresh_binder true,
+                Cic.Lambda (fresh_binder (),
                   (delta (uri, typeno) dependent leftno !consno
                     constructor p [mk_constructor !consno]),
                   acc))
@@ -343,21 +371,25 @@ 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
 (*
-prerr_endline (CicPp.ppterm eliminator_type);
-prerr_endline (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));
 *)
-prerr_endline "generato l'eliminatore";
-prerr_endline "inizio type checking";
       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
-prerr_endline "fine type checking";
-prerr_endline "inizio are convertible";
       if not (fst (CicReduction.are_convertible []
         eliminator_type computed_type ugraph))
       then
@@ -372,6 +404,12 @@ prerr_endline "inizio are convertible";
         | _ -> assert false
       in
       let name = UriManager.name_of_uri uri ^ suffix in
-      Cic.Constant (name, Some eliminator_body, eliminator_type, [])
-  | _ -> assert false
+      let buri = UriManager.buri_of_uri uri in
+      let uri = UriManager.uri_of_string (buri ^ "/" ^ name ^ ".con") in
+      let obj_attrs = [`Class (`Elim sort); `Generated] in
+       uri,
+       Cic.Constant (name, Some eliminator_body, eliminator_type, [], obj_attrs)
+  | _ ->
+      failwith (sprintf "not an inductive definition (%s)"
+        (UriManager.string_of_uri uri))