atoms.ml 2.49 KB
Newer Older
1
module AtomPool  = Pool.Make(SortedList.String)
2
3
4
5
type v = AtomPool.t
let value = AtomPool.value
let mk = AtomPool.mk

6
module SList = SortedList.Make_transp(SortedList.Lift(AtomPool))
7
type t = Finite of unit SList.t | Cofinite of unit SList.t
8
9

let empty = Finite []
10
let any   = Cofinite []
11
12
13
14
15

let atom x = Finite [x]

let cup s t =
  match (s,t) with
16
17
18
19
    | (Finite s, Finite t) -> Finite (SList.cup s t)
    | (Finite s, Cofinite t) -> Cofinite (SList.diff t s)
    | (Cofinite s, Finite t) -> Cofinite (SList.diff s t)
    | (Cofinite s, Cofinite t) -> Cofinite (SList.cap s t)
20
21
22

let cap s t =
  match (s,t) with
23
24
25
26
    | (Finite s, Finite t) -> Finite (SList.cap s t)
    | (Finite s, Cofinite t) -> Finite (SList.diff s t)
    | (Cofinite s, Finite t) -> Finite (SList.diff t s)
    | (Cofinite s, Cofinite t) -> Cofinite (SList.cup s t)
27
28
29

let diff s t =
  match (s,t) with
30
31
32
33
    | (Finite s, Cofinite t) -> Finite (SList.cap s t)
    | (Finite s, Finite t) -> Finite (SList.diff s t)
    | (Cofinite s, Cofinite t) -> Finite (SList.diff t s)
    | (Cofinite s, Finite t) -> Cofinite (SList.cup s t)
34
35
	
let contains x = function
36
37
  | Finite s -> SList.mem s x
  | Cofinite s -> not (SList.mem s x)
38
39
40
41

let is_empty = function
  | Finite [] -> true
  | _ -> false
42
43
44
45

let is_atom = function
  | Finite [a] -> Some a
  | _ -> None
46
      
47
let sample = function
48
  | Finite (x :: _) -> x
49
  | Cofinite l -> AtomPool.dummy_min
50
51
  | Finite [] -> raise Not_found

52
53
54
55
56
let print_v ppf a = 
  if a = AtomPool.dummy_min then
    Format.fprintf ppf "(almost any atom)"
  else
    Format.fprintf ppf "`%s" (value a)
57

58
59
let print = function
  | Finite l -> List.map (fun x ppf -> print_v ppf x) l
60
  | Cofinite [] ->
61
      [ fun ppf -> Format.fprintf ppf "Atom" ]
62
  | Cofinite [h] ->
63
      [ fun ppf -> Format.fprintf ppf "@[Atom - %a@]" print_v h ]
64
65
  | Cofinite (h::t) -> 
      [ fun ppf -> 
66
67
68
	  Format.fprintf ppf "@[Atom - (";
	  print_v ppf h;
	  List.iter (fun x -> Format.fprintf ppf " |@ %a" print_v x) t;
69
70
	  Format.fprintf ppf ")@]" ]

71
72

(* TODO: clean what follow to re-use SList operations *)
73
74
75
76
77
78
79
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
80

81
82
83
84
85
86
87
88
89
90
91
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
  
92