]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/software/components/ng_kernel/nCicTypeChecker.ml
...
[helm.git] / helm / software / components / ng_kernel / nCicTypeChecker.ml
index a1fe5d3b1be09b4c3b1d310e685ea3cf177bb44d..bd660903cc86fa682b74fbff028694f46c2e2e0f 100644 (file)
@@ -22,6 +22,11 @@ module PP = NCicPp
 exception TypeCheckerFailure of string Lazy.t
 exception AssertFailure of string Lazy.t
 
+let raise = function
+  | TypeCheckerFailure s as e -> prerr_endline (Lazy.force s); raise e
+  | e -> raise e
+;;
+
 type recf_entry = 
   | Evil of int (* rno *) 
   | UnfFix of bool list (* fixed arguments *) 
@@ -89,10 +94,12 @@ let fixed_args bos j n nn =
    (let rec f = function 0 -> [] | n -> true :: f (n-1) in f j) bos
 ;;
 
+(* if n < 0, then splits all prods from an arity, returning a sort *)
 let rec split_prods ~subst context n te =
   match (n, R.whd ~subst context te) with
    | (0, _) -> context,te
-   | (n, C.Prod (name,so,ta)) when n > 0 ->
+   | (n, C.Sort _) when n <= 0 -> context,te
+   | (n, C.Prod (name,so,ta)) ->
        split_prods ~subst ((name,(C.Decl so))::context) (n - 1) ta
    | (_, _) -> raise (AssertFailure (lazy "split_prods"))
 ;;
@@ -117,7 +124,7 @@ let sort_of_prod ~metasenv ~subst context (name,s) (t1, t2) =
    let t2 = R.whd ~subst ((name,C.Decl s)::context) t2 in
    match t1, t2 with
    | C.Sort s1, C.Sort C.Prop -> t2
-   | C.Sort (C.Type u1), C.Sort (C.Type u2) -> C.Sort (C.Type (max u1 u2)) 
+   | C.Sort (C.Type u1), C.Sort (C.Type u2) -> C.Sort (C.Type (u1@u2)) 
    | C.Sort _,C.Sort (C.Type _) -> t2
    | C.Sort (C.Type _) , C.Sort C.CProp -> t1
    | C.Sort _, C.Sort C.CProp
@@ -265,7 +272,7 @@ let rec eat_or_subst_lambdas ~subst ~metasenv n te to_be_subst args
 (*CSC definizione (giusta???) tratta dalla mail di Hugo ;-)              *)
 let rec weakly_positive ~subst context n nn uri te =
 (*CSC: Che schifo! Bisogna capire meglio e trovare una soluzione ragionevole!*)
