man-pages/man3/Stdlib.Lazy.3o.html
2021-03-31 01:06:50 +01:00

372 lines
4.8 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of Stdlib.Lazy</TITLE>
</HEAD><BODY>
<H1>Stdlib.Lazy</H1>
Section: OCaml library (3o)<BR>Updated: 2020-01-30<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
Stdlib.Lazy - no description
<A NAME="lbAC">&nbsp;</A>
<H2>Module</H2>
Module Stdlib.Lazy
<A NAME="lbAD">&nbsp;</A>
<H2>Documentation</H2>
<P>
Module
<B>Lazy</B>
<BR>&nbsp;:&nbsp;
<B>(module Stdlib__lazy)</B>
<P>
<P>
<P>
<P>
<P>
<P>
<P>
<I>type </I>
<B>'a</B>
<I>t </I>
=
<B>'a CamlinternalLazy.t</B>
<P>
<P>
A value of type
<B>'a Lazy.t</B>
is a deferred computation, called
a suspension, that has a result of type
<B>'a</B>
. The special
expression syntax
<B>lazy (expr)</B>
makes a suspension of the
computation of
<B>expr</B>
, without computing
<B>expr</B>
itself yet.
&quot;Forcing&quot; the suspension will then compute
<B>expr</B>
and return its
result. Matching a suspension with the special pattern syntax
<B>lazy(pattern)</B>
also computes the underlying expression and
tries to bind it to
<B>pattern</B>
:
<P>
<P>
<B>let lazy_option_map f x =</B>
<B>match x with</B>
<B>| lazy (Some x) -&gt; Some (Lazy.force f x)</B>
<B>| _ -&gt; None</B>
<B><P>
</B>
Note: If lazy patterns appear in multiple cases in a pattern-matching,
lazy expressions may be forced even outside of the case ultimately selected
by the pattern matching. In the example above, the suspension
<B>x</B>
is always
computed.
<P>
Note:
<B>lazy_t</B>
is the built-in type constructor used by the compiler
for the
<B>lazy</B>
keyword. You should not use it directly. Always use
<B>Lazy.t</B>
instead.
<P>
Note:
<B>Lazy.force</B>
is not thread-safe. If you use this module in
a multi-threaded program, you will need to add some locks.
<P>
Note: if the program is compiled with the
<B>-rectypes</B>
option,
ill-founded recursive definitions of the form
<B>let rec x = lazy x</B>
or
<B>let rec x = lazy(lazy(...(lazy x)))</B>
are accepted by the type-checker
and lead, when forced, to ill-formed values that trigger infinite
loops in the garbage collector and other parts of the run-time system.
Without the
<B>-rectypes</B>
option, such ill-founded recursive definitions
are rejected by the type-checker.
<P>
<P>
<P>
<I>exception Undefined </I>
<P>
<P>
<P>
<P>
<P>
<I>val force </I>
:
<B>'a t -&gt; 'a</B>
<P>
<P>
<B>force x</B>
forces the suspension
<B>x</B>
and returns its result.
If
<B>x</B>
has already been forced,
<B>Lazy.force x</B>
returns the
same value again without recomputing it. If it raised an exception,
the same exception is raised again.
Raise
<B>Lazy.Undefined</B>
if the forcing of
<B>x</B>
tries to force
<B>x</B>
itself
recursively.
<P>
<P>
<P>
<I>val force_val </I>
:
<B>'a t -&gt; 'a</B>
<P>
<P>
<B>force_val x</B>
forces the suspension
<B>x</B>
and returns its
result. If
<B>x</B>
has already been forced,
<B>force_val x</B>
returns the same value again without recomputing it.
Raise
<B>Lazy.Undefined</B>
if the forcing of
<B>x</B>
tries to force
<B>x</B>
itself
recursively.
If the computation of
<B>x</B>
raises an exception, it is unspecified
whether
<B>force_val x</B>
raises the same exception or
<B>Lazy.Undefined</B>
.
<P>
<P>
<P>
<I>val from_fun </I>
:
<B>(unit -&gt; 'a) -&gt; 'a t</B>
<P>
<P>
<B>from_fun f</B>
is the same as
<B>lazy (f ())</B>
but slightly more efficient.
<P>
<P>
<B>from_fun</B>
should only be used if the function
<B>f</B>
is already defined.
In particular it is always less efficient to write
<B>from_fun (fun () -&gt; expr)</B>
than
<B>lazy expr</B>
.
<P>
<P>
<B>Since</B>
4.00.0
<P>
<P>
<P>
<I>val from_val </I>
:
<B>'a -&gt; 'a t</B>
<P>
<P>
<B>from_val v</B>
returns an already-forced suspension of
<B>v</B>
.
This is for special purposes only and should not be confused with
<B>lazy (v)</B>
.
<P>
<P>
<B>Since</B>
4.00.0
<P>
<P>
<P>
<I>val is_val </I>
:
<B>'a t -&gt; bool</B>
<P>
<P>
<B>is_val x</B>
returns
<B>true</B>
if
<B>x</B>
has already been forced and
did not raise an exception.
<P>
<P>
<B>Since</B>
4.00.0
<P>
<P>
<P>
<I>val lazy_from_fun </I>
:
<B>(unit -&gt; 'a) -&gt; 'a t</B>
<P>
<B>Deprecated.</B>
synonym for
<B>from_fun</B>
.
<P>
<P>
<P>
<I>val lazy_from_val </I>
:
<B>'a -&gt; 'a t</B>
<P>
<B>Deprecated.</B>
synonym for
<B>from_val</B>
.
<P>
<P>
<P>
<I>val lazy_is_val </I>
:
<B>'a t -&gt; bool</B>
<P>
<B>Deprecated.</B>
synonym for
<B>is_val</B>
.
<P>
<P>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="1"><A HREF="#lbAB">NAME</A><DD>
<DT id="2"><A HREF="#lbAC">Module</A><DD>
<DT id="3"><A HREF="#lbAD">Documentation</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:57 GMT, March 31, 2021
</BODY>
</HTML>