Recursive dataviewtype with array

hello,
I struggling with making a dataviewtype that may contain a set of himself.

I started with a simple static datatype :

staload _(anonymous)=“prelude/DATS/array.dats”

datatype toto =
Titi of (string)
| {n: nat} Totos of (array(toto, n), size_t n)

extern fun make_totos(t1: toto, t2: toto, t3: toto): array(toto, 3)
extern fun print_toto(t: toto): void
extern fun test_toto(): void

implement make_totos(t1, t2, t3) = array_make_view_ptr(pf|p) where {
val (_, pf|p, sz) = $arrsz {toto}(t1, t2, t3)
}

implement print_toto(t) = case+ t of
| Titi (s) => printf("-> %s\n", @(s))
| Totos (ar, sz) => array_foreach_fun(ar, f, sz) where {
fun f .<>. (t: &toto): void = $effmask_all(print_toto t)
}

implement test_toto() = let
val totos = Totos(make_totos(Titi(“hello”), Titi(“world”), Titi("!")), 3)
in
print_toto totos
end

implement main(argc, argv) = test_toto()

I am unable to translate into a dataviewtype.
My incomplete attempt :

staload _(anonymous)=“prelude/DATS/array.dats”

dataviewtype toto_vt =
Titi_vt of (string)
| {n: nat}{l: addr} Totos_vt of (free_gc_v (toto_vt, n, l), array_v (
toto_vt, n, l) | ptr l, size_t n)

extern fun make_totos_vt(t1: toto_vt, t2: toto_vt, t3: toto_vt): [n: nat] [l
: addr] (free_gc_v (toto_vt, n, l), array_v (toto_vt, n, l) | ptr l)
extern fun print_toto_vt(t: toto_vt): void
extern fun test_toto_vt(): void

implement make_totos_vt(t1, t2, t3) = let
val (pf2gc, pf | p) = array_ptr_alloc<toto_vt>(3)
… // <- assign values to array here ?
in
(pf2gc, pf| p)
end

implement print_toto_vt(t) = case+ t of
| ~Titi_vt (s) => printf("-> %s\n", @(s))
| ~Totos_vt (pf2gc, pf| p, sz_t) => …// loop on array here and release
memory of array here

implement test_toto_vt() = let
val (pf2gc, pf | p) = make_totos_vt(Titi_vt(“hello”), Titi_vt(“world”),
Titi_vt("!"))
val totos = Totos_vt(pf2gc, pf| p, 3)
in
print_toto_vt totos
end

implement main(argc, argv) = test_toto_vt()

My problem is that most of array_v examples are dealing with static type
and not viewtypes.
For example to initialize array, the array_ptr_set_elt_at function deals
with values based on t@ype.
Any advices ?

thanks a lot
cyrille

thanks a lot !
I struggled a little bit more of translating your solution to ATS1 but
finally got it :wink:

Hi Cyrille,

There was a discussion a while back that also made use of a dataviewtype
similar to this:
https://groups.google.com/d/msg/ats-lang-users/QMpqEalEXTc/nnnV3eLrqG0J

However, in that example, I was using a pretty bad mix of GC and linear
types, so I need to double check some things - I hope to try it soon.

My first inclination is that, if you have a viewtype, you also have a type
that makes up the “type part” of the viewtype, and that you only need to
specify this, since really the array type should not depend on the address
that each pointer in the array is pointing to (I think).

I did your example in ATS2:

I have to say that using linear arrays containing linear elements can be
hard. What makes it more difficult
in this case is that the function printfree_toto also frees its argument.
If you want to see how printfree_toto
can be implemented directly (without using template), please see the
implementation of array_ptr_initialize_lst
in $ATSHOME/prelude/DATS/array.dats.