]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/ocaml/cic_proof_checking/cicTypeChecker.ml
moved tactics from gTopLevel to the new module ocaml/tactics
[helm.git] / helm / ocaml / cic_proof_checking / cicTypeChecker.ml
index ce52754702d9c77ac3318480548210e2d2b7f621..b07c6e4a01f4cc811ec8bdfd107d7dadc06aa271 100644 (file)
@@ -58,7 +58,9 @@ let debrujin_constructor uri number_of_types =
  let rec aux k =
   let module C = Cic in
    function
-      C.Rel _ as t -> t
+      C.Rel n as t when n <= k -> t
+    | C.Rel _ ->
+        raise (NotWellTyped ("Debrujin: open term found"))
     | C.Var (uri,exp_named_subst) ->
        let exp_named_subst' = 
         List.map (function (uri,t) -> (uri,aux k t)) exp_named_subst
@@ -124,7 +126,7 @@ let rec type_of_constant uri =
  let module R = CicReduction in
  let module U = UriManager in
   let cobj =
-   match CicEnvironment.is_type_checked uri with
+   match CicEnvironment.is_type_checked ~trust:true uri with
       CicEnvironment.CheckedObj cobj -> cobj
     | CicEnvironment.UncheckedObj uobj ->
        Logger.log (`Start_type_checking uri) ;
@@ -138,16 +140,22 @@ let rec type_of_constant uri =
            (* only to check that ty is well-typed *)
            let _ = type_of ty in ()
          | C.CurrentProof (_,conjs,te,ty,_) ->
-             (*CSC: bisogna controllare anche il metasenv!!! *)
-             let _ = type_of_aux' conjs [] ty in
-              if not (R.are_convertible [] (type_of_aux' conjs [] te) ty)
-              then
-               raise (NotWellTyped ("CurrentProof" ^ (U.string_of_uri uri)))
+             let _ =
+              List.fold_left
+               (fun metasenv ((_,context,ty) as conj) ->
+                 ignore (type_of_aux' metasenv context ty) ;
+                 metasenv @ [conj]
+               ) [] conjs
+             in
+              let _ = type_of_aux' conjs [] ty in
+               if not (R.are_convertible [] (type_of_aux' conjs [] te) ty)
+               then
+                raise (NotWellTyped ("CurrentProof" ^ (U.string_of_uri uri)))
          | _ -> raise (WrongUriToConstant (U.string_of_uri uri))
        ) ;
        CicEnvironment.set_type_checking_info uri ;
        Logger.log (`Type_checking_completed uri) ;
-       match CicEnvironment.is_type_checked uri with
+       match CicEnvironment.is_type_checked ~trust:false uri with
           CicEnvironment.CheckedObj cobj -> cobj
         | CicEnvironment.UncheckedObj _ -> raise CicEnvironmentError
   in
@@ -161,7 +169,7 @@ and type_of_variable uri =
  let module R = CicReduction in
  let module U = UriManager in
   (* 0 because a variable is never cooked => no partial cooking at one level *)
