]> matita.cs.unibo.it Git - helm.git/blobdiff - components/tactics/closeCoercionGraph.ml
compose tactic restore and added nocomposites keyword
[helm.git] / components / tactics / closeCoercionGraph.ml
index 68e4e7cb2c2b2c176e274e76b6257ea7e09cf2a0..7ed3c36e6b86d24d4a46b753a750e11597d919c0 100644 (file)
@@ -33,44 +33,62 @@ 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 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
-      CoercDb.eq_carr ?exact s t
+            let rc = CoercDb.eq_carr ?exact s t in
+            debug_print(lazy(string_of_bool rc));
+            rc
     with
-    | CoercDb.EqCarrNotImplemented _ | CoercDb.EqCarrOnNonMetaClosed -> false
+    | CoercDb.EqCarrNotImplemented _ | CoercDb.EqCarrOnNonMetaClosed -> 
+        debug_print (lazy("false"));            
+        false
   in
   match src,tgt with
   | CoercDb.Uri _, CoercDb.Uri _ ->
+                  debug_print (lazy ("Uri, Uri4"));
       let c_from_tgt = 
         List.filter 
-          (fun (f,t,_) -> eq_carr f tgt (*&& not (eq_carr t src)*)) 
+          (fun (f,t,_) -> 
+                  
+                  debug_print (lazy ("Uri, Uri3"));
+                  eq_carr f tgt (*&& not (eq_carr t src)*)) 
           coercions 
       in
       let c_to_src = 
         List.filter 
