| 1 |
= PrintfGentle |
| 2 |
:toc: |
| 3 |
|
| 4 |
This page provides a gentle introduction and derivation of <<Printf#>>, |
| 5 |
with sections and arrangement more suitable to a talk. |
| 6 |
|
| 7 |
|
| 8 |
== Introduction |
| 9 |
|
| 10 |
SML does not have `printf`. Could we define it ourselves? |
| 11 |
|
| 12 |
[source,sml] |
| 13 |
---- |
| 14 |
val () = printf ("here's an int %d and a real %f.\n", 13, 17.0) |
| 15 |
val () = printf ("here's three values (%d, %f, %f).\n", 13, 17.0, 19.0) |
| 16 |
---- |
| 17 |
|
| 18 |
What could the type of `printf` be? |
| 19 |
|
| 20 |
This obviously can't work, because SML functions take a fixed number |
| 21 |
of arguments. Actually they take one argument, but if that's a tuple, |
| 22 |
it can only have a fixed number of components. |
| 23 |
|
| 24 |
|
| 25 |
== From tupling to currying |
| 26 |
|
| 27 |
What about currying to get around the typing problem? |
| 28 |
|
| 29 |
[source,sml] |
| 30 |
---- |
| 31 |
val () = printf "here's an int %d and a real %f.\n" 13 17.0 |
| 32 |
val () = printf "here's three values (%d, %f, %f).\n" 13 17.0 19.0 |
| 33 |
---- |
| 34 |
|
| 35 |
That fails for a similar reason. We need two types for `printf`. |
| 36 |
|
| 37 |
---- |
| 38 |
val printf: string -> int -> real -> unit |
| 39 |
val printf: string -> int -> real -> real -> unit |
| 40 |
---- |
| 41 |
|
| 42 |
This can't work, because `printf` can only have one type. SML doesn't |
| 43 |
support programmer-defined overloading. |
| 44 |
|
| 45 |
|
| 46 |
== Overloading and dependent types |
| 47 |
|
| 48 |
Even without worrying about number of arguments, there is another |
| 49 |
problem. The type of `printf` depends on the format string. |
| 50 |
|
| 51 |
[source,sml] |
| 52 |
---- |
| 53 |
val () = printf "here's an int %d and a real %f.\n" 13 17.0 |
| 54 |
val () = printf "here's a real %f and an int %d.\n" 17.0 13 |
| 55 |
---- |
| 56 |
|
| 57 |
Now we need |
| 58 |
|
| 59 |
---- |
| 60 |
val printf: string -> int -> real -> unit |
| 61 |
val printf: string -> real -> int -> unit |
| 62 |
---- |
| 63 |
|
| 64 |
Again, this can't possibly working because SML doesn't have |
| 65 |
overloading, and types can't depend on values. |
| 66 |
|
| 67 |
|
| 68 |
== Idea: express type information in the format string |
| 69 |
|
| 70 |
If we express type information in the format string, then different |
| 71 |
uses of `printf` can have different types. |
| 72 |
|
| 73 |
[source,sml] |
| 74 |
---- |
| 75 |
type 'a t (* the type of format strings *) |
| 76 |
val printf: 'a t -> 'a |
| 77 |
infix D F |
| 78 |
val fs1: (int -> real -> unit) t = "here's an int "D" and a real "F".\n" |
| 79 |
val fs2: (int -> real -> real -> unit) t = |
| 80 |
"here's three values ("D", "F", "F").\n" |
| 81 |
val () = printf fs1 13 17.0 |
| 82 |
val () = printf fs2 13 17.0 19.0 |
| 83 |
---- |
| 84 |
|
| 85 |
Now, our two calls to `printf` type check, because the format |
| 86 |
string specializes `printf` to the appropriate type. |
| 87 |
|
| 88 |
|
| 89 |
== The types of format characters |
| 90 |
|
| 91 |
What should the type of format characters `D` and `F` be? Each format |
| 92 |
character requires an additional argument of the appropriate type to |
| 93 |
be supplied to `printf`. |
| 94 |
|
| 95 |
Idea: guess the final type that will be needed for `printf` the format |
| 96 |
string and verify it with each format character. |
| 97 |
|
| 98 |
[source,sml] |
| 99 |
---- |
| 100 |
type ('a, 'b) t (* 'a = rest of type to verify, 'b = final type *) |
| 101 |
val ` : string -> ('a, 'a) t (* guess the type, which must be verified *) |
| 102 |
val D: (int -> 'a, 'b) t * string -> ('a, 'b) t (* consume an int *) |
| 103 |
val F: (real -> 'a, 'b) t * string -> ('a, 'b) t (* consume a real *) |
| 104 |
val printf: (unit, 'a) t -> 'a |
| 105 |
---- |
| 106 |
|
| 107 |
Don't worry. In the end, type inference will guess and verify for us. |
| 108 |
|
| 109 |
|
| 110 |
== Understanding guess and verify |
| 111 |
|
| 112 |
Now, let's build up a format string and a specialized `printf`. |
| 113 |
|
| 114 |
[source,sml] |
| 115 |
---- |
| 116 |
infix D F |
| 117 |
val f0 = `"here's an int " |
| 118 |
val f1 = f0 D " and a real " |
| 119 |
val f2 = f1 F ".\n" |
| 120 |
val p = printf f2 |
| 121 |
---- |
| 122 |
|
| 123 |
These definitions yield the following types. |
| 124 |
|
| 125 |
[source,sml] |
| 126 |
---- |
| 127 |
val f0: (int -> real -> unit, int -> real -> unit) t |
| 128 |
val f1: (real -> unit, int -> real -> unit) t |
| 129 |
val f2: (unit, int -> real -> unit) t |
| 130 |
val p: int -> real -> unit |
| 131 |
---- |
| 132 |
|
| 133 |
So, `p` is a specialized `printf` function. We could use it as |
| 134 |
follows |
| 135 |
|
| 136 |
[source,sml] |
| 137 |
---- |
| 138 |
val () = p 13 17.0 |
| 139 |
val () = p 14 19.0 |
| 140 |
---- |
| 141 |
|
| 142 |
|
| 143 |
== Type checking this using a functor |
| 144 |
|
| 145 |
[source,sml] |
| 146 |
---- |
| 147 |
signature PRINTF = |
| 148 |
sig |
| 149 |
type ('a, 'b) t |
| 150 |
val ` : string -> ('a, 'a) t |
| 151 |
val D: (int -> 'a, 'b) t * string -> ('a, 'b) t |
| 152 |
val F: (real -> 'a, 'b) t * string -> ('a, 'b) t |
| 153 |
val printf: (unit, 'a) t -> 'a |
| 154 |
end |
| 155 |
|
| 156 |
functor Test (P: PRINTF) = |
| 157 |
struct |
| 158 |
open P |
| 159 |
infix D F |
| 160 |
|
| 161 |
val () = printf (`"here's an int "D" and a real "F".\n") 13 17.0 |
| 162 |
val () = printf (`"here's three values ("D", "F ", "F").\n") 13 17.0 19.0 |
| 163 |
end |
| 164 |
---- |
| 165 |
|
| 166 |
|
| 167 |
== Implementing `Printf` |
| 168 |
|
| 169 |
Think of a format character as a formatter transformer. It takes the |
| 170 |
formatter for the part of the format string before it and transforms |
| 171 |
it into a new formatter that first does the left hand bit, then does |
| 172 |
its bit, then continues on with the rest of the format string. |
| 173 |
|
| 174 |
[source,sml] |
| 175 |
---- |
| 176 |
structure Printf: PRINTF = |
| 177 |
struct |
| 178 |
datatype ('a, 'b) t = T of (unit -> 'a) -> 'b |
| 179 |
|
| 180 |
fun printf (T f) = f (fn () => ()) |
| 181 |
|
| 182 |
fun ` s = T (fn a => (print s; a ())) |
| 183 |
|
| 184 |
fun D (T f, s) = |
| 185 |
T (fn g => f (fn () => fn i => |
| 186 |
(print (Int.toString i); print s; g ()))) |
| 187 |
|
| 188 |
fun F (T f, s) = |
| 189 |
T (fn g => f (fn () => fn i => |
| 190 |
(print (Real.toString i); print s; g ()))) |
| 191 |
end |
| 192 |
---- |
| 193 |
|
| 194 |
|
| 195 |
== Testing printf |
| 196 |
|
| 197 |
[source,sml] |
| 198 |
---- |
| 199 |
structure Z = Test (Printf) |
| 200 |
---- |
| 201 |
|
| 202 |
|
| 203 |
== User-definable formats |
| 204 |
|
| 205 |
The definition of the format characters is pretty much the same. |
| 206 |
Within the `Printf` structure we can define a format character |
| 207 |
generator. |
| 208 |
|
| 209 |
[source,sml] |
| 210 |
---- |
| 211 |
val newFormat: ('a -> string) -> ('a -> 'b, 'c) t * string -> ('b, 'c) t = |
| 212 |
fn toString => fn (T f, s) => |
| 213 |
T (fn th => f (fn () => fn a => (print (toString a); print s ; th ()))) |
| 214 |
val D = fn z => newFormat Int.toString z |
| 215 |
val F = fn z => newFormat Real.toString z |
| 216 |
---- |
| 217 |
|
| 218 |
|
| 219 |
== A core `Printf` |
| 220 |
|
| 221 |
We can now have a very small `PRINTF` signature, and define all |
| 222 |
the format strings externally to the core module. |
| 223 |
|
| 224 |
[source,sml] |
| 225 |
---- |
| 226 |
signature PRINTF = |
| 227 |
sig |
| 228 |
type ('a, 'b) t |
| 229 |
val ` : string -> ('a, 'a) t |
| 230 |
val newFormat: ('a -> string) -> ('a -> 'b, 'c) t * string -> ('b, 'c) t |
| 231 |
val printf: (unit, 'a) t -> 'a |
| 232 |
end |
| 233 |
|
| 234 |
structure Printf: PRINTF = |
| 235 |
struct |
| 236 |
datatype ('a, 'b) t = T of (unit -> 'a) -> 'b |
| 237 |
|
| 238 |
fun printf (T f) = f (fn () => ()) |
| 239 |
|
| 240 |
fun ` s = T (fn a => (print s; a ())) |
| 241 |
|
| 242 |
fun newFormat toString (T f, s) = |
| 243 |
T (fn th => |
| 244 |
f (fn () => fn a => |
| 245 |
(print (toString a) |
| 246 |
; print s |
| 247 |
; th ()))) |
| 248 |
end |
| 249 |
---- |
| 250 |
|
| 251 |
|
| 252 |
== Extending to fprintf |
| 253 |
|
| 254 |
One can implement fprintf by threading the outstream through all the |
| 255 |
transformers. |
| 256 |
|
| 257 |
[source,sml] |
| 258 |
---- |
| 259 |
signature PRINTF = |
| 260 |
sig |
| 261 |
type ('a, 'b) t |
| 262 |
val ` : string -> ('a, 'a) t |
| 263 |
val fprintf: (unit, 'a) t * TextIO.outstream -> 'a |
| 264 |
val newFormat: ('a -> string) -> ('a -> 'b, 'c) t * string -> ('b, 'c) t |
| 265 |
val printf: (unit, 'a) t -> 'a |
| 266 |
end |
| 267 |
|
| 268 |
structure Printf: PRINTF = |
| 269 |
struct |
| 270 |
type out = TextIO.outstream |
| 271 |
val output = TextIO.output |
| 272 |
|
| 273 |
datatype ('a, 'b) t = T of (out -> 'a) -> out -> 'b |
| 274 |
|
| 275 |
fun fprintf (T f, out) = f (fn _ => ()) out |
| 276 |
|
| 277 |
fun printf t = fprintf (t, TextIO.stdOut) |
| 278 |
|
| 279 |
fun ` s = T (fn a => fn out => (output (out, s); a out)) |
| 280 |
|
| 281 |
fun newFormat toString (T f, s) = |
| 282 |
T (fn g => |
| 283 |
f (fn out => fn a => |
| 284 |
(output (out, toString a) |
| 285 |
; output (out, s) |
| 286 |
; g out))) |
| 287 |
end |
| 288 |
---- |
| 289 |
|
| 290 |
|
| 291 |
== Notes |
| 292 |
|
| 293 |
* Lesson: instead of using dependent types for a function, express the |
| 294 |
the dependency in the type of the argument. |
| 295 |
|
| 296 |
* If `printf` is partially applied, it will do the printing then and |
| 297 |
there. Perhaps this could be fixed with some kind of terminator. |
| 298 |
+ |
| 299 |
A syntactic or argument terminator is not necessary. A formatter can |
| 300 |
either be eager (as above) or lazy (as below). A lazy formatter |
| 301 |
accumulates enough state to print the entire string. The simplest |
| 302 |
lazy formatter concatenates the strings as they become available: |
| 303 |
+ |
| 304 |
[source,sml] |
| 305 |
---- |
| 306 |
structure PrintfLazyConcat: PRINTF = |
| 307 |
struct |
| 308 |
datatype ('a, 'b) t = T of (string -> 'a) -> string -> 'b |
| 309 |
|
| 310 |
fun printf (T f) = f print "" |
| 311 |
|
| 312 |
fun ` s = T (fn th => fn s' => th (s' ^ s)) |
| 313 |
|
| 314 |
fun newFormat toString (T f, s) = |
| 315 |
T (fn th => |
| 316 |
f (fn s' => fn a => |
| 317 |
th (s' ^ toString a ^ s))) |
| 318 |
end |
| 319 |
---- |
| 320 |
+ |
| 321 |
It is somewhat more efficient to accumulate the strings as a list: |
| 322 |
+ |
| 323 |
[source,sml] |
| 324 |
---- |
| 325 |
structure PrintfLazyList: PRINTF = |
| 326 |
struct |
| 327 |
datatype ('a, 'b) t = T of (string list -> 'a) -> string list -> 'b |
| 328 |
|
| 329 |
fun printf (T f) = f (List.app print o List.rev) [] |
| 330 |
|
| 331 |
fun ` s = T (fn th => fn ss => th (s::ss)) |
| 332 |
|
| 333 |
fun newFormat toString (T f, s) = |
| 334 |
T (fn th => |
| 335 |
f (fn ss => fn a => |
| 336 |
th (s::toString a::ss))) |
| 337 |
end |
| 338 |
---- |
| 339 |
|
| 340 |
|
| 341 |
== Also see |
| 342 |
|
| 343 |
* <<Printf#>> |
| 344 |
* <<References#Danvy98,Functional Unparsing>> |