1 (* Copyright Jeremy Yallop 2007.
2 This file is free software, distributed under the MIT license.
3 See the file COPYING for details.
11 val format
: Format.formatter
-> a
-> unit
12 val format_list
: Format.formatter
-> a list
-> unit
13 val show
: a
-> string
14 val show_list
: a list
-> string
17 module type SimpleFormatter
=
20 val format
: Format.formatter
-> a
-> unit
23 module ShowFormatterDefault
(S
: SimpleFormatter
) =
26 let format_list formatter items
=
27 let rec writeItems formatter
= function
29 | [x
] -> S.format formatter x
;
30 | x
:: xs
-> Format.fprintf formatter
"%a;@;%a" S.format x
writeItems xs
32 Format.fprintf formatter
"@[<hov 1>[%a]@]" writeItems items
38 val format
: Format.formatter
-> a
-> unit
39 val format_list : Format.formatter
-> a list
-> unit
40 end)) : Show
with type a
= S.a
=
43 let showFormatted f item
=
44 let b = Buffer.create
16 in
45 let formatter = Format.formatter_of_buffer
b in
46 Format.fprintf
formatter "@[<hov 0>%a@]@?" f item
;
47 Buffer.sub
b 0 (Buffer.length
b)
49 (* Warning: do not eta-reduce either of the following *)
50 let show item
= showFormatted S.format item
51 let show_list items
= showFormatted S.format_list items
54 module Defaults
(S
: SimpleFormatter
) : Show
with type a
= S.a
=
55 ShowDefaults'
(ShowFormatterDefault
(S
))
57 module Show_unprintable
(S
: sig type a
end) (*: Show with type a = S.a *) =
60 let format formatter _
= Format.pp_print_string
formatter "..."
63 (* instance Show a => Show [a] *)
64 module Show_list
(S
: Show
) : Show
with type a
= S.a list
=
67 let format = S.format_list
70 (* instance Show a => Show (a option) *)
71 module Show_option
(S
: Show
) : Show
with type a
= S.a
option =
74 let format formatter = function
75 | None
-> Format.fprintf
formatter "@[None@]"
76 | Some s
-> Format.fprintf
formatter "@[Some@;<1 2>(%a)@]" S.format s
79 (* instance Show a => Show (a array) *)
80 module Show_array
(S
: Show
) : Show
with type a
= S.a array
=
83 let format formatter obj
=
84 let writeItems formatter items
=
85 let length = Array.length items
in
86 for i
= 0 to length - 2 do
87 Format.fprintf
formatter "@[%a;@;@]" S.format (Array.get items i
)
90 S.format formatter (Array.get items
(length -1));
92 Format.fprintf
formatter "@[[|%a|]@]" writeItems obj
97 (K
: Show
with type a
= O.t
)
99 : Show
with type a
= V.a
Map.Make
(O
).t
=
102 module M
= Map.Make
(O
)
104 let format formatter map
=
105 Format.pp_open_box
formatter 0;
106 Format.pp_print_string
formatter "{";
107 M.iter
(fun key
value ->
108 Format.pp_open_box
formatter 0;
109 K.format formatter key
;
110 Format.pp_print_string
formatter " => ";
111 V.format formatter value;
112 Format.pp_close_box
formatter ();
114 Format.pp_print_string
formatter "}";
115 Format.pp_close_box
formatter ();
120 (O
: Set.OrderedType
)
121 (K
: Show
with type a
= O.t
)
122 : Show
with type a
= Set.Make
(O
).t
=
125 module S
= Set.Make
(O
)
127 let format formatter set
=
128 Format.pp_open_box
formatter 0;
129 Format.pp_print_string
formatter "{";
131 Format.pp_open_box
formatter 0;
132 K.format formatter elt
;
133 Format.pp_close_box
formatter ();
135 Format.pp_print_string
formatter "}";
136 Format.pp_close_box
formatter ();
139 module Show_bool
= Defaults
(struct
141 let format formatter item
=
143 | true -> Format.pp_print_string
formatter "true"
144 | false -> Format.pp_print_string
formatter "false"
147 module Show_integer
(S
: sig type t
val to_string
: t
-> string end) = Defaults
(struct
149 let format formatter item
= Format.pp_print_string
formatter (S.to_string item
)
152 module Show_int32
= Show_integer
(Int32
)
153 module Show_int64
= Show_integer
(Int64
)
154 module Show_nativeint
= Show_integer
(Nativeint
)
156 module Show_char
= Defaults
(struct
158 let format formatter item
= Format.pp_print_string
formatter ("'" ^
Char.escaped item ^
"'")
161 module Show_int
= Defaults
(struct
163 let format formatter item
= Format.pp_print_string
formatter (string_of_int item
)
166 module Show_num
= Defaults
(struct
168 let format formatter item
= Format.pp_print_string
formatter (Num.string_of_num item
)
171 module Show_float
= Defaults
(struct
173 let format formatter item
= Format.pp_print_string
formatter (string_of_float item
)
176 module Show_string
= Defaults
(struct
178 let format formatter item
=
179 Format.pp_print_char
formatter '
"';
180 Format.pp_print_string formatter (String.escaped item);
181 Format.pp_print_char formatter '"'
184 module Show_unit
= Defaults
(struct
186 let format formatter () = Format.pp_print_string
formatter "()"
190 include Deriving_Show
192 type open_flag
= Pervasives.open_flag
=
204 type fpclass
= Pervasives.fpclass
=
212 type 'a
ref = 'a
Pervasives.ref = { mutable contents
: 'a
; }