]> matita.cs.unibo.it Git - helm.git/blob - helm/software/components/ng_paramodulation/index.ml
Fixes
[helm.git] / helm / software / components / ng_paramodulation / index.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 module Index(B : Orderings.Blob) = struct
15   module U = FoUtils.Utils(B)
16
17   module ClauseOT =
18     struct 
19       type t = Terms.direction * (* direction if it is an equality *)
20           bool *                 (* true if indexed literal is positive *)
21           int *                  (* position of literal in clause *)
22           B.t Terms.clause
23  
24       let compare (d1,p1,pos1,uc1) (d2,p2,pos2,uc2) = 
25         let c = Pervasives.compare (d1,p1,pos1) (d2,p2,pos2) in
26         if c <> 0 then c else U.compare_clause uc1 uc2
27       ;;
28     end
29
30   module ClauseSet : 
31     Set.S with type elt = Terms.direction * (* direction if it is an equality *)
32                           bool *       (* true if indexed literal is positive *)
33                           int *        (* position of literal in clause *)
34                           B.t Terms.clause
35     = Set.Make(ClauseOT)
36
37   open Discrimination_tree
38
39   module FotermIndexable : Indexable with
40     type constant_name = B.t and
41     type input = B.t Terms.foterm 
42   =
43     struct
44
45       type input = B.t Terms.foterm
46       type constant_name = B.t
47
48       let path_string_of =
49         let rec aux arity = function
50           | Terms.Leaf a -> [Constant (a, arity)]
51           | Terms.Var i -> assert (arity = 0); [Variable]
52           | Terms.Node (Terms.Var _::_) ->
53               (* FIXME : should this be allowed or not ? *)
54               assert false
55           | Terms.Node ([] | [ _ ] ) -> assert false
56           | Terms.Node (Terms.Node _::_) -> assert false              
57           | Terms.Node (hd::tl) ->
58               aux (List.length tl) hd @ List.flatten (List.map (aux 0) tl) 
59         in 
60           aux 0
61       ;;
62
63       let compare e1 e2 = 
64         match e1,e2 with 
65         | Constant (a1,ar1), Constant (a2,ar2) ->
66             let c = B.compare a1 a2 in
67             if c <> 0 then c else Pervasives.compare ar1 ar2
68         | Variable, Variable -> 0
69         | Constant _, Variable -> ~-1
70         | Variable, Constant _ -> 1
71         | Proposition, _ | _, Proposition
72         | Datatype, _ | _, Datatype
73         | Dead, _ | _, Dead
74         | Bound _, _ | _, Bound _ -> assert false
75       ;;
76
77       let string_of_path l = String.concat "." (List.map (fun _ -> "*") l) ;;
78
79     end
80
81     module DT : DiscriminationTree with
82       type constant_name = B.t and 
83       type input = B.t Terms.foterm and 
84       type data = ClauseSet.elt and 
85       type dataset = ClauseSet.t
86     = Make(FotermIndexable)(ClauseSet)
87
88   let index_literal t c is_pos pos = function
89     | Terms.Equation (l,_,_,Terms.Invertible)
90     | Terms.Equation (l,_,_,Terms.Gt) -> 
91           DT.index t l (Terms.Left2Right,is_pos,pos,c)
92     | Terms.Equation (_,r,_,Terms.Lt) ->
93           DT.index t r (Terms.Right2Left,is_pos,pos,c)
94     | Terms.Equation (l,r,_,Terms.Incomparable) ->
95           DT.index
96            (DT.index t l (Terms.Left2Right,is_pos,pos,c))
97            r (Terms.Right2Left,is_pos,pos,c)
98     | Terms.Equation (_,_,_,Terms.Eq) -> assert false
99     | Terms.Predicate p ->
100           DT.index t p (Terms.Nodir,is_pos,pos,c)
101   ;;
102
103   let index_clause t (_,nlit,plit,_,_ as c) =
104     let index_iter is_pos  (t,pos) (lit,sel) =
105       if sel then index_literal t c is_pos pos lit,pos+1 else t,pos+1
106     in
107     let (res,_) = List.fold_left (index_iter false) (t,0) nlit in
108       fst (List.fold_left (index_iter true) (res,0) plit)
109   ;;
110
111   type active_set = B.t Terms.clause list * DT.t
112
113 end