]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/software/components/cic_proof_checking/cicTypeChecker.ml
- new tactic applyP for use in the *P*rocedural script reconstruction
[helm.git] / helm / software / components / cic_proof_checking / cicTypeChecker.ml
index 35015e541202d36fe82a245ddd167e7322fc2c1c..1756497c017bfac8d9333a94114c2041da721c01 100644 (file)
@@ -557,25 +557,41 @@ and typecheck_mutual_inductive_defs ~logger uri (itl,_,indparamsno) ugraph =
   (* 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
+    List.rev_map (fun (n,_,ty,_) -> Some (Cic.Name n,(Cic.Decl ty))) itl in
   let _,ugraph2 =
     List.fold_right
-      (fun (_,_,_,cl) (i,ugraph) ->
+      (fun (_,_,ty,cl) (i,ugraph) ->
+        let _,ty_sort = split_prods ~subst:[] [] ~-1 ty in
        let ugraph'' = 
           List.fold_left
             (fun ugraph (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 _,ugraph' = type_of ~logger augmented_term ugraph in
+              let te = debrujin_constructor uri len [] te in
+              let context,te = split_prods ~subst:[] tys indparamsno te in
+              let con_sort,ugraph = type_of_aux' ~logger [] context te ugraph in
+              let ugraph =
+               match
+                CicReduction.whd context con_sort, CicReduction.whd [] ty_sort
+               with
+                  Cic.Sort (Cic.Type u1), Cic.Sort (Cic.Type u2) 
+                | Cic.Sort (Cic.CProp u1), Cic.Sort (Cic.CProp u2) 
+                | Cic.Sort (Cic.Type u1), Cic.Sort (Cic.CProp u2) ->
+                   CicUniv.add_ge u2 u1 ugraph
+                | Cic.Sort (Cic.CProp u1), Cic.Sort (Cic.Type u2) ->
+                   CicUniv.add_gt u2 u1 ugraph
+                | Cic.Sort _, Cic.Sort Cic.Prop
+                | Cic.Sort _, Cic.Sort Cic.CProp _
+                | Cic.Sort _, Cic.Sort Cic.Set
+                | Cic.Sort _, Cic.Sort Cic.Type _ -> ugraph
+                | a,b ->
+                   raise
+                    (TypeCheckerFailure
+                      (lazy ("Wrong constructor or inductive arity shape: "^
+                        CicPp.ppterm a ^ " --- " ^ CicPp.ppterm b))) in
               (* let's check also the positivity conditions *)
               if
                not
-                 (are_all_occurrences_positive tys uri indparamsno i 0 len
-                     debrujinedte)
+                 (are_all_occurrences_positive context uri indparamsno
+                    (i+indparamsno) indparamsno (len+indparamsno) te)
               then
                 begin
                 prerr_endline (UriManager.string_of_uri uri);
@@ -584,7 +600,7 @@ and typecheck_mutual_inductive_defs ~logger uri (itl,_,indparamsno) ugraph =
                  (TypeCheckerFailure
                     (lazy ("Non positive occurence in " ^ U.string_of_uri uri)))                end 
               else
-               ugraph'
+               ugraph
             ) ugraph cl in
        (i + 1),ugraph''
       ) itl (1,ugrap1)
@@ -708,7 +724,8 @@ and split_prods ~subst context n te =
  let module R = CicReduction in
   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 ((Some (name,(C.Decl so)))::context) (n - 1) ta
    | (_, _) -> raise (AssertFailure (lazy "8"))
 
@@ -1143,7 +1160,7 @@ and check_allowed_sort_elimination ~subst ~metasenv ~logger context uri i
          ((Some (name,C.Decl so))::context) ta true
    | (C.Sort C.Prop, C.Sort C.Prop) when need_dummy -> true,ugraph
    | (C.Sort C.Prop, C.Sort C.Set)
-   | (C.Sort C.Prop, C.Sort C.CProp)
+   | (C.Sort C.Prop, C.Sort (C.CProp _))
    | (C.Sort C.Prop, C.Sort (C.Type _) ) when need_dummy ->
        (let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
         match o with
@@ -1169,12 +1186,9 @@ and check_allowed_sort_elimination ~subst ~metasenv ~logger context uri i
                       UriManager.string_of_uri uri)))
        )
    | (C.Sort C.Set, C.Sort C.Prop) when need_dummy -> true , ugraph
-   | (C.Sort C.CProp, C.Sort C.Prop) when need_dummy -> true , ugraph
    | (C.Sort C.Set, C.Sort C.Set) when need_dummy -> true , ugraph
-   | (C.Sort C.Set, C.Sort C.CProp) when need_dummy -> true , ugraph
-   | (C.Sort C.CProp, C.Sort C.Set) when need_dummy -> true , ugraph
-   | (C.Sort C.CProp, C.Sort C.CProp) when need_dummy -> true , ugraph
-   | ((C.Sort C.Set, C.Sort (C.Type _)) | (C.Sort C.CProp, C.Sort (C.Type _)))
+   | (C.Sort C.Set, C.Sort (C.Type _)) 
+   | (C.Sort C.Set, C.Sort (C.CProp _))
       when need_dummy ->
        (let o,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
         match o with
@@ -1196,6 +1210,7 @@ and check_allowed_sort_elimination ~subst ~metasenv ~logger context uri i
               UriManager.string_of_uri uri)))
        )
    | (C.Sort (C.Type _), C.Sort _) when need_dummy -> true , ugraph
