<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2002/07/efd8bf1158b6562718435420a02fd7f2"
  from="Pierre Weis &lt;pierre.weis@i...&gt;"
  author="Pierre Weis"
  date="2002-07-06T15:26:37"
  subject="Re: [Caml-list] yet another question on lazy lists"
  prev="2002/07/061b98c60a3aae3e13a775ccfea73028"
  next="2002/07/55f251b04e7d3d2f236094ccb9aebe84"
  prev-in-thread="2002/07/746ede35d7b862c4b263842d67508891"
  next-in-thread="2002/07/d1b9c9518820db66620939f4ea73e1a4"
  prev-thread="2002/07/1eefe810f7f0f0aa410a2eab708b2234"
  next-thread="2002/07/061b98c60a3aae3e13a775ccfea73028"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="[Caml-list] yet another question on lazy lists">
<msg 
  url="2002/07/746ede35d7b862c4b263842d67508891"
  from="Michael Vanier &lt;mvanier@c...&gt;"
  author="Michael Vanier"
  date="2002-07-06T14:18:48"
  subject="[Caml-list] yet another question on lazy lists">
<msg 
  url="2002/07/efd8bf1158b6562718435420a02fd7f2"
  from="Pierre Weis &lt;pierre.weis@i...&gt;"
  author="Pierre Weis"
  date="2002-07-06T15:26:37"
  subject="Re: [Caml-list] yet another question on lazy lists">
<msg 
  url="2002/07/d1b9c9518820db66620939f4ea73e1a4"
  from="William Lovas &lt;wlovas@s...&gt;"
  author="William Lovas"
  date="2002-07-06T18:22:57"
  subject="Re: [Caml-list] yet another question on lazy lists">
<msg 
  url="2002/07/f3f66088ad1575a8c6b10b09ffdfcded"
  from="Michael Vanier &lt;mvanier@c...&gt;"
  author="Michael Vanier"
  date="2002-07-07T09:20:21"
  subject="Re: [Caml-list] yet another question on lazy lists">
<msg 
  url="2002/07/99c5bb60f441d6e61974021204d72812"
  from="John Max Skaller &lt;skaller@o...&gt;"
  author="John Max Skaller"
  date="2002-07-08T00:34:06"
  subject="Re: [Caml-list] yet another question on lazy lists">
</msg>
</msg>
</msg>
</msg>
</msg>
</thread>

<contents>
[...]
&gt; # let rec fibs2 =
&gt;     stream_cons 0 (stream_cons 1 (add_streams (stream_tl fibs2) fibs2))
&gt;     -------------------------------------------------------------------
&gt;   ;;
&gt; This kind of expression is not allowed as right-hand side of `let rec'
&gt; 
&gt; What does this mean?  My best guess is that ocaml sees that you're defining
&gt; a value in terms of itself (like e.g. "let rec a = 2 * a") and won't allow
&gt; 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 -&gt; .... 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.

&gt;  Does this mean
&gt; that my "stream_cons" function is useless?
&gt;
&gt; Thanks in advance,
&gt; 
&gt; 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

</contents>

</message>

