* [Caml-list] Printing text with holes @ 2003-09-29 13:16 Martin Jambon 2003-09-29 14:10 ` Christian Lindig ` (4 more replies) 0 siblings, 5 replies; 24+ messages in thread From: Martin Jambon @ 2003-09-29 13:16 UTC (permalink / raw) To: caml-list Hello, I am curious to know what people use to print long text written in a natural language, and containing many holes, like dynamically generated web pages. Printf is not satisfying for this kind of task because the formatting directives are far from the arguments: adding, removing or swapping parameters in the text becomes really difficult with long text and several arguments unless you make the text completely unreadable. The solution I adopted some time ago is the following: intead of this: printf "Hello %s %s! It is %i o'clock." first_name last_name time we write: << Hello $first_name $last_name! It is $int{time} o'clock. >> which is converted into let _ = Pervasives.print_string "Hello "; Pervasives.print_string first_name; Pervasives.print_string " "; Pervasives.print_string last_name; Pervasives.print_string "! It is "; print_int time; Pervasives.print_string " o'clock." Any function prefixed by print_ can be defined and used as a `format specifier'. It is also possible to pass several arguments to one function: << $x{arg1 arg2} >> is equivalent to print_x arg1 arg2 but << ${"a" ^ "b"} >> becomes print_string ("a" ^ "b") << $data.field >> becomes print_string data.field Does anyone need this kind of approach? Any better ideas? I put my implementation here (it is quite ugly, I still ignore everything about Camlp4): http://martin.jambon.free.fr/bazar/printfer.tar.gz -- Martin ------------------- 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 ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] Printing text with holes 2003-09-29 13:16 [Caml-list] Printing text with holes Martin Jambon @ 2003-09-29 14:10 ` Christian Lindig 2003-09-29 16:25 ` Benjamin Geer ` (3 subsequent siblings) 4 siblings, 0 replies; 24+ messages in thread From: Christian Lindig @ 2003-09-29 14:10 UTC (permalink / raw) To: Martin Jambon; +Cc: caml-list On Mon, Sep 29, 2003 at 03:16:38PM +0200, Martin Jambon wrote: > I am curious to know what people use to print long text written in a > natural language, and containing many holes, like dynamically > generated web pages. I just like to point out two papers that you might find inspiring: Danvy, Olivier, Functional Unparsing, Journal of Functional Programming, 1998, 8(6) 621--625, Philip Wadler, A prettier printer. The Fun of Programming. A symposium in honour of Professor Richard Bird's 60th birthday Examination Schools, Oxford, 24-25 March 2003. -- Christian ------------------- 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 ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] Printing text with holes 2003-09-29 13:16 [Caml-list] Printing text with holes Martin Jambon 2003-09-29 14:10 ` Christian Lindig @ 2003-09-29 16:25 ` Benjamin Geer 2003-09-29 16:39 ` Brian Hurt ` (2 subsequent siblings) 4 siblings, 0 replies; 24+ messages in thread From: Benjamin Geer @ 2003-09-29 16:25 UTC (permalink / raw) To: Martin Jambon; +Cc: caml-list Martin Jambon wrote: > I am curious to know what people use to print long text written in a > natural language, and containing many holes, like dynamically generated > web pages. I'm writing a general-purpose template processor for Caml; an early release should be ready for download in a week or so. Ben ------------------- 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 ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] Printing text with holes 2003-09-29 13:16 [Caml-list] Printing text with holes 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:16 ` Richard Jones 2003-09-29 17:00 ` Pierre Weis 2003-09-29 17:03 ` Karl Zilles 4 siblings, 2 replies; 24+ messages in thread From: Brian Hurt @ 2003-09-29 16:39 UTC (permalink / raw) To: Martin Jambon; +Cc: caml-list On Mon, 29 Sep 2003, Martin Jambon wrote: > Hello, > > I am curious to know what people use to print long text written in a > natural language, and containing many holes, like dynamically generated > web pages. I agree. I think printf is a horrible way to do output. Reasons include, but are not limited to, the fact that you are basically creating a new "mini-language" (the formatting language) which has to be type checked etc. seperately from the main language, that the values being put into the string are listed far from their positions in the string (your problem) leading to missing parameters, parameters in the wrong order, etc. > The solution I adopted some time ago is the following: IMHO, this is certainly a better solution to the problem. But it does need p4 (I think). What might be even better is an output operator, ala C++. Maybe something like: let ( <$ ) chan str = output_string chan str; chan This would allow you to do: let _ = stdout <$ "Hello " <$ first_name <$ " " <$ last_name <$ "! It is " <$ (string_of_int time) <$ " o'clock.\n";; Not quite as clean, but close, and it doesn't require p4. 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 ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] Printing text with holes 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 18:57 ` Martin Jambon 2003-09-29 18:16 ` Richard Jones 1 sibling, 2 replies; 24+ messages in thread From: Jean-Marc EBER @ 2003-09-29 17:45 UTC (permalink / raw) To: Brian Hurt; +Cc: Martin Jambon, caml-list > > This would allow you to do: > let _ = stdout <$ "Hello " <$ first_name <$ " " <$ last_name <$ "! It is " > <$ (string_of_int time) <$ " o'clock.\n";; > > Not quite as clean, but close, and it doesn't require p4. > With "old" printf approach, you write something like: let _ = printf "Hello %s %s! It is %i o'clock.\n" first_name last_name time Matter of taste: I prefer the printf version. More generally, I have found the printf approach (more precisely OCaml's Format 'magic' standard module) of incredible power and flexibility, if (and only if :-) ) used consistently through *all* your program. This is especially true when you generate "big" documents, where you compose recursively many "nice" ways to pretty-print your result(s). Jean-Marc Eber ------------------- 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 ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] Printing text with holes 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 18:57 ` Martin Jambon 1 sibling, 1 reply; 24+ messages in thread From: Brian Hurt @ 2003-09-29 18:22 UTC (permalink / raw) To: Jean-Marc EBER; +Cc: Martin Jambon, caml-list 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 To detect this is a problem, the compiler has to not only parse Ocaml, it also has to parse printf format strings. The one advantage that printf has is familiarity. All of us who are old C programmers (and I fall into this category) know printf. But that doesn't make it a good interface. 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 ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] Printing text with holes 2003-09-29 18:22 ` Brian Hurt @ 2003-09-29 20:48 ` Pierre Weis 2003-09-29 21:52 ` Richard Jones 2003-09-30 4:40 ` Brian Hurt 0 siblings, 2 replies; 24+ messages in thread From: Pierre Weis @ 2003-09-29 20:48 UTC (permalink / raw) To: Brian Hurt; +Cc: jeanmarc.eber, martin_jambon, caml-list > 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: # Printf.printf "Hello %s %s! It is %i o'clock.\n" name time;; ^^^^ This expression has type int but is here used with type string > To detect this is a problem, the compiler has to not only parse Ocaml, it > also has to parse printf format strings. 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. As long as you realize that format strings are yet another kind of primitive constants you easily admit that the compiler has to type-check them. It is the usual compiler stuff, although a bit more complex, since format string constants have complex types. > The one advantage that printf has is familiarity. All of us who are old C > programmers (and I fall into this category) know printf. But that doesn't > make it a good interface. > > Brian 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 ?) However, concerning printf, you should realize that the Caml printf facility (or scanf, to mention another facility that uses format strings) are not mere transpositions of their C equivalent. They have powerful additional features (%a, %C, %S, %t for instance), their typechecking is entirely static, their interface is much more in the spirit of functional programming, and last but not least the kprintf or kscanf versions of the basic primitives have no equivalent in C. May I also mention specifically for printf the additional facility of the Format module with its associated high-level pretty-printing facilities that are completely and smoothly integrated into the format strings mechanism ? Concerning these powerful additions, I'm not sure that good old C programmers always have a fluent knowledge of the kind of features our Caml printf and scanf primitives provide. Pierre Weis INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/ PS: As an example, I should mention that the Caml scanf function is powerful enough to allow the definition of a polymorphic list scanner generator, that can be apply to any element specific scanner to provide a scanner for lists of those elements' type: this is indeed polymorphic higher-order scanning, well in the spirit of functional programming, and I doubt you can easily obtain that with the C scanf (nor program the Caml polymorphic list scanner with your good old C scanf knowledge)... ------------------- 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 ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] Printing text with holes 2003-09-29 20:48 ` Pierre Weis @ 2003-09-29 21:52 ` Richard Jones 2003-09-29 22:07 ` Fred Yankowski 2003-09-30 4:40 ` Brian Hurt 1 sibling, 1 reply; 24+ messages in thread From: Richard Jones @ 2003-09-29 21:52 UTC (permalink / raw) Cc: caml-list On Mon, Sep 29, 2003 at 10:48:02PM +0200, Pierre Weis wrote: > However, concerning printf, you should realize that the Caml printf > facility (or scanf, to mention another facility that uses format > strings) are not mere transpositions of their C equivalent. [...] their > typechecking is entirely static, On this one small point, people who have been using gcc have had type safe printf/scanf for at least the last 15 years. Rich. -- Richard Jones. http://www.annexia.org/ http://freshmeat.net/users/rwmj Merjis Ltd. http://www.merjis.com/ - all your business data are belong to you. MONOLITH is an advanced framework for writing web applications in C, easier than using Perl & Java, much faster and smaller, reusable widget-based arch, database-backed, discussion, chat, calendaring: http://www.annexia.org/freeware/monolith/ ------------------- 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 ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] Printing text with holes 2003-09-29 21:52 ` Richard Jones @ 2003-09-29 22:07 ` Fred Yankowski 2003-09-29 22:14 ` Daniel M. Albro ` (2 more replies) 0 siblings, 3 replies; 24+ messages in thread From: Fred Yankowski @ 2003-09-29 22:07 UTC (permalink / raw) To: Richard Jones; +Cc: caml-list On Mon, Sep 29, 2003 at 10:52:34PM +0100, Richard Jones wrote: > On this one small point, people who have been using gcc have had > type safe printf/scanf for at least the last 15 years. What do you mean? Compiling the following with gcc 2.95.4 and executing it causes a seg fault on my Linux 2.4.20 system: main() { printf("%s", 'a'); } -- Fred Yankowski fred@ontosys.com tel: +1.630.879.1312 OntoSys, Inc PGP keyID: 7B449345 fax: +1.630.879.1370 www.ontosys.com 38W242 Deerpath Rd, Batavia, IL 60510-9461, USA ------------------- 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 ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] Printing text with holes 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 2 siblings, 1 reply; 24+ messages in thread From: Daniel M. Albro @ 2003-09-29 22:14 UTC (permalink / raw) To: Fred Yankowski; +Cc: Richard Jones, caml-list I assume he means that it won't compile if you use "gcc -Wall -Werror". - Dan Albro On Mon, 2003-09-29 at 15:07, Fred Yankowski wrote: > On Mon, Sep 29, 2003 at 10:52:34PM +0100, Richard Jones wrote: > > On this one small point, people who have been using gcc have had > > type safe printf/scanf for at least the last 15 years. > > What do you mean? Compiling the following with gcc 2.95.4 and > executing it causes a seg fault on my Linux 2.4.20 system: > > main() { printf("%s", 'a'); } -- Daniel M. Albro <albro@humnet.ucla.edu> ------------------- 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 ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] Printing text with holes 2003-09-29 22:14 ` Daniel M. Albro @ 2003-09-29 22:19 ` Richard Jones 0 siblings, 0 replies; 24+ messages in thread From: Richard Jones @ 2003-09-29 22:19 UTC (permalink / raw) Cc: caml-list On Mon, Sep 29, 2003 at 03:14:31PM -0700, Daniel M. Albro wrote: > > I assume he means that it won't compile if you use > "gcc -Wall -Werror". Indeed. Which for all practical purposes means it's type safe. Rich. -- Richard Jones. http://www.annexia.org/ http://freshmeat.net/users/rwmj Merjis Ltd. http://www.merjis.com/ - all your business data are belong to you. All new technology is irrelevant until it is taken up by the public. ------------------- 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 ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] Printing text with holes 2003-09-29 22:07 ` Fred Yankowski 2003-09-29 22:14 ` Daniel M. Albro @ 2003-09-29 22:23 ` Matthieu Sozeau 2003-09-30 3:56 ` Brian Hurt 2 siblings, 0 replies; 24+ messages in thread From: Matthieu Sozeau @ 2003-09-29 22:23 UTC (permalink / raw) To: caml-list On Tuesday 30 September 2003 00:07, Fred Yankowski wrote: > On Mon, Sep 29, 2003 at 10:52:34PM +0100, Richard Jones wrote: > > On this one small point, people who have been using gcc have had > > type safe printf/scanf for at least the last 15 years. > > What do you mean? Compiling the following with gcc 2.95.4 and > executing it causes a seg fault on my Linux 2.4.20 system: > > main() { printf("%s", 'a'); } gcc is permissive by default, use -Wall ! -- Matthieu Sozeau www: http://mattam.ath.cx ------------------- 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 ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] Printing text with holes 2003-09-29 22:07 ` Fred Yankowski 2003-09-29 22:14 ` Daniel M. Albro 2003-09-29 22:23 ` Matthieu Sozeau @ 2003-09-30 3:56 ` Brian Hurt 2 siblings, 0 replies; 24+ messages in thread From: Brian Hurt @ 2003-09-30 3:56 UTC (permalink / raw) To: Fred Yankowski; +Cc: Richard Jones, caml-list On Mon, 29 Sep 2003, Fred Yankowski wrote: > On Mon, Sep 29, 2003 at 10:52:34PM +0100, Richard Jones wrote: > > On this one small point, people who have been using gcc have had > > type safe printf/scanf for at least the last 15 years. > > What do you mean? Compiling the following with gcc 2.95.4 and > executing it causes a seg fault on my Linux 2.4.20 system: > > main() { printf("%s", 'a'); } > > It spits out warnings, not errors, if you add the -Wformat (or, alternatively, the -Wall) option to your compile flags. 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 ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] Printing text with holes 2003-09-29 20:48 ` Pierre Weis 2003-09-29 21:52 ` Richard Jones @ 2003-09-30 4:40 ` Brian Hurt 2003-09-30 6:11 ` David Brown 1 sibling, 1 reply; 24+ messages in thread From: Brian Hurt @ 2003-09-30 4:40 UTC (permalink / raw) To: Pierre Weis; +Cc: jeanmarc.eber, martin_jambon, caml-list 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 ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] Printing text with holes 2003-09-30 4:40 ` Brian Hurt @ 2003-09-30 6:11 ` David Brown 0 siblings, 0 replies; 24+ messages in thread From: David Brown @ 2003-09-30 6:11 UTC (permalink / raw) To: Brian Hurt; +Cc: Pierre Weis, jeanmarc.eber, martin_jambon, caml-list On Mon, Sep 29, 2003 at 11:40:21PM -0500, Brian Hurt wrote: > At heart, printf is doing two seperate jobs... I've done considerable programming both in languages that have printf-style formatting (checked and unchecked), as well as languages that require operators or such to build up formatting. Any time I've wanted to output something, I have one of two responses, depending on the language: - Grumble, grumble, grumble. - Just write it. You can probably guess which is which. That ocaml has been able to integrate printf formatting in a type-safe way is truly amazing, and to me one of the things that makes ocaml a wonderful language. True, it isn't universally extensibly, but it is a lot more so than most people think. Dave ------------------- 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 ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] Printing text with holes 2003-09-29 17:45 ` Jean-Marc EBER 2003-09-29 18:22 ` Brian Hurt @ 2003-09-29 18:57 ` Martin Jambon 2003-09-29 21:25 ` Pierre Weis 2003-09-30 12:52 ` skaller 1 sibling, 2 replies; 24+ messages in thread From: Martin Jambon @ 2003-09-29 18:57 UTC (permalink / raw) To: caml-list On Mon, 29 Sep 2003, Jean-Marc EBER wrote: > > > > This would allow you to do: > > let _ = stdout <$ "Hello " <$ first_name <$ " " <$ last_name <$ "! It is " > > <$ (string_of_int time) <$ " o'clock.\n";; > > > > Not quite as clean, but close, and it doesn't require p4. > > > > With "old" printf approach, you write something like: > let _ = > printf "Hello %s %s! It is %i o'clock.\n" first_name last_name time > > Matter of taste: I prefer the printf version. > > More generally, I have found the printf approach (more precisely OCaml's > Format 'magic' standard module) of incredible power and flexibility, if > (and only if :-) ) used consistently through *all* your program. This > is especially true when you generate "big" documents, where you compose > recursively many "nice" ways to pretty-print your result(s). Yes, printf is OK if you don't have to do many substitutions in one time. This is probably the case most of the time for printing structured output. But printing human-readable text has a strong constraint: it must fit on one 2-dimensional page, with a nice appearance and few empty parts. This is why the programmer likes to have an overview of the output. Having an overview of the output (its shape) is totally useless if you need to print tree-like data to be read by a machine. Just imagine that we want to use a kind of LaTeX or HTML formatting style from Caml, or find some better solution :-) In our situation, the problem is that the efforts required for a modification (let's say the insertion of a new variable) grow with the size of the output. This is even harder when all your variables have the same type and if there is more than one screen between the format specifier and the variable. Well, I just extracted some code from one of my CGI programs. Here, the purpose is to print one line of an HTML table, where one column should appear in bold face (using arrays containing strings "" or "<b>" or "</b>"). Here is the code: let print_binary_patch id bb be ranking p = let volume1 = p.patch_1.volume in let volume2 = p.patch_2.volume in let vol_percentage1 = p.patch_1.relative_volume *. 100. in let vol_percentage2 = p.patch_2.relative_volume *. 100. in let print_option = function None -> () | Some s -> << ($s) >> in << <tr> <td>$int{ranking} $matched_tags{p} [<a href="$sumo_focus_url?id=$id&index=$int{p.patch_global.patch_number}&quick=false" target=_blank>View</a>] [<a href="$sumo_focus_url?id=$id&index=$int{p.patch_global.patch_number}&quick=true" target=_blank>Quick view</a>]</td> <td>${bb.(1)}$g{p.patch_global.patch_size}${be.(1)}</td> <td>${bb.(2)}$r1{vol_percentage1}% ($r1{volume1})${be.(2)}</td> <td>${bb.(3)}$r2{vol_percentage2}% ($r2{volume2})${be.(3)}</td> <td>${bb.(4)}$int{p.patch_1.pdb_group_number}${be.(4)}</td> <td>${bb.(5)}$int{p.patch_2.pdb_group_number}${be.(5)}</td> <td>${bb.(6)}$f3{p.patch_global.patch_sumo_score}${be.(6)}</td> </tr> >> This does not look very beautiful, but for me it is important to have a compact representation where one line of source code produces not less than one line of output. This allows to have a global overview of the output without having to run the program. The above piece of code has 28 holes... that we must be able to modify at any time, just because we don't print all the data. I would like to thank everyone for expressing their opinion on the subject! -- Martin ------------------- 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 ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] Printing text with holes 2003-09-29 18:57 ` Martin Jambon @ 2003-09-29 21:25 ` Pierre Weis 2003-09-30 12:52 ` skaller 1 sibling, 0 replies; 24+ messages in thread From: Pierre Weis @ 2003-09-29 21:25 UTC (permalink / raw) To: Martin Jambon; +Cc: caml-list [...] > Well, I just extracted some code from one of my CGI programs. > Here, the purpose is to print one line of an HTML table, where one column > should appear in bold face (using arrays containing strings "" or "<b>" or > "</b>"). > Here is the code: > > let print_binary_patch id bb be ranking p = > let volume1 = p.patch_1.volume in > let volume2 = p.patch_2.volume in > let vol_percentage1 = p.patch_1.relative_volume *. 100. in > let vol_percentage2 = p.patch_2.relative_volume *. 100. in > > let print_option = function > None -> () > | Some s -> << ($s) >> in > << > <tr> > <td>$int{ranking} $matched_tags{p} > [<a href="$sumo_focus_url?id=$id&index=$int{p.patch_global.patch_number}&quick=false" target=_blank>View</a>] > [<a href="$sumo_focus_url?id=$id&index=$int{p.patch_global.patch_number}&quick=true" target=_blank>Quick view</a>]</td> > <td>${bb.(1)}$g{p.patch_global.patch_size}${be.(1)}</td> > <td>${bb.(2)}$r1{vol_percentage1}% ($r1{volume1})${be.(2)}</td> > <td>${bb.(3)}$r2{vol_percentage2}% ($r2{volume2})${be.(3)}</td> > <td>${bb.(4)}$int{p.patch_1.pdb_group_number}${be.(4)}</td> > <td>${bb.(5)}$int{p.patch_2.pdb_group_number}${be.(5)}</td> > <td>${bb.(6)}$f3{p.patch_global.patch_sumo_score}${be.(6)}</td> > </tr> > >> > > This does not look very beautiful, but for me it is important to have a > compact representation where one line of source code produces not less > than one line of output. This allows to have a global overview of the > output without having to run the program. > The above piece of code has 28 holes... that we must be able to modify at > any time, just because we don't print all the data. > > I would like to thank everyone for expressing their opinion on the > subject! > > -- Martin In this case, I surely would use a template file, read the template in memory, build the substitution function (with a hash table ?) for the dollar identifiers and then use string substitution with Buffer.add_subtitute to produce the output in a Buffer.t value. Hope this helps, Pierre Weis INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/ ------------------- 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 ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] Printing text with holes 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 1 sibling, 1 reply; 24+ messages in thread From: skaller @ 2003-09-30 12:52 UTC (permalink / raw) To: Martin Jambon; +Cc: caml-list On Tue, 2003-09-30 at 04:57, Martin Jambon wrote: > On Mon, 29 Sep 2003, Jean-Marc EBER wrote: > << > <tr> > <td>$int{ranking} $matched_tags{p} > [<a href="$sumo_focus_url?id=$id&index=$int{p.patch_global.patch_number}&quick=false" target=_blank>View</a>] > [<a href="$sumo_focus_url?id=$id&index=$int{p.patch_global.patch_number}&quick=true" target=_blank>Quick view</a>]</td> > <td>${bb.(1)}$g{p.patch_global.patch_size}${be.(1)}</td> > <td>${bb.(2)}$r1{vol_percentage1}% ($r1{volume1})${be.(2)}</td> > <td>${bb.(3)}$r2{vol_percentage2}% ($r2{volume2})${be.(3)}</td> > <td>${bb.(4)}$int{p.patch_1.pdb_group_number}${be.(4)}</td> > <td>${bb.(5)}$int{p.patch_2.pdb_group_number}${be.(5)}</td> > <td>${bb.(6)}$f3{p.patch_global.patch_sumo_score}${be.(6)}</td> > </tr> > >> > > This does not look very beautiful, but for me it is important to have a > compact representation where one line of source code produces not less > than one line of output. You have it easy. You're mainly concerned with nice line breaks. Consider if you were formatting a plain text table and needed to have a view of *column* positions. That's where you start renaming all your variables to exactly 4 letters so you can check that things line up vertically .. :) I have this kind of problem (vaguely) in Felix and the only solution I found was "suck it and see". [I'm generating C++ and I want the output layout to look nice ..] ------------------- 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 ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] Printing text with holes 2003-09-30 12:52 ` skaller @ 2003-09-30 17:48 ` Martin Jambon 0 siblings, 0 replies; 24+ messages in thread From: Martin Jambon @ 2003-09-30 17:48 UTC (permalink / raw) To: skaller; +Cc: caml-list On 30 Sep 2003, skaller wrote: > You have it easy. You're mainly concerned with nice line breaks. > Consider if you were formatting a plain text table and needed > to have a view of *column* positions. That's where you start > renaming all your variables to exactly 4 letters so you can check > that things line up vertically .. :) > > I have this kind of problem (vaguely) in Felix and the only > solution I found was "suck it and see". > > [I'm generating C++ and I want the output layout to look nice ..] So we need some kind of WYSIWYG editors. Imagine your source code contains "special regions", that you can edit using a custom WYSIaWYG editor (what you see is almost what you get), called from your favorite text editor. Maybe it would be enough to specify line-breaking keywords and tabulation marks to generate a minimalist WYSIaWYG editor. (*BEGIN { tabs = [Before "<td>"; Before "<th>"; Before "</tr>"]; newlines = [After "<br>"; After "</tr>"] } *) <table> <tr><th>$header_number_1</th><th>$short</th></tr> <tr><td>$x</td><td>$y</td></tr> </table> (*END *) would be displayed as: <table> <tr><th>$header_number_1</th><th>$short</th></tr> <tr><td>$x</td> <td>$y</td> </tr> </table> (+ syntax highlighting for fake spaces, insertions, keywords, ...) -- Martin ------------------- 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 ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] Printing text with holes 2003-09-29 16:39 ` Brian Hurt 2003-09-29 17:45 ` Jean-Marc EBER @ 2003-09-29 18:16 ` Richard Jones 1 sibling, 0 replies; 24+ messages in thread From: Richard Jones @ 2003-09-29 18:16 UTC (permalink / raw) Cc: caml-list On Mon, Sep 29, 2003 at 11:39:32AM -0500, Brian Hurt wrote: > On Mon, 29 Sep 2003, Martin Jambon wrote: > > > Hello, > > > > I am curious to know what people use to print long text written in a > > natural language, and containing many holes, like dynamically generated > > web pages. > > I agree. I think printf is a horrible way to do output. Reasons include, > but are not limited to, the fact that you are basically creating a new > "mini-language" (the formatting language) which has to be type checked > etc. seperately from the main language, that the values being put into the > string are listed far from their positions in the string (your problem) > leading to missing parameters, parameters in the wrong order, etc. Well it may not be theoretically any good, but practically printf is immensely usable. Rich. -- Richard Jones. http://www.annexia.org/ http://freshmeat.net/users/rwmj Merjis Ltd. http://www.merjis.com/ - all your business data are belong to you. "My karma ran over your dogma" ------------------- 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 ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] Printing text with holes 2003-09-29 13:16 [Caml-list] Printing text with holes Martin Jambon ` (2 preceding siblings ...) 2003-09-29 16:39 ` Brian Hurt @ 2003-09-29 17:00 ` Pierre Weis 2003-09-29 17:03 ` Karl Zilles 4 siblings, 0 replies; 24+ messages in thread From: Pierre Weis @ 2003-09-29 17:00 UTC (permalink / raw) To: Martin Jambon; +Cc: caml-list Hello, > I am curious to know what people use to print long text written in a > natural language, and containing many holes, like dynamically generated > web pages. [...] > Does anyone need this kind of approach? > Any better ideas? > > I put my implementation here (it is quite ugly, I still ignore everything > about Camlp4): > http://martin.jambon.free.fr/bazar/printfer.tar.gz Many people use the same approach with dollars. In the 3.07 release of Objective Caml there is some support to easily obtain such a feature: in the interface of the module Buffer, we find val add_substitute : t -> (string -> string) -> string -> unit (** [add_substitute b f s] appends the string pattern [s] at the end of the buffer [b] with substitution. The substitution process looks for variables into the pattern and substitutes each variable name by its value, as obtained by applying the mapping [f] to the variable name. Inside the string pattern, a variable name immediately follows a non-escaped [$] character and is one of the following: - a non empty sequence of alphanumeric or [_] characters, - an arbitrary sequence of characters enclosed by a pair of matching parentheses or curly brackets. An escaped [$] character is a [$] that immediately follows a backslash character; it then stands for a plain [$]. Raise [Not_found] if the closing character of a parenthesized variable cannot be found. *) This may be used to create strings with $identifiers to expand in them. Hope this helps, Pierre Weis INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/ ------------------- 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 ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] Printing text with holes 2003-09-29 13:16 [Caml-list] Printing text with holes Martin Jambon ` (3 preceding siblings ...) 2003-09-29 17:00 ` Pierre Weis @ 2003-09-29 17:03 ` Karl Zilles 2003-09-29 21:47 ` Pierre Weis 4 siblings, 1 reply; 24+ messages in thread From: Karl Zilles @ 2003-09-29 17:03 UTC (permalink / raw) To: Martin Jambon; +Cc: caml-list Martin Jambon wrote: > Hello, > > I am curious to know what people use to print long text written in a > natural language, and containing many holes, like dynamically generated > web pages. > I just used HereDoc to create a set of web pages, and found it pleasant to work in. I used his old codebase, instead of the alpha version, since it looks like the alpha version hasn't been touched in 2 years. http://www.eleves.ens.fr/home/frisch/soft.html#HereDoc He has a syntax like yours: << Hello, $firstname $lastname. >> or << I am $$ string_of_int age $$ years old >> He doesn't appear to have any syntactic sugar for conversions. Which wasn't a problem. He has constructs for creating loops, and the ability to include templates from other files. He's also abstracted out the printing so that you can tell it where you want to send the generated text. ------------------- 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 ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] Printing text with holes 2003-09-29 17:03 ` Karl Zilles @ 2003-09-29 21:47 ` Pierre Weis 2003-09-30 7:20 ` Jean-Christophe Filliatre 0 siblings, 1 reply; 24+ messages in thread From: Pierre Weis @ 2003-09-29 21:47 UTC (permalink / raw) To: Karl Zilles; +Cc: martin_jambon, caml-list > Martin Jambon wrote: > > Hello, > > > > I am curious to know what people use to print long text written in a > > natural language, and containing many holes, like dynamically generated > > web pages. I just use htmlc, which is simple and efficient: http://pauillac.inria.fr/htmlc/ It uses $identifiers as you suggested, + some Server Side Includes expansions, + local environment definition facilities, + what you would like to add to it in the future :) Here is a brief excerpt of the Changes file: 1.20 (Documents environment variables expansion) ---- - Inside documents variables of the form $X, $(X), or ${X} are considered to be bound in the Htmlc's global environment hash table and expanded as their corresponding string associated value. - New -e argument to load environment files that set up Htmlc's global environment table. Environment files are list of lines that contain a variable name and an associated string (following Caml' lexical conventions). - New server side directive: the line <!--#include environment="filename"--> in a document loads environment file ``filename'' and enriches the global environment accordingly. - New -s compilation flag to expand .shtml files into .html files. 1.10 (User's environment variables expansion) ---- - Throughout documents passed to htmlc, tags of the form <$X> are considered to reference the variable X in the user's gloabal environment: <$HOME> is thus replaced by the string representation of the user's HOME directory. (This expansion mechanism is disabled when including verbatim files). Hope this helps, Pierre Weis INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/ ------------------- 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 ^ permalink raw reply [flat|nested] 24+ messages in thread
* Re: [Caml-list] Printing text with holes 2003-09-29 21:47 ` Pierre Weis @ 2003-09-30 7:20 ` Jean-Christophe Filliatre 0 siblings, 0 replies; 24+ messages in thread From: Jean-Christophe Filliatre @ 2003-09-30 7:20 UTC (permalink / raw) To: Pierre Weis; +Cc: Karl Zilles, martin_jambon, caml-list > > Martin Jambon wrote: > > > Hello, > > > > > > I am curious to know what people use to print long text written in a > > > natural language, and containing many holes, like dynamically generated > > > web pages. I use my own HTML pre-processor, yamlpp, available at http://www.lri.fr/~filliatr/yamlpp.en.html which is no more than a 130 lines long ocamllex program. -- Jean-Christophe ------------------- 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 ^ permalink raw reply [flat|nested] 24+ messages in thread
end of thread, other threads:[~2003-09-30 17:49 UTC | newest] Thread overview: 24+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2003-09-29 13:16 [Caml-list] Printing text with holes 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 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
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox