Sven Panne wrote:

> Simon L Peyton Jones wrote:
> > [...] Let me advertise Olivier Danvy's very cunning idea to implement
> > printf in Haskell/ML.
> >
> >         http://www.brics.dk/RS/98/5/index.html
>
> Very cunning, indeed. But it has as small efficiency problem because
> of the use of (++). This can be easily cured by using ShowS:
>
> --------------------------------------------------------------------
> -- Slightly inefficient "Formatting Strings in Haskell" version of
> -- Olivier Danvy's "Formatting Strings in ML"
>
> -- p_ is like i_, but works for everything Show-able
> p_ :: Show b => (String -> a) -> String -> b -> a
> p_ k s x = k (s ++ show x)
>
> s_ :: (String -> a) -> String -> String -> a
> s_ k s x = k (s ++ x)
>
> l_ :: String -> (String -> a) -> String -> a
> l_ x k s = k (s ++ x)
>
> n_ :: (String -> a) -> String -> a
> n_ k s = k (s ++ "\n")
>
> format :: ((String -> String) -> String -> a) -> a
> format c = c id ""
> --------------------------------------------------------------------
>
> --------------------------------------------------------------------
> -- (Hopefully) improved version
>
> p_ :: Show c => (ShowS -> a) -> ShowS -> c -> a
> p_ k s x = k (s . shows x)
>
> s_ :: (ShowS -> a) -> ShowS -> String -> a
> s_ k s x = k (s . showString x)
>
> l_ :: String -> (ShowS -> a) -> ShowS -> a
> l_ x k s = k (s . showString x)
>
> n_ :: (ShowS -> a) -> ShowS -> a
> n_ k s = k (s . showChar '\n')
>
> format :: ((ShowS -> ShowS) -> ShowS -> a) -> a
> format c = c id id
> --------------------------------------------------------------------
>
> Example:
>    format (l_ "Foo" . p_ . s_ . p_ . n_) (2+2) "y" True ""
>    => "Foo4yTrue\n"
>
> --
> Sven Panne                                        Tel.: +49/89/2178-2235
> LMU, Institut fuer Informatik                     FAX : +49/89/2178-2211
> LFE Programmier- und Modellierungssprachen              Oettingenstr. 67
> mailto:[EMAIL PROTECTED]            D-80538 Muenchen
> http://www.pms.informatik.uni-muenchen.de/mitarbeiter/panne

Note that "s_" and "n_" are special cases of "p_", with the virtual method
"show" in type class "Show" inlined.  It is possible to write a function
"%" that takes this method as an argument.  Here is a comparison between
function "p_" and function "%":
"p_": make uses of Haskell type class, so that the compiler can
automatically infer the type of the argument, and a user does not need to
provide it explicitly.
"%": need the user to provide the method explicitly, but it allows one to
supply different formatting option for the same type.  This makes it
possible, for example, to customize different padding, or delimiters in a
list (e.g., format (l_ "The set is: " . % (List "{" "}" ", " (n_ 5 1)))
[5.3, 4, 16.23] to print
    {  5.3,   4.0,  16.2}
)

My suggestion is to have both of them in a Haskell implementation of the
format function, where the method "show" used by "p_" gives the default
layout.  While function "%" and other constructs can be implemented in ML,
function "p_" does not seem to.

My paper titled "Encoding Types in ML-like Languages" (To appear in
ICFP'98) has a section discussing Olivier's printf in ML, and the paper
talked about type-indexed values and type encoding (both value-dependent
approach, such as the natural solution offered by Haskell's type class, and

value-independent approach) in general.  The preprint is available at the
URL:

http://cs.nyu.edu/zheyang/papers/type-encoding.icfp98.preprint.ps

And a more detailed version appears as a preliminary report in BRICS report
series:

http://www.brics.dk/RS/98/Ref/BRICS-RS-98-Ref/BRICS-RS-98-Ref.html#BRICS-RS-98-9

- Zhe

--
+---------------------------------------------------------------+
| Zhe Yang                      [EMAIL PROTECTED]              |
| (* New York University        (212) 998-3295 *)               |
+---------------------------------------------------------------+
| Summer 1998:                  NECI (NEC Research Institute)   |
|                               (609) 951-2783                  |
+---------------------------------------------------------------+




Reply via email to