author | wenzelm |
Sun, 28 Dec 2014 22:03:11 +0100 | |
changeset 59194 | b51489b75bb9 |
parent 44387 | 0f0ba362ce50 |
permissions | -rw-r--r-- |
32817 | 1 |
(* Title: Pure/Concurrent/lazy_sequential.ML |
2 |
Author: Florian Haftmann and Makarius, TU Muenchen |
|
3 |
||
34278 | 4 |
Lazy evaluation with memoing of results and regular exceptions |
5 |
(sequential version). |
|
32817 | 6 |
*) |
7 |
||
8 |
structure Lazy: LAZY = |
|
9 |
struct |
|
10 |
||
11 |
(* datatype *) |
|
12 |
||
13 |
datatype 'a expr = |
|
14 |
Expr of unit -> 'a | |
|
15 |
Result of 'a Exn.result; |
|
16 |
||
17 |
abstype 'a lazy = Lazy of 'a expr Unsynchronized.ref |
|
18 |
with |
|
19 |
||
59194 | 20 |
fun lazy e = Lazy (Unsynchronized.ref (Expr e)); |
21 |
fun value a = Lazy (Unsynchronized.ref (Result (Exn.Res a))); |
|
22 |
||
32817 | 23 |
fun peek (Lazy r) = |
24 |
(case ! r of |
|
25 |
Expr _ => NONE |
|
32844 | 26 |
| Result res => SOME res); |
32817 | 27 |
|
59194 | 28 |
fun is_running _ = false; |
44198 | 29 |
fun is_finished x = is_some (peek x); |
59194 | 30 |
val finished_result = peek; |
44198 | 31 |
|
32817 | 32 |
|
33 |
(* force result *) |
|
34 |
||
35 |
fun force_result (Lazy r) = |
|
34278 | 36 |
let |
37 |
val result = |
|
38 |
(case ! r of |
|
39 |
Expr e => Exn.capture e () |
|
40 |
| Result res => res); |
|
39232
69c6d3e87660
more abstract treatment of interrupts in structure Exn -- hardly ever need to mention Interrupt literally;
wenzelm
parents:
34293
diff
changeset
|
41 |
val _ = if Exn.is_interrupt_exn result then () else r := Result result; |
34278 | 42 |
in result end; |
32817 | 43 |
|
44 |
fun force r = Exn.release (force_result r); |
|
44386
4048ca2658b7
some support for toplevel printing wrt. editor perspective (still inactive);
wenzelm
parents:
44198
diff
changeset
|
45 |
fun map f x = lazy (fn () => f (force x)); |
4048ca2658b7
some support for toplevel printing wrt. editor perspective (still inactive);
wenzelm
parents:
44198
diff
changeset
|
46 |
|
4048ca2658b7
some support for toplevel printing wrt. editor perspective (still inactive);
wenzelm
parents:
44198
diff
changeset
|
47 |
|
4048ca2658b7
some support for toplevel printing wrt. editor perspective (still inactive);
wenzelm
parents:
44198
diff
changeset
|
48 |
(* future evaluation *) |
4048ca2658b7
some support for toplevel printing wrt. editor perspective (still inactive);
wenzelm
parents:
44198
diff
changeset
|
49 |
|
4048ca2658b7
some support for toplevel printing wrt. editor perspective (still inactive);
wenzelm
parents:
44198
diff
changeset
|
50 |
fun future params x = |
4048ca2658b7
some support for toplevel printing wrt. editor perspective (still inactive);
wenzelm
parents:
44198
diff
changeset
|
51 |
if is_finished x then Future.value_result (force_result x) |
4048ca2658b7
some support for toplevel printing wrt. editor perspective (still inactive);
wenzelm
parents:
44198
diff
changeset
|
52 |
else (singleton o Future.forks) params (fn () => force x); |
32817 | 53 |
|
54 |
end; |
|
55 |
end; |
|
56 |
||
57 |
type 'a lazy = 'a Lazy.lazy; |
|
58 |