-  let dummy = C.Sort (C.Type ~-1) in
+  let dummy = C.Sort C.Prop in
   (*CSC: mettere in cicSubstitution *)
   let rec subst_inductive_type_with_dummy _ = function
     | C.Const (Ref.Ref (uri',Ref.Ind (true,0))) when NUri.eq uri' uri -> dummy
@@ -359,6 +366,7 @@ and are_all_occurrences_positive ~subst context uri indparamsno i n nn te =
        are_all_occurrences_positive ~subst ((name,C.Decl source)::context)
         uri indparamsno (i+1) (n + 1) (nn + 1) dest
    | _ ->
+prerr_endline ("MM: " ^ NCicPp.ppterm ~subst ~metasenv:[] ~context te);
      raise
       (TypeCheckerFailure (lazy ("Malformed inductive constructor type " ^
         (NUri.string_of_uri uri))))
@@ -376,8 +384,11 @@ let rec typeof ~subst ~metasenv context term =
          | (_,C.Decl ty) -> S.lift n ty
          | (_,C.Def (_,ty)) -> S.lift n ty
         with Failure _ -> raise (TypeCheckerFailure (lazy "unbound variable")))
-    | C.Sort (C.Type i) -> C.Sort (C.Type (i+1))
-    | C.Sort s -> C.Sort (C.Type 0)
+    | C.Sort (C.Type [false,u]) -> C.Sort (C.Type [true, u])
+    | C.Sort (C.Type _) -> 
+        raise (AssertFailure (lazy ("Cannot type an inferred type: "^
+          NCicPp.ppterm ~subst ~metasenv ~context t)))
+    | C.Sort _ -> C.Sort (C.Type NCicEnvironment.type0)
     | C.Implicit _ -> raise (AssertFailure (lazy "Implicit found"))
     | C.Meta (n,l) as t -> 
        let canonical_ctx,ty =
@@ -412,7 +423,7 @@ let rec typeof ~subst ~metasenv context term =
     | C.LetIn (n,ty,t,bo) ->
        let ty_t = typeof_aux context t in
        let _ = typeof_aux context ty in
-       if not (R.are_convertible ~subst context ty ty_t) then
+       if not (R.are_convertible ~subst context ty_t ty) then
          raise 
           (TypeCheckerFailure 
             (lazy (Printf.sprintf
@@ -664,7 +675,7 @@ let rec typeof ~subst ~metasenv context term =
             (PP.ppterm ~subst ~metasenv ~context ind)
             (PP.ppterm ~subst ~metasenv ~context so)
             )));
-          (match arity1,ta with
+          (match arity1, R.whd ~subst ((name,C.Decl so)::context) ta with
             | (C.Sort (C.CProp | C.Type _), C.Sort _)
             | (C.Sort C.Prop, C.Sort C.Prop) -> ()
             | (C.Sort C.Prop, C.Sort (C.CProp | C.Type _)) ->
@@ -697,19 +708,62 @@ and check_mutual_inductive_defs uri ~metasenv ~subst is_ind leftno tyl =
   List.iter (fun (_,_,x,_) -> ignore (typeof ~subst ~metasenv [] x)) tyl;
   (* let's check if the types of the inductive constructors are well formed. *)
   let len = List.length tyl in
-  let tys = List.rev (List.map (fun (_,n,ty,_) -> (n,(C.Decl ty))) tyl) in
+  let tys = List.rev_map (fun (_,n,ty,_) -> (n,(C.Decl ty))) tyl in
   ignore
    (List.fold_right
-    (fun (_,_,_,cl) i ->
+    (fun (_,_,ty,cl) i ->
+       let context,ty_sort = split_prods ~subst [] ~-1 ty in
+       let sx_context_ty_rev,_ = HExtlib.split_nth leftno (List.rev context) in
        List.iter
          (fun (_,name,te) -> 
-           let debruijnedte = debruijn uri len [] te in
-           ignore (typeof ~subst ~metasenv tys debruijnedte);
+           let te = debruijn uri len [] te in
+           let context,te = split_prods ~subst tys leftno te in
+           let _,chopped_context_rev =
+            HExtlib.split_nth (List.length tys) (List.rev context) in
+           let sx_context_te_rev,_ =
+            HExtlib.split_nth leftno chopped_context_rev in
+           (try
+             ignore (List.fold_left2
+              (fun context item1 item2 ->
+                let convertible =
+                 match item1,item2 with
+                   (n1,C.Decl ty1),(n2,C.Decl ty2) ->
+                     n1 = n2 && R.are_convertible ~subst context ty1 ty2
+                 | (n1,C.Def (bo1,ty1)),(n2,C.Def (bo2,ty2)) ->
+                     n1 = n2
+                     && R.are_convertible ~subst context ty1 ty2
+                     && R.are_convertible ~subst context bo1 bo2
+                 | _,_ -> false
+                in
+                 if not convertible then
+                  raise (TypeCheckerFailure (lazy
+                   ("Mismatch between the left parameters of the constructor " ^
+                    "and those of its inductive type")))
+                 else
+                  item1::context
+              ) [] sx_context_ty_rev sx_context_te_rev)
+            with Invalid_argument _ -> assert false);
+           let con_sort = typeof ~subst ~metasenv context te in
+           (match R.whd ~subst context con_sort, R.whd ~subst [] ty_sort with
+               (C.Sort (C.Type u1) as s1), (C.Sort (C.Type u2) as s2) ->
+                if not (E.universe_leq u1 u2) then
+                 raise
+                  (TypeCheckerFailure
+                    (lazy ("The type " ^ PP.ppterm ~metasenv ~subst ~context s1^
+                      " of the constructor is not included in the inductive" ^
+                      " type sort " ^ PP.ppterm ~metasenv ~subst ~context s2)))
+             | C.Sort _, C.Sort C.Prop
+             | C.Sort C.CProp, C.Sort C.CProp
+             | C.Sort _, C.Sort C.Type _ -> ()
+             | _, _ ->
+                raise
+                 (TypeCheckerFailure
+                   (lazy ("Wrong constructor or inductive arity shape"))));
            (* let's check also the positivity conditions *)
            if 
              not
-               (are_all_occurrences_positive ~subst tys uri leftno i 0 len
-                  debruijnedte) 
+               (are_all_occurrences_positive ~subst context uri leftno
+                 (i+leftno) leftno (len+leftno) te) 
            then
              raise
                (TypeCheckerFailure
@@ -956,17 +1010,28 @@ and returns_a_coinductive ~subst context ty =
   | _ -> None
 
 and type_of_constant ((Ref.Ref (uri,_)) as ref) = 
+ let error () =
+  raise (TypeCheckerFailure (lazy "Inconsistent cached infos in reference"))
+ in
   match E.get_checked_obj uri, ref with
-  | (_,_,_,_,C.Inductive (_,_,tl,_)), Ref.Ref (_,Ref.Ind (_,i))  ->
+  | (_,_,_,_,C.Inductive (isind1,_,tl,_)), Ref.Ref (_,Ref.Ind (isind2,i))  ->
+      if isind1 <> isind2 then error ();
       let _,_,arity,_ = List.nth tl i in arity
   | (_,_,_,_,C.Inductive (_,_,tl,_)), Ref.Ref (_,Ref.Con (i,j))  ->
       let _,_,_,cl = List.nth tl i in 
       let _,_,arity = List.nth cl (j-1) in 
       arity
-  | (_,_,_,_,C.Fixpoint (_,fl,_)), Ref.Ref (_,(Ref.Fix (i,_,_)|Ref.CoFix i)) ->
+  | (_,_,_,_,C.Fixpoint (_,fl,_)), Ref.Ref (_,Ref.CoFix i) ->
       let _,_,_,arity,_ = List.nth fl i in
       arity
-  | (_,_,_,_,C.Constant (_,_,_,ty,_)), Ref.Ref (_,(Ref.Def _|Ref.Decl)) -> ty
+  | (_,h1,_,_,C.Fixpoint (_,fl,_)), Ref.Ref (_,Ref.Fix (i,recno2,h2)) ->
+      let _,_,recno1,arity,_ = List.nth fl i in
+      if h1 <> h2 || recno1 <> recno2 then error ();
+      arity
+  | (_,_,_,_,C.Constant (_,_,_,ty,_)), Ref.Ref (_,Ref.Decl) -> ty
+  | (_,h1,_,_,C.Constant (_,_,_,ty,_)), Ref.Ref (_,Ref.Def h2) ->
+     if h1 <> h2 then error ();
+     ty
   | _ -> raise (AssertFailure (lazy "type_of_constant: environment/reference"))
 ;;