caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Brian Hurt <bhurt@spnz.org>
To: Pierre Weis <pierre.weis@inria.fr>
Cc: jeanmarc.eber@lexifi.com, <martin_jambon@emailuser.net>,
	<caml-list@inria.fr>
Subject: Re: [Caml-list] Printing text with holes
Date: Mon, 29 Sep 2003 23:40:21 -0500 (CDT)	[thread overview]
Message-ID: <Pine.LNX.4.44.0309292258310.3771-100000@localhost.localdomain> (raw)
In-Reply-To: <200309292048.WAA16827@pauillac.inria.fr>

On Mon, 29 Sep 2003, Pierre Weis wrote:

> > On Mon, 29 Sep 2003, Jean-Marc EBER wrote:
> > 
> > > With "old" printf approach, you write something like:
> > > let _ =
> > >    printf "Hello %s %s! It is %i o'clock.\n" first_name last_name time
> > 
> > It also allows you to do:
> > 
> > let _ = printf "Hello %s %s! It is %i o'clock.\n" name time
> 
> This is not well-typed and the error is properly spotted by the compiler:

Because the compiler understands printf strings as a special case.  I do 
not know of a way I could write printf in standard Ocaml.  You need 
specialized compiler support.  Which makes it very difficult to extend, at 
best.  You basically end up mucking about in p4.

At heart, printf is doing two seperate jobs.  First, it's formating binary 
data (integers, floating points, etc) into there textual/formatted 
representations.  And second, they are outputing said formatted strings.  
These are two completely different jobs which should be done by different 
blocks of code.  Java started recognizing this when they seperated the 
formatting objects from the i/o objects.  And for all of it's limitations, 
and Java I/O has it's limitations, I think it hit upon a better solution 
than printf.  Of course, Java's I/O model isn't radical or new, as any 
Unix shell programmer will tell you.

> Absolutely. This exactly similar to what the compiler does when
> reading string constants in order to properly look for escapes, or
> when it reads strings of digits to decide if it means an integer or a
> floating point number.

Printf is at a completely different level of complexity than simply 
escaping strings.  Escaping strings (and differentiating integers from 
floats) can be done entirely in the lexer.

I've actually written printf implementations.  Even ignoring "amusing" 
complications like locales and floating point numbers, they are not simple 
beasts.  

> You are absolutely right: good old C functions have not automatically
> good old interfaces (for a counterexample just consider ioctl, malloc
> and free, compared to their void equivalent in Caml, or longjump and
> setjump compared with the nice try raise constructs). Conversely, we
> must admit that good old C functions do not necessarily have a bad
> interface, isn't it ?  (May I mention as an example the basic
> arithmetic operators ?)

There is very little in the standard C libraries I would consider "good" 
outside of C.  Primarily because (IMHO) there is very little in the 
standard C libraries.  Nor is C particularly noted for it's ease of 
manipulation of strings and I/O.  About all I ever hear anyone say about C 
and text manipulation is that it's better than FORTRAN, which qualifies as 
damning with faint praise.  Now, the Unix command line, *there* is an 
environment good for text processing.  Good enough that it's been stolen 
in one way or another at least twice (Java and Perl- Perl doing a more 
thorough job of stealing, while Java stole the basic concepts).

Here's how it would work- you would have three categories of objects.  
The first category would be sources and sinks.  A source is an object with
a read function, a sink is an object with a write function.  These objects 
would represent files and strings and network sockets etc.  The second 
category is filters.  A filter looks to the outside just like a source or 
a sink, but instead of being a final destination it just does something to 
the data and then passes it along.  A classic example of a filter is an 
encryption object- looks just like a sink, but when you write data to it, 
it encrypts the data and then writes it in turn to another sink (which may 
be another filter).  The third category of objects is formating 
objects or functions.  These take binary data and turn them into string 
representations.  A classic example here is string_of_int.

Note that since I'm working within the language, all the parts are easily 
extensible and replaceable.

Brian


-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  parent reply	other threads:[~2003-09-30  4:35 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-09-29 13:16 Martin Jambon
2003-09-29 14:10 ` Christian Lindig
2003-09-29 16:25 ` Benjamin Geer
2003-09-29 16:39 ` Brian Hurt
2003-09-29 17:45   ` Jean-Marc EBER
2003-09-29 18:22     ` Brian Hurt
2003-09-29 20:48       ` Pierre Weis
2003-09-29 21:52         ` Richard Jones
2003-09-29 22:07           ` Fred Yankowski
2003-09-29 22:14             ` Daniel M. Albro
2003-09-29 22:19               ` Richard Jones
2003-09-29 22:23             ` Matthieu Sozeau
2003-09-30  3:56             ` Brian Hurt
2003-09-30  4:40         ` Brian Hurt [this message]
2003-09-30  6:11           ` David Brown
2003-09-29 18:57     ` Martin Jambon
2003-09-29 21:25       ` Pierre Weis
2003-09-30 12:52       ` skaller
2003-09-30 17:48         ` Martin Jambon
2003-09-29 18:16   ` Richard Jones
2003-09-29 17:00 ` Pierre Weis
2003-09-29 17:03 ` Karl Zilles
2003-09-29 21:47   ` Pierre Weis
2003-09-30  7:20     ` Jean-Christophe Filliatre

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.44.0309292258310.3771-100000@localhost.localdomain \
    --to=bhurt@spnz.org \
    --cc=caml-list@inria.fr \
    --cc=jeanmarc.eber@lexifi.com \
    --cc=martin_jambon@emailuser.net \
    --cc=pierre.weis@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).