-  match CicEnvironment.is_type_checked uri with
+  match CicEnvironment.is_type_checked ~trust:true uri with
      CicEnvironment.CheckedObj (C.Variable (_,_,ty,_)) -> ty
    | CicEnvironment.UncheckedObj (C.Variable (_,bo,ty,_)) ->
       Logger.log (`Start_type_checking uri) ;
@@ -422,63 +430,69 @@ and are_all_occurrences_positive context uri indparamsno i n nn te =
    | _ -> raise (NotWellFormedTypeOfInductiveConstructor (UriManager.string_of_uri uri))
 
 (* Main function to checks the correctness of a mutual *)
-(* inductive block definition.                         *)
-and check_mutual_inductive_defs uri =
+(* inductive block definition. This is the function    *)
+(* exported to the proof-engine.                       *)
+and typecheck_mutual_inductive_defs uri (itl,_,indparamsno) =
  let module U = UriManager in
-  function
-     Cic.InductiveDefinition (itl, _, indparamsno) ->
-      (* let's check if the arity of the inductive types are well *)
-      (* formed                                                   *)
-      List.iter (fun (_,_,x,_) -> let _ = type_of x in ()) itl ;
+  (* let's check if the arity of the inductive types are well *)
+  (* formed                                                   *)
+  List.iter (fun (_,_,x,_) -> let _ = type_of x in ()) itl ;
 
-      (* let's check if the types of the inductive constructors  *)
-      (* are well formed.                                        *)
-      (* In order not to use type_of_aux we put the types of the *)
-      (* mutual inductive types at the head of the types of the  *)
-      (* constructors using Prods                                *)
-      let len = List.length itl in
-       let tys =
-        List.map (fun (n,_,ty,_) -> Some (Cic.Name n,(Cic.Decl ty))) itl in
-       let _ =
-        List.fold_right
-         (fun (_,_,_,cl) i ->
-           List.iter
-            (fun (name,te) -> 
-              let debrujinedte = debrujin_constructor uri len te in
-              let augmented_term =
-               List.fold_right
-                (fun (name,_,ty,_) i -> Cic.Prod (Cic.Name name, ty, i))
-                itl debrujinedte
-              in
-               let _ = type_of augmented_term in
-                (* let's check also the positivity conditions *)
-                if
-                 not
-                  (are_all_occurrences_positive tys uri indparamsno i 0 len
-                    debrujinedte)
-                then
-                 raise (NotPositiveOccurrences (U.string_of_uri uri))
-            ) cl ;
-           (i + 1)
-        ) itl 1
-       in
-        ()
-   | _ ->
-     raise (WrongUriToMutualInductiveDefinitions (U.string_of_uri uri))
+  (* let's check if the types of the inductive constructors  *)
+  (* are well formed.                                        *)
+  (* In order not to use type_of_aux we put the types of the *)
+  (* mutual inductive types at the head of the types of the  *)
+  (* constructors using Prods                                *)
+  let len = List.length itl in
+   let tys =
+    List.map (fun (n,_,ty,_) -> Some (Cic.Name n,(Cic.Decl ty))) itl in
+   let _ =
+    List.fold_right
+     (fun (_,_,_,cl) i ->
+       List.iter
+        (fun (name,te) -> 
+          let debrujinedte = debrujin_constructor uri len te in
+          let augmented_term =
+           List.fold_right
+            (fun (name,_,ty,_) i -> Cic.Prod (Cic.Name name, ty, i))
+            itl debrujinedte
+          in
+           let _ = type_of augmented_term in
+            (* let's check also the positivity conditions *)
+            if
+             not
+              (are_all_occurrences_positive tys uri indparamsno i 0 len
+                debrujinedte)
+            then
+             raise (NotPositiveOccurrences (U.string_of_uri uri))
+        ) cl ;
+       (i + 1)
+    ) itl 1
+   in
+    ()
+
+(* Main function to checks the correctness of a mutual *)
+(* inductive block definition.                         *)
+and check_mutual_inductive_defs uri =
+ function
+    Cic.InductiveDefinition (itl, params, indparamsno) ->
+     typecheck_mutual_inductive_defs uri (itl,params,indparamsno)
+  | _ ->
+    raise (WrongUriToMutualInductiveDefinitions (UriManager.string_of_uri uri))
 
 and type_of_mutual_inductive_defs uri i =
  let module C = Cic in
  let module R = CicReduction in
  let module U = UriManager in
   let cobj =
-   match CicEnvironment.is_type_checked uri with
+   match CicEnvironment.is_type_checked ~trust:true uri with
       CicEnvironment.CheckedObj cobj -> cobj
     | CicEnvironment.UncheckedObj uobj ->
        Logger.log (`Start_type_checking uri) ;
        check_mutual_inductive_defs uri uobj ;
        CicEnvironment.set_type_checking_info uri ;
        Logger.log (`Type_checking_completed uri) ;
-       (match CicEnvironment.is_type_checked uri with
+       (match CicEnvironment.is_type_checked ~trust:false uri with
           CicEnvironment.CheckedObj cobj -> cobj
         | CicEnvironment.UncheckedObj _ -> raise CicEnvironmentError
        )
@@ -494,14 +508,14 @@ and type_of_mutual_inductive_constr uri i j =
  let module R = CicReduction in
  let module U = UriManager in
   let cobj =
-   match CicEnvironment.is_type_checked uri with
+   match CicEnvironment.is_type_checked ~trust:true uri with
       CicEnvironment.CheckedObj cobj -> cobj
     | CicEnvironment.UncheckedObj uobj ->
        Logger.log (`Start_type_checking uri) ;
        check_mutual_inductive_defs uri uobj ;
        CicEnvironment.set_type_checking_info uri ;
        Logger.log (`Type_checking_completed uri) ;
-       (match CicEnvironment.is_type_checked uri with
+       (match CicEnvironment.is_type_checked ~trust:false uri with
           CicEnvironment.CheckedObj cobj -> cobj
         | CicEnvironment.UncheckedObj _ -> raise CicEnvironmentError
        )
@@ -635,7 +649,7 @@ and check_is_really_smaller_arg context n nn kl x safes te =
                   let cl' =
                    List.map
                     (fun (id,ty) ->
-                      (id, ty, snd (split_prods tys paramsno ty))) cl
+                      (id, snd (split_prods tys paramsno ty))) cl
                   in
                    (tys,List.length tl,isinductive,paramsno,cl')
              | _ ->
@@ -648,9 +662,9 @@ and check_is_really_smaller_arg context n nn kl x safes te =
                pl true
             else
               List.fold_right
-               (fun (p,(_,te,c)) i ->
+               (fun (p,(_,c)) i ->
                  let rl' =
-                  let debrujinedte = debrujin_constructor uri len te in
+                  let debrujinedte = debrujin_constructor uri len c in
                    recursive_args tys 0 len debrujinedte
                  in
                   let (e,safes',n',nn',x',context') =
@@ -670,7 +684,7 @@ and check_is_really_smaller_arg context n nn kl x safes te =
                   let cl' =
                    List.map
                     (fun (id,ty) ->
-                      (id, ty, snd (split_prods tys paramsno ty))) cl
+                      (id, snd (split_prods tys paramsno ty))) cl
                   in
                    (tys,List.length tl,isinductive,paramsno,cl')
              | _ ->
@@ -685,9 +699,9 @@ and check_is_really_smaller_arg context n nn kl x safes te =
               (*CSC: supponiamo come prima che nessun controllo sia necessario*)
               (*CSC: sugli argomenti di una applicazione                      *)
               List.fold_right
-               (fun (p,(_,te,c)) i ->
+               (fun (p,(_,c)) i ->
                  let rl' =
-                  let debrujinedte = debrujin_constructor uri len te in
+                  let debrujinedte = debrujin_constructor uri len c in
                    recursive_args tys 0 len debrujinedte
                  in
                   let (e, safes',n',nn',x',context') =
@@ -791,7 +805,7 @@ and guarded_by_destructors context n nn kl x safes =
                   let cl' =
                    List.map
                     (fun (id,ty) ->
-                      (id, ty, snd (split_prods tys paramsno ty))) cl
+                      (id, snd (split_prods tys paramsno ty))) cl
                   in
                    (tys,List.length tl,isinductive,paramsno,cl')
              | _ ->
@@ -809,9 +823,9 @@ and guarded_by_destructors context n nn kl x safes =
              guarded_by_destructors context n nn kl x safes outtype &&
               (*CSC: manca ??? il controllo sul tipo di term? *)
               List.fold_right
-               (fun (p,(_,te,c)) i ->
+               (fun (p,(_,c)) i ->
                  let rl' =
-                  let debrujinedte = debrujin_constructor uri len te in
+                  let debrujinedte = debrujin_constructor uri len c in
                    recursive_args tys 0 len debrujinedte
                  in
                   let (e,safes',n',nn',x',context') =
@@ -831,7 +845,7 @@ and guarded_by_destructors context n nn kl x safes =
                   let cl' =
                    List.map
                     (fun (id,ty) ->
-                      (id, ty, snd (split_prods tys paramsno ty))) cl
+                      (id, snd (split_prods tys paramsno ty))) cl
                   in
                    (tys,List.length tl,isinductive,paramsno,cl')
              | _ ->
@@ -853,9 +867,9 @@ and guarded_by_destructors context n nn kl x safes =
                  i && guarded_by_destructors context n nn kl x safes t)
                tl true &&
               List.fold_right
-               (fun (p,(_,te,c)) i ->
+               (fun (p,(_,c)) i ->
                  let rl' =
-                  let debrujinedte = debrujin_constructor uri len te in
+                  let debrujinedte = debrujin_constructor uri len c in
                    recursive_args tys 0 len debrujinedte
                  in
                   let (e, safes',n',nn',x',context') =
@@ -928,7 +942,7 @@ and guarded_by_constructors context n nn h te args coInductiveTypeURI =
        List.fold_right (fun x i -> i && does_not_occur context n nn x) tl true
    | C.Appl ((C.MutConstruct (uri,i,j,exp_named_subst))::tl) ->
       let consty =
-       match CicEnvironment.get_cooked_obj uri with
+       match CicEnvironment.get_cooked_obj ~trust:false uri with
           C.InductiveDefinition (itl,_,_) ->
            let (_,_,_,cl) = List.nth itl i in
             let (_,cons) = List.nth cl (j - 1) in
@@ -1387,7 +1401,7 @@ and type_of_aux' metasenv context t =
 
         (* let's check if the type of branches are right *)
         let parsno =
-         match CicEnvironment.get_cooked_obj uri with
+         match CicEnvironment.get_cooked_obj ~trust:false uri with
             C.InductiveDefinition (_,_,parsno) -> parsno
           | _ ->
             raise (WrongUriToMutualInductiveDefinitions (U.string_of_uri uri))
@@ -1498,7 +1512,7 @@ in if not res then prerr_endline ("#### " ^ CicPp.ppterm (type_of_aux context p)
     | ((uri,t) as subst)::tl ->
        let typeofvar =
         CicSubstitution.subst_vars substs (type_of_variable uri) in
-       (match CicEnvironment.get_cooked_obj uri with
+       (match CicEnvironment.get_cooked_obj ~trust:false uri with
            Cic.Variable (_,Some bo,_,_) ->
             raise
              (NotWellTyped
@@ -1562,7 +1576,7 @@ in if not res then prerr_endline ("#### " ^ CicPp.ppterm (type_of_aux context p)
    match CicReduction.whd context ty with
       C.MutInd (uri,i,_) ->
        (*CSC: definire una funzioncina per questo codice sempre replicato *)
-       (match CicEnvironment.get_cooked_obj uri with
+       (match CicEnvironment.get_cooked_obj ~trust:false uri with
            C.InductiveDefinition (itl,_,_) ->
             let (_,is_inductive,_,_) = List.nth itl i in
              if is_inductive then None else (Some uri)
@@ -1624,7 +1638,7 @@ let typecheck uri =
  let module C = Cic in
  let module R = CicReduction in
  let module U = UriManager in
-  match CicEnvironment.is_type_checked uri with
+  match CicEnvironment.is_type_checked ~trust:false uri with
      CicEnvironment.CheckedObj _ -> ()
    | CicEnvironment.UncheckedObj uobj ->
       (* let's typecheck the uncooked object *)
@@ -1638,10 +1652,16 @@ let typecheck uri =
           (* only to check that ty is well-typed *)
           let _ = type_of ty in ()
         | C.CurrentProof (_,conjs,te,ty,_) ->
-            (*CSC: bisogna controllare anche il metasenv!!! *)
+           let _ =
+            List.fold_left
+             (fun metasenv ((_,context,ty) as conj) ->
+               ignore (type_of_aux' metasenv context ty) ;
+               metasenv @ [conj]
+             ) [] conjs
+           in
             let _ = type_of_aux' conjs [] ty in
-             debug (type_of_aux' conjs [] te) [] ;
-             if not (R.are_convertible [] (type_of_aux' conjs [] te) ty) then
+             if not (R.are_convertible [] (type_of_aux' conjs [] te) ty)
+             then
               raise (NotWellTyped ("CurrentProof" ^ (U.string_of_uri uri)))
         | C.Variable (_,bo,ty,_) ->
            (* only to check that ty is well-typed *)