src/Pure/Concurrent/lazy_sequential.ML
author wenzelm
Sun, 28 Dec 2014 22:03:11 +0100
changeset 59194 b51489b75bb9
parent 44387 0f0ba362ce50
permissions -rw-r--r--
proper sequential version;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32817
4ed308181f7f Lazy evaluation with memoing (sequential version).
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Concurrent/lazy_sequential.ML
4ed308181f7f Lazy evaluation with memoing (sequential version).
wenzelm
parents:
diff changeset
     2
    Author:     Florian Haftmann and Makarius, TU Muenchen
4ed308181f7f Lazy evaluation with memoing (sequential version).
wenzelm
parents:
diff changeset
     3
34278
228f27469139 do not memoize interrupts;
wenzelm
parents: 33023
diff changeset
     4
Lazy evaluation with memoing of results and regular exceptions
228f27469139 do not memoize interrupts;
wenzelm
parents: 33023
diff changeset
     5
(sequential version).
32817
4ed308181f7f Lazy evaluation with memoing (sequential version).
wenzelm
parents:
diff changeset
     6
*)
4ed308181f7f Lazy evaluation with memoing (sequential version).
wenzelm
parents:
diff changeset
     7
4ed308181f7f Lazy evaluation with memoing (sequential version).
wenzelm
parents:
diff changeset
     8
structure Lazy: LAZY =
4ed308181f7f Lazy evaluation with memoing (sequential version).
wenzelm
parents:
diff changeset
     9
struct
4ed308181f7f Lazy evaluation with memoing (sequential version).
wenzelm
parents:
diff changeset
    10
4ed308181f7f Lazy evaluation with memoing (sequential version).
wenzelm
parents:
diff changeset
    11
(* datatype *)
4ed308181f7f Lazy evaluation with memoing (sequential version).
wenzelm
parents:
diff changeset
    12
4ed308181f7f Lazy evaluation with memoing (sequential version).
wenzelm
parents:
diff changeset
    13
datatype 'a expr =
4ed308181f7f Lazy evaluation with memoing (sequential version).
wenzelm
parents:
diff changeset
    14
  Expr of unit -> 'a |
4ed308181f7f Lazy evaluation with memoing (sequential version).
wenzelm
parents:
diff changeset
    15
  Result of 'a Exn.result;
4ed308181f7f Lazy evaluation with memoing (sequential version).
wenzelm
parents:
diff changeset
    16
4ed308181f7f Lazy evaluation with memoing (sequential version).
wenzelm
parents:
diff changeset
    17
abstype 'a lazy = Lazy of 'a expr Unsynchronized.ref
4ed308181f7f Lazy evaluation with memoing (sequential version).
wenzelm
parents:
diff changeset
    18
with
4ed308181f7f Lazy evaluation with memoing (sequential version).
wenzelm
parents:
diff changeset
    19
59194
b51489b75bb9 proper sequential version;
wenzelm
parents: 44387
diff changeset
    20
fun lazy e = Lazy (Unsynchronized.ref (Expr e));
b51489b75bb9 proper sequential version;
wenzelm
parents: 44387
diff changeset
    21
fun value a = Lazy (Unsynchronized.ref (Result (Exn.Res a)));
b51489b75bb9 proper sequential version;
wenzelm
parents: 44387
diff changeset
    22
32817
4ed308181f7f Lazy evaluation with memoing (sequential version).
wenzelm
parents:
diff changeset
    23
fun peek (Lazy r) =
4ed308181f7f Lazy evaluation with memoing (sequential version).
wenzelm
parents:
diff changeset
    24
  (case ! r of
4ed308181f7f Lazy evaluation with memoing (sequential version).
wenzelm
parents:
diff changeset
    25
    Expr _ => NONE
32844
wenzelm
parents: 32817
diff changeset
    26
  | Result res => SOME res);
32817
4ed308181f7f Lazy evaluation with memoing (sequential version).
wenzelm
parents:
diff changeset
    27
59194
b51489b75bb9 proper sequential version;
wenzelm
parents: 44387
diff changeset
    28
fun is_running _ = false;
44198
a41ea419de68 explicit check of finished evaluation;
wenzelm
parents: 43761
diff changeset
    29
fun is_finished x = is_some (peek x);
59194
b51489b75bb9 proper sequential version;
wenzelm
parents: 44387
diff changeset
    30
val finished_result = peek;
44198
a41ea419de68 explicit check of finished evaluation;
wenzelm
parents: 43761
diff changeset
    31
32817
4ed308181f7f Lazy evaluation with memoing (sequential version).
wenzelm
parents:
diff changeset
    32
4ed308181f7f Lazy evaluation with memoing (sequential version).
wenzelm
parents:
diff changeset
    33
(* force result *)
4ed308181f7f Lazy evaluation with memoing (sequential version).
wenzelm
parents:
diff changeset
    34
4ed308181f7f Lazy evaluation with memoing (sequential version).
wenzelm
parents:
diff changeset
    35
fun force_result (Lazy r) =
34278
228f27469139 do not memoize interrupts;
wenzelm
parents: 33023
diff changeset
    36
  let
228f27469139 do not memoize interrupts;
wenzelm
parents: 33023
diff changeset
    37
    val result =
228f27469139 do not memoize interrupts;
wenzelm
parents: 33023
diff changeset
    38
      (case ! r of
228f27469139 do not memoize interrupts;
wenzelm
parents: 33023
diff changeset
    39
        Expr e => Exn.capture e ()
228f27469139 do not memoize interrupts;
wenzelm
parents: 33023
diff changeset
    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
228f27469139 do not memoize interrupts;
wenzelm
parents: 33023
diff changeset
    42
  in result end;
32817
4ed308181f7f Lazy evaluation with memoing (sequential version).
wenzelm
parents:
diff changeset
    43
4ed308181f7f Lazy evaluation with memoing (sequential version).
wenzelm
parents:
diff changeset
    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
4ed308181f7f Lazy evaluation with memoing (sequential version).
wenzelm
parents:
diff changeset
    53
4ed308181f7f Lazy evaluation with memoing (sequential version).
wenzelm
parents:
diff changeset
    54
end;
4ed308181f7f Lazy evaluation with memoing (sequential version).
wenzelm
parents:
diff changeset
    55
end;
4ed308181f7f Lazy evaluation with memoing (sequential version).
wenzelm
parents:
diff changeset
    56
4ed308181f7f Lazy evaluation with memoing (sequential version).
wenzelm
parents:
diff changeset
    57
type 'a lazy = 'a Lazy.lazy;
4ed308181f7f Lazy evaluation with memoing (sequential version).
wenzelm
parents:
diff changeset
    58