
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#1197 · main · 10 min 35 s
1Étoiles
0Forks
0Issues
| 1 | /// Mutable references, scoped to regions (ADR 0002, Revision 1). |
| 2 | /// |
| 3 | /// let total = region { |
| 4 | /// let r = ref.new(0) |
| 5 | /// list.each(xs, fn(x) { ref.set(r, ref.get(r) + x) }) |
| 6 | /// ref.get(r) |
| 7 | /// } |
| 8 | /// |
| 9 | /// A reference belongs to the region `h` it was created in. Creating one |
| 10 | /// performs `alloc<h>`, using it performs `st<h>`; the region handles both. The type checker ensures no |
| 11 | /// reference escapes its region, so when the region ends the runtime |
| 12 | /// clears its references: reference cycles cannot outlive a region. |
| 13 | |
| 14 | /// A mutable cell holding an `a`, created in region `h`. |
| 15 | @builtin |
| 16 | pub type Ref<h, a> |
| 17 | |
| 18 | /// Allocating references in region `h` (only in the innermost region). |
| 19 | pub effect alloc<h> |
| 20 | { |
| 21 | fn new :: value: a -> Ref<h, a> |
| 22 | } |
| 23 | |
| 24 | /// Reading and writing references of region `h` (also from regions nested |
| 25 | /// inside it). |
| 26 | pub effect st<h> |
| 27 | { |
| 28 | fn get :: r: Ref<h, a> -> a |
| 29 | fn set :: r: Ref<h, a> value: a -> () |
| 30 | } |
| 31 | |
| 32 | /// Apply `f` to the contents of `r`. |
| 33 | pub fn update :: r: Ref<h, a> f: fn(a) -> a / <st<h> | e> -> () / <st<h> | e> |
| 34 | { |
| 35 | set r (f (get r)) |
| 36 | } |