var.ml 925 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
7
8
let compare (x : var) (y : var) = Pervasives.compare x y
let hash (v : var) = Hashtbl.hash v

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

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

22
23
24
25
26
27
28
29
30
31
32
  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
33
34
35
36
37
38

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