]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/ocaml/metadata/metadataConstraints.ml
renamed Http_client to Http_user_agent to avoid clashes with Gerd's
[helm.git] / helm / ocaml / metadata / metadataConstraints.ml
index 3a985deb9304c34537029fab103294cc9648198a..fb782494a6a0c99318a9850a991260b6040f9be1 100644 (file)
@@ -39,74 +39,92 @@ type cardinality_condition =
 
 let tbln n = "table" ^ string_of_int n
 
+(*
 let add_depth_constr depth_opt cur_tbl where =
   match depth_opt with
   | None -> where
   | Some depth -> (sprintf "%s.h_depth = %d" cur_tbl depth) :: where
+*)
+
+let mk_positions positions cur_tbl =
+  "(" ^
+  String.concat " or "
+    (List.map
+      (fun pos ->
+        let pos_str = MetadataPp.pp_position_tag pos in
+        match pos with
+        | `InBody
+        | `InConclusion
+        | `InHypothesis
+        | `MainConclusion None
+        | `MainHypothesis None ->
+            sprintf "%s.h_position = \"%s\"" cur_tbl pos_str
+        | `MainConclusion (Some d)
+        | `MainHypothesis (Some d) ->
+            sprintf "(%s.h_position = \"%s\" and %s.h_depth = %d)"
+              cur_tbl pos_str cur_tbl d)
+      (positions :> MetadataTypes.position list)) ^
+  ")"
 
 let add_card_constr tbl (n,from,where) = function
   | None -> (n,from,where)
   | Some (Eq card) ->
+      let cur_tbl = tbln n in
       (n+1,
-       (sprintf "%s as %s" tbl (tbln n) :: from),
-       (sprintf "no=%d" card ::
+       (sprintf "%s as %s" tbl cur_tbl :: from),
+       (sprintf "%s.no=%d" cur_tbl card ::
         (if n=0 then []
-        else [sprintf "table0.source = %s.source" (tbln n)]) @
+        else [sprintf "table0.source = %s.source" cur_tbl]) @
         where))
   | Some (Gt card) ->
+      let cur_tbl = tbln n in
       (n+1,
-       (sprintf "%s as %s" tbl (tbln n) :: from),
-       (sprintf "no>%d" card ::
+       (sprintf "%s as %s" tbl cur_tbl :: from),
+       (sprintf "%s.no>%d" cur_tbl card ::
         (if n=0 then []
-        else [sprintf "table0.source = %s.source" (tbln n)]) @
+        else [sprintf "table0.source = %s.source" cur_tbl]) @
         where))
 
 let at_least ~(dbh:Dbi.connection) ?concl_card ?full_card
-  (metadata: MetadataTypes.metadata list)
+  (metadata: MetadataTypes.constr list)
 =
   if (metadata = []) && concl_card = None && full_card = None then
     failwith "MetadataQuery.at_least: no constraints given";
   let add_constraint (n,from,where) metadata =
     let cur_tbl = tbln n in
     match metadata with
-    | `Obj (uri, pos, depth_opt) ->
+    | `Obj (uri, positions) ->
         let tbl = MetadataTypes.obj_tbl in
-        let pos_str = MetadataPp.pp_position pos in
         let from = (sprintf "%s as %s" tbl cur_tbl) :: from in
         let where =
-          (sprintf "%s.h_position = \"%s\"" cur_tbl pos_str) ::
           (sprintf "%s.h_occurrence = \"%s\"" cur_tbl uri) ::
+          mk_positions positions cur_tbl ::
           (if n=0 then []
           else [sprintf "table0.source = %s.source" cur_tbl]) @
           where
         in
-        let where = add_depth_constr depth_opt cur_tbl where in
         ((n+1), from, where)
-    | `Rel (pos, depth) ->
+    | `Rel positions ->
         let tbl = MetadataTypes.rel_tbl in
-        let pos_str = MetadataPp.pp_position (pos :> MetadataTypes.position) in
         let from = (sprintf "%s as %s" tbl cur_tbl) :: from in
         let where =
-          (sprintf "%s.h_position = \"%s\"" cur_tbl pos_str) ::
+          mk_positions positions cur_tbl ::
           (if n=0 then []
           else [sprintf "table0.source = %s.source" cur_tbl]) @
           where
         in
-        let where = add_depth_constr (Some depth) cur_tbl where in
         ((n+1), from, where)
-    | `Sort (sort, pos, depth) ->
+    | `Sort (sort, positions) ->
         let tbl = MetadataTypes.sort_tbl in
-        let pos_str = MetadataPp.pp_position (pos :> MetadataTypes.position) in
         let sort_str = MetadataPp.pp_sort sort in
         let from = (sprintf "%s as %s" tbl cur_tbl) :: from in
         let where =
-          (sprintf "%s.h_position = \"%s\"" cur_tbl pos_str) ::
           (sprintf "%s.h_sort = \"%s\"" cur_tbl sort_str) ::
+          mk_positions positions cur_tbl ::
           (if n=0 then []
           else [sprintf "table0.source = %s.source" cur_tbl]) @
           where
         in
-        let where = add_depth_constr (Some depth) cur_tbl where in
         ((n+1), from, where)
   in
   let (n,from,where) = List.fold_left add_constraint (0,[],[]) metadata in
@@ -119,7 +137,6 @@ let at_least ~(dbh:Dbi.connection) ?concl_card ?full_card
   let from = String.concat ", " from in
   let where = String.concat " and " where in
   let query = sprintf "select table0.source from %s where %s" from where in
-prerr_endline query;
   let query = dbh#prepare query in
   query#execute [];
   List.map (function [`String s] -> s | _ -> assert false) (query#fetchall ())
@@ -154,8 +171,8 @@ and add_root n root childs =
       (SetSet.singleton (StringSet.singleton root))
 
 and inspect_conclusion n t = 
-if n = 0 then SetSet.empty
-else match t with
+  if n = 0 then SetSet.empty
+  else match t with
       Cic.Rel _                    
     | Cic.Meta _                     
     | Cic.Sort _ 
@@ -346,17 +363,30 @@ let union l =
     | u::l -> u::(drop_repetitions l) in
   drop_repetitions (List.sort Pervasives.compare (List.concat l))
 
-let must_of_prefix m s =
-  let s' = List.map (fun u -> `Obj (u, `InConclusion, None)) s in
-  `Obj (m, `MainConclusion, None) :: s'
+let must_of_prefix ?(where = `Conclusion) m s =
+  let positions =
+    match where with
+    | `Conclusion -> [`InConclusion]
+    | `Statement -> [`InConclusion; `InHypothesis; `MainHypothesis None]
+  in
+  let s' = List.map (fun u -> `Obj (u, positions)) s in
+  `Obj (m, [`MainConclusion None]) :: s'
 
 let escape = Str.global_replace (Str.regexp_string "\'") "\\'"
 
