A reference is just a persistent array of size 1. It is persistent in the sense that the memory associated with a reference is heap-allocated and can only be reclaimed by GC. While it is allowed to store linear values in references, doing so demands caution as resources contained in references may be inadvertently leaked.
castfn ref_get_ptr
{a:vt0p} (r: ref a):<> [l:agz] ptr (l)
castfn ref_get_viewptr
{a:vt0p} (r: ref a):<> [l:agz] (vbox (a @ l) | ptr l)
fun{a:vt0p} ref_exch_at ( r: ref (a), x: &a >> a ) : void = let val ( vbox pf | p ) = ref_get_viewptr (r) val tmp = !p in !p := x; x := tmp end // end of [ref_exch_at]Note that the type for the contents of r and x can be linear.
fun{a:vt0p} ref(x: a):<!wrt> ref(a)
fun{a:vt0p} ref_make_elt(x: a):<!wrt> ref(a)
castfn
ref_make_viewptr
{a:vt0p}{l:addr} (pf: a @ l | p: ptr l):<> ref(a)
local var __count: int = 0 // it is statically allocated val theCount = ref_make_viewptr {int} (view@(__count) | addr@(__count)) // end of [val] in (* in of [local] *) fun theCount_get (): int = !theCount fun theCount_inc (): void = !theCount := !theCount + 1 fun theCount_getinc (): int = let val n = !theCount; val () = !theCount := n + 1 in n end // end of [theCount_getind] fun theCount_reset (): void = !theCount := 0 end // end of [local]The specialty of this implementation is that it does not make use of any dynamic memory allocation. This is a style often necessary for low-level embedded systems programming.
fun{a:t0p} ref_get_elt (r: ref a):<!ref> a
fun{a:t0p} ref_set_elt (r: ref a, x: a):<!refwrt> void
fun{a:vt0p} ref_exch_elt (r: ref a, x: &a>>a):<!refwrt> void
fun{} ref_app_fun{a:vt0p} ( r: ref a, f: (&(a)>>_) -<0,!wrt> void ) :<!refwrt> void // end of [ref_app_fun]
fun{} ref_app_funenv{a:vt0p} {v:view}{vt:viewtype} ( pfv: !v | r: ref a, f: (!v | &(a)>>_, !vt) -<0,!wrt> void, env: !vt ) :<!refwrt> void // end of [ref_app_funenv]
fun{}
ref_vtakeout
{a:vt0p}
(
ref: ref (a)
) :<!ref> [l:addr] (a @ l, (a @ l) -<lin,prf> void | ptr(l))
This page is created with ATS by Hongwei Xi and also maintained by Hongwei Xi. |