var.ml 1.56 KB
Newer Older
1
module V = struct
2 3 4 5 6
  type t = { id : Ident.U.t ; fr : int ; kind : int }
  type kind = int
  let function_kind = 1
  let argument_kind = 2

7
  let dump ppf t = Format.fprintf ppf "{%a(%d_%d)}" Ident.U.print t.id t.fr t.kind
8
  let compare x y = Pervasives.compare (x.kind,x.id,x.fr) (y.kind,y.id,y.fr)
9 10
  let equal x y =
    x == y || (x.kind == y.kind && x.fr == y.fr && Ident.U.equal x.id y.id)
11
  let hash x = Hashtbl.hash (x.id,x.fr,x.kind)
12
  let check _ = ()
13

14 15
  let freshcounter = ref 0

16
  let is_fresh x = x.fr > 0
17

18
  let fresh v = { v with fr = (incr freshcounter;!freshcounter) }
19

20 21 22 23
  let mk id = { id = Ident.U.mk id; fr = 0; kind = 0; }

  let id x = Ident.U.get_str x.id
  let set_kind k v = { v with kind = k }
Pietro Abate's avatar
Pietro Abate committed
24
  let pp ppf x = Format.fprintf ppf "'%a" Ident.U.print x.id
25 26 27 28
end

include V
type var = t
29

Pietro Abate's avatar
Pietro Abate committed
30
module Set = struct
31 32 33
  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)
34
  let printf = pp Format.std_formatter
Pietro Abate's avatar
Pietro Abate committed
35
end
36

37
type 'a var_or_atom = [ `Atm of 'a | `Var of t ]
38

39
module Make (X : Custom.T) = struct
40 41
  type t = X.t var_or_atom
  let hash = function `Atm t -> 17 + 17 * X.hash t | `Var x -> 997 + 17 * V.hash x
42

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

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

52
  let equal t1 t2 = (compare t1 t2) == 0
53

54 55
  let dump ppf = function
    |`Atm x -> X.dump ppf x
56
    |`Var x -> V.dump ppf x
57
end