The type for a plain ungarnished array containing N elements of type T is denoted by the special syntax @[T][N]. The size of this array-type is N times the size of T and its linearity coincides with the linearity of T, that is, the type @[T][N] is linear if and only if T is linear. It should be noted that a value of this form of array-type can be passed to a function call only as a call-by-reference parameter (unless certain special arrangement is made for it to be passed as a call-by-value parameter).
Please see array.sats and array.dats (plus array_bsearch.dats, array_quicksort.dats) for the SATS file and DATS files in ATSLIB where the functions in this package are declared and implemented.
viewtypedef array_v (a: viewt@ype, l:addr, n: int) = @[a][n] @ l
dataview array_v (a:vt@ype+, l:addr, int) = | array_v_nil (a, l, 0) of ((*none*)) | {n:nat} array_v_cons (a, l, n+1) of (a @ l, array_v (a, l+sizeof(a), n)) // end of [array_v]where array_v is (recursively) defined as a dataview.
exception ArraySubscriptExn of ()
praxi
lemma_array_param
{a:vt0p}{l:addr}{n:int}
(A: &(@[INV(a)][n])): [n >= 0] void
praxi
lemma_array_v_param
{a:vt0p}{l:addr}{n:int}
(pf: !array_v (INV(a), l, n)): [n >= 0] void
praxi
array_v_nil :
{a:vt0p}{l:addr} () -<prf> array_v (a, l, 0)
praxi
array_v_unnil :
{a:vt0p}{l:addr} array_v (a, l, 0) -<prf> void
prfun
array_v_unnil_nil :
{a1,a2:vt0p}{l:addr} array_v (a1, l, 0) -<prf> array_v (a2, l, 0)
praxi
array_v_cons :
{a:vt0p}{l:addr}{n:int}
(a @ l, array_v (INV(a), l+sizeof(a), n)) -<prf> array_v (a, l, n+1)
praxi
array_v_uncons :
{a:vt0p}{l:addr}{n:int | n > 0}
array_v (INV(a), l, n) -<prf> (a @ l, array_v (a, l+sizeof(a), n-1))
prfun
array_v_sing
{a:vt0p}{l:addr} (pf: INV(a) @ l): array_v (a, l, 1)
prfun
array_v_unsing
{a:vt0p}{l:addr} (pf: array_v (INV(a), l, 1)): a @ l
fun
{a:vt0p}
array_getref_at
{n:int} (A: &RD(@[INV(a)][n]), i: sizeLt n):<> cPtr1(a)
// staload UN = "prelude/SATS/unsafe.sats" // fun{a:t0p} array_get_at{n:int} ( A: &(@[a][n]), i: sizeLt n ) : a = let val p = array_getref_at<a> (A, i) in $UN.cptr_get (p) end // end of [array_get_at] fun{a:t0p} array_set_at{n:int} ( A: &(@[a][n]), i: sizeLt n, x: a ) : void = let val p = array_getref_at<a> (A, i) in $UN.cptr_set (p, x) end // end of [array_set_at]Note that unsafe functions cptr_get and cptr_set are called here to read from and write through the pointer p.
overload array_get_at with array_get_at_gint of 0
overload array_get_at with array_get_at_guint of 0
fun{
a:t0p}{tk:tk
} array_get_at_gint
{n:int}
(A: &RD(@[INV(a)][n]), i: g1intLt(tk, n)):<> a
fn tally{n:nat} ( A: &(@[double][n]), n: int n ) :<(*none*)> double = let // fun loop {i:nat | i <= n} .<n-i>. ( A: &(@[double][n]), n: int n, i: int i, res: double ) :<(*none*)> double = if n > i then loop (A, n, i+1, res + A[i]) else res // in loop (A, n, 0(*i*), 0.0(*res*)) end // end of [tally]
fun{
a:t0p}{tk:tk
} array_get_at_guint
{n:int}
(A: &RD(@[INV(a)][n]), i: g1uintLt(tk, n)):<> a
overload array_set_at with array_set_at_gint of 0
overload array_set_at with array_set_at_guint of 0
fun{
a:t0p}{tk:tk
} array_set_at_gint
{n:int}
(A: &(@[INV(a)][n]), i: g1intLt(tk, n), x: a):<!wrt> void
fn doubling{n:nat} ( A: &(@[int][n]), n: int n ) :<!wrt> void = let // fun loop {i:nat | i <= n} .<n-i>. ( A: &(@[int][n]), n: int n, i: int i ) :<!wrt> void = if n > i then (A[i] := 2 * A[i]; loop (A, n, i+1)) else () // in loop (A, n, 0) end // end of [doubling]
fun{
a:t0p}{tk:tk
} array_set_at_guint
{n:int}
(A: &(@[INV(a)][n]), i: g1uintLt(tk, n), x: a):<!wrt> void
overload array_exch_at with array_exch_at_gint of 0
overload array_exch_at with array_exch_at_guint of 0
fun{
a:vt0p}{tk:tk
} array_exch_at_gint{n:int}
(
A: &(@[INV(a)][n]), i: g1intLt (tk, n), x: &a >> _
) :<!wrt> void
fun{
a:vt0p}{tk:tk
} array_exch_at_guint{n:int}
(
A: &(@[INV(a)][n]), i: g1uintLt (tk, n), x: &a >> _
) :<!wrt> void
fun {a:vt0p} array_interchange {n:int} ( A: &(@[INV(a)][n]), i: sizeLt (n), j: sizeLt (n) ) :<!wrt> void // end of [array_interchange]
fn{a:t0p} insertion_sort {n:int} ( A: &(@[a][n]), n: size_t n ) :<!wrt> void = let // fun loop1 {i:nat | i < n} .<i>. ( A: &(@[a][n]), i: size_t i ) :<!wrt> void = if i > 0 then let val i1 = pred (i) val sgn = gcompare_ref<a> (A.[i1], A.[i]) in if sgn > 0 then ( array_interchange (A, i1, i); loop1 (A, i1) ) // end of [if] end else () // end of [if] // fun loop2 {i:nat | i <= n} .<n-i>. ( A: &(@[a][n]), n: size_t n, i: size_t i ) :<!wrt> void = if i < n then (loop1 (A, i); loop2 (A, n, succ(i))) // in // if n >= 2 then loop2 (A, n, g1i2u(1)) else ((*exit*)) // end // end of [insertion_sort]
fun {a:vt0p} array_subcirculate {n:int} ( A: &(@[INV(a)][n]), i: sizeLt (n), j: sizeLt (n) ) :<!wrt> void // end of [array_subcirculate]
fun {a:vt0p} array_ptr_alloc {n:int} ( asz: size_t n ) :<!wrt> [l:agz] ( array_v (a?, l, n), mfree_gc_v (l) | ptr l ) (* end of [array_ptr_alloc] *)
fun {(*void*)} array_ptr_free {a:vt0p}{l:addr}{n:int} ( array_v (a?, l, n), mfree_gc_v (l) | ptr l ) :<!wrt> void // end-of-function
fun
{a:vt0p}
array_ptr_tabulate
{n:int}
(
asz: size_t(n)
) : [l:addr] (array_v(a, l, n), mfree_gc_v(l) | ptr(l))
fun
{a:vt0p}
array_tabulate$fopr(i: size_t): (a)
overload fprint_array with fprint_array_int
overload fprint_array with fprint_array_size
fun
{(*void*)}
fprint_array$sep (out: FILEref): void
fun {a:vt0p} fprint_array_sep{n:int} ( out: FILEref , A: &RD(@[INV(a)][n]), n: size_t n, sep: NSH(string) ) : void // end of [fprint_array_sep]
fun{ a:vt0p } array_foreach{n:int} ( A: &(@[INV(a)][n]) >> @[a][n], asz: size_t(n) ) : sizeLte(n) // end of [array_foreach]
fun{a:t@ype} array_find{n:int} ( A: &(@[a][n]) >> @[a][n], n: size_t n, p: (a) -> bool ) : Option_vt (sizeLt(n)) = let // implement(env) array_foreach$cont<a><env> (x, env) = ~p(x) implement(env) array_foreach$fwork<a><env> (x, env) = ((*nothing*)) val i = array_foreach (A, n) // in if n > i then Some_vt (i) else None_vt () end // end of [array_find]
fun{ a:vt0p}{env:vt0p } array_foreach_env{n:int} ( A: &(@[INV(a)][n]) >> @[a][n], asz: size_t(n), env: &(env) >> _ ) : sizeLte(n) // end of [array_foreach_env]
fun{ a:t0p}{res:t0p } array_foldleft{n:int} ( A: &(@[a][n]) , asz: size_t n, ini: res, f: (res, a) -> res ) : res = let // var env: res = ini // implement array_foreach$fwork<a><res> (x, env) = env := f (env, x) // val _(*asz*) = array_foreach_env (A, asz, env) // in env end // end of [array_foldleft]
fun{
a:vt0p}{env:vt0p
} array_foreach$cont (x: &a, env: &env): bool
fun{
a:vt0p}{env:vt0p
} array_foreach$fwork (x: &a >> _, env: &(env) >> _): void
fun
{a:vt0p}
array_foreach_funenv
{v:view}
{vt:vtype}
{n:int}
{fe:eff}
(
pfv: !v
| A: &(@[INV(a)][n]) >> @[a][n]
, asz: size_t n
, f: (!v | &a >> _, !vt) -<fun,fe> void
, env: !vt
) :<fe> void
fun {a:vt0p} array_foreach_fun {n:int}{fe:eff} ( &(@[INV(a)][n]) >> @[a][n] , size_t (n), (&a >> _) -<fun,fe> void ) :<fe> void // end of [array_foreach_fun]
fun{ a1,a2:vt0p } array_foreach2 {n:int} ( A1: &(@[INV(a1)][n]) >> @[a1][n] , A2: &(@[INV(a2)][n]) >> @[a2][n] , asz: size_t (n) ) : sizeLte(n) // end of [array_foreach2]
fun{ a1,a2:vt0p}{env:vt0p } array_foreach2_env {n:int} ( A1: &(@[INV(a1)][n]) >> @[a1][n] , A2: &(@[INV(a2)][n]) >> @[a2][n] , asz:size_t (n) , env: &(env) >> env ) : sizeLte(n) // end of [array_foreach2_env]
fun{
a1,a2:vt0p}{env:vt0p
} array_foreach2$cont
(x1: &a1, x2: &a2, env: &env): bool
fun{
a1,a2:vt0p}{env:vt0p
} array_foreach2$fwork
(x1: &a1 >> _, x2: &a2 >> _, env: &(env) >> _): void
fun{ a:vt0p } array_rforeach{n:int} ( A: &(@[INV(a)][n]) >> @[a][n], asz: size_t(n) ) : sizeLte(n) // end of [array_rforeach]
fun{a:t@ype} array_rfind{n:int} ( A: &(@[a][n]) >> @[a][n], n: size_t n, p: (a) -> bool ) : Option_vt (sizeLt(n)) = let // implement(env) array_rforeach$cont<a><env> (x, env) = ~p(x) implement(env) array_rforeach$fwork<a><env> (x, env) = ((*nothing*)) val i = array_rforeach (A, n) // in if n > i then Some_vt (pred(n)-i) else None_vt () end // end of [array_rfind]
fun{ a:vt0p}{env:vt0p } array_rforeach_env{n:int} ( A: &(@[INV(a)][n]) >> @[a][n], asz: size_t(n), env: &(env) >> _ ) : sizeLte(n) // end of [array_rforeach_env]
fun{ a:t0p}{res:t0p } array_foldright{n:int} ( A: &(@[a][n]) , asz: size_t n, f: (a, res) -> res, snk: res ) : res = let // var env: res = snk // implement array_rforeach$fwork<a><res> (x, env) = env := f (x, env) // val _(*asz*) = array_rforeach_env<a><env> (A, asz, env) // in env end // end of [array_foldright]Please note that array_rforeach_env is given a tail-recursive implementation in ATSLIB.
fun{
a:vt0p}{env:vt0p
} array_rforeach$cont(x: &a, env: &env): bool
fun{
a:vt0p}{env:vt0p
} array_rforeach$fwork(x: &a >> _, env: &(env) >> _): void
fun{a:vt0p} array_initize{n:int} ( A: &(@[a?][n]) >> @[a][n], asz: size_t(n) ) : void // end of [array_initize]
fun{a:vt0p}
array_initize$init (i: size_t, x: &a? >> a): void
fun{a:t0p} array_initize_elt{n:int} ( A: &(@[a?][n]) >> @[a][n], asz: size_t n, elt: (a) ) :<!wrt> void // end of [array_initize_elt]
fun{a:t0p} array_initize_list{n:int} ( A: &(@[a?][n]) >> @[a][n], asz: int n, xs: list(INV(a), n) ) :<!wrt> void // end of [array_initize_list]
fun{a:t0p} array_initize_rlist{n:int} ( A: &(@[a?][n]) >> @[a][n], asz: int n, xs: list(INV(a), n) ) :<!wrt> void // end of [array_initize_rlist]
fun{a:vt0p} array_initize_list_vt{n:int} ( A: &(@[a?][n]) >> @[a][n], asz: int n, xs: list_vt(INV(a), n) ) :<!wrt> void // end of [array_initize_list_vt]
fun{a:vt0p} array_initize_rlist_vt{n:int} ( A: &(@[a?][n]) >> @[a][n], asz: int n, xs: list_vt(INV(a), n) ) :<!wrt> void // end of [array_initize_rlist_vt]
fun {a:vt0p} array_uninitize {n:int} ( A: &(@[INV(a)][n]) >> @[a?][n], asz: size_t n ) : void // end of [array_uninitize]
fun{a:vt0p}
array_uninitize$clear(i: size_t, x: &a >> a?): void
fun
{a:vt0p}
array_bsearch
{n:int}
(A: &RD(@[a][n]), n: size_t(n)):<> sizeLte(n)
fun{a:vt0p}
array_bsearch$ford (x: &RD(a)):<> int
fun {a:vt0p} array_bsearch_stdlib {n:int} ( A: &RD(@[a][n]), asz: size_t (n), key: &RD(a), cmp: cmpref(a) ) :<> Ptr0 (* found/~found : ~null/null *)
fun {a:vt0p} array_quicksort {n:int} ( A: &(@[INV(a)][n]) >> @[a][n], n: size_t n ) :<!wrt> void // end-of-function
fun{a:vt0p} array_quicksort$cmp(x1: &RD(a), x2: &RD(a)):<> int(*sgn*)
implement{a}
array_quicksort$cmp (x, y) = gcompare_ref<a> (x, y)
fun {a:vt0p} array_quicksort_stdlib {n:int} ( A: &(@[INV(a)][n]) >> @[a][n], n: size_t(n), cmp: cmpref(a) ) :<!wrt> void // end of [array_quicksort_stdlib]
fun{ a:vt0p}{b:vt0p } array_mapto{n:int} ( A: &array(INV(a), n) , B: &array(b?, n) >> array (b, n) , n: size_t (n) ) : void // end of [array_mapto]
fun{
a:vt0p}{b:vt0p
} array_mapto$fwork(x: &a, y: &b? >> b): void
fun{ a,b:vt0p}{c:vt0p } array_map2to{n:int} ( A: &array(INV(a), n) , B: &array(INV(b), n) , C: &array(c?, n) >> array (c, n) , n: size_t (n) ) : void // end of [array_map2to]
fun{
a,b:vt0p}{c:vt0p
} array_map2to$fwork(x: &a, y: &b, z: &c? >> c): void
fun{a:vt0p}
array_permute{n:int}
(A: &(@[INV(a)][n]) >> @[a][n], n: size_t(n)): void
implement main0 () = { // #define N 5 val asz = g1i2u (N) val out = stdout_ref // val (pf, pfgc | p) = array_ptr_alloc<int> (asz) // implement array_initize$init<int> (i, x) = x := g0u2i(i)+1 val () = array_initize<int> (!p, asz) // array: 1, 2, ..., N-1, N // val ( ) = fprint_array_sep (out, !p, asz, ",") val () = fprint_newline (out) // implement array_permute$randint<> (n) = pred(n) // this is not random val () = array_permute<int> (!p, asz) // array: N, 1, 2, ..., N-1 // val ( ) = fprint_array_sep (out, !p, asz, ",") val () = fprint_newline (out) // val () = array_ptr_free (pf, pfgc | p) // } // end of [main0]Note that the created array is properly freed before the code exits.
fun{(*void*)}
array_permute$randint{n:int | n > 0}(size_t(n)): sizeLt(n)
overload [] with array_get_at_gint of 0
overload [] with array_get_at_guint of 0
overload [] with array_set_at_gint of 0
overload [] with array_set_at_guint of 0
This page is created with ATS by Hongwei Xi and also maintained by Hongwei Xi. |