src/Pure/Concurrent/lazy.ML
changeset 59193 59f1591a11cb
parent 59191 682aa538c5c0
child 59195 f8588372d70e
equal deleted inserted replaced
59192:a1d6d6db781b 59193:59f1591a11cb
     7 *)
     7 *)
     8 
     8 
     9 signature LAZY =
     9 signature LAZY =
    10 sig
    10 sig
    11   type 'a lazy
    11   type 'a lazy
    12   val peek: 'a lazy -> 'a Exn.result option
       
    13   val is_finished: 'a lazy -> bool
       
    14   val lazy: (unit -> 'a) -> 'a lazy
    12   val lazy: (unit -> 'a) -> 'a lazy
    15   val value: 'a -> 'a lazy
    13   val value: 'a -> 'a lazy
       
    14   val peek: 'a lazy -> 'a Exn.result option
       
    15   val is_running: 'a lazy -> bool
       
    16   val is_finished: 'a lazy -> bool
       
    17   val finished_result: 'a lazy -> 'a Exn.result option
    16   val force_result: 'a lazy -> 'a Exn.result
    18   val force_result: 'a lazy -> 'a Exn.result
    17   val force: 'a lazy -> 'a
    19   val force: 'a lazy -> 'a
    18   val map: ('a -> 'b) -> 'a lazy -> 'b lazy
    20   val map: ('a -> 'b) -> 'a lazy -> 'b lazy
    19   val future: Future.params -> 'a lazy -> 'a future
    21   val future: Future.params -> 'a lazy -> 'a future
    20 end;
    22 end;
    29   Result of 'a future;
    31   Result of 'a future;
    30 
    32 
    31 abstype 'a lazy = Lazy of 'a expr Synchronized.var
    33 abstype 'a lazy = Lazy of 'a expr Synchronized.var
    32 with
    34 with
    33 
    35 
       
    36 fun lazy e = Lazy (Synchronized.var "lazy" (Expr e));
       
    37 fun value a = Lazy (Synchronized.var "lazy" (Result (Future.value a)));
       
    38 
    34 fun peek (Lazy var) =
    39 fun peek (Lazy var) =
    35   (case Synchronized.peek var of
    40   (case Synchronized.peek var of
    36     Expr _ => NONE
    41     Expr _ => NONE
    37   | Result res => Future.peek res);
    42   | Result res => Future.peek res);
    38 
    43 
    39 fun is_finished (Lazy var) =
    44 
       
    45 (* status *)
       
    46 
       
    47 fun is_future pred (Lazy var) =
    40   (case Synchronized.value var of
    48   (case Synchronized.value var of
    41     Expr _ => false
    49     Expr _ => false
       
    50   | Result res => pred res);
       
    51 
       
    52 fun is_running x = is_future (not o Future.is_finished) x;
       
    53 
       
    54 fun is_finished x =
       
    55   is_future (fn res =>
       
    56     Future.is_finished res andalso not (Exn.is_interrupt_exn (Future.join_result res))) x;
       
    57 
       
    58 fun finished_result (Lazy var) =
       
    59   (case Synchronized.value var of
       
    60     Expr _ => NONE
    42   | Result res =>
    61   | Result res =>
    43       Future.is_finished res andalso not (Exn.is_interrupt_exn (Future.join_result res)));
    62       if Future.is_finished res then
    44 
    63         let val result = Future.join_result res
    45 fun lazy e = Lazy (Synchronized.var "lazy" (Expr e));
    64         in if Exn.is_interrupt_exn result then NONE else SOME result end
    46 fun value a = Lazy (Synchronized.var "lazy" (Result (Future.value a)));
    65       else NONE);
    47 
    66 
    48 
    67 
    49 (* force result *)
    68 (* force result *)
    50 
    69 
    51 fun force_result (Lazy var) =
    70 fun force_result (Lazy var) =