| author | wenzelm | 
| Sun, 29 Jul 2007 22:41:59 +0200 | |
| changeset 24066 | fb455cb475df | 
| parent 24058 | 81aafd465662 | 
| child 24299 | 91d893799212 | 
| permissions | -rw-r--r-- | 
| 20594 | 1 | (* Title: Pure/General/susp.ML | 
| 2 | ID: $Id$ | |
| 3 | Author: Sebastian Skalberg and Florian Haftmann, TU Muenchen | |
| 4 | ||
| 5 | Delayed evaluation. | |
| 6 | *) | |
| 7 | ||
| 8 | signature SUSP = | |
| 9 | sig | |
| 10 | type 'a T | |
| 11 | val value: 'a -> 'a T | |
| 12 | val delay: (unit -> 'a) -> 'a T | |
| 13 | val force: 'a T -> 'a | |
| 14 | val peek: 'a T -> 'a option | |
| 15 | val same: 'a T * 'a T -> bool | |
| 16 | end | |
| 17 | ||
| 18 | structure Susp : SUSP = | |
| 19 | struct | |
| 20 | ||
| 21 | datatype 'a susp = | |
| 22 | Value of 'a | |
| 23 | | Delay of unit -> 'a; | |
| 24 | ||
| 25 | type 'a T = 'a susp ref; | |
| 26 | ||
| 27 | fun value v = ref (Value v); | |
| 28 | ||
| 29 | fun delay f = ref (Delay f); | |
| 30 | ||
| 24058 | 31 | fun force susp = NAMED_CRITICAL "susp" (fn () => | 
| 23922 
707639e9497d
marked some CRITICAL sections (for multithreading);
 wenzelm parents: 
20594diff
changeset | 32 | (case ! susp of | 
| 
707639e9497d
marked some CRITICAL sections (for multithreading);
 wenzelm parents: 
20594diff
changeset | 33 | Value v => v | 
| 
707639e9497d
marked some CRITICAL sections (for multithreading);
 wenzelm parents: 
20594diff
changeset | 34 | | Delay f => | 
| 20594 | 35 | let | 
| 23922 
707639e9497d
marked some CRITICAL sections (for multithreading);
 wenzelm parents: 
20594diff
changeset | 36 | val v = f (); | 
| 
707639e9497d
marked some CRITICAL sections (for multithreading);
 wenzelm parents: 
20594diff
changeset | 37 | val _ = susp := Value v; | 
| 
707639e9497d
marked some CRITICAL sections (for multithreading);
 wenzelm parents: 
20594diff
changeset | 38 | in v end)); | 
| 20594 | 39 | |
| 23922 
707639e9497d
marked some CRITICAL sections (for multithreading);
 wenzelm parents: 
20594diff
changeset | 40 | fun peek susp = | 
| 
707639e9497d
marked some CRITICAL sections (for multithreading);
 wenzelm parents: 
20594diff
changeset | 41 | (case ! susp of | 
| 
707639e9497d
marked some CRITICAL sections (for multithreading);
 wenzelm parents: 
20594diff
changeset | 42 | Value v => SOME v | 
| 
707639e9497d
marked some CRITICAL sections (for multithreading);
 wenzelm parents: 
20594diff
changeset | 43 | | Delay _ => NONE); | 
| 20594 | 44 | |
| 45 | fun same (r1 : 'a T, r2) = (r1 = r2); (*equality on references*) | |
| 46 | ||
| 47 | end |