]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/software/components/ng_kernel/nCicTypeChecker.ml
efficiency improvement (buffers are now used everywhere)
[helm.git] / helm / software / components / ng_kernel / nCicTypeChecker.ml
index db449c16aaa2cabbd0427003f137d5f05c2059b2..8beca4cf0504285f32355529739aabfa73700e1a 100644 (file)
@@ -96,16 +96,6 @@ 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.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"))
-;;
-
 let debruijn uri number_of_types context = 
  let rec aux k t =
   match t with
@@ -360,7 +350,9 @@ let type_of_branch ~subst context leftno outty cons tycons =
         | t -> C.Appl [t ; C.Rel 1]
        in
         C.Prod (name,so, aux (liftno+1) ((name,(C.Decl so))::context) cons de)
-   | _ -> raise (AssertFailure (lazy "type_of_branch"))
+   | t -> raise (AssertFailure 
+      (lazy ("type_of_branch, the contructor has type: " ^ NCicPp.ppterm
+       ~metasenv:[] ~context:[] ~subst:[] t)))
  in
   aux 0 context cons tycons
 ;;
@@ -375,7 +367,9 @@ let rec typeof ~subst ~metasenv context term =
          match List.nth context (n - 1) with
          | (_,C.Decl ty) -> S.lift n ty
          | (_,C.Def (_,ty)) -> S.lift n ty
-        with Failure _ -> raise (TypeCheckerFailure (lazy "unbound variable")))
+        with Failure _ -> 
+          raise (TypeCheckerFailure (lazy ("unbound variable " ^ string_of_int n
+            ^" under: " ^ NCicPp.ppcontext ~metasenv ~subst context))))
     | C.Sort (C.Type [false,u]) -> C.Sort (C.Type [true, u])
     | C.Sort (C.Type _) -> 
         raise (AssertFailure (lazy ("Cannot type an inferred type: "^
@@ -678,7 +672,7 @@ and eat_prods ~subst ~metasenv context he ty_he args_with_ty =
             raise 
               (TypeCheckerFailure 
                 (lazy (Printf.sprintf
-                  ("Appl: wrong application of %s: the parameter %s has type"^^
+                  ("Appl: wrong application of %s: the argument %s has type"^^
                    "\n%s\nbut it should have type \n%s\nContext:\n%s\n")
                   (PP.ppterm ~subst ~metasenv ~context he)
                   (PP.ppterm ~subst ~metasenv ~context arg)
@@ -720,7 +714,7 @@ and is_non_informative ~metasenv ~subst paramsno c =
        let s = typeof ~metasenv ~subst context so in
        s = C.Sort C.Prop && aux ((n,(C.Decl so))::context) de
     | _ -> true in
- let context',dx = split_prods ~subst [] paramsno c in
+ let context',dx = NCicReduction.split_prods ~subst [] paramsno c in
   aux context' dx
 
 and check_mutual_inductive_defs uri ~metasenv ~subst leftno tyl = 
@@ -732,13 +726,15 @@ and check_mutual_inductive_defs uri ~metasenv ~subst leftno tyl =
   ignore
    (List.fold_right
     (fun (it_relev,_,ty,cl) i ->
-       let context,ty_sort = split_prods ~subst [] ~-1 ty in
+       let context,ty_sort = NCicReduction.split_prods ~subst [] ~-1 ty in
        let sx_context_ty_rev,_ = HExtlib.split_nth leftno (List.rev context) in
        List.iter
          (fun (k_relev,_,te) ->
-          let _,k_relev = HExtlib.split_nth leftno k_relev in
+          let k_relev =
+            try snd (HExtlib.split_nth leftno k_relev)
+            with Failure _ -> k_relev in
            let te = debruijn uri len [] te in
-           let context,te = split_prods ~subst tys leftno te in
+           let context,te = NCicReduction.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,_ =
@@ -1087,7 +1083,10 @@ and type_of_constant ((Ref.Ref (uri,_)) as ref) =
   | (_,h1,_,_,C.Constant (_,_,_,ty,_)), Ref.Ref (_,Ref.Def h2) ->
      if h1 <> h2 then error ();
      ty
-  | _ -> raise (AssertFailure (lazy "type_of_constant: environment/reference"))
+  | _ ->
+    raise (AssertFailure
+     (lazy ("type_of_constant: environment/reference: " ^
+       Ref.string_of_reference ref)))
 
 and get_relevance ~metasenv ~subst context t args = 
    let ty = typeof ~subst ~metasenv context t in
@@ -1303,4 +1302,31 @@ E.set_typecheck_obj
 
 let _ = NCicReduction.set_get_relevance get_relevance;;
 
+
+let indent = ref 0;;
+let debug = true;;
+let logger =
+    let do_indent () = String.make !indent ' ' in  
+    (function 
+      | `Start_type_checking s ->
+          if debug then
+           prerr_endline (do_indent () ^ "Start: " ^ NUri.string_of_uri s); 
+          incr indent
+      | `Type_checking_completed s ->
+          decr indent;
+          if debug then
+           prerr_endline (do_indent () ^ "End: " ^ NUri.string_of_uri s)
+      | `Type_checking_interrupted s ->
+          decr indent;
+          if debug then
+           prerr_endline (do_indent () ^ "Break: " ^ NUri.string_of_uri s)
+      | `Type_checking_failed s ->
+          decr indent;
+          if debug then
+           prerr_endline (do_indent () ^ "Fail: " ^ NUri.string_of_uri s)
+      | `Trust_obj s ->
+          if debug then
+           prerr_endline (do_indent () ^ "Trust: " ^ NUri.string_of_uri s))
+;;
+(* let _ = set_logger logger ;; *)
 (* EOF *)