]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/software/components/tactics/closeCoercionGraph.ml
Patch to add a debugging string to HExtlib.split_nth reverted
[helm.git] / helm / software / components / tactics / closeCoercionGraph.ml
index e9a123ad8a411781733a6dd50d3660ecee6ddf97..2c67c95ee2a333154b80bce2ac3cc3760b783548 100644 (file)
@@ -33,39 +33,32 @@ let debug_print s = if debug then prerr_endline (Lazy.force s) else ()
  * (source, list of coercions to follow, target)
  *)
 let get_closure_coercions src tgt uri coercions =
-  let enrich (uri,sat) tgt =
+  let enrich (uri,sat,_) tgt =
    let arity = match tgt with CoercDb.Fun n -> n | _ -> 0 in
     uri,sat,arity
   in
   let uri = enrich uri tgt in
   let eq_carr ?exact s t = 
-    debug_print (lazy(CoercDb.name_of_carr s^" VS "^CoercDb.name_of_carr t));
-    try
-            let rc = CoercDb.eq_carr ?exact s t in
-            debug_print(lazy(string_of_bool rc));
-            rc
-    with
-    | CoercDb.EqCarrNotImplemented _ | CoercDb.EqCarrOnNonMetaClosed -> 
-        debug_print (lazy("false"));            
-        false
+    debug_print(lazy(CoercDb.string_of_carr s^" VS "^CoercDb.string_of_carr t));
+    let rc = CoercDb.eq_carr ?exact s t in
+    debug_print(lazy(string_of_bool rc));
+    rc
   in
   match src,tgt with
   | CoercDb.Uri _, CoercDb.Uri _ ->
