caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Re: Let rec trouble
       [not found] <199807280014.CAA01029@pauillac.inria.fr>
@ 1998-07-28  6:15 ` Christopher Oliver
  0 siblings, 0 replies; 3+ messages in thread
From: Christopher Oliver @ 1998-07-28  6:15 UTC (permalink / raw)
  To: Pierre Weis

> > Um... I'm getting "syntax error" with OLABL 1.07.
> 
> Not in O'Caml :)

Jacques Garrigue found the problem, and it likely applies to OCAML
as well.  I didn't terminate my open directives with a semicolon.
He say the compiler was expecting a top level definition rather than
a lambda binding because of this.  This version compiles just fine:

  open Num
  open Nat
  open Big_int
  open Ratio;;  (* Note the trailing statement delimiter. *)
  
  let rec n k l =
    let rec m i =
      if i =/ Int 0 then
        Int 1 else
        Int 2
  	*/ (m (pred_num i))
   	*/ (n (k **/ (m (pred_num i))) (pred_num l)) in
    if l =/ Int 2 then succ_num k else m k
  in
    print_string (string_of_num (n (Int 3) (Int 3)));;

Given that some of the system sources conspicuously omit the double
semicolons, maybe it would be good to document where this is permitted
and why, else people studying the compiler as example code will get
very confused.

Small question: is there any chance your CAML books will be available
in an English translation in the future?  While I can work my way
through something like "Miroir du Cyclism" with difficulty, my French
is hardly up to technical reading.  Also, I'd like something I can
recommend to colleagues who may have no exposure to French whatsoever.

Thank you,

-- 
Christopher Oliver                     Traverse Internet
Systems Coordinator                    223 Grandview Pkwy, Suite 108
oliver@traverse.net                    Traverse City, Michigan, 49684
let magic f = fun x -> x and more_magic n f = fun x -> f ((n f) x);;




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

* Re: Let rec trouble
  1998-07-25 19:06 Christopher Oliver
@ 1998-07-27 18:30 ` Pierre Weis
  0 siblings, 0 replies; 3+ messages in thread
From: Pierre Weis @ 1998-07-27 18:30 UTC (permalink / raw)
  To: Christopher Oliver; +Cc: caml-list

> I'm having trouble with the syntax of let rec.  Consider the following
> program for computing Van der Waerden's bound:
> 
>   open Num
>   open Nat
>   open Big_int
>   open Ratio
> 
>   let rec n k l =
>     let rec m i =
>       if i =/ Int 0 then
>         Int 1 else
>         Int 2
>           */ (m (pred_num i))
>           */ (n (k **/ (m (pred_num i))) (pred_num l)) in
>     if l =/ Int 2 then succ_num k else m k;;
> 
>   print_string (string_of_num (n (Int 3) (Int 3)));;
> 
> I would like to restrict the lexical scope of 'n' by replacing the first
> double semicolon with 'in.'  I nest m precisely to capture k and l in m's
> lexical environment.  Why is this use forbidden?  I.e. Why shouldn't I be
> able to write:
> 
>   let rec n k l =
>     let rec m i =
>       if i =/ Int 0 then
>         Int 1 else
>         Int 2
>           */ (m (pred_num i))
>           */ (n (k **/ (m (pred_num i))) (pred_num l)) in
>     if l =/ Int 2 then succ_num k else m k
>   in
>     print_string (string_of_num (n (Int 3) (Int 3)));;
> 
> I would prefer not to define a top level symbol, and this seems an
> inconsistancy.  Am I missing something?
> 
> -- 
> Christopher Oliver                     Traverse Internet
> Systems Coordinator                    223 Grandview Pkwy, Suite 108
> oliver@traverse.net                    Traverse City, Michigan, 49684
> let magic f = fun x -> x and more_magic n f = fun x -> f ((n f) x);;

You're second program is perfectly legal in Caml (and Objective Caml)
and indeed works fine.

The problem (if any) is that this program leads to a huge computation
(the result is an integer number with 50100 decimal digits). Thus it
takes a while to run, and that may be the reason why you thought the
second version did not work properly: in the first version, when
defining the n function first you had not to wait for any
computation. You just have to wait for the answer when evaluating
print_string (string_of_num (n (Int 3) (Int 3)));;. On the other hand,
when mixing the final computation and the function definition you had
to wait until the end of the entire computation.

I assume you were using the interactive system, hence your
surprise. If you were writing stand alone programs, the compiler
whould have properly compiled the two versions of your program and you
would never have noticed any problem!

Pierre Weis

INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/







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

* Let rec trouble
@ 1998-07-25 19:06 Christopher Oliver
  1998-07-27 18:30 ` Pierre Weis
  0 siblings, 1 reply; 3+ messages in thread
From: Christopher Oliver @ 1998-07-25 19:06 UTC (permalink / raw)
  To: caml-list

I'm having trouble with the syntax of let rec.  Consider the following
program for computing Van der Waerden's bound:

  open Num
  open Nat
  open Big_int
  open Ratio

  let rec n k l =
    let rec m i =
      if i =/ Int 0 then
        Int 1 else
        Int 2
          */ (m (pred_num i))
          */ (n (k **/ (m (pred_num i))) (pred_num l)) in
    if l =/ Int 2 then succ_num k else m k;;

  print_string (string_of_num (n (Int 3) (Int 3)));;

I would like to restrict the lexical scope of 'n' by replacing the first
double semicolon with 'in.'  I nest m precisely to capture k and l in m's
lexical environment.  Why is this use forbidden?  I.e. Why shouldn't I be
able to write:

  let rec n k l =
    let rec m i =
      if i =/ Int 0 then
        Int 1 else
        Int 2
          */ (m (pred_num i))
          */ (n (k **/ (m (pred_num i))) (pred_num l)) in
    if l =/ Int 2 then succ_num k else m k
  in
    print_string (string_of_num (n (Int 3) (Int 3)));;

I would prefer not to define a top level symbol, and this seems an
inconsistancy.  Am I missing something?

-- 
Christopher Oliver                     Traverse Internet
Systems Coordinator                    223 Grandview Pkwy, Suite 108
oliver@traverse.net                    Traverse City, Michigan, 49684
let magic f = fun x -> x and more_magic n f = fun x -> f ((n f) x);;





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

end of thread, other threads:[~1998-07-28 16:36 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <199807280014.CAA01029@pauillac.inria.fr>
1998-07-28  6:15 ` Let rec trouble Christopher Oliver
1998-07-25 19:06 Christopher Oliver
1998-07-27 18:30 ` Pierre Weis

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