]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/ocaml/cic_omdoc/doubleTypeInference.ml
first moogle template checkin
[helm.git] / helm / ocaml / cic_omdoc / doubleTypeInference.ml
index 362422cac59fa61509a66f76d5ce2fda950702c8..441d7c9a938425a573c15f7236308f189e2be165 100644 (file)
@@ -31,6 +31,22 @@ exception WrongUriToMutualInductiveDefinitions of string;;
 exception ListTooShort;;
 exception RelToHiddenHypothesis;;
 
+let syntactic_equality_add_time = ref 0.0;;
+let type_of_aux'_add_time = ref 0.0;;
+let number_new_type_of_aux'_double_work = ref 0;;
+let number_new_type_of_aux' = ref 0;;
+let number_new_type_of_aux'_prop = ref 0;;
+
+let double_work = ref 0;;
+
+let xxx_type_of_aux' m c t =
+ let t1 = Sys.time () in
+ let res = CicTypeChecker.type_of_aux' m c t in
+ let t2 = Sys.time () in
+ type_of_aux'_add_time := !type_of_aux'_add_time +. t2 -. t1 ;
+ res
+;;
+
 type types = {synthesized : Cic.term ; expected : Cic.term option};;
 
 (* does_not_occur n te                              *)
@@ -42,7 +58,7 @@ let rec does_not_occur n =
    | C.Rel _
    | C.Meta _
    | C.Sort _
-   | C.Implicit -> true 
+   | C.Implicit -> true 
    | C.Cast (te,ty) ->
       does_not_occur n te && does_not_occur n ty
    | C.Prod (name,so,dest) ->
@@ -107,7 +123,7 @@ let rec head_beta_reduce =
          (function None -> None | Some t -> Some (head_beta_reduce t)) l
        )
     | C.Sort _ as t -> t
-    | C.Implicit -> assert false
+    | C.Implicit -> assert false
     | C.Cast (te,ty) ->
        C.Cast (head_beta_reduce te, head_beta_reduce ty)
     | C.Prod (n,s,t) ->
