caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] C style for loop
@ 2001-10-11  5:47 Jeff Henrikson
  2001-10-11  8:58 ` Daniel de Rauglaudre
  2001-10-11 12:47 ` [Caml-list] " Berke Durak
  0 siblings, 2 replies; 5+ messages in thread
From: Jeff Henrikson @ 2001-10-11  5:47 UTC (permalink / raw)
  To: caml-list, jprevost

Okay, so maybe I should be more specific about what I want in a "C-style for loop."  Its readablity merits are hopefully self
evident.  Well, unless you're a compulsive CPS addict who wishes even his grocery list could be written to tail recurse. . .

Suppose

(*  loop var | init val | while | expr for next val *)
for     c         0      (c<10)        (c+1)         do
  (* bla *)
done;

desugars into:

let rec iter_gensymXXX c =
  (* bla *)
  let c = (c+1) in
    if (c<10) then iter_gensymXXX c else ()
  in
iter_gensymXXX 0


Then we could write nice readable nested loops for square arrays, etc.  And then there's the canonical loop form which enters in a
place different from the exit test.  For that, C style "break" is a readable idiom:


for c 0 true c do
  (* bla1 *)
  let c = c+1 in
    if !(c<10) then break;
  (* bla2 *)
end;

could desugar into:

let rec iter_gensymXXX c =
  (* bla1 *)
  let c = c+1 in
    if !(c<10) then
	()
    else begin
      (* bla2 *)
	let c = c in
	  if true then iter_gensymXXX c else ()
    end
 in
 iter_gensymXXX 0


The transformation for "continue" is similar.

Pardon my laziness, camlp4 looks really cool, but I haven't taken the time to learn it.  For the time being, if somebody has
already written a macro kind of like this, I'll take it.  IMHO it would be a boon to the caml community to have a standard form for
this.  Even if it were only a "style guide" addition or what have you.


Jeff Henrikson


-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] C style for loop
  2001-10-11  5:47 [Caml-list] C style for loop Jeff Henrikson
@ 2001-10-11  8:58 ` Daniel de Rauglaudre
  2001-10-11 18:05   ` [Caml-list] Thanks: " Jeff Henrikson
  2001-10-11 12:47 ` [Caml-list] " Berke Durak
  1 sibling, 1 reply; 5+ messages in thread
From: Daniel de Rauglaudre @ 2001-10-11  8:58 UTC (permalink / raw)
  To: caml-list, jprevost

Hi,

On Thu, Oct 11, 2001 at 01:47:07AM -0400, Jeff Henrikson wrote:

> Okay, so maybe I should be more specific about what I want in a
> "C-style for loop."  Its readablity merits are hopefully self
> evident.  Well, unless you're a compulsive CPS addict who wishes
> even his grocery list could be written to tail recurse. . .

> (*  loop var | init val | while | expr for next val *)
> for     c         0      (c<10)        (c+1)         do
>   (* bla *)
> done;

------------------------------------- file cloop.ml
#load "q_MLast.cmo";
#load "pa_extend.cmo";

open Pcaml;

value gensym =
  let cnt = ref 0 in
  fun var ->
    let x = do { incr cnt; cnt.val } in
    var ^ "_gensym" ^ string_of_int x
;

value gen_for loc v iv wh nx e =
  let loop_fun = gensym "iter" in
  <:expr<
    let rec $lid:loop_fun$ $lid:v$ =
      if $wh$ then do { $e$; $lid:loop_fun$ $nx$ } else ()
    in
    $lid:loop_fun$ $iv$ >>
;

EXTEND
  expr: LEVEL "expr1"
    [ [ "for"; v = LIDENT; iv = expr LEVEL "simple"; wh = expr LEVEL "simple";
        nx = expr LEVEL "simple"; "do"; e = expr; "done" ->
          gen_for loc v iv wh nx e ] ]
  ;
END;
-------------------------------------

Compilation:
   $ ocamlc -pp camlp4r -I `camlp4 -where` -c cloop.ml

Example under the toplevel:
   $ ocaml -I `camlp4 -where`
	   Objective Caml version 3.02+7 (2001-09-29)
   
   # #load "camlp4o.cma";;
	   Camlp4 Parsing version 3.02+7 (2001-09-29)
   
   # #load "cloop.cmo";;
   # for i = 0 to 10 do print_int i; done;; (* normal loop *)
   012345678910- : unit = ()
   # for c 0 (c<10) (c+1) do print_int c; done;;
   0123456789- : unit = ()
   # for c 0 (c<10) (c+3) do print_int c; done;;
   0369- : unit = ()

Compilation:
   $ cat foo.ml
   for c 0 (c<10) (c+2) do print_int c; done
   $ ocamlc -pp "camlp4o ./cloop.cmo" -c foo.ml
   $

Pretty print the resulting program:
   $ camlp4o ./cloop.cmo pr_o.cmo foo.ml
   let rec iter_gensym1 c =
     if c < 10 then begin print_int c; iter_gensym1 (c + 2) end
   in
   iter_gensym1 0;;

Hope this help.

-- 
Daniel de RAUGLAUDRE
daniel.de_rauglaudre@inria.fr
http://cristal.inria.fr/~ddr/
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] C style for loop
  2001-10-11  5:47 [Caml-list] C style for loop Jeff Henrikson
  2001-10-11  8:58 ` Daniel de Rauglaudre
