1997-10-14 06:29:58 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
2011-07-27 07:17:02 -07:00
|
|
|
(* OCaml *)
|
1997-10-14 06:29:58 -07:00
|
|
|
(* *)
|
|
|
|
(* Damien Doligez, projet Para, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1997-10-22 06:26:05 -07:00
|
|
|
(* Copyright 1997 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
2001-12-07 05:41:02 -08:00
|
|
|
(* under the terms of the GNU Library General Public License, with *)
|
|
|
|
(* the special exception on linking described in file ../LICENSE. *)
|
1997-10-14 06:29:58 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
2001-10-26 15:38:48 -07:00
|
|
|
(** Deferred computations. *)
|
1997-10-14 06:29:58 -07:00
|
|
|
|
2002-01-20 09:39:10 -08:00
|
|
|
type 'a t = 'a lazy_t;;
|
|
|
|
(** A value of type ['a Lazy.t] is a deferred computation, called
|
|
|
|
a suspension, that has a result of type ['a]. The special
|
|
|
|
expression syntax [lazy (expr)] makes a suspension of the
|
|
|
|
computation of [expr], without computing [expr] itself yet.
|
|
|
|
"Forcing" the suspension will then compute [expr] and return its
|
|
|
|
result.
|
|
|
|
|
|
|
|
Note: [lazy_t] is the built-in type constructor used by the compiler
|
|
|
|
for the [lazy] keyword. You should not use it directly. Always use
|
|
|
|
[Lazy.t] instead.
|
2002-07-30 06:02:56 -07:00
|
|
|
|
2010-05-12 07:56:09 -07:00
|
|
|
Note: [Lazy.force] is not thread-safe. If you use this module in
|
|
|
|
a multi-threaded program, you will need to add some locks.
|
|
|
|
|
2002-07-30 06:02:56 -07:00
|
|
|
Note: if the program is compiled with the [-rectypes] option,
|
|
|
|
ill-founded recursive definitions of the form [let rec x = lazy x]
|
|
|
|
or [let rec x = lazy(lazy(...(lazy x)))] 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 [-rectypes] option, such ill-founded recursive definitions
|
|
|
|
are rejected by the type-checker.
|
2002-01-20 09:39:10 -08:00
|
|
|
*)
|
|
|
|
|
|
|
|
|
|
|
|
exception Undefined;;
|
|
|
|
|
2008-08-01 09:57:10 -07:00
|
|
|
(* val force : 'a t -> 'a ;; *)
|
2012-03-14 09:03:15 -07:00
|
|
|
external force : 'a t -> 'a = "%lazy_force";;
|
2002-01-20 09:39:10 -08:00
|
|
|
(** [force x] forces the suspension [x] and returns its result.
|
|
|
|
If [x] has already been forced, [Lazy.force x] returns the
|
|
|
|
same value again without recomputing it. If it raised an exception,
|
|
|
|
the same exception is raised again.
|
|
|
|
Raise [Undefined] if the forcing of [x] tries to force [x] itself
|
|
|
|
recursively.
|
1997-10-14 06:29:58 -07:00
|
|
|
*)
|
2001-10-26 15:38:48 -07:00
|
|
|
|
2002-01-20 09:39:10 -08:00
|
|
|
val force_val : 'a t -> 'a;;
|
|
|
|
(** [force_val x] forces the suspension [x] and returns its
|
|
|
|
result. If [x] has already been forced, [force_val x]
|
|
|
|
returns the same value again without recomputing it.
|
|
|
|
Raise [Undefined] if the forcing of [x] tries to force [x] itself
|
|
|
|
recursively.
|
|
|
|
If the computation of [x] raises an exception, it is unspecified
|
|
|
|
whether [force_val x] raises the same exception or [Undefined].
|
|
|
|
*)
|
|
|
|
|
2012-03-08 11:52:03 -08:00
|
|
|
val from_fun : (unit -> 'a) -> 'a t;;
|
2012-03-08 14:27:57 -08:00
|
|
|
(** [from_fun f] is the same as [lazy (f ())] but slightly more efficient.
|
2015-12-14 02:15:47 -08:00
|
|
|
|
|
|
|
[from_fun] should only be used if the function [f] is already defined.
|
|
|
|
In particular it is always less efficient to write
|
|
|
|
[from_fun (fun () -> expr)] than [lazy expr].
|
|
|
|
|
2012-03-08 14:27:57 -08:00
|
|
|
@since 4.00.0 *)
|
2002-01-20 09:39:10 -08:00
|
|
|
|
2012-03-08 11:52:03 -08:00
|
|
|
val from_val : 'a -> 'a t;;
|
|
|
|
(** [from_val v] returns an already-forced suspension of [v].
|
2002-01-20 09:39:10 -08:00
|
|
|
This is for special purposes only and should not be confused with
|
2012-03-08 14:27:57 -08:00
|
|
|
[lazy (v)].
|
|
|
|
@since 4.00.0 *)
|
2002-01-20 09:39:10 -08:00
|
|
|
|
2012-03-08 11:52:03 -08:00
|
|
|
val is_val : 'a t -> bool;;
|
|
|
|
(** [is_val x] returns [true] if [x] has already been forced and
|
2012-03-08 14:27:57 -08:00
|
|
|
did not raise an exception.
|
|
|
|
@since 4.00.0 *)
|
2012-03-08 11:52:03 -08:00
|
|
|
|
2014-10-15 06:34:58 -07:00
|
|
|
val lazy_from_fun : (unit -> 'a) -> 'a t
|
|
|
|
[@@ocaml.deprecated "Use Lazy.from_fun instead."];;
|
2012-03-08 11:52:03 -08:00
|
|
|
(** @deprecated synonym for [from_fun]. *)
|
|
|
|
|
2014-10-15 06:34:58 -07:00
|
|
|
val lazy_from_val : 'a -> 'a t
|
|
|
|
[@@ocaml.deprecated "Use Lazy.from_val instead."];;
|
2012-03-08 11:52:03 -08:00
|
|
|
(** @deprecated synonym for [from_val]. *)
|
|
|
|
|
2014-10-15 06:34:58 -07:00
|
|
|
val lazy_is_val : 'a t -> bool
|
|
|
|
[@@ocaml.deprecated "Use Lazy.is_val instead."];;
|
2012-03-08 11:52:03 -08:00
|
|
|
(** @deprecated synonym for [is_val]. *)
|