@@ -160,8 +176,7 @@ let rec head_beta_reduce =
         C.CoFix (i,fl')
 ;;
 
-(* syntactic_equality up to cookingsno for uris *)
-(* (which is often syntactically irrilevant),   *)
+(* syntactic_equality up to the                 *)
 (* distinction between fake dependent products  *)
 (* and non-dependent products, alfa-conversion  *)
 (*CSC: must alfa-conversion be considered or not? *)
@@ -230,6 +245,15 @@ let syntactic_equality t t' =
    _ -> false
 ;;
 
+let xxx_syntactic_equality t t' =
+ let t1 = Sys.time () in
+ let res = syntactic_equality t t' in
+ let t2 = Sys.time () in
+ syntactic_equality_add_time := !syntactic_equality_add_time +. t2 -. t1 ;
+ res
+;;
+
+
 let rec split l n =
  match (l,n) with
     (l,0) -> ([], l)
@@ -325,8 +349,10 @@ let rec type_of_aux' subterms_to_types metasenv context t expectedty =
         (try
           match List.nth context (n - 1) with
              Some (_,C.Decl t) -> S.lift n t
-           | Some (_,C.Def bo) -> type_of_aux context (S.lift n bo) expectedty
-          | None -> raise RelToHiddenHypothesis
+           | Some (_,C.Def (_,Some ty)) -> S.lift n ty
+           | Some (_,C.Def (bo,None)) ->
+              type_of_aux context (S.lift n bo) expectedty
+                | None -> raise RelToHiddenHypothesis
          with
           _ -> raise (NotWellTyped "Not a close term")
         )
@@ -344,9 +370,11 @@ let rec type_of_aux' subterms_to_types metasenv context t expectedty =
               [] -> []
             | (Some (n,C.Decl t))::tl ->
                (Some (n,C.Decl (S.lift_meta l (S.lift i t))))::(aux (i+1) tl)
-            | (Some (n,C.Def t))::tl ->
-               (Some (n,C.Def (S.lift_meta l (S.lift i t))))::(aux (i+1) tl)
+            | (Some (n,C.Def (t,None)))::tl ->
+               (Some (n,C.Def ((S.lift_meta l (S.lift i t)),None)))::
+                (aux (i+1) tl)
             | None::tl -> None::(aux (i+1) tl)
+            | (Some (_,C.Def (_,Some _)))::_ -> assert false
           in
            aux 1 canonical_context
          in
@@ -355,10 +383,10 @@ let rec type_of_aux' subterms_to_types metasenv context t expectedty =
             (fun t ct ->
               match t,ct with
                  _,None -> ()
-               | Some t,Some (_,C.Def ct) ->
+               | Some t,Some (_,C.Def (ct,_)) ->
                   let expected_type =
                    R.whd context
-                    (CicTypeChecker.type_of_aux' metasenv context ct)
+                    (xxx_type_of_aux' metasenv context ct)
                   in
                    (* Maybe I am a bit too paranoid, because   *)
                    (* if the term is well-typed than t and ct  *)
@@ -375,8 +403,14 @@ let rec type_of_aux' subterms_to_types metasenv context t expectedty =
            in
             (* Checks suppressed *)
             CicSubstitution.lift_meta l ty
-     | C.Sort s -> C.Sort C.Type (*CSC manca la gestione degli universi!!! *)
-     | C.Implicit -> raise (Impossible 21)
+     | C.Sort (C.Type t) -> (* TASSI: CONSTRAINT *)
+        let t' = CicUniv.fresh() in
+        if not (CicUniv.add_gt t' t ) then
+         assert false (* t' is fresh! an error in CicUniv *)
+       else
+          C.Sort (C.Type t')
+     | C.Sort _ -> C.Sort (C.Type (CicUniv.fresh())) (* TASSI: CONSTRAINT *)
+     | C.Implicit _ -> raise (Impossible 21)
      | C.Cast (te,ty) ->
         (* Let's visit all the subterms that will not be visited later *)
         let _ = type_of_aux context te (Some (head_beta_reduce ty)) in
@@ -389,7 +423,7 @@ let rec type_of_aux' subterms_to_types metasenv context t expectedty =
          sort_of_prod context (name,s) (sort1,sort2)
      | C.Lambda (n,s,t) ->
         (* Let's visit all the subterms that will not be visited later *)
-        let _ = type_of_aux context s None in
+         let _ = type_of_aux context s None in 
          let expected_target_type =
           match expectedty with
              None -> None
@@ -401,7 +435,7 @@ let rec type_of_aux' subterms_to_types metasenv context t expectedty =
                 | _ -> assert false
               in
                Some ty
-         in
+         in 
           let type2 =
            type_of_aux ((Some (n,(C.Decl s)))::context) t expected_target_type
           in
@@ -411,25 +445,37 @@ let rec type_of_aux' subterms_to_types metasenv context t expectedty =
 (*CSC: What are the right expected types for the source and *)
 (*CSC: target of a LetIn? None used.                        *)
         (* Let's visit all the subterms that will not be visited later *)
-        let _ = type_of_aux context s None in
+        let ty = type_of_aux context s None in
          let t_typ =
           (* Checks suppressed *)
-          type_of_aux ((Some (n,(C.Def s)))::context) t None
-         in
+          type_of_aux ((Some (n,(C.Def (s,Some ty))))::context) t None
+         in  (* CicSubstitution.subst s t_typ *)
           if does_not_occur 1 t_typ then
            (* since [Rel 1] does not occur in typ, substituting any term *)
            (* in place of [Rel 1] is equivalent to delifting once        *)
-           CicSubstitution.subst C.Implicit t_typ
+           CicSubstitution.subst (C.Implicit None) t_typ
           else
            C.LetIn (n,s,t_typ)
      | C.Appl (he::tl) when List.length tl > 0 ->
+        (* 
         let expected_hetype =
          (* Inefficient, the head is computed twice. But I know *)
-         (* of no other solution.                               *)
+         (* of no other solution. *)                               
          (head_beta_reduce
-          (R.whd context (CicTypeChecker.type_of_aux' metasenv context he)))
-        in
-         let hetype = type_of_aux context he (Some expected_hetype) in
+          (R.whd context (xxx_type_of_aux' metasenv context he)))
+        in 
+         let hetype = type_of_aux context he (Some expected_hetype) in 
+         let tlbody_and_type =
+          let rec aux =
+           function
+              _,[] -> []
+            | C.Prod (n,s,t),he::tl ->
+               (he, type_of_aux context he (Some (head_beta_reduce s)))::
+                (aux (R.whd context (S.subst he t), tl))
+            | _ -> assert false
+          in
+           aux (expected_hetype, tl) *)
+         let hetype = R.whd context (type_of_aux context he None) in 
          let tlbody_and_type =
           let rec aux =
            function
@@ -439,7 +485,7 @@ let rec type_of_aux' subterms_to_types metasenv context t expectedty =
                 (aux (R.whd context (S.subst he t), tl))
             | _ -> assert false
           in
-           aux (expected_hetype, tl)
+           aux (hetype, tl)
          in
           eat_prods context hetype tlbody_and_type
      | C.Appl _ -> raise (NotWellTyped "Appl: no arguments")
@@ -479,7 +525,7 @@ let rec type_of_aux' subterms_to_types metasenv context t expectedty =
         in
         let (parameters, arguments,exp_named_subst) =
          let type_of_term =
-          CicTypeChecker.type_of_aux' metasenv context term
+          xxx_type_of_aux' metasenv context term
          in
           match
            R.whd context (type_of_aux context term
@@ -516,7 +562,7 @@ let rec type_of_aux' subterms_to_types metasenv context t expectedty =
               in
                let expectedtype =
                 type_of_branch context parsno need_dummy outtype cons
-                  (CicTypeChecker.type_of_aux' metasenv context cons)
+                  (xxx_type_of_aux' metasenv context cons)
                in
                 ignore (type_of_aux context p
                  (Some (head_beta_reduce expectedtype))) ;
@@ -584,7 +630,7 @@ let rec type_of_aux' subterms_to_types metasenv context t expectedty =
          None ->
           (* No expected type *)
           {synthesized = synthesized' ; expected = None}, synthesized
-       | Some ty when syntactic_equality synthesized' ty ->
+       | Some ty when xxx_syntactic_equality synthesized' ty ->
           (* The expected type is synthactically equal to *)
           (* the synthesized type. Let's forget it.       *)
           {synthesized = synthesized' ; expected = None}, synthesized
@@ -628,10 +674,23 @@ let rec type_of_aux' subterms_to_types metasenv context t expectedty =
    let t1' = CicReduction.whd context t1 in
    let t2' = CicReduction.whd ((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) -> (* different from Coq manual!!! *)
+      (C.Sort _, C.Sort s2)
+        when (s2 = C.Prop or s2 = C.Set or s2 = C.CProp) -> 
+        (* different from Coq manual!!! *)
          C.Sort s2
-    | (C.Sort s1, C.Sort s2) -> C.Sort C.Type (*CSC manca la gestione degli universi!!! *)
+    | (C.Sort (C.Type t1), C.Sort (C.Type t2)) -> 
+       (* TASSI: CONSRTAINTS: the same in cictypechecker,cicrefine *)
+       let t' = CicUniv.fresh() in
+       if not (CicUniv.add_ge t' t1) || not (CicUniv.add_ge t' t2) then
+         assert false ; (* not possible, error in CicUniv *)
+       C.Sort (C.Type t')
+    | (C.Sort _,C.Sort (C.Type t1)) -> 
+        (* TASSI: CONSRTAINTS: the same in cictypechecker,cicrefine *)
+       C.Sort (C.Type t1) (* c'e' bisogno di un fresh? *)
+    | (C.Meta _, C.Sort _) -> t2'
+    | (C.Meta _, (C.Meta (_,_) as t))
+    | (C.Sort _, (C.Meta (_,_) as t)) when CicUtil.is_closed t ->
+        t2'
     | (_,_) ->
       raise
        (NotWellTyped