var.ml 968 Bytes
Newer Older
1
2
3
4
5

type t = String.t
type var = [ `Var of t ]
type 'a pairvar = [ `Atm of 'a | var ]

6
let compare (x : var) (y : var) = Pervasives.compare x y
Pietro Abate's avatar
Pietro Abate committed
7
let equal x y = Pervasives.compare x y = 0
8
9
let hash (v : var) = Hashtbl.hash v

10
11
module Make (X : Custom.T) = struct
  type t = X.t pairvar
12
  let hash = function `Atm t -> X.hash t | `Var s -> hash (`Var s)
13
14
15
  let check = function `Atm t -> X.check t | `Var _ -> ()
  let compare t1 t2 =
    match t1,t2 with
16
    |`Var x, `Var y -> compare (`Var x) (`Var y)
17
18
19
20
21
    |`Atm x, `Atm y -> X.compare x y
    |`Var _, `Atm _ -> -1
    |`Atm _, `Var _ -> 1

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

23
24
25
26
27
28
29
30
31
32
33
  let dump ppf = function
    |`Atm x -> X.dump ppf x
    |`Var x -> Format.fprintf ppf "`$%s" x
end

let fresh : unit -> var =
  let counter = ref 0 in
  fun _ ->
    let v = `Var (Printf.sprintf "_fresh_%d" !counter) in
    incr counter;
    v
34
35
36
37
38
39

module Set = Set.Make(
  struct 
    type t = var
    let compare = Pervasives.compare
  end)