+   | (C.Sort (C.CProp _), C.Sort _) when need_dummy -> true , ugraph
    | (_,_) -> false,ugraph
  in
   check_allowed_sort_elimination_aux ugraph context arity2 need_dummy
@@ -1328,7 +1343,7 @@ and type_of_aux' ~logger ?(subst = []) metasenv context t ugraph =
     | C.Var (uri,exp_named_subst) ->
       incr fdebug ;
        let ugraph1 = 
-         check_exp_named_subst ~logger ~subst context exp_named_subst ugraph 
+         check_exp_named_subst uri ~logger ~subst context exp_named_subst ugraph 
        in 
        let ty,ugraph2 = type_of_variable ~logger uri ugraph1 in
        let ty1 = CicSubstitution.subst_vars exp_named_subst ty in
@@ -1441,7 +1456,7 @@ and type_of_aux' ~logger ?(subst = []) metasenv context t ugraph =
    | C.Const (uri,exp_named_subst) ->
        incr fdebug ;
        let ugraph1 = 
-        check_exp_named_subst ~logger ~subst  context exp_named_subst ugraph 
+        check_exp_named_subst uri ~logger ~subst  context exp_named_subst ugraph 
        in
        let cty,ugraph2 = type_of_constant ~logger uri ugraph1 in
        let cty1 =
@@ -1452,11 +1467,9 @@ and type_of_aux' ~logger ?(subst = []) metasenv context t ugraph =
    | C.MutInd (uri,i,exp_named_subst) ->
       incr fdebug ;
        let ugraph1 = 
-        check_exp_named_subst ~logger  ~subst context exp_named_subst ugraph 
+        check_exp_named_subst uri ~logger  ~subst context exp_named_subst ugraph 
        in