-let get_inconcl (dbh:Dbi.connection) uri =
+let get_constants (dbh:Dbi.connection) ~where uri =
   let uri = escape uri in
+  let positions =
+    match where with
+    | `Conclusion -> ["\"MainConclusion\""; "\"InConclusion\""]
+    | `Statement ->
+        ["\"MainConclusion\""; "\"InConclusion\""; "\"InHypothesis\"";
+         "\"MainHypothesis\""]
+  in
   let query = 
-    dbh#prepare (sprintf "select h_occurrence from refObj where source=\"%s\" and (h_position=\"MainConclusion\" or h_position=\"InConclusion\")"
-      uri)
+    dbh#prepare
+      (sprintf "select h_occurrence from refObj where source=\"%s\" and (%s)"
+        uri (String.concat " or " positions))
   in
   query#execute [];
   query#fold_left (* transform the result in a set *)
@@ -365,8 +395,8 @@ let get_inconcl (dbh:Dbi.connection) uri =
       StringSet.add uri set)
     StringSet.empty
 
-let test_only ~(dbh:Dbi.connection) only u =
-  let inconcl = get_inconcl dbh u in
+let at_most ~(dbh:Dbi.connection) ?(where = `Conclusion) only u =
+  let inconcl = get_constants dbh ~where u in
   StringSet.subset inconcl only
 
   (* Special handling of equality. The problem is filtering out theorems just
@@ -380,7 +410,7 @@ let myspeciallist =
    0,"cic:/Coq/Init/Logic/f_equal2.con";
    0,"cic:/Coq/Init/Logic/f_equal3.con"]
 
-let compute_exactly ~(dbh:Dbi.connection) main prefixes =
+let compute_exactly ~(dbh:Dbi.connection) where main prefixes =
   List.concat
     (List.map 
       (fun (m,s) -> 
@@ -388,13 +418,18 @@ let compute_exactly ~(dbh:Dbi.connection) main prefixes =
           myspeciallist
         else
           let res =
-            at_least ~dbh ~concl_card:(Eq (m+1)) (must_of_prefix main s)
+            let must = must_of_prefix ~where main s in
+            match where with
+            | `Conclusion -> at_least ~dbh ~concl_card:(Eq (m+1)) must
+            | `Statement -> at_least ~dbh ~full_card:(Eq (m+1)) must
           in
           List.map (fun uri -> (m, uri)) res)
       prefixes)
 
   (* critical value reached, fallback to "only" constraints *)
-let compute_with_only ~(dbh:Dbi.connection) main prefixes constants =
+let compute_with_only ~(dbh:Dbi.connection) ?(where = `Conclusion) main
+  prefixes constants
+=
   let max_prefix_length = 
     match prefixes with
     | [] -> assert false 
