Debian Codegraph @

clib/dyn.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 module type ValueS =
12 sig
13 type 'a t
14 end
15
16 module type MapS =
17 sig
18 type t
19 type 'a key
20 type 'a value
21 val empty : t
22 val add : 'a key -> 'a value -> t -> t
23 val remove : 'a key -> t -> t
24 val find : 'a key -> t -> 'a value
25 val mem : 'a key -> t -> bool
26 val modify : 'a key -> ('a value -> 'a value) -> t -> t
27
28 type map = { map : 'a. 'a key -> 'a value -> 'a value }
29 val map : map -> t -> t
30
31 type any = Any : 'a key * 'a value -> any
32 val iter : (any -> unit) -> t -> unit
33 val fold : (any -> 'r -> 'r) -> t -> 'r -> 'r
34
35 type filter = { filter : 'a. 'a key -> 'a value -> bool }
36 val filter : filter -> t -> t
37 end
38
39 module type PreS =
40 sig
41 type 'a tag
42 type t = Dyn : 'a tag * 'a -> t
43
44 val create : string -> 'a tag
45 val eq : 'a tag -> 'b tag -> ('a, 'b) CSig.eq option
46 val repr : 'a tag -> string
47
48 val dump : unit -> (int * string) list
49
50 type any = Any : 'a tag -> any
51 val name : string -> any option
52
53 module Map(Value : ValueS) :
54 MapS with type 'a key = 'a tag and type 'a value = 'a Value.t
55
56 module HMap (V1 : ValueS)(V2 : ValueS) :
57 sig
58 type map = { map : 'a. 'a tag -> 'a V1.t -> 'a V2.t }
59 val map : map -> Map(V1).t -> Map(V2).t
60
61 type filter = { filter : 'a. 'a tag -> 'a V1.t -> bool }
62 val filter : filter -> Map(V1).t -> Map(V1).t
63 end
64
65 end
66
67 module type S =
68 sig
69 include PreS
70
71 module Easy : sig
72 val make_dyn_tag : string -> ('a -> t) * (t -> 'a) * 'a tag
73 val make_dyn : string -> ('a -> t) * (t -> 'a)
74 val inj : 'a -> 'a tag -> t
75 val prj : t -> 'a tag -> 'a option
76 end
77 end
78
79 module Make () = struct
80
81 module Self : PreS = struct
82 (* Dynamics, programmed with DANGER !!! *)
83
84 type 'a tag = int
85
86 type t = Dyn : 'a tag * 'a -> t
87
88 type any = Any : 'a tag -> any
89
90 let dyntab = ref (Int.Map.empty : string Int.Map.t)
91 (** Instead of working with tags as strings, which are costly, we use their
92 hash. We ensure unicity of the hash in the [create] function. If ever a
93 collision occurs, which is unlikely, it is sufficient to tweak the offending
94 dynamic tag. *)
95
96 let create (s : string) =
97 let hash = Hashtbl.hash s in
98 if Int.Map.mem hash !dyntab then begin
99 let old = Int.Map.find hash !dyntab in
100 Printf.eprintf "Dynamic tag collision: %s vs. %s\n%!" s old;
101 assert false
102 end;
103 dyntab := Int.Map.add hash s !dyntab;
104 hash
105
106 let eq : 'a 'b. 'a tag -> 'b tag -> ('a, 'b) CSig.eq option =
107 fun h1 h2 -> if Int.equal h1 h2 then Some (Obj.magic CSig.Refl) else None
108
109 let repr s =
110 try Int.Map.find s !dyntab
111 with Not_found ->
112 let () = Printf.eprintf "Unknown dynamic tag %i\n%!" s in
113 assert false
114
115 let name s =
116 let hash = Hashtbl.hash s in
117 if Int.Map.mem hash !dyntab then Some (Any hash) else None
118
119 let dump () = Int.Map.bindings !dyntab
120
121 module Map(Value: ValueS) =
122 struct
123 type t = Obj.t Value.t Int.Map.t
124 type 'a key = 'a tag
125 type 'a value = 'a Value.t
126 let cast : 'a value -> 'b value = Obj.magic
127 let empty = Int.Map.empty
128 let add tag v m = Int.Map.add tag (cast v) m
129 let remove tag m = Int.Map.remove tag m
130 let find tag m = cast (Int.Map.find tag m)
131 let mem = Int.Map.mem
132 let modify tag f m = Int.Map.modify tag (fun _ v -> cast (f (cast v))) m
133
134 type map = { map : 'a. 'a tag -> 'a value -> 'a value }
135 let map f m = Int.Map.mapi f.map m
136
137 type any = Any : 'a tag * 'a value -> any
138 let iter f m = Int.Map.iter (fun k v -> f (Any (k, v))) m
139 let fold f m accu = Int.Map.fold (fun k v accu -> f (Any (k, v)) accu) m accu
140
141 type filter = { filter : 'a. 'a tag -> 'a value -> bool }
142 let filter f m = Int.Map.filter f.filter m
143 end
144
145 module HMap (V1 : ValueS) (V2 : ValueS) =
146 struct
147 type map = { map : 'a. 'a tag -> 'a V1.t -> 'a V2.t }
148
149 let map (f : map) (m : Map(V1).t) : Map(V2).t =
150 Int.Map.mapi f.map m
151
152 type filter = { filter : 'a. 'a tag -> 'a V1.t -> bool }
153
154 let filter (f : filter) (m : Map(V1).t) : Map(V1).t =
155 Int.Map.filter f.filter m
156
157 end
158
159 end
160 include Self
161
162 module Easy = struct
163 (* now tags are opaque, we can do the trick *)
164 let make_dyn_tag (s : string) =
165 (fun (type a) (tag : a tag) ->
166 let infun : (a -> t) = fun x -> Dyn (tag, x) in
167 let outfun : (t -> a) = fun (Dyn (t, x)) ->
168 match eq tag t with
169 | None -> assert false
170 | Some CSig.Refl -> x
171 in
172 infun, outfun, tag)
173 (create s)
174
175 let make_dyn (s : string) =
176 let inf, outf, _ = make_dyn_tag s in inf, outf
177
178 let inj x tag = Dyn(tag,x)
179 let prj : type a. t -> a tag -> a option =
180 fun (Dyn(tag',x)) tag ->
181 match eq tag tag' with
182 | None -> None
183 | Some CSig.Refl -> Some x
184 end
185
186 end
187