]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_paramodulation/terms.ml
First tests for paramodulation (pretty printer, unification)
[helm.git] / helm / software / components / ng_paramodulation / terms.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$ *)
13
14 type 'a foterm = 
15   | Leaf of 'a
16   | Var of int
17   | Node of ('a foterm) list
18
19 type 'a substitution = (int * 'a foterm) list
20
21 type comparison = Lt | Eq | Gt | Incomparable
22
23 type rule = SuperpositionRight | SuperpositionLeft | Demodulation
24 type direction = Left2Right | Right2Left | Nodir
25 type position = int list
26
27 type 'a proof =
28   | Exact of 'a
29   | Step of rule * int * int * direction * position * 'a substitution
30          (* rule, eq1, eq2, direction of eq2, position, substitution *)
31
32 type 'a literal = 
33  | Equation of    'a foterm  (* lhs *)
34                 * 'a foterm  (* rhs *)
35                 * 'a foterm  (* type *)
36                 * comparison (* orientation *)
37  | Predicate of   'a foterm 
38
39 type varlist = int list
40
41 type 'a unit_clause =
42    int        (* ID *)
43  * 'a literal
44  * varlist       (* variable list *)
45  * 'a proof      (* proof *)
46
47 type 'a passive_clause = int * 'a unit_clause (* weight * equation *)
48
49
50 module OT =
51  struct
52    type t = int 
53    let compare = Pervasives.compare
54  end
55
56 module M : Map.S with type key = int 
57   = Map.Make(OT) 
58
59 type 'a bag = 'a unit_clause M.t
60
61 module type Blob =
62   sig
63     type t
64     val eq : t -> t -> bool
65     val compare : t -> t -> int
66     val pp : t -> string
67     val embed : t -> t foterm * int list
68   end
69
70 module Utils (B : Blob) = struct
71   let rec eq_foterm x y =
72     x == y ||
73     match x, y with
74     | Leaf t1, Leaf t2 -> B.eq t1 t2
75     | Var i, Var j -> i = j
76     | Node l1, Node l2 -> List.for_all2 eq_foterm l1 l2
77     | _ -> false
78   ;;
79
80   let rec lexicograph f l1 l2 =
81     match l1, l2 with
82     | [], [] -> 0
83     | x::xs, y::ys ->
84        let c = f x y in
85        if c <> 0 then c else lexicograph f xs ys
86     | [],_ -> ~-1
87     | _,[] -> 1
88   ;;
89
90   let rec compare_foterm x y =
91     match x, y with
92     | Leaf t1, Leaf t2 -> B.compare t1 t2
93     | Var i, Var j -> i - j
94     | Node l1, Node l2 -> lexicograph compare_foterm l1 l2
95     | Leaf _, ( Node _ | Var _ ) -> ~-1
96     | Node _, Leaf _ -> 1
97     | Node _, Var _ -> ~-1
98     | Var _, _ ->  1
99   ;;
100
101   let eq_literal l1 l2 =
102     match l1, l2 with
103     | Predicate p1, Predicate p2 -> eq_foterm p1 p2
104     | Equation (l1,r1,ty1,o1), Equation (l2,r2,ty2,o2) ->
105         o1 = o2 && eq_foterm l1 l2 && eq_foterm r1 r2 && eq_foterm ty1 ty2
106     | _ -> false
107   ;;
108
109   let compare_literal l1 l2 =
110     match l1, l2 with
111     | Predicate p1, Predicate p2 -> compare_foterm p1 p2
112     | Equation (l1,r1,ty1,o1), Equation (l2,r2,ty2,o2) ->
113         let c = Pervasives.compare o1 o2 in
114         if c <> 0 then c else
115           let c = compare_foterm l1 l2 in
116           if c <> 0 then c else
117             let c = compare_foterm r1 r2 in
118             if c <> 0 then c else
119               compare_foterm ty1 ty2
120     | Predicate _, Equation _ -> ~-1
121     | Equation _, Predicate _ -> 1
122   ;;
123
124   let eq_unit_clause (id1,_,_,_) (id2,_,_,_) = id1 = id2
125   let compare_unit_clause (id1,_,_,_) (id2,_,_,_) = Pervasives.compare id1 id2
126
127 end