aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib/store.ml
blob: bc1b335fd0484d711d2faed560826584bb99c475 (plain)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
(***********************************************************************)
(*  v      *   The Coq Proof Assistant  /  The Coq Development Team    *)
(* <O___,, *        INRIA-Rocquencourt  &  LRI-CNRS-Orsay              *)
(*   \VV/  *************************************************************)
(*    //   *      This file is distributed under the terms of the      *)
(*         *       GNU Lesser General Public License Version 2.1       *)
(***********************************************************************)

(*** This module implements an "untyped store", in this particular case we
        see it as an extensible record whose fields are left unspecified. ***)

(* We give a short implementation of a universal type. This is mostly equivalent
    to what is proposed by module Dyn.ml, except that it requires no explicit tag. *)
module type Universal = sig
  type t
  
  type 'a etype = {
     put : 'a -> t ;
     get : t -> 'a option
  }

  val embed : unit -> 'a etype
end

(* We use a dynamic "name" allocator. But if we needed to serialise stores, we
might want something static to avoid troubles with plugins order. *)

let next =
  let count = ref 0 in fun () ->
  let n = !count in
  incr count;
  n

type t = Obj.t Util.Intmap.t

module Field = struct
  type 'a field = {
    set : 'a -> t -> t ;
    get : t -> 'a option ;
    remove : t -> t 
  }
  type 'a t = 'a field
end

open Field

let empty = Util.Intmap.empty

let field () =
  let fid = next () in
  let set a s =
    Util.Intmap.add fid (Obj.repr a) s
  in
  let get s =
    try Some (Obj.obj (Util.Intmap.find fid s))
    with _ -> None
  in
  let remove s =
    Util.Intmap.remove fid s
  in
  { set = set ; get = get ; remove = remove }