Version française
Home     About     Download     Resources     Contact us    
Browse thread
Search for the smallest possible possible Ocaml segfault....
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jon Harrop <jon@f...>
Subject: Re: [Caml-list] OCaml's formatting libraries
On Saturday 10 November 2007 15:43, Bünzli Daniel wrote:
> Le 10 nov. 07 à 15:58, Jon Harrop a écrit :
> > Functional unparsing requires a lot more code,
>
> It's a little bit less concise but I wouldn't say it is a *lot*.

  printf "%25s  %d\n%25s  %g\n%25s  %s\n\n%25s ]%*d[\n%25s ]%*d[\n\
    %25s ]%*g[\n%25s ]%*g[\n%25s ]%*s[\n%25s ]%*s[\n"
    "int:" 10 "float:" 1.234 "string:" "foo"
    "int with width:" 20 24 "int with -width:" (-20) 42
    "float with width:" 20 1.234 "float with -width:" (-20) 567.8
    "string with width:" 20 "Hello"
    "string with -width:" (-20) "Goodbye"

vs:

  print_string
    (format
       (wlit "int:"                25 $ lit "  " $ int             $ nl    $
	wlit "float:"              25 $ lit "  " $ flt             $ nl    $
	wlit "string:"             25 $ lit "  " $ str             $ nnl 2 $
	wlit "int with width:"     25 $ lit " ]" $ intw $ lit "["  $ nl    $
	wlit "int with -width:"    25 $ lit " ]" $ intw $ lit "["  $ nl    $
	wlit "float with width:"   25 $ lit " ]" $ fltw $ lit "["  $ nl    $
	wlit "float with -width:"  25 $ lit " ]" $ fltw $ lit "["  $ nl    $
	wlit "string with width:"  25 $ lit " ]" $ strw $ lit "["  $ nl    $
	wlit "string with -width:" 25 $ lit " ]" $ strw $ lit "["  $ nl    )
       ()
       10 1.234 "foo"			(* int, float, string *)
       24 20 42 (-20)		        (* int with width, -width spec *)
       1.234 20 567.8 (-20)	        (* float with width, -width spec *)
       "Hello" 20			(* string with width spec *)
       "Goodbye" (-20)			(* string with -width spec *)
    )

> > produces worse error messages,
>
> Example please.

Oops, I forgot one of the 36 superfluous "$" operators. OCaml now fails to 
catch my trivial (but likely) mistake and now my program produces incorrect 
output and the patient dies on the table whilst gurgling out "should've spent 
less time proving correctness and more time testing":

# let test1 () =
    print_string
      (format
         (wlit "int:"                25 $ lit "  " $ int             $ nl    $
          wlit "float:"              25 $ lit "  " $ flt             $ nl    $
          wlit "string:"             25 $ lit "  " $ str             $ nnl 2 $
          wlit "int with width:"     25 $ lit " ]" $ intw $ lit "["  $ nl
          wlit "int with -width:"    25 $ lit " ]" $ intw $ lit "["  $ nl    $
          wlit "float with width:"   25 $ lit " ]" $ fltw $ lit "["  $ nl    $
          wlit "float with -width:"  25 $ lit " ]" $ fltw $ lit "["  $ nl    $
          wlit "string with width:"  25 $ lit " ]" $ strw $ lit "["  $ nl    $
          wlit "string with -width:" 25 $ lit " ]" $ strw $ lit "["  $ nl    )
       ()
         10 1.234 "foo"                   (* int, float, string *)
         24 20 42 (-20)                   (* int with width, -width spec *)
         1.234 20 567.8 (-20)             (* float with width, -width spec *)
         "Hello" 20                       (* string with width spec *)
         "Goodbye" (-20)                  (* string with -width spec *)
    );;
val test1 : unit -> unit = <fun>
# test1();;
                     int:  10
                   float:  1.234
                  string:  foo

          int with width: ]                  24[        int with -width:
 ]42                  [
        float with width: ]               1.234[
       float with -width: ]567.8               [
       string with width: ]               Hello[
      string with -width: ]Goodbye             [
- : unit = ()

> > is much harder to learn,
>
> I don't think so. There really nothing hard in it, it is just ...
> different.

Exactly: how many people have heard of printf and how many have heard of 
continuation passing style, let alone functional unparsers?

> > is incompatible with the excellent Format module,
>
> Which wouldn't prevent the design of a new format module.

Sure. But we're an extremely finite-sized community and need to prioritize 
where we put our efforts. My vote simply goes to putting effort elsewhere.

> > I'd much rather see effort put into visualization and GUI tools
> > rather than
> > ASCII text tools...
>
> I'd rather have a simple and correct type system.

I'd rather have feature-complete software that works.

In practice, a language implementation will only become robust if it has both 
a solid theoretical foundation and a significant user base to test the 
implementation. As Knuth said "beware: I have proven it correct but not 
tested it".

With the benefit of hindsight, Standard ML put too much emphasis on 
theoretical correctness and not enough on practical utility. Consequently, 
Standard ML does not enjoy OCaml's popularity, has fewer libraries, no 
competitively performant compilers and so on. I would not like to see OCaml 
take that route.

In this context, I would have thought that printf is very commonly used but 
scanf is not. If scanf is difficult to fix then these features could simply 
be removed from it.

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/products/?e