-          (fun (f,t,_) -> eq_carr t src (*&& not (eq_carr f tgt)*)) 
+          (fun (f,t,_) -> 
+                  
+                  debug_print (lazy ("Uri, Uri2"));
+                  eq_carr t src (*&& not (eq_carr f tgt)*)) 
           coercions 
       in
         (HExtlib.flatten_map 
           (fun (_,t,ul) -> 
              if eq_carr ~exact:true src t then [] else
-             List.map (fun u -> src,[uri; u],t) ul) c_from_tgt) @
+             List.map (fun u -> src,[uri; enrich u t],t) ul) c_from_tgt) @
         (HExtlib.flatten_map 
-          (fun (s,_,ul) -> 
+          (fun (s,t,ul) -> 
              if eq_carr ~exact:true s tgt then [] else
-             List.map (fun u -> s,[u; uri],tgt) ul) c_to_src) @
+             List.map (fun u -> s,[enrich u t; uri],tgt) ul) c_to_src) @
         (HExtlib.flatten_map 
-          (fun (s,_,u1l) ->
+          (fun (s,t1,u1l) ->
             HExtlib.flatten_map 
               (fun (_,t,u2l) ->
                 HExtlib.flatten_map
                   (fun u1 ->
+                  debug_print (lazy ("Uri, Uri1"));
                     if  eq_carr ~exact:true s t
                      || eq_carr ~exact:true s tgt
                      || eq_carr ~exact:true src t
                     then [] else
                     List.map 
-                      (fun u2 -> (s,[u1;uri;u2],t)) 
+                      (fun u2 -> (s,[enrich u1 t1;uri;enrich u2 t],t)) 
                       u2l)
                   u1l) 
               c_from_tgt) 
@@ -83,8 +101,14 @@ let obj_attrs n = [`Class (`Coercion n); `Generated]
 exception UnableToCompose
 
 (* generate_composite (c2 (c1 s)) in the universe graph univ
- * both living in the same context and metasenv *)
-let generate_composite' (c1,sat1) (c2,sat2) context metasenv univ arity =
+   both living in the same context and metasenv
+
+    c2 ?p2 (c1 ?p1 ?x ?s1) ?s2
+
+    where:
+     ?pn + 1 + ?sn = count_pi n - arity n
+*)
+let generate_composite' (c1,sat1,arity1) (c2,sat2,arity2) context metasenv univ=
   let original_metasenv = metasenv in 
   let c1_ty,univ = CicTypeChecker.type_of_aux' metasenv context c1 univ in
   let c2_ty,univ = CicTypeChecker.type_of_aux' metasenv context c2 univ in
@@ -111,8 +135,8 @@ let generate_composite' (c1,sat1) (c2,sat2) context metasenv univ arity =
       names
   in
   let compose c1 nc1 c2 nc2 =
-   Cic.Appl (CicSubstitution.lift 1 c2 :: mk_implicits (nc2 - sat2 - 1) @
-     Cic.Appl (CicSubstitution.lift 1 c1 :: mk_implicits nc1 ) ::
+   Cic.Appl ((*CicSubstitution.lift 1*) c2 :: mk_implicits (nc2 - sat2 - 1) @
+     Cic.Appl ((*CicSubstitution.lift 1*) c1 :: mk_implicits nc1 ) ::
      mk_implicits sat2)
   in
   let rec create_subst_from_metas_to_rels n = function 
@@ -141,11 +165,40 @@ let generate_composite' (c1,sat1) (c2,sat2) context metasenv univ arity =
       | t -> t
     in
     let skip_appl = function | Cic.Appl l -> List.tl l | _ -> assert false in
+    let rec metas_of_term_and_types t =
+      let metas = CicUtil.metas_of_term t in
+      let types = 
+       List.flatten       
+        (List.map 
+          (fun (i,_) -> try 
+            let _,_,ty = CicUtil.lookup_meta i body_metasenv in metas_of_term_and_types ty
+            with CicUtil.Meta_not_found _ -> []) 
+          metas)
+      in
+      metas @ types
+    in
+    let sorted_metas_of_term world t = 
+      let metas = metas_of_term_and_types t in
+      (* this check should be useless *)
+      let metas = List.filter (fun (i,_)->List.exists (fun (j,_,_) -> j=i) world) metas in
+      let order_metas metasenv metas = 
+        let module OT = struct type t = int let compare = Pervasives.compare end in
+        let module S = HTopoSort.Make (OT) in
+        let dep i = 
+          try 
+            let _,_,ty = List.find (fun (j,_,_) -> j=i) metasenv in
+            let metas = List.map fst (CicUtil.metas_of_term ty) in
+            HExtlib.list_uniq (List.sort Pervasives.compare metas)
+          with Not_found -> []
+        in
+          S.topological_sort (List.map (fun (i,_) -> i) metas) dep 
+      in 
+      order_metas world metas
+    in
     let metas_that_saturate l =
       List.fold_left 
         (fun (acc,n) t ->
-          let metas = CicUtil.metas_of_term t in
-          let metas = List.map fst metas in
+          let metas = sorted_metas_of_term body_metasenv t in
           let metas = 
             List.filter (fun i -> List.for_all (fun (j,_) -> j<>i) acc) metas in
           let metas = List.map (fun i -> i,n) metas in
@@ -165,12 +218,24 @@ let generate_composite' (c1,sat1) (c2,sat2) context metasenv univ arity =
     let meta_to_be_coerced =
      try
       match List.nth l_c1 (c1_pis - sat1 - 1) with
-       | Cic.Meta (i,_) -> i
-       | _ -> assert false
+       | Cic.Meta (i,_) -> Some i
+       | t -> 
+          debug_print 
+            (lazy("meta_to_be_coerced: " ^ CicPp.ppterm t));
+          debug_print 
+            (lazy("c1_pis: " ^ string_of_int c1_pis ^ 
+             " sat1:" ^ string_of_int sat1));
+          None
      with
       Failure _ -> assert false
     in
-    let meta2no = fst (metas_that_saturate (l_c2_b @ l_c1 @ l_c2_a)) in
+    (* BIG HACK ORRIBLE:
+     * it should be (l_c2_b @ l_c1 @ l_c2_a), but in this case sym (eq_f) gets
+     *  \A,B,f,x,y,Exy and not \B,A,f,x,y,Exy
+     * as an orrible side effect, the other composites get a type lyke
+     *  \A,x,y,Exy,B,f with 2 saturations
+     *)
+    let meta2no = fst (metas_that_saturate (l_c1 @ l_c2_b @ l_c2_a)) in
     let sorted =
      List.sort 
       (fun (i,ctx1,ty1) (j,ctx1,ty1) -> 
@@ -184,15 +249,20 @@ let generate_composite' (c1,sat1) (c2,sat2) context metasenv univ arity =
       | (i,_,_)::_ when i = n -> acc
       | _::tl -> position_of n (acc + 1) tl
     in
-    debug_print
-     (lazy ("META_TO_BE_COERCED: " ^ string_of_int meta_to_be_coerced));
-    let position_of_meta_to_be_coerced =
-     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));
-     debug_print (lazy ("SATURATIONS: " ^
-      string_of_int (List.length sorted - position_of_meta_to_be_coerced - 1)));
-     sorted, List.length sorted - position_of_meta_to_be_coerced - 1
+    let saturations_res = 
+      match meta_to_be_coerced with
+      | None -> 0
+      | Some meta_to_be_coerced -> 
+          debug_print
+            (lazy ("META_TO_BE_COERCED: " ^ string_of_int meta_to_be_coerced));
+          let position_of_meta_to_be_coerced =
+            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
+    in
+    debug_print (lazy ("SATURATIONS: " ^ string_of_int saturations_res));
+    sorted, saturations_res
   in
   let namer l n = 
     let l = List.map (function Cic.Name s -> s | _ -> "A") l in
@@ -210,8 +280,8 @@ let generate_composite' (c1,sat1) (c2,sat2) context metasenv univ arity =
   debug_print (lazy ("\nCOMPOSING"));
   debug_print (lazy (" c1= "^CicPp.ppterm c1 ^"  :  "^ CicPp.ppterm c1_ty));
   debug_print (lazy (" c2= "^CicPp.ppterm c2 ^"  :  "^ CicPp.ppterm c2_ty));
-  let c1_pis, names_c1 = count_pis c1_ty 0 in 
-  let c2_pis, names_c2 = count_pis c2_ty arity in
+  let c1_pis, names_c1 = count_pis c1_ty arity1 in 
+  let c2_pis, names_c2 = count_pis c2_ty arity2 in
   let c = compose c1 c1_pis c2 c2_pis in
   let spine_len = c1_pis + c2_pis in
   let c = mk_lambda_spine c (namer (names_c1 @ names_c2)) spine_len in
@@ -224,6 +294,8 @@ let generate_composite' (c1,sat1) (c2,sat2) context metasenv univ arity =
         CicRefine.type_of_aux' metasenv context c univ
       in
       debug_print(lazy("COMPOSED REFINED: "^CicPp.ppterm term));
+      debug_print(lazy("COMPOSED REFINED (pretty): "^
+        CicMetaSubst.ppterm_in_context [] ~metasenv term context));
 (*       let metasenv = order_metasenv metasenv in *)
 (*       debug_print(lazy("ORDERED MENV: "^CicMetaSubst.ppmetasenv [] metasenv)); *)
       let body_metasenv, lambdas_metasenv = 
@@ -278,7 +350,7 @@ let generate_composite' (c1,sat1) (c2,sat2) context metasenv univ arity =
         CicRefine.insert_coercions := old_insert_coercions;
         raise exn
   in
-  c, metasenv, univ, saturationsres
+  c, metasenv, univ, saturationsres, arity2
 ;;
 
 let build_obj c univ arity =
@@ -305,9 +377,9 @@ let filter_duplicates l coercions =
           CoercDb.eq_carr s src && 
           CoercDb.eq_carr t tgt &&
           try 
-            List.for_all2 (fun (u1,_) (u2,_) -> UriManager.eq u1 u2) l1 l2
+            List.for_all2 (fun (u1,_,_) (u2,_) -> UriManager.eq u1 u2) l1 l2
           with
-          | Invalid_argument "List.for_all2" -> false)
+          | Invalid_argument "List.for_all2" -> debug_print (lazy("XXX")); false)
         coercions))
   l
 
@@ -358,6 +430,7 @@ 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
+  debug_print (lazy("composed " ^ string_of_int (List.length todo_list)));
   let todo_list = filter_duplicates todo_list coercions in
   try
     let new_coercions = 
@@ -366,44 +439,45 @@ let close_coercion_graph src tgt uri saturations baseuri =
           try 
             (match l with
             | [] -> assert false 
-            | (he,saturations1) :: tl ->
-                let arity = match tgt with CoercDb.Fun n -> n | _ -> 0 in
+            | (he,saturations1,arity1) :: tl ->
                 let first_step = 
                   Cic.Constant ("", 
-                    Some (CoercDb.term_of_carr (CoercDb.Uri he)),
-                    Cic.Sort Cic.Prop, [], obj_attrs arity), saturations1
+                   Some (CoercDb.term_of_carr (CoercDb.Uri he)),
+                    Cic.Sort Cic.Prop, [], obj_attrs arity1),
+                  saturations1,
+                  arity1
                 in
                 let o,_ = 
-                  List.fold_left (fun (o,univ) (coer,saturations) ->
+                  List.fold_left (fun (o,univ) (coer,saturations2,arity2) ->
                     match o with 
-                    | Cic.Constant (_,Some u,_,[],_),saturations1 ->
-                        let t, menv, univ, saturationsres = 
-                          generate_composite' (u,saturations1) 
+                    | Cic.Constant (_,Some u,_,[],_),saturations1,arity1 ->
+                        let t, menv, univ, saturationsres, arityres = 
+                          generate_composite' (u,saturations1,arity1
                             (CoercDb.term_of_carr (CoercDb.Uri coer),
-                             saturations) [] [] univ arity
+                             saturations2, arity2) [] [] univ
                         in
                         if (menv = []) then
                           HLog.warn "MENV non empty after composing coercions";
-                        let o,univ = build_obj t univ arity in
-                         (o,saturationsres),univ
+                        let o,univ = build_obj t univ arityres in
+                         (o,saturationsres,arityres),univ
                     | _ -> assert false 
                   ) (first_step, CicUniv.empty_ugraph) tl
                 in
                 let name_src = CoercDb.name_of_carr src in
                 let name_tgt = CoercDb.name_of_carr tgt in
-                let by = List.map (fun u,_ -> UriManager.name_of_uri u) l 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 = 
+                let named_obj,saturations,arity = 
                   match o with
-                  | Cic.Constant (_,bo,ty,vl,attrs),saturations ->
-                      Cic.Constant (name,bo,ty,vl,attrs),saturations
+                  | Cic.Constant (_,bo,ty,vl,attrs),saturations,arity ->
+                      Cic.Constant (name,bo,ty,vl,attrs),saturations,arity
                   | _ -> assert false 
                 in
-                  (src,tgt,c_uri,saturations,named_obj))::acc
+                  (src,tgt,c_uri,saturations,named_obj,arity))::acc
           with UnableToCompose -> acc
       ) [] todo_list
     in
@@ -415,9 +489,9 @@ 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 arity =
- let a,b,c,_ =
-  generate_composite' (c1,0) (c2,0) context metasenv univ arity
+let generate_composite c1 c2 context metasenv univ sat1 sat2 =
+ let a,b,c,_,_ =
+  generate_composite' (c1,sat1,0) (c2,sat2,0) context metasenv univ
  in
   a,b,c
 ;;