var.ml 1.83 KB
Newer Older
1

2
3
type t = { 
  fresh : bool;
4
  id : string;
5
}
6
7
let make_id ?(fresh=false) id = 
   { id = id ; fresh = fresh }
8

9
let dump ppf t = Format.fprintf ppf "{id=%s;fresh=%b}" t.id t.fresh
10
11
12
13
14

let compare x y = Pervasives.compare x.id y.id
let equal x y = Pervasives.compare x.id y.id = 0
let hash x = Hashtbl.hash x.id

15
16
17
type var = [ `Var of t ]
type 'a pairvar = [ `Atm of 'a | var ]

18
19
20
21
22
let dump ppf (`Var x) = Format.fprintf ppf "%a" dump x
let print ppf (`Var x) = Format.fprintf ppf "`$%s" x.id
let compare (`Var x) (`Var y) = compare x y
let equal v1 v2 = (compare v1 v2) = 0

Pietro Abate's avatar
Pietro Abate committed
23
let id (`Var t) = t.id
Pietro Abate's avatar
Pietro Abate committed
24
let is_fresh (`Var t) = t.fresh
25

Pietro Abate's avatar
Pietro Abate committed
26
27
28
29
30
31
32
33
34
35
36
module Set = struct
  include Set.Make( struct type t = var let compare = compare end) 
  let aux_print sep printer ppf s =
    let rec aux ppf = function
      |[] -> ()
      |[h] -> printer ppf h
      |h::l -> Format.fprintf ppf "%a %s %a" printer h sep aux l
    in
    aux ppf (elements s)

  let dump ppf s = aux_print ";" dump ppf s
37
38
39
  let print ppf s = aux_print ";" print ppf s
  let is_empty s = equal s empty
  let from_list l = List.fold_left (fun acc x -> add x acc) empty l
Pietro Abate's avatar
Pietro Abate committed
40
end
41

42
43
module Make (X : Custom.T) = struct
  type t = X.t pairvar
44
  let hash = function `Atm t -> X.hash t | `Var x -> hash x
45
46
47
  let check = function `Atm t -> X.check t | `Var _ -> ()
  let compare t1 t2 =
    match t1,t2 with
48
    |`Var x, `Var y -> compare (`Var x) (`Var y)
49
50
51
52
53
    |`Atm x, `Atm y -> X.compare x y
    |`Var _, `Atm _ -> -1
    |`Atm _, `Var _ -> 1

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

55
56
  let dump ppf = function
    |`Atm x -> X.dump ppf x
Pietro Abate's avatar
Pietro Abate committed
57
    |`Var x -> dump ppf (`Var x)
58
59
end

60
61
let mk ?fresh id = 
  `Var (make_id ?fresh id)
62

63
let fresh : ?pre: string -> unit -> [> var ] =
64
  let counter = ref 0 in
65
  fun ?(pre="_fresh_") -> fun _ ->
Pietro Abate's avatar
Pietro Abate committed
66
    let id = (Printf.sprintf "%s%d" pre !counter) in
67
    let v = mk ~fresh:true id in
68
69
    incr counter;
    v
70