caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Pierre Weis <pierre.weis@inria.fr>
To: mvanier@cs.caltech.edu (Michael Vanier)
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] yet another question on lazy lists
Date: Sat, 6 Jul 2002 17:26:33 +0200 (MET DST)	[thread overview]
Message-ID: <200207061526.RAA06532@pauillac.inria.fr> (raw)
In-Reply-To: <200207060148.g661mZp06460@orchestra.cs.caltech.edu> from Michael Vanier at "Jul 5, 102 06:48:35 pm"

[...]
> # let rec fibs2 =
>     stream_cons 0 (stream_cons 1 (add_streams (stream_tl fibs2) fibs2))
>     -------------------------------------------------------------------
>   ;;
> This kind of expression is not allowed as right-hand side of `let rec'
> 
> What does this mean?  My best guess is that ocaml sees that you're defining
> a value in terms of itself (like e.g. "let rec a = 2 * a") and won't allow
> it, but that doesn't explain why the lazy version works.

It means right-hand side of `let rec' expressions are submitted to
conditions in order to be able to compile the recursively defined
value.

Roughly speaking, you cannot use expressions that contain function
calls, variables, or constants that are not under an explicit
(i.e. syntactic) function abstration or arguments of a construtor
application. Hence, neither let rec x = x nor let rec x = 1 is
allowed. Hence, all recursive definitions of the form let rec f =
function x -> .... are legal.

On the other hand, constructor applications are allowed (since those
are not function calls per se., and recursive values defined with
constructors are easy to compile). For instance,
# let rec x = 1 :: x;;
val x : int list =
  [1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1;
   1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1;
   1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1;
   1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1;
   1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1;
   1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1;
   1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1;
   1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1;
   1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1;
   1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1;
   1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1;
   1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1;
   ...]

This explains why replacing a constructor application by a function
call may turn a legal recursive definition into an illegal one. This
also explain why your previous ``lazy'' definition worked.

>  Does this mean
> that my "stream_cons" function is useless?
>
> Thanks in advance,
> 
> Mike

No. It means you cannot always use it in place of a constructor
application. Note that a similar phenomenon occurs for pattern
matching: constructors are mandatory, you cannot subtitute them by
equivalent function calls!

Best regards,

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


  reply	other threads:[~2002-07-06 15:26 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-07-06  1:48 Michael Vanier
2002-07-06 15:26 ` Pierre Weis [this message]
2002-07-06 18:22   ` William Lovas
2002-07-07  0:26     ` Michael Vanier
2002-07-08  0:33       ` John Max Skaller

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=200207061526.RAA06532@pauillac.inria.fr \
    --to=pierre.weis@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=mvanier@cs.caltech.edu \
    /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).