-                  debug_print (lazy ("Uri, Uri4"));
+      debug_print (lazy ("Uri, Uri4"));
       let c_from_tgt = 
         List.filter 
           (fun (f,t,_) -> 
-                  
-                  debug_print (lazy ("Uri, Uri3"));
-                  eq_carr f tgt (*&& not (eq_carr t src)*)) 
+             debug_print (lazy ("Uri, Uri3"));
+             eq_carr f tgt) 
           coercions 
       in
       let c_to_src = 
         List.filter 
           (fun (f,t,_) -> 
-                  
-                  debug_print (lazy ("Uri, Uri2"));
-                  eq_carr t src (*&& not (eq_carr f tgt)*)) 
+             debug_print (lazy ("Uri, Uri2"));
+             eq_carr t src) 
           coercions 
       in
         (HExtlib.flatten_map 
@@ -96,8 +89,6 @@ let get_closure_coercions src tgt uri coercions =
   | _ -> [] (* do not close in case source or target is not an indty ?? *)
 ;;
 
-let obj_attrs n = [`Class (`Coercion n); `Generated]
-
 exception UnableToCompose
 
 (* generate_composite (c2 (c1 s)) in the universe graph univ
@@ -249,9 +240,9 @@ let generate_composite' (c1,sat1,arity1) (c2,sat2,arity2) context metasenv univ=
       | (i,_,_)::_ when i = n -> acc
       | _::tl -> position_of n (acc + 1) tl
     in
-    let saturations_res = 
+    let saturations_res, position_of_meta_to_be_coerced = 
       match meta_to_be_coerced with
-      | None -> 0
+      | None -> 0,0
       | Some meta_to_be_coerced -> 
           debug_print
             (lazy ("META_TO_BE_COERCED: " ^ string_of_int meta_to_be_coerced));
@@ -259,10 +250,11 @@ let generate_composite' (c1,sat1,arity1) (c2,sat2,arity2) context metasenv univ=
             position_of meta_to_be_coerced 0 sorted in
           debug_print (lazy ("POSITION_OF_META_TO_BE_COERCED: " ^
             string_of_int position_of_meta_to_be_coerced));
-          List.length sorted - position_of_meta_to_be_coerced - 1
+          List.length sorted - position_of_meta_to_be_coerced - 1,
+          position_of_meta_to_be_coerced
     in
     debug_print (lazy ("SATURATIONS: " ^ string_of_int saturations_res));
-    sorted, saturations_res
+    sorted, saturations_res, position_of_meta_to_be_coerced
   in
   let namer l n = 
     let l = List.map (function Cic.Name s -> s | _ -> "A") l in
@@ -287,9 +279,11 @@ let generate_composite' (c1,sat1,arity1) (c2,sat2,arity2) context metasenv univ=
   let c = mk_lambda_spine c (namer (names_c1 @ names_c2)) spine_len in
   debug_print (lazy ("COMPOSTA: " ^ CicPp.ppterm c));
   let old_insert_coercions = !CicRefine.insert_coercions in
-  let c, metasenv, univ, saturationsres =
+  let old_pack_coercions = !CicRefine.pack_coercions in
+  let c, metasenv, univ, saturationsres, cpos =
     try
       CicRefine.insert_coercions := false;
+      CicRefine.pack_coercions := false;
       let term, ty, metasenv, ugraph = 
         CicRefine.type_of_aux' metasenv context c univ
       in
@@ -303,7 +297,7 @@ let generate_composite' (c1,sat1,arity1) (c2,sat2,arity2) context metasenv univ=
       in
       debug_print(lazy("B_MENV: "^CicMetaSubst.ppmetasenv [] body_metasenv));
       debug_print(lazy("L_MENV: "^CicMetaSubst.ppmetasenv [] lambdas_metasenv));
-      let body_metasenv, saturationsres =
+      let body_metasenv, saturationsres, cpos =
        order_body_menv term body_metasenv c1_pis c2_pis
       in
       debug_print(lazy("ORDERED_B_MENV: "^CicMetaSubst.ppmetasenv [] body_metasenv));
@@ -340,48 +334,110 @@ let generate_composite' (c1,sat1,arity1) (c2,sat2,arity2) context metasenv univ=
       debug_print (lazy ("MENV: "^CicMetaSubst.ppmetasenv [] metasenv));
       debug_print (lazy ("####################"));
       CicRefine.insert_coercions := old_insert_coercions;
-      term, metasenv, ugraph, saturationsres
+      CicRefine.pack_coercions := old_pack_coercions;
+      term, metasenv, ugraph, saturationsres, cpos
     with
     | CicRefine.RefineFailure s 
     | CicRefine.Uncertain s -> debug_print s; 
         CicRefine.insert_coercions := old_insert_coercions;
+        CicRefine.pack_coercions := old_pack_coercions;
         raise UnableToCompose
     | exn ->
         CicRefine.insert_coercions := old_insert_coercions;
+        CicRefine.pack_coercions := old_pack_coercions;
         raise exn
   in
-  c, metasenv, univ, saturationsres, arity2
+  let c_ty, univ = 
+    CicTypeChecker.type_of_aux' ~subst:[] [] [] c univ
+  in
+  let real_composed = ref true in
+  let c = 
+    let rec is_id = function
+      | Cic.Lambda(_,_,t) -> is_id t
+      | Cic.Rel 1 -> true
+      | _ -> false
+    in
+    let is_id = function
+      | Cic.Const (u,_) -> 
+          (match CicEnvironment.get_obj CicUniv.empty_ugraph u with
+          | Cic.Constant (_,Some bo,_,_,_), _ ->  is_id bo
+          | _ -> false)
+      | _ -> false
+    in
+    let unvariant u =
+     match CicEnvironment.get_obj CicUniv.empty_ugraph u with
+     | Cic.Constant (_,Some (Cic.Const (u',_)),_,_,attrs), _
+       when List.exists ((=) (`Flavour `Variant)) attrs ->
+         u'
+     | _ -> u
+    in
+    let is_variant u =
+     match CicEnvironment.get_obj CicUniv.empty_ugraph u with
+     | Cic.Constant (_,Some (Cic.Const (u',_)),_,_,attrs), _
+       when List.exists ((=) (`Flavour `Variant)) attrs -> true
+     | _ -> false
+    in
+    let rec aux = function
+      | Cic.Lambda(n,s,t) -> Cic.Lambda(n,s,aux t)
+      | Cic.Appl (c::_) as t -> 
+          let t = 
+            if is_id c then
+              (real_composed := false ;
+               CicReduction.head_beta_reduce ~delta:true t)
+            else t
+          in 
+          (match t with
+          | Cic.Appl l -> Cic.Appl (List.map aux l)
+          | Cic.Const (u,[]) when is_variant u -> Cic.Const (unvariant u,[])
+          | t -> t)
+       | Cic.Const (u,[]) when is_variant u -> Cic.Const (unvariant u,[])
+       | t -> t
+    in
+    let simple_eta_c t = 
+      let incr = 
+        List.map (function Cic.Rel n -> Cic.Rel (n+1) | _ -> assert false)
+      in
+      let rec aux acc ctx = function
+        | Cic.Lambda (n,s,tgt) -> 
+            aux (incr acc @ [Cic.Rel 1]) (Some (n,Cic.Decl s) ::ctx) tgt
+        | Cic.Appl (t::tl) when tl = acc && 
+            CicTypeChecker.does_not_occur ctx 0 (List.length acc) t -> true, t
+        | t -> false, t
+      in
+      let b, newt = aux [] [] t in
+      if b then newt else t
+    in
+     simple_eta_c (aux c)
+  in
+  debug_print (lazy ("COMPOSED COMPRESSED: " ^ string_of_bool !real_composed ^" : " ^ CicPp.ppterm c));
+  c, c_ty, metasenv, univ, saturationsres, arity2, cpos, !real_composed
 ;;
 
-let build_obj c univ arity =
-  let c_ty,univ = 
-    try 
-      CicTypeChecker.type_of_aux' [] [] c univ
-    with CicTypeChecker.TypeCheckerFailure s ->
-      debug_print (lazy (Printf.sprintf "Generated composite coercion:\n%s\n%s" 
-        (CicPp.ppterm c) (Lazy.force s)));
-      raise UnableToCompose
-  in
+let build_obj c c_ty univ arity is_var =
   let cleaned_ty =
     FreshNamesGenerator.clean_dummy_dependent_types c_ty 
   in
-  let obj = Cic.Constant ("xxxx",Some c,cleaned_ty,[],obj_attrs arity) in 
+  let obj = Cic.Constant ("xxxx",Some c,cleaned_ty,[],
+    [`Generated] @ if not is_var then [`Flavour `Variant] else [] )  in 
+
     obj,univ
 ;;
 
 (* removes from l the coercions that are in !coercions *)
 let filter_duplicates l coercions =
   List.filter (
-      fun (src,l1,tgt) ->
-        not (List.exists (fun (s,t,l2) -> 
-          CoercDb.eq_carr s src && 
-          CoercDb.eq_carr t tgt &&
-          try 
-            List.for_all2 (fun (u1,_,_) (u2,_) -> UriManager.eq u1 u2) l1 l2
-          with
-          | Invalid_argument "List.for_all2" -> debug_print (lazy("XXX")); false)
-        coercions))
+   fun (src,l1,tgt) ->
+     not (List.exists (fun (s,t,l2) -> 
+       CoercDb.eq_carr s src && 
+       CoercDb.eq_carr t tgt &&
+       try 
+         List.for_all2 (fun (u1,_,_) (u2,_,_) -> UriManager.eq u1 u2) l1 l2
+       with
+       | Invalid_argument "List.for_all2" -> 
+           debug_print (lazy("XXX")); false)
+     coercions))
   l