-        (* TASSI: da me c'era anche questa, ma in CVS no *)
        let mty,ugraph2 = type_of_mutual_inductive_defs ~logger uri i ugraph1 in
-        (* fine parte dubbia *)
        let cty =
         CicSubstitution.subst_vars exp_named_subst mty
        in
@@ -1464,9 +1477,8 @@ and type_of_aux' ~logger ?(subst = []) metasenv context t ugraph =
         cty,ugraph2
    | C.MutConstruct (uri,i,j,exp_named_subst) ->
        let ugraph1 = 
-        check_exp_named_subst ~logger ~subst context exp_named_subst ugraph 
+        check_exp_named_subst uri ~logger ~subst context exp_named_subst ugraph
        in
-        (* TASSI: idem come sopra *)
        let mty,ugraph2 = 
         type_of_mutual_inductive_constr ~logger uri i j ugraph1 
        in
@@ -1773,7 +1785,24 @@ end;
        let (_,ty,_) = List.nth fl i in
         ty,ugraph2
 
- and check_exp_named_subst ~logger ~subst context =
+ and check_exp_named_subst uri ~logger ~subst context ens ugraph =
+   let params =
+    let obj,_ = CicEnvironment.get_obj CicUniv.empty_ugraph uri in
+    (match obj with
+        Cic.Constant (_,_,_,params,_) -> params
+      | Cic.Variable (_,_,_,params,_) -> params
+      | Cic.CurrentProof (_,_,_,_,params,_) -> params
+      | Cic.InductiveDefinition (_,params,_,_) -> params
+    ) in
+   let rec check_same_order params ens =
+    match params,ens with
+     | _,[] -> ()
+     | [],_::_ ->
+        raise (TypeCheckerFailure (lazy "Bad explicit named substitution"))
+     | uri::tl,(uri',_)::tl' when UriManager.eq uri uri' ->
+        check_same_order tl tl'
+     | _::tl,l -> check_same_order tl l
+   in
    let rec check_exp_named_subst_aux ~logger esubsts l ugraph =
      match l with
         [] -> ugraph
@@ -1799,19 +1828,18 @@ end;
                 raise (TypeCheckerFailure (lazy "Wrong Explicit Named Substitution"))
                end
    in
-     check_exp_named_subst_aux ~logger []
+    check_same_order params ens ;
+    check_exp_named_subst_aux ~logger [] ens ugraph
        
  and sort_of_prod ~subst context (name,s) (t1, t2) ugraph =
   let module C = Cic in
    let t1' = CicReduction.whd ~subst context t1 in
    let t2' = CicReduction.whd ~subst ((Some (name,C.Decl s))::context) t2 in
    match (t1', t2') with
-      (C.Sort s1, C.Sort s2)
-        when (s2 = C.Prop or s2 = C.Set or s2 = C.CProp) -> 
+    | (C.Sort s1, C.Sort (C.Prop | C.Set)) ->
          (* different from Coq manual!!! *)
-         C.Sort s2,ugraph
+         t2',ugraph
     | (C.Sort (C.Type t1), C.Sort (C.Type t2)) -> 
-      (* TASSI: CONSRTAINTS: the same in doubletypeinference, cicrefine *)
        let t' = CicUniv.fresh() in
         (try
          let ugraph1 = CicUniv.add_ge t' t1 ugraph in
@@ -1819,9 +1847,32 @@ end;
           C.Sort (C.Type t'),ugraph2
         with
          CicUniv.UniverseInconsistency msg -> raise (TypeCheckerFailure msg))
-    | (C.Sort _,C.Sort (C.Type t1)) -> 
-        (* TASSI: CONSRTAINTS: the same in doubletypeinference, cicrefine *)
-        C.Sort (C.Type t1),ugraph (* c'e' bisogno di un fresh? *)
+    | (C.Sort (C.CProp t1), C.Sort (C.CProp t2)) -> 
+       let t' = CicUniv.fresh() in
+        (try
+         let ugraph1 = CicUniv.add_ge t' t1 ugraph in
+         let ugraph2 = CicUniv.add_ge t' t2 ugraph1 in
+          C.Sort (C.CProp t'),ugraph2
+        with
+         CicUniv.UniverseInconsistency msg -> raise (TypeCheckerFailure msg))
+    | (C.Sort (C.Type t1), C.Sort (C.CProp t2)) -> 
+       let t' = CicUniv.fresh() in
+        (try
+         let ugraph1 = CicUniv.add_ge t' t1 ugraph in
+         let ugraph2 = CicUniv.add_ge t' t2 ugraph1 in
+          C.Sort (C.CProp t'),ugraph2
+        with
+         CicUniv.UniverseInconsistency msg -> raise (TypeCheckerFailure msg))
+    | (C.Sort (C.CProp t1), C.Sort (C.Type t2)) -> 
+       let t' = CicUniv.fresh() in
+        (try
+         let ugraph1 = CicUniv.add_gt t' t1 ugraph in
+         let ugraph2 = CicUniv.add_ge t' t2 ugraph1 in
+          C.Sort (C.Type t'),ugraph2
+        with
+         CicUniv.UniverseInconsistency msg -> raise (TypeCheckerFailure msg))
+    | (C.Sort _,C.Sort (C.Type t1)) -> C.Sort (C.Type t1),ugraph 
+    | (C.Sort _,C.Sort (C.CProp t1)) -> C.Sort (C.CProp t1),ugraph 
     | (C.Meta _, C.Sort _) -> t2',ugraph
     | (C.Meta _, (C.Meta (_,_) as t))
     | (C.Sort _, (C.Meta (_,_) as t)) when CicUtil.is_closed t ->