@ 2001-10-11 12:47 ` Berke Durak
  2001-10-11 13:11   ` Bruce Hoult
  1 sibling, 1 reply; 5+ messages in thread
From: Berke Durak @ 2001-10-11 12:47 UTC (permalink / raw)
  To: Jeff Henrikson; +Cc: caml-list

On Thu, Oct 11, 2001 at 01:47:07AM -0400, Jeff Henrikson wrote:

> Okay, so maybe I should be more specific about what I want in a
> "C-style for loop."  Its readablity merits are hopefully self
> evident.  Well, unless you're a compulsive CPS addict who wishes
> even his grocery list could be written to tail recurse. . .

[...]

Do you really pretend that ``C-style for loops'' have ``self-evident
readability merits'' ?! My opinion is that ```C-style'' loop syntax
IS unreadable, ununderstandable and unprovable. How many people using
C know the _exact_ semantics of :

	for(exp1;expr2;expr3){expr4}

I never manage to remember if expr3 is evaluated if expr2 is always
zero. However with

	for i = 0 to 33 do
		f i
	done

the ONLY little point about which you MIGHT hesitate is : does f 33
get called or does the loop stop at 32 ? So I'd rather write

	for i = 0 to m - 1 do
		for j = 0 to n - 1 do
			f i j (i * n + j)
		done
	done

and let the compiler sort it out. Further, with C syntax, a for
loop is not guaranteed to terminate, even if its body is guaranteed
to. With Caml-syntax, if your program hangs, you know it's not because
of a for-loop.

Die-hard C-style ``for'' syntax fanatics :) can define functionals to
do the same thing and pressure the Caml team to improve their
compilers if they find it's too slow. If you're sure nobody will ever
read your code, you can hack a Camlp4 syntax for it, but I don't think
it's worth it.

I'd like to mention that Scheme has some kind of generalized, totally
awful and superfluous loop-construction (I think it's called ``do'' or
``while'', don't remember, these were old, weakly typed times). We
don't want such a thing, do we ?
--
Berke
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] C style for loop
  2001-10-11 12:47 ` [Caml-list] " Berke Durak
@ 2001-10-11 13:11   ` Bruce Hoult
  0 siblings, 0 replies; 5+ messages in thread
From: Bruce Hoult @ 2001-10-11 13:11 UTC (permalink / raw)
  To: Berke Durak, Jeff Henrikson; +Cc: caml-list

At 2:47 PM +0200 11/10/01, Berke Durak wrote:
>On Thu, Oct 11, 2001 at 01:47:07AM -0400, Jeff Henrikson wrote:
>
>>  Okay, so maybe I should be more specific about what I want in a
>>  "C-style for loop."  Its readablity merits are hopefully self
>>  evident.  Well, unless you're a compulsive CPS addict who wishes
>>  even his grocery list could be written to tail recurse. . .
>
>[...]
>
>Do you really pretend that ``C-style for loops'' have ``self-evident
>readability merits'' ?! My opinion is that ```C-style'' loop syntax
>IS unreadable, ununderstandable and unprovable. How many people using
>C know the _exact_ semantics of :
>
>	for(exp1;expr2;expr3){expr4}

Sure, it's easy:

   {
      exp1;
      while (expr2){
          expr4;
          expr3;
      }
   }

It's damn ugly, though, and with much unnecessary repetition of the 
control variable in simple cases.


>I never manage to remember if expr3 is evaluated if expr2 is always
>zero.

No.  See above.



>  However with
>
>	for i = 0 to 33 do
>		f i
>	done
>
>the ONLY little point about which you MIGHT hesitate is : does f 33
>get called or does the loop stop at 32 ?

Dylan makes has explicit versions:

   for (i from 0 to 33)
   end

   for (i from 0 below 33)
   end

