Skip to content

Commit dceb4be

Browse files
committed
Extract Printable.PrefixName functor to deduplicate expand code
1 parent ea029bc commit dceb4be

File tree

1 file changed

+51
-26
lines changed

1 file changed

+51
-26
lines changed

src/common/domains/printable.ml

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,41 @@ struct
184184
end
185185

186186

187+
module type PrefixNameConf =
188+
sig
189+
val expand: bool
190+
end
191+
192+
module PrefixName (Conf: PrefixNameConf) (Base: S): S with type t = Base.t =
193+
struct
194+
include Base
195+
196+
let pretty () x =
197+
if Conf.expand then
198+
Pretty.dprintf "%s:%a" (Base.name ()) Base.pretty x
199+
else
200+
Base.pretty () x
201+
202+
let show x =
203+
if Conf.expand then
204+
Base.name () ^ ":" ^ Base.show x
205+
else
206+
Base.show x
207+
208+
let printXml f x =
209+
if Conf.expand then
210+
BatPrintf.fprintf f "<value><map>\n<key>\n%s\n</key>\n%a</map>\n</value>\n" (Base.name ()) Base.printXml x
211+
else
212+
Base.printXml f x
213+
214+
let to_yojson x =
215+
if Conf.expand then
216+
`Assoc [(Base.name (), Base.to_yojson x)]
217+
else
218+
Base.to_yojson x
219+
end
220+
221+
187222
module type LiftConf =
188223
sig
189224
val bot_name: string
@@ -257,34 +292,31 @@ end
257292

258293
module EitherConf (Conf: EitherConf) (Base1: S) (Base2: S) =
259294
struct
295+
open struct
296+
module Base1 = PrefixName (struct let expand = Conf.expand1 end) (Base1)
297+
module Base2 = PrefixName (struct let expand = Conf.expand2 end) (Base2)
298+
end
299+
260300
type t = [`Left of Base1.t | `Right of Base2.t] [@@deriving eq, ord, hash]
261301
include Std
262302

263303
let pretty () (state:t) =
264304
match state with
265-
| `Left n when Conf.expand1 -> Pretty.dprintf "%s:%a" (Base1.name ()) Base1.pretty n
266305
| `Left n -> Base1.pretty () n
267-
| `Right n when Conf.expand2 -> Pretty.dprintf "%s:%a" (Base2.name ()) Base2.pretty n
268306
| `Right n -> Base2.pretty () n
269307

270308
let show state =
271309
match state with
272-
| `Left n when Conf.expand1 -> (Base1.name ()) ^ ":" ^ Base1.show n
273310
| `Left n -> Base1.show n
274-
| `Right n when Conf.expand2 -> (Base2.name ()) ^ ":" ^ Base2.show n
275311
| `Right n -> Base2.show n
276312

277313
let name () = "either " ^ Base1.name () ^ " or " ^ Base2.name ()
278314
let printXml f = function
279-
| `Left x when Conf.expand1 -> BatPrintf.fprintf f "<value><map>\n<key>\nLeft\n</key>\n%a</map>\n</value>\n" Base1.printXml x
280315
| `Left x -> Base1.printXml f x
281-
| `Right x when Conf.expand2 -> BatPrintf.fprintf f "<value><map>\n<key>\nRight\n</key>\n%a</map>\n</value>\n" Base2.printXml x
282316
| `Right x -> Base2.printXml f x
283317

284318
let to_yojson = function
285-
| `Left x when Conf.expand1 -> `Assoc [ Base1.name (), Base1.to_yojson x ]
286319
| `Left x -> Base1.to_yojson x
287-
| `Right x when Conf.expand2 -> `Assoc [ Base2.name (), Base2.to_yojson x ]
288320
| `Right x -> Base2.to_yojson x
289321

290322
let relift = function
@@ -302,42 +334,36 @@ end
302334

303335
module Either3Conf (Conf: Either3Conf) (Base1: S) (Base2: S) (Base3: S) =
304336
struct
337+
open struct
338+
module Base1 = PrefixName (struct let expand = Conf.expand1 end) (Base1)
339+
module Base2 = PrefixName (struct let expand = Conf.expand2 end) (Base2)
340+
module Base3 = PrefixName (struct let expand = Conf.expand3 end) (Base3)
341+
end
342+
305343
type t = [`Left of Base1.t | `Middle of Base2.t | `Right of Base3.t] [@@deriving eq, ord, hash]
306344
include Std
307345

308346
let pretty () (state:t) =
309347
match state with
310-
| `Left n when Conf.expand1 -> Pretty.dprintf "%s:%a" (Base1.name ()) Base1.pretty n
311348
| `Left n -> Base1.pretty () n
312-
| `Middle n when Conf.expand2 -> Pretty.dprintf "%s:%a" (Base2.name ()) Base2.pretty n
313349
| `Middle n -> Base2.pretty () n
314-
| `Right n when Conf.expand3 -> Pretty.dprintf "%s:%a" (Base3.name ()) Base3.pretty n
315350
| `Right n -> Base3.pretty () n
316351

