atoms.ml 2.52 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
module HashedString = 
struct 
  type t = string 
  let hash = Hashtbl.hash
  let equal = (=)
end

module AtomPool  = Pool.Make(HashedString)
type v = AtomPool.t
let value = AtomPool.value
let mk = AtomPool.mk

type t = Finite of v list | Cofinite of v list
14
15

let empty = Finite []
16
let any   = Cofinite []
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

let atom x = Finite [x]

let cup s t =
  match (s,t) with
    | (Finite s, Finite t) -> Finite (SortedList.cup s t)
    | (Finite s, Cofinite t) -> Cofinite (SortedList.diff t s)
    | (Cofinite s, Finite t) -> Cofinite (SortedList.diff s t)
    | (Cofinite s, Cofinite t) -> Cofinite (SortedList.cap s t)

let cap s t =
  match (s,t) with
    | (Finite s, Finite t) -> Finite (SortedList.cap s t)
    | (Finite s, Cofinite t) -> Finite (SortedList.diff s t)
    | (Cofinite s, Finite t) -> Finite (SortedList.diff t s)
    | (Cofinite s, Cofinite t) -> Cofinite (SortedList.cup s t)

let diff s t =
  match (s,t) with
    | (Finite s, Cofinite t) -> Finite (SortedList.cap s t)
    | (Finite s, Finite t) -> Finite (SortedList.diff s t)
    | (Cofinite s, Cofinite t) -> Finite (SortedList.diff t s)
    | (Cofinite s, Finite t) -> Cofinite (SortedList.cup s t)
	
let contains x = function
  | Finite s -> SortedList.mem s x
  | Cofinite s -> not (SortedList.mem s x)

let is_empty = function
  | Finite [] -> true
  | _ -> false
48
49
50
51

let is_atom = function
  | Finite [a] -> Some a
  | _ -> None
52
      
53
let sample = function
54
  | Finite (x :: _) -> x
55
  | Cofinite l -> AtomPool.dummy_min
56
57
  | Finite [] -> raise Not_found

58
59
60
61
62
let print_v ppf a = 
  if a = AtomPool.dummy_min then
    Format.fprintf ppf "(almost any atom)"
  else
    Format.fprintf ppf "`%s" (value a)
63

64
65
let print = function
  | Finite l -> List.map (fun x ppf -> print_v ppf x) l
66
  | Cofinite [] ->
67
      [ fun ppf -> Format.fprintf ppf "Atom" ]
68
  | Cofinite [h] ->
69
      [ fun ppf -> Format.fprintf ppf "@[Atom - %a@]" print_v h ]
70
71
  | Cofinite (h::t) -> 
      [ fun ppf -> 
72
73
74
	  Format.fprintf ppf "@[Atom - (";
	  print_v ppf h;
	  List.iter (fun x -> Format.fprintf ppf " |@ %a" print_v x) t;
75
	  Format.fprintf ppf ")@]" ]
76

77
78
79
80
81
82
83
let rec hash_seq accu = function
  | t::rem -> hash_seq (accu * 17 + t) rem
  | [] -> accu

let hash accu = function
  | Finite l -> hash_seq (accu + 1) l
  | Cofinite l -> hash_seq (accu + 3) l
84

85
86
87
88
89
90
91
92
93
94
95
let rec equal_rec l1 l2 =
  (l1 == l2) ||
  match (l1,l2) with
    | (x1::l1,x2::l2) -> (x1 == x2) && (equal_rec l1 l2)
    | _ -> false

let equal t1 t2 = match (t1,t2) with
  | (Finite l1, Finite l2) -> equal_rec l1 l2
  | (Cofinite l1, Cofinite l2) -> equal_rec l1 l2
  | _ -> false