-- Bruce
-------------------
Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Caml-list] Thanks: C style for loop
  2001-10-11  8:58 ` Daniel de Rauglaudre
@ 2001-10-11 18:05   ` Jeff Henrikson
  0 siblings, 0 replies; 5+ messages in thread
From: Jeff Henrikson @ 2001-10-11 18:05 UTC (permalink / raw)
  To: Daniel de Rauglaudre; +Cc: caml-list

Awesome, thanks.  What an impressive tool.  I've gotta learn it for myself so I can do all the things I was never able to do with
the C preprocessor!


Jeff Henrikson

> -----Original Message-----
> From: owner-caml-list@pauillac.inria.fr
> [mailto:owner-caml-list@pauillac.inria.fr]On Behalf Of Daniel de
> Rauglaudre
> Sent: Thursday, October 11, 2001 4:59 AM
> To: caml-list@inria.fr; jprevost@panasas.com
> Subject: Re: [Caml-list] C style for loop
>
>
> Hi,
>
> On Thu, Oct 11, 2001 at 01:47:07AM -0400, Jeff Henrikson wrote:
>
> > Okay, so maybe I should be more specific about what I want in a
> > "C-style for loop."  Its readablity merits are hopefully self
> > evident.  Well, unless you're a compulsive CPS addict who wishes
> > even his grocery list could be written to tail recurse. . .
>
> > (*  loop var | init val | while | expr for next val *)
> > for     c         0      (c<10)        (c+1)         do
> >   (* bla *)
> > done;
>
> ------------------------------------- file cloop.ml
> #load "q_MLast.cmo";
> #load "pa_extend.cmo";
>
> open Pcaml;
>
> value gensym =
>   let cnt = ref 0 in
>   fun var ->
>     let x = do { incr cnt; cnt.val } in
>     var ^ "_gensym" ^ string_of_int x
> ;
>
> value gen_for loc v iv wh nx e =
>   let loop_fun = gensym "iter" in
>   <:expr<
>     let rec $lid:loop_fun$ $lid:v$ =
>       if $wh$ then do { $e$; $lid:loop_fun$ $nx$ } else ()
>     in
>     $lid:loop_fun$ $iv$ >>
> ;
>
> EXTEND
>   expr: LEVEL "expr1"
>     [ [ "for"; v = LIDENT; iv = expr LEVEL "simple"; wh = expr LEVEL "simple";
>         nx = expr LEVEL "simple"; "do"; e = expr; "done" ->
>           gen_for loc v iv wh nx e ] ]
>   ;
> END;
> -------------------------------------
>
> Compilation:
>    $ ocamlc -pp camlp4r -I `camlp4 -where` -c cloop.ml
>
> Example under the toplevel:
>    $ ocaml -I `camlp4 -where`
> 	   Objective Caml version 3.02+7 (2001-09-29)
>
>    # #load "camlp4o.cma";;
> 	   Camlp4 Parsing version 3.02+7 (2001-09-29)
>
>    # #load "cloop.cmo";;
>    # for i = 0 to 10 do print_int i; done;; (* normal loop *)
>    012345678910- : unit = ()
>    # for c 0 (c<10) (c+1) do print_int c; done;;
>    0123456789- : unit = ()
>    # for c 0 (c<10) (c+3) do print_int c; done;;
>    0369- : unit = ()
>
> Compilation:
>    $ cat foo.ml
>    for c 0 (c<10) (c+2) do print_int c; done
>    $ ocamlc -pp "camlp4o ./cloop.cmo" -c foo.ml
>    $
>
> Pretty print the resulting program:
>    $ camlp4o ./cloop.cmo pr_o.cmo foo.ml
>    let rec iter_gensym1 c =
>      if c < 10 then begin print_int c; iter_gensym1 (c + 2) end
>    in
>    iter_gensym1 0;;
>
> Hope this help.
>
> --
> Daniel de RAUGLAUDRE
> daniel.de_rauglaudre@inria.fr
> http://cristal.inria.fr/~ddr/
> -------------------
> Bug reports: http://caml.inria.fr/bin/caml-bugs  FAQ: http://caml.inria.fr/FAQ/
> 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/
To unsubscribe, mail caml-list-request@inria.fr  Archives: http://caml.inria.fr


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2001-10-11 17:53 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-10-11  5:47 [Caml-list] C style for loop Jeff Henrikson
2001-10-11  8:58 ` Daniel de Rauglaudre
2001-10-11 18:05   ` [Caml-list] Thanks: " Jeff Henrikson
2001-10-11 12:47 ` [Caml-list] " Berke Durak
2001-10-11 13:11   ` Bruce Hoult

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).