| author | huffman | 
| Tue, 13 Jan 2009 10:18:23 -0800 | |
| changeset 29475 | c06d1b0a970f | 
| parent 29422 | fdf396a24a9f | 
| child 32738 | 15bb09ca0378 | 
| permissions | -rw-r--r-- | 
| 28673 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 1 | (* Title: Pure/General/lazy.ML | 
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 2 | Author: Florian Haftmann and Makarius, TU Muenchen | 
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 3 | |
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 4 | Lazy evaluation with memoing. Concurrency may lead to multiple | 
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 5 | attempts of evaluation -- the first result persists. | 
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 6 | *) | 
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 7 | |
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 8 | signature LAZY = | 
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 9 | sig | 
| 28971 | 10 | type 'a lazy | 
| 11 | val same: 'a lazy * 'a lazy -> bool | |
| 12 | val lazy: (unit -> 'a) -> 'a lazy | |
| 13 | val value: 'a -> 'a lazy | |
| 14 | val peek: 'a lazy -> 'a Exn.result option | |
| 29422 | 15 | val force_result: 'a lazy -> 'a Exn.result | 
| 28971 | 16 | val force: 'a lazy -> 'a | 
| 17 |   val map_force: ('a -> 'a) -> 'a lazy -> 'a lazy
 | |
| 28673 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 18 | end | 
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 19 | |
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 20 | structure Lazy :> LAZY = | 
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 21 | struct | 
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 22 | |
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 23 | (* datatype *) | 
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 24 | |
| 28971 | 25 | datatype 'a T = | 
| 28673 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 26 | Lazy of unit -> 'a | | 
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 27 | Result of 'a Exn.result; | 
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 28 | |
| 28971 | 29 | type 'a lazy = 'a T ref; | 
| 28673 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 30 | |
| 28971 | 31 | fun same (r1: 'a lazy, r2) = r1 = r2; | 
| 28673 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 32 | |
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 33 | fun lazy e = ref (Lazy e); | 
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 34 | fun value x = ref (Result (Exn.Result x)); | 
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 35 | |
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 36 | fun peek r = | 
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 37 | (case ! r of | 
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 38 | Lazy _ => NONE | 
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 39 | | Result res => SOME res); | 
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 40 | |
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 41 | |
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 42 | (* force result *) | 
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 43 | |
| 29422 | 44 | fun force_result r = | 
| 28673 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 45 | let | 
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 46 | (*potentially concurrent evaluation*) | 
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 47 | val res = | 
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 48 | (case ! r of | 
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 49 | Lazy e => Exn.capture e () | 
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 50 | | Result res => res); | 
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 51 | (*assign result -- first one persists*) | 
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 52 | val res' = NAMED_CRITICAL "lazy" (fn () => | 
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 53 | (case ! r of | 
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 54 | Lazy _ => (r := Result res; res) | 
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 55 | | Result res' => res')); | 
| 29422 | 56 | in res' end; | 
| 57 | ||
| 58 | fun force r = Exn.release (force_result r); | |
| 28673 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 59 | |
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 60 | fun map_force f = value o f o force; | 
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 61 | |
| 
d746a8c12c43
renamed structure Susp to Lazy, and Susp.delay to Lazy.lazy;
 wenzelm parents: diff
changeset | 62 | end; | 
| 28971 | 63 | |
| 64 | type 'a lazy = 'a Lazy.lazy; | |
| 65 |