+;;
 
 let mangle s t l = 
   (*List.fold_left
@@ -399,18 +455,10 @@ let number_if_already_defined buri name l =
         ("Unable to give an altenative name to " ^ buri ^ "/" ^ name ^ ".con"))
   in
   let rec aux n =
-    let suffix = if n > 0 then string_of_int n else "" in
+    let suffix = if n > 0 then ("__" ^ string_of_int n) else "" in
     let suri = buri ^ "/" ^ name ^ suffix ^ ".con" in
     let uri = UriManager.uri_of_string suri in
-    let retry () = 
-      if n < 100 then 
-        begin
-          HLog.warn ("Uri " ^ suri ^ " already exists.");
-          aux (n+1)
-        end
-      else
-        err ()
-    in
+    let retry () = if n < max_int then aux (n+1) else err () in
     if List.exists (UriManager.eq uri) l then retry ()
     else
       try
@@ -429,7 +477,7 @@ let number_if_already_defined buri name l =
 let close_coercion_graph src tgt uri saturations baseuri =
   (* check if the coercion already exists *)
   let coercions = CoercDb.to_list () in
-  let todo_list = get_closure_coercions src tgt (uri,saturations) coercions in
+  let todo_list = get_closure_coercions src tgt (uri,saturations,0) coercions in
   debug_print (lazy("composed " ^ string_of_int (List.length todo_list)));
   let todo_list = filter_duplicates todo_list coercions in
   try
