var.ml 1.37 KB
Newer Older
Pietro Abate's avatar
Pietro Abate committed
1

2
module V = struct
Pietro Abate's avatar
Pietro Abate committed
3
4
  type t = { id : Ident.U.t ; fr : int }
  let dump ppf t = Format.fprintf ppf "{%a(%d)}" Ident.U.print t.id t.fr
5
6
7
  let compare x y = Pervasives.compare (x.id,x.fr) (y.id,y.fr)
  let equal x y = (compare x y) = 0
  let hash x = Hashtbl.hash (x.id,x.fr)
8
  let check _ = ()
9

10
  let is_fresh x = x.fr > 0
Pietro Abate's avatar
Pietro Abate committed
11
  let fresh v = { v with fr = v.fr + 1 }
12

Pietro Abate's avatar
Pietro Abate committed
13
14
  let mk id = { id = Ident.U.mk id; fr = 0 }
  let id x = Ident.U.get_str x.id
15

Pietro Abate's avatar
Pietro Abate committed
16
  let pp ppf x = Format.fprintf ppf "'%a" Ident.U.print x.id
17
18
19
20
end

include V
type var = t
21

Pietro Abate's avatar
Pietro Abate committed
22
module Set = struct
23
24
25
  include SortedList.Make(V)
  let dump ppf s = Utils.pp_list ~sep:";" ~delim:("{","}") V.dump ppf (get s)
  let pp ppf s = Utils.pp_list ~sep:";" ~delim:("{","}") V.pp ppf (get s)
26
  let printf = pp Format.std_formatter
27
28
29
30
  let union = cup
  let inter = cap
  let cardinal = length
  let mem t v = mem v t
31
  let fold = fold
Pietro Abate's avatar
Pietro Abate committed
32
end
33

34
type 'a pairvar = [ `Atm of 'a | `Var of t ]
35

36
37
module Make (X : Custom.T) = struct
  type t = X.t pairvar
38
  let hash = function `Atm t -> X.hash t | `Var x -> V.hash x
39

40
  let check = function `Atm t -> X.check t | `Var _ -> ()
41

42
43
  let compare t1 t2 =
    match t1,t2 with
44
    |`Var x, `Var y -> compare x y
45
46
47
48
49
    |`Atm x, `Atm y -> X.compare x y
    |`Var _, `Atm _ -> -1
    |`Atm _, `Var _ -> 1

  let equal t1 t2 = (compare t1 t2) = 0
50

51
52
  let dump ppf = function
    |`Atm x -> X.dump ppf x
53
    |`Var x -> V.dump ppf x
54
end