var.ml 864 Bytes
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

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

module Make (X : Custom.T) = struct
  type t = X.t pairvar
  let hash = function `Atm t -> X.hash t | `Var s -> Hashtbl.hash (`Var s)
  let check = function `Atm t -> X.check t | `Var _ -> ()
  let compare t1 t2 =
    match t1,t2 with
    |`Atm x, `Atm y -> X.compare x y
    |`Var x, `Var y when x = y -> 0
    (* HACK fix BoolVar.get to get variables in the correct order *)
    |`Var x, `Var y -> if String.compare x y = -1 then 1 else -1
    |`Var _, `Atm _ -> -1
    |`Atm _, `Var _ -> 1

  let equal t1 t2 = (compare t1 t2) = 0
  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