@@ -437,47 +485,48 @@ let close_coercion_graph src tgt uri saturations baseuri =
       List.fold_left 
         (fun acc (src, l , tgt) ->
           try 
-            (match l with
+            match l with
             | [] -> assert false 
             | (he,saturations1,arity1) :: tl ->
                 let first_step = 
-                  Cic.Constant ("", 
-                   Some (CoercDb.term_of_carr (CoercDb.Uri he)),
-                    Cic.Sort Cic.Prop, [], obj_attrs arity1),
+                  Cic.Constant ("", Some (CicUtil.term_of_uri he),
+                  Cic.Sort Cic.Prop, [], [`Generated]),
                   saturations1,
-                  arity1
+                  arity1,0
                 in
                 let o,_ = 
                   List.fold_left (fun (o,univ) (coer,saturations2,arity2) ->
                     match o with 
-                    | Cic.Constant (_,Some u,_,[],_),saturations1,arity1 ->
-                        let t, menv, univ, saturationsres, arityres = 
+                    | Cic.Constant (_,Some u,_,[],_),saturations1,arity1,_ ->
+                        let t, t_ty, menv, univ, saturationsres, 
+                          arityres, cposres, is_var 
+                        = 
                           generate_composite' (u,saturations1,arity1) 
-                            (CoercDb.term_of_carr (CoercDb.Uri coer),
+                            (CicUtil.term_of_uri coer,
                              saturations2, arity2) [] [] univ
                         in
-                        if (menv = []) then
+                        if (menv <> []) then
                           HLog.warn "MENV non empty after composing coercions";
-                        let o,univ = build_obj t univ arityres in
-                         (o,saturationsres,arityres),univ
+                        let o,univ = build_obj t t_ty univ arityres is_var in
+                         (o,saturationsres,arityres,cposres),univ
                     | _ -> assert false 
                   ) (first_step, CicUniv.oblivion_ugraph) tl
                 in
-                let name_src = CoercDb.name_of_carr src in
-                let name_tgt = CoercDb.name_of_carr tgt in
+                let name_src = CoercDb.string_of_carr src in
+                let name_tgt = CoercDb.string_of_carr tgt in
                 let by = List.map (fun u,_,_ -> UriManager.name_of_uri u) l in
                 let name = mangle name_tgt name_src by in
                 let c_uri = 
                   number_if_already_defined baseuri name 
-                    (List.map (fun (_,_,u,_,_,_) -> u) acc) 
+                    (List.map (fun (_,_,u,_,_,_,_) -> u) acc) 
                 in
-                let named_obj,saturations,arity = 
+                let named_obj,saturations,arity,cpos = 
                   match o with
-                  | Cic.Constant (_,bo,ty,vl,attrs),saturations,arity ->
-                      Cic.Constant (name,bo,ty,vl,attrs),saturations,arity
+                  | Cic.Constant (_,bo,ty,vl,attrs),saturations,arity,cpos ->
+                      Cic.Constant (name,bo,ty,vl,attrs),saturations,arity,cpos
                   | _ -> assert false 
                 in
-                  (src,tgt,c_uri,saturations,named_obj,arity))::acc
+                  (src,tgt,c_uri,saturations,named_obj,arity,cpos)::acc
           with UnableToCompose -> acc
       ) [] todo_list
     in
@@ -490,7 +539,7 @@ CicCoercion.set_close_coercion_graph close_coercion_graph;;
 (* generate_composite (c2 (c1 s)) in the universe graph univ
  * both living in the same context and metasenv *)
 let generate_composite c1 c2 context metasenv univ sat1 sat2 =
- let a,b,c,_,_ =
+ let a,_,b,c,_,_,_,_ =
   generate_composite' (c1,sat1,0) (c2,sat2,0) context metasenv univ
  in
   a,b,c