Debian Codegraph @

clib/polyMap.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 Tag = sig type _ tag = .. end
17
18 module Make (Tag:Tag) = struct
19 open Tag
20
21 module type OneTag = sig
22 type a
23 type _ tag += T : a tag
24 end
25 type 'a onetag = (module OneTag with type a = 'a)
26
27 let refl = Some CSig.Refl
28
29 let eq_onetag (type a b) (tag:a onetag) (tag':b tag) : (a,b) CSig.eq option =
30 let module T = (val tag) in
31 match tag' with
32 | T.T -> refl
33 | _ -> None
34
35 let make (type a) () : a onetag =
36 (module struct type nonrec a = a type _ tag += T : a tag end)
37
38 let tag_of_onetag (type a) (tag:a onetag) : a tag =
39 let module T = (val tag) in
40 T.T
41
42 module type MapS = sig
43 type t
44 type _ value
45
46 val empty : t
47
48 val find : 'a tag -> t -> 'a value
49
50 val add : 'a onetag -> 'a value -> t -> t
51
52 val mem : 'a tag -> t -> bool
53
54 val update : 'a onetag -> ('a value option -> 'a value option) -> t -> t
55
56 val modify : 'a tag -> ('a value -> 'a value) -> t -> t
57
58 type 'acc fold = { fold : 'a. 'a onetag -> 'a value -> 'acc -> 'acc }
59
60 val fold : 'acc fold -> t -> 'acc -> 'acc
61
62 end
63
64 module Map (V:ValueS) = struct
65
66 type v = V : 'a onetag * 'a V.t -> v
67
68 let key t = Obj.Extension_constructor.(id (of_val t))
69 let onekey t = key (tag_of_onetag t)
70
71 module M = Int.Map
72 type t = v M.t
73
74 let empty = M.empty
75
76 let find (type a) (tag:a tag) m : a V.t =
77 let V (tag', v) = M.find (key tag) m in
78 let module T = (val tag') in
79 match tag with
80 | T.T -> v
81 | _ -> assert false
82
83 let add tag v m = M.add (onekey tag) (V (tag, v)) m
84
85 let mem tag m = M.mem (key tag) m
86
87 let update (type a) (tag:a onetag) (f:a V.t option -> a V.t option) m =
88 M.update (key tag) (fun optv ->
89 let optv : a V.t option = match optv with
90 | None -> None
91 | Some (V (tag', v)) ->
92 let module T = (val tag) in
93 match tag_of_onetag tag' with
94 | T.T -> Some v
95 | _ -> assert false
96 in
97 match f optv with
98 | None -> None
99 | Some v -> Some (V (tag,v))) m
100
101 let modify (type a) (tag:a tag) (f:a V.t -> a V.t) m =
102 M.modify (key tag) (fun _ (V (tag', v)) ->
103 let module T = (val tag') in
104 match tag with
105 | T.T -> V (tag', f v)
106 | _ -> assert false) m
107
108 type 'acc fold = { fold : 'a. 'a onetag -> 'a V.t -> 'acc -> 'acc }
109
110 let fold f m acc = M.fold (fun _ (V (tag,v)) acc -> f.fold tag v acc) m acc
111
112 end
113 end