var.ml 2.44 KB
Newer Older
1

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
type t = { 
  mutable variance : [ `Covariant | `ContraVariant | `Both | `None ] ;
  fresh : bool;
  id : String.t;
}
let make_id ?(fresh=false) ?(variance=`None) id = 
   { id = id ; variance = variance; fresh = fresh }

let dump ppf t =
  let to_string = function
    |`ContraVariant -> "contravariant"
    |`Covariant -> "covariant"
    |`Both -> "invariant"
    |`None -> "indetermined"
  in
  Format.fprintf ppf "{id=%s;variance=%s;fresh=%b}" t.id (to_string t.variance) t.fresh

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

23
24
25
type var = [ `Var of t ]
type 'a pairvar = [ `Atm of 'a | var ]

26
27
28
29
30
31
32
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

let ch_variance variance (`Var t) =
  match t.variance,variance with
Pietro Abate's avatar
Pietro Abate committed
33
34
  |`None,_ -> t.variance <- variance
  |`Both ,_ -> ()
35
  |`ContraVariant,`ContraVariant 
Pietro Abate's avatar
Pietro Abate committed
36
37
38
39
  |`Covariant,`Covariant -> ()
  |_,_ -> t.variance <- `Both

let set_variance variance (`Var t) = t.variance <- variance
40
41

let variance (`Var t) = t.variance
Pietro Abate's avatar
Pietro Abate committed
42
let id (`Var t) = t.id
Pietro Abate's avatar
Pietro Abate committed
43
let is_fresh (`Var t) = t.fresh
44

Pietro Abate's avatar
Pietro Abate committed
45
46
47
48
49
50
51
52
53
54
55
56
57
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
  let print ppf s = aux_print ";" print ppf 
end
58

59
60
module Make (X : Custom.T) = struct
  type t = X.t pairvar
61
  let hash = function `Atm t -> X.hash t | `Var x -> hash x
62
63
64
  let check = function `Atm t -> X.check t | `Var _ -> ()
  let compare t1 t2 =
    match t1,t2 with
65
    |`Var x, `Var y -> compare (`Var x) (`Var y)
66
67
68
69
70
    |`Atm x, `Atm y -> X.compare x y
    |`Var _, `Atm _ -> -1
    |`Atm _, `Var _ -> 1

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

72
73
  let dump ppf = function
    |`Atm x -> X.dump ppf x
Pietro Abate's avatar
Pietro Abate committed
74
    |`Var x -> dump ppf (`Var x)
75
76
end

77
78
79
let mk ?fresh ?variance id = 
  `Var (make_id ?fresh ?variance id)

Pietro Abate's avatar
Pietro Abate committed
80
let fresh : ?pre: string -> ?variance:[ `None| `Both | `ContraVariant | `Covariant ] -> unit -> [> var ] =
81
  let counter = ref 0 in
Pietro Abate's avatar
Pietro Abate committed
82
83
  fun ?(pre="_fresh_") -> fun ?variance -> fun _ ->
    let id = (Printf.sprintf "%s%d" pre !counter) in
84
    let v = mk ~fresh:true ?variance id in
85
86
    incr counter;
    v
87