@@ -410,15 +445,18 @@ let compute_with_only ~(dbh:Dbi.connection) main prefixes constants =
       union
         (List.map 
           (fun (m,s) -> 
+            let must = must_of_prefix ~where main s in
             (let res = 
-              at_least ~dbh ~concl_card:(Gt (m+1)) (must_of_prefix main s)
+              match where with
+              | `Conclusion -> at_least ~dbh ~concl_card:(Gt (m+1)) must
+              | `Statement -> at_least ~dbh ~full_card:(Gt (m+1)) must
             in
             (* we tag the uri with m+1, for sorting purposes *)
             List.map (fun uri -> (m+1, uri)) res))
           maximal_prefixes)
     in
-    List.filter (function (_,uri) -> test_only ~dbh constants uri) all in
-    let equal_to = compute_exactly ~dbh main prefixes in
+    List.filter (function (_,uri) -> at_most ~dbh ~where constants uri) all in
+    let equal_to = compute_exactly ~dbh where main prefixes in
     greater_than @ equal_to
 
   (* real match query implementation *)
@@ -428,7 +466,8 @@ let cmatch ~(dbh:Dbi.connection) t =
   | None -> []
   | Some (main, types) ->
       (* the type of eq is not counted in constants_no *)
-      let constants_no = StringSet.cardinal constants in
+      let types_no = List.length types in
+      let constants_no = StringSet.cardinal constants + types_no in
       if (constants_no > critical_value) then 
         let prefixes = prefixes just_factor t in
         (match prefixes with
@@ -438,23 +477,25 @@ let cmatch ~(dbh:Dbi.connection) t =
             in
             compute_with_only ~dbh main all_concl all_constants
          | _, _ -> [])
-      else if constants_no = 0 then []
       else
         (* in this case we compute all prefixes, and we do not need
            to apply the only constraints *)
-        let prefixes = prefixes constants_no t in
+        let prefixes =
+          if constants_no = types_no then
+            Some main, [0, []; types_no, types]
+          else
+            prefixes constants_no t
+        in
         (match prefixes with
-             Some main, all_concl ->
-               List.concat
-                 (List.map 
-                    (fun (m,s) -> 
-                      (let res = 
-                        at_least ~dbh ~concl_card:(Eq (m+1))
-                          (must_of_prefix main s)
-                      in
-                      List.map (fun uri -> (m, uri)) res))
-                    all_concl)
-           | _, _ -> [])
+           Some main, all_concl ->
+             List.concat
+               (List.map 
+                  (fun (m,s) -> 
+                    let must = must_of_prefix ~where:`Conclusion main s in
+                    let res = at_least ~dbh ~concl_card:(Eq (m+1)) must in
+                    List.map (fun uri -> (m, uri)) res)
+                  all_concl)
+         | _, _ -> [])
 
 let power_upto upto consts =
   let l = StringSet.elements consts in
@@ -472,7 +513,9 @@ let power consts =
     (fun res a -> res@(List.map (function (n,l) -> (n+1,a::l)) res)) 
      [(0,[])] l)
 
-let sigmatch ~(dbh:Dbi.connection) (main, constants) =
+type where = [ `Conclusion | `Statement ]
+
+let sigmatch ~(dbh:Dbi.connection) ?(where = `Conclusion) (main, constants) =
   match main with
     None -> []
   | Some (main, types) ->
@@ -486,14 +529,14 @@ let sigmatch ~(dbh:Dbi.connection) (main, constants) =
         let all_constants = 
           List.fold_right StringSet.add types (StringSet.add main constants)
         in
-        compute_with_only ~dbh main subsets all_constants
+        compute_with_only ~dbh ~where main subsets all_constants
       else
         let subsets = 
           let subsets = power constants in
           let types_no = List.length types in
-          List.map (function (n,l) -> (n+types_no,types@l)) subsets
+          (0,[]) :: List.map (function (n,l) -> (n+types_no,types@l)) subsets
         in
-        compute_exactly ~dbh main subsets
+        compute_exactly ~dbh where main subsets
 
   (* match query wrappers *)
 let cmatch' = cmatch