]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/software/components/ng_paramodulation/orderings.ml
functorial abstraction over term blobs
[helm.git] / helm / software / components / ng_paramodulation / orderings.ml
index 4e84c49b6250856ef5b00aa488bc1870f1a851d5..dde09e81f6a0087b46237aaad8421d5e96b52ea7 100644 (file)
 
 (* $Id$ *)
 
-(* (weight of constants, [(meta, weight_of_meta)]) *)
-type weight = int * (int * int) list;;
-
-let string_of_weight (cw, mw) =
-  let s =
-    String.concat ", "
-      (List.map (function (m, w) -> Printf.sprintf "(%d,%d)" m w) mw)
-  in
-  Printf.sprintf "[%d; %s]" cw s
-;;
-
-let weight_of_term term =
-  let vars_dict = Hashtbl.create 5 in
-  let rec aux = function
-    | Terms.Var i -> 
-        (try
-           let oldw = Hashtbl.find vars_dict i in
-           Hashtbl.replace vars_dict i (oldw+1)
-         with Not_found ->
-           Hashtbl.add vars_dict i 1);
-        0
-    | Terms.Leaf _ -> 1
-    | Terms.Node l -> List.fold_left (+) 0 (List.map aux l)
-  in
-  let w = aux term in
-  let l =
-    Hashtbl.fold (fun meta metaw resw -> (meta, metaw)::resw) vars_dict [] 
-  in
-  let compare w1 w2 = 
-    match w1, w2 with
-    | (m1, _), (m2, _) -> m2 - m1 
-  in 
-  (w, List.sort compare l) (* from the biggest meta to the smallest (0) *)
-;;
-
-let compute_clause_weight = assert false (*
-  let factor = 2 in
-  match o with
-    | Terms.Lt -> 
-       let w, m = (weight_of_term 
-              ~consider_metas:true ~count_metas_occurrences:false right) in
-         w + (factor * (List.length m)) ;
-    | Terms.Le -> assert false
-    | Terms.Gt -> 
-       let w, m = (weight_of_term 
-              ~consider_metas:true ~count_metas_occurrences:false left) in
-         w + (factor * (List.length m)) ;
-  | Terms.Ge -> assert false
-  | Terms.Eq 
-  | Terms.Incomparable -> 
-      let w1, m1 = (weight_of_term 
-              ~consider_metas:true ~count_metas_occurrences:false right) in
-      let w2, m2 = (weight_of_term 
-              ~consider_metas:true ~count_metas_occurrences:false left) in
-      w1 + w2 + (factor * (List.length m1)) + (factor * (List.length m2))
-*)
-;;
-
-(* returns a "normalized" version of the polynomial weight wl (with type
- * weight list), i.e. a list sorted ascending by meta number,
- * from 0 to maxmeta. wl must be sorted descending by meta number. Example:
- * normalize_weight 5 (3, [(3, 2); (1, 1)]) ->
- *      (3, [(1, 1); (2, 0); (3, 2); (4, 0); (5, 0)]) *)
-let normalize_weight maxmeta (cw, wl) =
-  let rec aux = function
-    | 0 -> []
-    | m -> (m, 0)::(aux (m-1))
-  in
-  let tmpl = aux maxmeta in
-  let wl =
-    List.sort
-      (fun (m, _) (n, _) -> Pervasives.compare m n)
-      (List.fold_left
-         (fun res (m, w) -> (m, w)::(List.remove_assoc m res)) tmpl wl)
-  in
-  (cw, wl)
-;;
-
-
-let normalize_weights (cw1, wl1) (cw2, wl2) =
-  let rec aux wl1 wl2 =
-    match wl1, wl2 with
-    | [], [] -> [], []
-    | (m, w)::tl1, (n, w')::tl2 when m = n ->
-        let res1, res2 = aux tl1 tl2 in
-        (m, w)::res1, (n, w')::res2
-    | (m, w)::tl1, ((n, w')::_ as wl2) when m < n ->
-        let res1, res2 = aux tl1 wl2 in
-        (m, w)::res1, (m, 0)::res2
-    | ((m, w)::_ as wl1), (n, w')::tl2 when m > n ->
-        let res1, res2 = aux wl1 tl2 in
-        (n, 0)::res1, (n, w')::res2
-    | [], (n, w)::tl2 ->
-        let res1, res2 = aux [] tl2 in
-        (n, 0)::res1, (n, w)::res2
-    | (m, w)::tl1, [] ->
-        let res1, res2 = aux tl1 [] in
-        (m, w)::res1, (m, 0)::res2
-    | _, _ -> assert false
-  in
-  let cmp (m, _) (n, _) = compare m n in
-  let wl1, wl2 = aux (List.sort cmp wl1) (List.sort cmp wl2) in
-  (cw1, wl1), (cw2, wl2)
-;;
-
-(* Riazanov: 3.1.5 pag 38 *)
-(* TODO: optimize early detection of Terms.Incomparable case *)
-let compare_weights (h1, w1) (h2, w2) =
-  let res, diffs =
-    try
-      List.fold_left2
-        (fun ((lt, eq, gt), diffs) w1 w2 ->
-           match w1, w2 with
-           | (meta1, w1), (meta2, w2) when meta1 = meta2 ->
-               let diffs = (w1 - w2) + diffs in 
-               let r = compare w1 w2 in
-               if r < 0 then (lt+1, eq, gt), diffs
-               else if r = 0 then (lt, eq+1, gt), diffs
-               else (lt, eq, gt+1), diffs
-           | _ -> assert false)
-        ((0, 0, 0), 0) w1 w2
-    with Invalid_argument _ -> assert false
-  in
-  let hdiff = h1 - h2 in 
-  match res with
-  | (0, _, 0) ->
-      if hdiff < 0 then Terms.Lt
-      else if hdiff > 0 then Terms.Gt
-      else Terms.Eq 
-  | (m, _, 0) ->
-      if hdiff <= 0 then Terms.Lt
-      else if (- diffs) >= hdiff then Terms.Le else Terms.Incomparable
-  | (0, _, m) ->
-      if hdiff >= 0 then Terms.Gt
-      else if diffs >= (- hdiff) then Terms.Ge else Terms.Incomparable
-  | (m, _, n) when m > 0 && n > 0 -> Terms.Incomparable
-  | _ -> assert false 
-;;
-
-
-let rec aux_ordering t1 t2 =
-  match t1, t2 with
-  | Terms.Var _, _
-  | _, Terms.Var _ -> Terms.Incomparable
-
-  | Terms.Leaf a1, Terms.Leaf a2 -> 
-      let cmp = Pervasives.compare a1 a2 in
-      if cmp = 0 then Terms.Eq else if cmp < 0 then Terms.Lt else Terms.Gt
-
-  | Terms.Leaf _, Terms.Node _ -> Terms.Lt
-  | Terms.Node _, Terms.Leaf _ -> Terms.Gt
-
-  | Terms.Node l1, Terms.Node l2 ->
-      let rec cmp t1 t2 =
-        match t1, t2 with
-        | [], [] -> Terms.Eq
-        | _, [] -> Terms.Gt
-        | [], _ -> Terms.Lt
-        | hd1::tl1, hd2::tl2 ->
-            let o = aux_ordering hd1 hd2 in
-            if o = Terms.Eq then cmp tl1 tl2
-            else o
-      in
-      cmp l1 l2
-;;
-
-let nonrec_kbo t1 t2 =
-  let w1 = weight_of_term t1 in
-  let w2 = weight_of_term t2 in
-  let w1, w2 = normalize_weights w1 w2 in
-  match compare_weights w1 w2 with
-  | Terms.Le -> if aux_ordering t1 t2 = Terms.Lt then Terms.Lt else Terms.Incomparable
-  | Terms.Ge -> if aux_ordering t1 t2 = Terms.Gt then Terms.Gt else Terms.Incomparable
-  | Terms.Eq -> aux_ordering t1 t2
-  | res -> res
-;;
-
-(*
-let rec kbo t1 t2 =
-  let aux = aux_ordering ~recursion:false in
-  let w1 = weight_of_term t1
-  and w2 = weight_of_term t2 in
-  let rec cmp t1 t2 =
+module Orderings (B : Terms.Blob) = struct
+
+  type weight = int * (int * int) list;;
+  
+  let string_of_weight (cw, mw) =
+    let s =
+      String.concat ", "
+        (List.map (function (m, w) -> Printf.sprintf "(%d,%d)" m w) mw)
+    in
+    Printf.sprintf "[%d; %s]" cw s
+  ;;
+  
+  let weight_of_term term =
+    let vars_dict = Hashtbl.create 5 in
+    let rec aux = function
+      | Terms.Var i -> 
+          (try
+             let oldw = Hashtbl.find vars_dict i in
+             Hashtbl.replace vars_dict i (oldw+1)
+           with Not_found ->
+             Hashtbl.add vars_dict i 1);
+          0
+      | Terms.Leaf _ -> 1
+      | Terms.Node l -> List.fold_left (+) 0 (List.map aux l)
+    in
+    let w = aux term in
+    let l =
+      Hashtbl.fold (fun meta metaw resw -> (meta, metaw)::resw) vars_dict [] 
+    in
+    let compare w1 w2 = 
+      match w1, w2 with
+      | (m1, _), (m2, _) -> m2 - m1 
+    in 
+    (w, List.sort compare l) (* from the biggest meta to the smallest (0) *)
+  ;;
+  
+  let compute_clause_weight = assert false (*
+    let factor = 2 in
+    match o with
+      | Terms.Lt -> 
+       let w, m = (weight_of_term 
+              ~consider_metas:true ~count_metas_occurrences:false right) in
+         w + (factor * (List.length m)) ;
+      | Terms.Le -> assert false
+      | Terms.Gt -> 
+       let w, m = (weight_of_term 
+              ~consider_metas:true ~count_metas_occurrences:false left) in
+         w + (factor * (List.length m)) ;
+    | Terms.Ge -> assert false
+    | Terms.Eq 
+    | Terms.Incomparable -> 
+        let w1, m1 = (weight_of_term 
+              ~consider_metas:true ~count_metas_occurrences:false right) in
+        let w2, m2 = (weight_of_term 
+              ~consider_metas:true ~count_metas_occurrences:false left) in
+        w1 + w2 + (factor * (List.length m1)) + (factor * (List.length m2))
+  *)
+  ;;
+  
+  (* returns a "normalized" version of the polynomial weight wl (with type
+   * weight list), i.e. a list sorted ascending by meta number,
+   * from 0 to maxmeta. wl must be sorted descending by meta number. Example:
+   * normalize_weight 5 (3, [(3, 2); (1, 1)]) ->
+   *      (3, [(1, 1); (2, 0); (3, 2); (4, 0); (5, 0)]) *)
+  let normalize_weight maxmeta (cw, wl) =
+    let rec aux = function
+      | 0 -> []
+      | m -> (m, 0)::(aux (m-1))
+    in
+    let tmpl = aux maxmeta in
+    let wl =
+      List.sort
+        (fun (m, _) (n, _) -> Pervasives.compare m n)
+        (List.fold_left
+           (fun res (m, w) -> (m, w)::(List.remove_assoc m res)) tmpl wl)
+    in
+    (cw, wl)
+  ;;
+  
+  
+  let normalize_weights (cw1, wl1) (cw2, wl2) =
+    let rec aux wl1 wl2 =
+      match wl1, wl2 with
+      | [], [] -> [], []
+      | (m, w)::tl1, (n, w')::tl2 when m = n ->
+          let res1, res2 = aux tl1 tl2 in
+          (m, w)::res1, (n, w')::res2
+      | (m, w)::tl1, ((n, w')::_ as wl2) when m < n ->
+          let res1, res2 = aux tl1 wl2 in
+          (m, w)::res1, (m, 0)::res2
+      | ((m, w)::_ as wl1), (n, w')::tl2 when m > n ->
+          let res1, res2 = aux wl1 tl2 in
+          (n, 0)::res1, (n, w')::res2
+      | [], (n, w)::tl2 ->
+          let res1, res2 = aux [] tl2 in
+          (n, 0)::res1, (n, w)::res2
+      | (m, w)::tl1, [] ->
+          let res1, res2 = aux tl1 [] in
+          (m, w)::res1, (m, 0)::res2
+      | _, _ -> assert false
+    in
+    let cmp (m, _) (n, _) = compare m n in
+    let wl1, wl2 = aux (List.sort cmp wl1) (List.sort cmp wl2) in
+    (cw1, wl1), (cw2, wl2)
+  ;;
+  
+  (* Riazanov: 3.1.5 pag 38 *)
+  (* TODO: optimize early detection of Terms.Incomparable case *)
+  let compare_weights (h1, w1) (h2, w2) =
+    let res, diffs =
+      try
+        List.fold_left2
+          (fun ((lt, eq, gt), diffs) w1 w2 ->
+             match w1, w2 with
+             | (meta1, w1), (meta2, w2) when meta1 = meta2 ->
+                 let diffs = (w1 - w2) + diffs in 
+                 let r = compare w1 w2 in
+                 if r < 0 then (lt+1, eq, gt), diffs
+                 else if r = 0 then (lt, eq+1, gt), diffs
+                 else (lt, eq, gt+1), diffs
+             | _ -> assert false)
+          ((0, 0, 0), 0) w1 w2
+      with Invalid_argument _ -> assert false
+    in
+    let hdiff = h1 - h2 in 
+    match res with
+    | (0, _, 0) ->
+        if hdiff < 0 then Terms.Lt
+        else if hdiff > 0 then Terms.Gt
+        else Terms.Eq 
+    | (m, _, 0) ->
+        if hdiff <= 0 then Terms.Lt
+        else if (- diffs) >= hdiff then Terms.Le else Terms.Incomparable
+    | (0, _, m) ->
+        if hdiff >= 0 then Terms.Gt
+        else if diffs >= (- hdiff) then Terms.Ge else Terms.Incomparable
+    | (m, _, n) when m > 0 && n > 0 -> Terms.Incomparable
+    | _ -> assert false 
+  ;;
+  
+  
+  let rec aux_ordering t1 t2 =
     match t1, t2 with
-    | [], [] -> Terms.Eq
-    | _, [] -> Terms.Gt
-    | [], _ -> Terms.Lt
-    | hd1::tl1, hd2::tl2 ->
-        let o =
-          kbo hd1 hd2
+    | Terms.Var _, _
+    | _, Terms.Var _ -> Terms.Incomparable
+  
+    | Terms.Leaf a1, Terms.Leaf a2 -> 
+        let cmp = Pervasives.compare a1 a2 in
+        if cmp = 0 then Terms.Eq else if cmp < 0 then Terms.Lt else Terms.Gt
+  
+    | Terms.Leaf _, Terms.Node _ -> Terms.Lt
+    | Terms.Node _, Terms.Leaf _ -> Terms.Gt
+  
+    | Terms.Node l1, Terms.Node l2 ->
+        let rec cmp t1 t2 =
+          match t1, t2 with
+          | [], [] -> Terms.Eq
+          | _, [] -> Terms.Gt
+          | [], _ -> Terms.Lt
+          | hd1::tl1, hd2::tl2 ->
+              let o = aux_ordering hd1 hd2 in
+              if o = Terms.Eq then cmp tl1 tl2
+              else o
         in
-        if o = Terms.Eq then cmp tl1 tl2
-        else o
-  in
-  let w1, w2 = normalize_weights w1 w2 in
-  let comparison = compare_weights w1 w2 in
-  match comparison with
-  | Terms.Le ->
-      let r = aux t1 t2 in
-      if r = Terms.Lt then Terms.Lt
-      else if r = Terms.Eq then (
-        match t1, t2 with
-        | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
-            if cmp tl1 tl2 = Terms.Lt then Terms.Lt else Terms.Incomparable
-        | _, _ ->  Terms.Incomparable
-      ) else Terms.Incomparable
-  | Terms.Ge ->
-      let r = aux t1 t2 in
-      if r = Terms.Gt then Terms.Gt
-      else if r = Terms.Eq then (
-        match t1, t2 with
-        | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
-            if cmp tl1 tl2 = Terms.Gt then Terms.Gt else Terms.Incomparable
-        | _, _ ->  Terms.Incomparable
-      ) else Terms.Incomparable
-  | Terms.Eq ->
-      let r = aux t1 t2 in
-      if r = Terms.Eq then (
-        match t1, t2 with
-        | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
-            cmp tl1 tl2
-        | _, _ ->  Terms.Incomparable
-      ) else r 
-  | res -> res
-;;
-*)
-          
-let compare_terms = nonrec_kbo;; 
-
+        cmp l1 l2
+  ;;
+  
+  let nonrec_kbo t1 t2 =
+    let w1 = weight_of_term t1 in
+    let w2 = weight_of_term t2 in
+    let w1, w2 = normalize_weights w1 w2 in
+    match compare_weights w1 w2 with
+    | Terms.Le -> if aux_ordering t1 t2 = Terms.Lt then Terms.Lt else Terms.Incomparable
+    | Terms.Ge -> if aux_ordering t1 t2 = Terms.Gt then Terms.Gt else Terms.Incomparable
+    | Terms.Eq -> aux_ordering t1 t2
+    | res -> res
+  ;;
+  
+  (*
+  let rec kbo t1 t2 =
+    let aux = aux_ordering ~recursion:false in
+    let w1 = weight_of_term t1
+    and w2 = weight_of_term t2 in
+    let rec cmp t1 t2 =
+      match t1, t2 with
+      | [], [] -> Terms.Eq
+      | _, [] -> Terms.Gt
+      | [], _ -> Terms.Lt
+      | hd1::tl1, hd2::tl2 ->
+          let o =
+            kbo hd1 hd2
+          in
+          if o = Terms.Eq then cmp tl1 tl2
+          else o
+    in
+    let w1, w2 = normalize_weights w1 w2 in
+    let comparison = compare_weights w1 w2 in
+    match comparison with
+    | Terms.Le ->
+        let r = aux t1 t2 in
+        if r = Terms.Lt then Terms.Lt
+        else if r = Terms.Eq then (
+          match t1, t2 with
+          | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
+              if cmp tl1 tl2 = Terms.Lt then Terms.Lt else Terms.Incomparable
+          | _, _ ->  Terms.Incomparable
+        ) else Terms.Incomparable
+    | Terms.Ge ->
+        let r = aux t1 t2 in
+        if r = Terms.Gt then Terms.Gt
+        else if r = Terms.Eq then (
+          match t1, t2 with
+          | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
+              if cmp tl1 tl2 = Terms.Gt then Terms.Gt else Terms.Incomparable
+          | _, _ ->  Terms.Incomparable
+        ) else Terms.Incomparable
+    | Terms.Eq ->
+        let r = aux t1 t2 in
+        if r = Terms.Eq then (
+          match t1, t2 with
+          | Cic.Appl (h1::tl1), Cic.Appl (h2::tl2) when h1 = h2 ->
+              cmp tl1 tl2
+          | _, _ ->  Terms.Incomparable
+        ) else r 
+    | res -> res
+  ;;
+  *)
+            
+  let compare_terms = nonrec_kbo;; 
+
+end