forked from rocq-archive/coq-serapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserType.ml
197 lines (129 loc) · 5.49 KB
/
serType.ml
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
(************************************************************************)
(* * The Coq Proof Assistant / The Coq Development Team *)
(* v * INRIA, CNRS and contributors - Copyright 1999-2019 *)
(* <O___,, * (see CREDITS file for the list of authors) *)
(* \VV/ **************************************************************)
(* // * This file is distributed under the terms of the *)
(* * GNU Lesser General Public License Version 2.1 *)
(* * (see LICENSE file for the text of the license) *)
(************************************************************************)
(************************************************************************)
(* Coq serialization API/Plugin *)
(* Copyright 2016-2019 MINES ParisTech *)
(* Copyright 2019-2022 Inria *)
(************************************************************************)
module type SJHC = sig
type t [@@deriving sexp,yojson,hash,compare]
end
module type SJHC1 = sig
type 'a t [@@deriving sexp,yojson,hash,compare]
end
(* Bijection with serializable types *)
module type Bijectable = sig
(* Base Type *)
type t
(* Representation type *)
type _t [@@deriving sexp,yojson,hash,compare]
(* Need to be bijetive *)
val to_t : _t -> t
val of_t : t -> _t
end
module Biject(M : Bijectable) : SJHC with type t = M.t = struct
type t = M.t
let sexp_of_t x = M.sexp_of__t (M.of_t x)
let t_of_sexp s = M.to_t (M._t_of_sexp s)
let to_yojson p = M._t_to_yojson (M.of_t p)
let of_yojson p = M._t_of_yojson p |> Result.map M.to_t
let hash x = M.hash__t (M.of_t x)
let hash_fold_t st x = M.hash_fold__t st (M.of_t x)
let compare x1 x2 = M.compare__t (M.of_t x1) (M.of_t x2)
end
(* Bijection with serializable types *)
module type Bijectable1 = sig
(* Base Type *)
type 'a t
(* Representation type *)
type 'a _t [@@deriving sexp,yojson,hash,compare]
(* Need to be bijetive *)
val to_t : 'a _t -> 'a t
val of_t : 'a t -> 'a _t
end
module Biject1(M : Bijectable1) : SJHC1 with type 'a t = 'a M.t = struct
type 'a t = 'a M.t
let sexp_of_t f x = M.sexp_of__t f (M.of_t x)
let t_of_sexp f s = M.to_t (M._t_of_sexp f s)
let to_yojson f p = M._t_to_yojson f (M.of_t p)
let of_yojson f p = M._t_of_yojson f p |> Result.map M.to_t
let hash_fold_t f st x = M.hash_fold__t f st (M.of_t x)
let compare f x1 x2 = M.compare__t f (M.of_t x1) (M.of_t x2)
end
(* We do our own alias as to have better control *)
let _sercast = Obj.magic
(* Obj.magic piercing *)
module type Pierceable = sig
(* Type to pierce *)
type t
(* Representation type *)
type _t [@@deriving sexp,yojson,hash,compare]
end
module type Pierceable1 = sig
(* Type to pierce *)
type 'a t
(* Representation type *)
type 'a _t [@@deriving sexp,yojson,hash,compare]
end
module Pierce(M : Pierceable) : SJHC with type t = M.t = struct
type t = M.t
let sexp_of_t x = M.sexp_of__t (_sercast x)
let t_of_sexp s = _sercast (M._t_of_sexp s)
let to_yojson p = M._t_to_yojson (_sercast p)
let of_yojson p = M._t_of_yojson p |> Result.map _sercast
let hash x = M.hash__t (_sercast x)
let hash_fold_t st x = M.hash_fold__t st (_sercast x)
let compare x1 x2 = M.compare__t (_sercast x1) (_sercast x2)
end
module Pierce1(M : Pierceable1) : SJHC1 with type 'a t = 'a M.t = struct
type 'a t = 'a M.t
let sexp_of_t f x = M.sexp_of__t f (_sercast x)
let t_of_sexp f s = _sercast (M._t_of_sexp f s)
let to_yojson f p = M._t_to_yojson f (_sercast p)
let of_yojson f p = M._t_of_yojson f p |> Result.map _sercast
(* let hash x = M.hash__t (_sercast x) *)
let hash_fold_t f st x = M.hash_fold__t f st (_sercast x)
let compare f x1 x2 = M.compare__t f (_sercast x1) (_sercast x2)
end
(* Unfortunately this doesn't really work for types that are named as
the functions would have to be sexp_of_name etc... Maybe fixme in
the future *)
module PierceAlt(M : Pierceable) : SJHC with type t := M.t = struct
let sexp_of_t x = M.sexp_of__t (_sercast x)
let t_of_sexp s = _sercast (M._t_of_sexp s)
let to_yojson p = M._t_to_yojson (_sercast p)
let of_yojson p = M._t_of_yojson p |> Result.map _sercast
let hash x = M.hash__t (_sercast x)
let hash_fold_t st x = M.hash_fold__t st (_sercast x)
let compare x1 x2 = M.compare__t (_sercast x1) (_sercast x2)
end
module type OpaqueDesc = sig type t val name : string end
module Opaque(M : OpaqueDesc) : SJHC with type t = M.t = struct
type t = M.t
let typ = M.name
let sexp_of_t x = Serlib_base.sexp_of_opaque ~typ x
let t_of_sexp s = Serlib_base.opaque_of_sexp ~typ s
let to_yojson p = Serlib_base.opaque_to_yojson ~typ p
let of_yojson p = Serlib_base.opaque_of_yojson ~typ p
let hash x = Serlib_base.hash_opaque ~typ x
let hash_fold_t st x = Serlib_base.hash_fold_opaque ~typ st x
let compare x1 x2 = Serlib_base.compare_opaque ~typ x1 x2
end
module type OpaqueDesc1 = sig type 'a t val name : string end
module Opaque1(M : OpaqueDesc1) : SJHC1 with type 'a t = 'a M.t = struct
type 'a t = 'a M.t
let typ = M.name
let sexp_of_t _ x = Serlib_base.sexp_of_opaque ~typ x
let t_of_sexp _ s = Serlib_base.opaque_of_sexp ~typ s
let to_yojson _ p = Serlib_base.opaque_to_yojson ~typ p
let of_yojson _ p = Serlib_base.opaque_of_yojson ~typ p
let hash_fold_t _ st x = Serlib_base.hash_fold_opaque ~typ st x
let compare _ x1 x2 = Serlib_base.compare_opaque ~typ x1 x2
end