1Lazy(3)                          OCaml library                         Lazy(3)
2
3
4

NAME

6       Lazy - Deferred computations.
7

Module

9       Module   Lazy
10

Documentation

12       Module Lazy
13        : sig end
14
15
16       Deferred computations.
17
18
19
20
21
22       type 'a t = 'a CamlinternalLazy.t
23
24
25       A  value  of type 'a Lazy.t is a deferred computation, called a suspen‐
26       sion, that has a result of type 'a .   The  special  expression  syntax
27       lazy  (expr)  makes  a  suspension of the computation of expr , without
28       computing expr itself yet.  "Forcing" the suspension will then  compute
29       expr and return its result. Matching a suspension with the special pat‐
30       tern syntax lazy(pattern) also computes the underlying  expression  and
31       tries to bind it to pattern :
32
33
34           let lazy_option_map f x =
35           match x with
36           | lazy (Some x) -> Some (Lazy.force f x)
37           | _ -> None
38
39
40       Note:  If lazy patterns appear in multiple cases in a pattern-matching,
41       lazy expressions may be forced even outside of the case ultimately  se‐
42       lected  by the pattern matching. In the example above, the suspension x
43       is always computed.
44
45       Note: lazy_t is the built-in type constructor used by the compiler  for
46       the  lazy  keyword.  You should not use it directly.  Always use Lazy.t
47       instead.
48
49       Note: Lazy.force is not thread-safe.  If  you  use  this  module  in  a
50       multi-threaded program, you will need to add some locks.
51
52       Note: if the program is compiled with the -rectypes option, ill-founded
53       recursive definitions of the form let rec x = lazy x or  let  rec  x  =
54       lazy(lazy(...(lazy x))) are accepted by the type-checker and lead, when
55       forced, to ill-formed values that trigger infinite loops in the garbage
56       collector  and  other  parts of the run-time system.  Without the -rec‐
57       types option, such ill-founded recursive definitions  are  rejected  by
58       the type-checker.
59
60
61
62       exception Undefined
63
64
65
66
67
68       val force : 'a t -> 'a
69
70
71       force  x  forces the suspension x and returns its result.  If x has al‐
72       ready been forced, Lazy.force x returns the same  value  again  without
73       recomputing  it.   If  it  raised  an  exception, the same exception is
74       raised again.
75
76
77       Raises Undefined if the forcing of x tries to  force  x  itself  recur‐
78       sively.
79
80
81
82
83   Iterators
84       val map : ('a -> 'b) -> 'a t -> 'b t
85
86
87       map  f x returns a suspension that, when forced, forces x and applies f
88       to its value.
89
90       It is equivalent to lazy (f (Lazy.force x)) .
91
92
93       Since 4.13.0
94
95
96
97
98   Reasoning on already-forced suspensions
99       val is_val : 'a t -> bool
100
101
102       is_val x returns true if x has already been forced and did not raise an
103       exception.
104
105
106       Since 4.00.0
107
108
109
110       val from_val : 'a -> 'a t
111
112
113       from_val v evaluates v first (as any function would) and returns an al‐
114       ready-forced suspension of its result.  It is the same as let x = v  in
115       lazy x , but uses dynamic tests to optimize suspension creation in some
116       cases.
117
118
119       Since 4.00.0
120
121
122
123       val map_val : ('a -> 'b) -> 'a t -> 'b t
124
125
126       map_val f x applies f directly if x is already forced, otherwise it be‐
127       haves as map f x .
128
129       When  x  is  already  forced, this behavior saves the construction of a
130       suspension, but on the other hand it performs more  work  eagerly  that
131       may not be useful if you never force the function result.
132
133       If f raises an exception, it will be raised immediately when is_val x ,
134       or raised only when forcing the thunk otherwise.
135
136       If map_val f x does not raise an exception, then is_val (map_val  f  x)
137       is equal to is_val x .
138
139
140       Since 4.13.0
141
142
143
144
145   Advanced
146       The  following definitions are for advanced uses only; they require fa‐
147       miliary with the lazy compilation scheme to be used appropriately.
148
149       val from_fun : (unit -> 'a) -> 'a t
150
151
152       from_fun f is the same as lazy (f ()) but slightly more efficient.
153
154       It should only be used if the function f is already defined.   In  par‐
155       ticular  it is always less efficient to write from_fun (fun () -> expr)
156       than lazy expr .
157
158
159       Since 4.00.0
160
161
162
163       val force_val : 'a t -> 'a
164
165
166       force_val x forces the suspension x and returns its result.  If  x  has
167       already  been  forced, force_val x returns the same value again without
168       recomputing it.
169
170       If the computation of x raises an exception, it is unspecified  whether
171       force_val x raises the same exception or Lazy.Undefined .
172
173
174       Raises  Undefined  if  the  forcing of x tries to force x itself recur‐
175       sively.
176
177
178
179
180   Deprecated
181       val lazy_from_fun : (unit -> 'a) -> 'a t
182
183       Deprecated.  synonym for from_fun .
184
185
186
187       val lazy_from_val : 'a -> 'a t
188
189       Deprecated.  synonym for from_val .
190
191
192
193       val lazy_is_val : 'a t -> bool
194
195       Deprecated.  synonym for is_val .
196
197
198
199
200
201OCamldoc                          2023-01-23                           Lazy(3)
Impressum