]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_paramodulation/foUtils.ml
Branched paramodulation for CNF (Horn clauses)
[helm.git] / helm / software / components / ng_paramodulation / foUtils.ml
1 (*
2     ||M||  This file is part of HELM, an Hypertextual, Electronic        
3     ||A||  Library of Mathematics, developed at the Computer Science     
4     ||T||  Department, University of Bologna, Italy.                     
5     ||I||                                                                
6     ||T||  HELM is free software; you can redistribute it and/or         
7     ||A||  modify it under the terms of the GNU General Public License   
8     \   /  version 2 or (at your option) any later version.      
9      \ /   This software is distributed as is, NO WARRANTY.     
10       V_______________________________________________________________ *)
11
12 (* $Id: terms.ml 9836 2009-06-05 15:33:35Z denes $ *)
13
14 let rec lexicograph f l1 l2 =
15   match l1, l2 with
16   | [], [] -> 0
17   | x::xs, y::ys ->
18      let c = f x y in
19      if c <> 0 then c else lexicograph f xs ys
20   | [],_ -> ~-1
21   | _,[] -> 1
22 ;;
23   
24 module Utils (B : Orderings.Blob) = struct
25   module Subst = FoSubst;; 
26   module Order = B;;
27
28   let rec eq_foterm x y =
29     x == y ||
30     match x, y with
31     | Terms.Leaf t1, Terms.Leaf t2 -> B.eq t1 t2
32     | Terms.Var i, Terms.Var j -> i = j
33     | Terms.Node l1, Terms.Node l2 -> List.for_all2 eq_foterm l1 l2
34     | _ -> false
35   ;;
36
37
38   let rec compare_foterm x y =
39     match x, y with
40     | Terms.Leaf t1, Terms.Leaf t2 -> B.compare t1 t2
41     | Terms.Var i, Terms.Var j -> i - j
42     | Terms.Node l1, Terms.Node l2 -> lexicograph compare_foterm l1 l2
43     | Terms.Leaf _, ( Terms.Node _ | Terms.Var _ ) -> ~-1
44     | Terms.Node _, Terms.Leaf _ -> 1
45     | Terms.Node _, Terms.Var _ -> ~-1
46     | Terms.Var _, _ ->  1
47   ;;
48
49   let eq_literal l1 l2 =
50     match l1, l2 with
51     | Terms.Predicate p1, Terms.Predicate p2 -> eq_foterm p1 p2
52     | Terms.Equation (l1,r1,ty1,o1), Terms.Equation (l2,r2,ty2,o2) ->
53         o1 = o2 && eq_foterm l1 l2 && eq_foterm r1 r2 && eq_foterm ty1 ty2
54     | _ -> false
55   ;;
56
57   let compare_literal l1 l2 =
58     match l1, l2 with
59     | Terms.Predicate p1, Terms.Predicate p2 -> compare_foterm p1 p2
60     | Terms.Equation (l1,r1,ty1,o1), Terms.Equation (l2,r2,ty2,o2) ->
61         let c = Pervasives.compare o1 o2 in
62         if c <> 0 then c else
63           let c = compare_foterm l1 l2 in
64           if c <> 0 then c else
65             let c = compare_foterm r1 r2 in
66             if c <> 0 then c else
67               compare_foterm ty1 ty2
68     | Terms.Predicate _, Terms.Equation _ -> ~-1
69     | Terms.Equation _, Terms.Predicate _ -> 1
70   ;;
71
72   let eq_clause (id1,_,_,_,_) (id2,_,_,_,_) = id1 = id2
73   let compare_clause (id1,_,_,_,_) (id2,_,_,_,_) = Pervasives.compare id1 id2
74     
75   let relocate maxvar varlist subst =
76     List.fold_right
77       (fun i (maxvar, varlist, s) -> 
78          maxvar+1, maxvar::varlist, Subst.build_subst i (Terms.Var maxvar) s)
79       varlist (maxvar+1, [], subst)
80   ;;
81
82   let fresh_clause maxvar (id, nlit, plit, varlist, proof) =
83     let maxvar, varlist, subst = relocate maxvar varlist Subst.id_subst in
84     let apply_subst_on_lit = function
85       | Terms.Equation (l,r,ty,o) ->
86           let l = Subst.apply_subst subst l in
87           let r = Subst.apply_subst subst r in
88           let ty = Subst.apply_subst subst ty in
89           Terms.Equation (l,r,ty,o)
90       | Terms.Predicate p ->
91           let p = Subst.apply_subst subst p in
92           Terms.Predicate p
93     in
94     let nlit = List.map apply_subst_on_lit nlit in
95     let plit = List.map apply_subst_on_lit plit in
96     let proof =
97       match proof with
98       | Terms.Exact t -> Terms.Exact (Subst.apply_subst subst t)
99       | Terms.Step (rule,c1,c2,dir,pos,s) ->
100           Terms.Step(rule,c1,c2,dir,pos,Subst.concat subst s)
101     in
102      (id, nlit, plit, varlist, proof), maxvar
103   ;;
104
105   (* may be moved inside the bag *)
106   let mk_clause maxvar nlit plit proofterm =
107     let foterm_to_lit (acc,literals) ty =
108       let vars = Terms.vars_of_term ~start_acc:acc ty in
109       match ty with
110       | Terms.Node [ Terms.Leaf eq ; ty; l; r ] when B.eq B.eqP eq ->
111            let o = Order.compare_terms l r in
112            (vars,Terms.Equation (l, r, ty, o)::literals)
113       | _ -> (vars,Terms.Predicate ty::literals)
114     in
115     let varlist = Terms.vars_of_term proofterm in
116     let (varlist,nlit) = List.fold_left foterm_to_lit (varlist,[]) nlit in
117     let (varlist,plit) = List.fold_left foterm_to_lit (varlist,[]) plit in
118     let proof = Terms.Exact proofterm in
119     fresh_clause maxvar (0, nlit, plit, varlist, proof)
120   ;;
121
122   let mk_passive_clause cl =
123     (Order.compute_clause_weight cl, cl)
124   ;;
125
126   let mk_passive_goal g =
127     (Order.compute_clause_weight g, g)
128   ;;
129
130   let compare_passive_clauses_weight (w1,(id1,_,_,_,_)) (w2,(id2,_,_,_,_)) =
131     if w1 = w2 then id1 - id2
132     else w1 - w2
133   ;;
134
135   let compare_passive_clauses_age (_,(id1,_,_,_,_)) (_,(id2,_,_,_,_)) =
136     id1 - id2
137   ;;
138
139 end