317352
let show state =
318353
match state with
319-
| `Left n when Conf.expand1 -> (Base1.name ()) ^ ":" ^ Base1.show n
320354
| `Left n -> Base1.show n
321-
| `Middle n when Conf.expand2 -> (Base2.name ()) ^ ":" ^ Base2.show n
322355
| `Middle n -> Base2.show n
323-
| `Right n when Conf.expand3 -> (Base3.name ()) ^ ":" ^ Base3.show n
324356
| `Right n -> Base3.show n
325357

326358
let name () = "either " ^ Base1.name () ^ " or " ^ Base2.name () ^ " or " ^ Base3.name ()
327359
let printXml f = function
328-
| `Left x when Conf.expand1 -> BatPrintf.fprintf f "<value><map>\n<key>\nLeft\n</key>\n%a</map>\n</value>\n" Base1.printXml x
329360
| `Left x -> Base1.printXml f x
330-
| `Middle x when Conf.expand2 -> BatPrintf.fprintf f "<value><map>\n<key>\nMiddle\n</key>\n%a</map>\n</value>\n" Base2.printXml x
331361
| `Middle x -> Base2.printXml f x
332-
| `Right x when Conf.expand3 -> BatPrintf.fprintf f "<value><map>\n<key>\nRight\n</key>\n%a</map>\n</value>\n" Base3.printXml x
333362
| `Right x -> Base3.printXml f x
334363

335364
let to_yojson = function
336-
| `Left x when Conf.expand1 -> `Assoc [ Base1.name (), Base1.to_yojson x ]
337365
| `Left x -> Base1.to_yojson x
338-
| `Middle x when Conf.expand2 -> `Assoc [ Base2.name (), Base2.to_yojson x ]
339366
| `Middle x -> Base2.to_yojson x
340-
| `Right x when Conf.expand3 -> `Assoc [ Base3.name (), Base3.to_yojson x ]
341367
| `Right x -> Base3.to_yojson x
342368

343369
let relift = function
@@ -383,21 +409,24 @@ end
383409

384410
module Lift2Conf (Conf: Lift2Conf) (Base1: S) (Base2: S) =
385411
struct
412+
open struct
413+
module Base1 = PrefixName (struct let expand = Conf.expand1 end) (Base1)
414+
module Base2 = PrefixName (struct let expand = Conf.expand2 end) (Base2)
415+
end
416+
386417
type t = [`Bot | `Lifted1 of Base1.t | `Lifted2 of Base2.t | `Top] [@@deriving eq, ord, hash]
387418
include Std
388419
open Conf
389420

390421
let pretty () (state:t) =
391422
match state with
392-
(* TODO: expand *)
393423
| `Lifted1 n -> Base1.pretty () n
394424
| `Lifted2 n -> Base2.pretty () n
395425
| `Bot -> text bot_name
396426
| `Top -> text top_name
397427

398428
let show state =
399429
match state with
400-
(* TODO: expand *)
401430
| `Lifted1 n -> Base1.show n
402431
| `Lifted2 n -> Base2.show n
403432
| `Bot -> bot_name
@@ -412,17 +441,13 @@ struct
412441
let printXml f = function
413442
| `Bot -> BatPrintf.fprintf f "<value>\n<data>\n%s\n</data>\n</value>\n" bot_name
414443
| `Top -> BatPrintf.fprintf f "<value>\n<data>\n%s\n</data>\n</value>\n" top_name
415-
| `Lifted1 x when Conf.expand1 -> BatPrintf.fprintf f "<value>\n<map>\n<key>\nLifted1\n</key>\n%a</map>\n</value>\n" Base1.printXml x
416444
| `Lifted1 x -> Base1.printXml f x
417-
| `Lifted2 x when Conf.expand2 -> BatPrintf.fprintf f "<value>\n<map>\n<key>\nLifted2\n</key>\n%a</map>\n</value>\n" Base2.printXml x
418445
| `Lifted2 x -> Base2.printXml f x
419446

420447
let to_yojson = function
421448
| `Bot -> `String bot_name
422449
| `Top -> `String top_name
423-
| `Lifted1 x when Conf.expand1 -> `Assoc [ Base1.name (), Base1.to_yojson x ]
424450
| `Lifted1 x -> Base1.to_yojson x
425-
| `Lifted2 x when Conf.expand2 -> `Assoc [ Base2.name (), Base2.to_yojson x ]
426451
| `Lifted2 x -> Base2.to_yojson x
427452
end
428453

0 commit comments

Comments
 (0)