Merging stream_vt

47 views
Skip to first unread message

aditya siram

unread,
Jan 2, 2019, 1:10:48 AM1/2/19
to ats-lang-users
I trying to understand the stream_vt datatype by writing a function that merges two streams in a stream of tuples but not having much luck, the following:

fun merge
 
{a: t@ype}
 
{b: t@ype}
 
(
    s1
: stream_vt a,
    s2
: stream_vt b
 
) : stream_vt(@(a,b)) =
  let
    val _s1
= !s1
    val _s2
= !s2
 
in
    $ldelay
     
(
     
case+ (_s1,_s2) of
     
| (~stream_vt_cons(_s1, _s1s),
         
~stream_vt_cons(_s2, _s2s)) =>
             stream_vt_cons
((_s1,_s2), merge(_s1s,_s2s))
     
| (_,_) => stream_vt_nil()
     
)
 
end

gives me the errors:

...: 465(line=27, offs=9) -- 590(line=29, offs=57): error(3): the dynamic variable [_s2$4720(-1)] is consumed but it should be retained with the type [S2Eapp(S2Ecst(stream_vt_con); S2Evar(b(8451)))] instead.
...: 465(line=27, offs=9) -- 590(line=29, offs=57): error(3): the dynamic variable [_s1$4719(-1)] is consumed but it should be retained with the type [S2Eapp(S2Ecst(stream_vt_con); S2Evar(a(8450)))] instead.
...: 368(line=20, offs=3) -- 636(line=32, offs=6): error(3): the linear dynamic variable [_s1$4719(-1)] needs to be consumed but it is preserved with the type [S2Eapp(S2Ecst(stream_vt_con); S2Evar(a(8450)))] instead.
...: 368(line=20, offs=3) -- 636(line=32, offs=6): error(3): the linear dynamic variable [_s2$4720(-1)] needs to be consumed but it is preserved with the type [S2Eapp(S2Ecst(stream_vt_con); S2Evar(b(8451)))] instead.
patsopt
(TRANS3): there are [4] errors in total.
exit(ATS): uncaught exception: _2home_2deech_2ATS_2triples_2dats_2ATS_2ATS2_2src_2pats_error_2esats__FatalErrorExn(1025)

Any help is appreciated.

Thanks!

Artyom Shalkhakov

unread,
Jan 2, 2019, 2:21:24 AM1/2/19
to ats-lang-users
Hello Aditya!

I've modified this a bit by inserting freeing:

fun merge
  {a: t@ype}
  {b: t@ype}
  (
    s1: stream_vt a,
    s2: stream_vt b
  ) : stream_vt(@(a,b)) =
  let
    val _s1 = !s1
    val _s2 = !s2
  in
    $ldelay
     (
      (
      case+ (_s1,_s2) of
      | (~stream_vt_cons(_s1e, _s1s),
         ~stream_vt_cons(_s2e, _s2s)) =>
             stream_vt_cons(@(_s1e,_s2e), merge(_s1s,_s2s))
      | (~stream_vt_nil (), _) => (stream_vt_con_free(_s2); stream_vt_nil())
      | (_, ~stream_vt_nil ()) => (stream_vt_con_free(_s1); stream_vt_nil())
      )
     , (stream_vt_con_free(_s1); stream_vt_con_free(_s2))
     )
  end

This type-checks (but I have not run it).

$ldelay requires us to supply as its second argument an expression that will free up all resources that are used in its first argument.

ср, 2 янв. 2019 г. в 08:10, aditya siram <aditya...@gmail.com>:
--
You received this message because you are subscribed to the Google Groups "ats-lang-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ats-lang-user...@googlegroups.com.
To post to this group, send email to ats-lan...@googlegroups.com.
Visit this group at https://groups.google.com/group/ats-lang-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/ats-lang-users/21f9263f-e0be-4dcb-8bf6-45031eaea85a%40googlegroups.com.


--
Cheers,
Artyom Shalkhakov

aditya siram

unread,
Jan 2, 2019, 2:41:24 AM1/2/19
to ats-lang-users
Awesome. That does typecheck but when I try to build it with:

"$PATSHOME/bin/patscc" -O3 -flto -s -D_GNU_SOURCE -DATS_MEMALLOC_LIBC -I${PATSHOME}/contrib -O3 -o triples triples.dats -latslib



I get:

/home/deech/ATS/test/ATS/ATS2/ccomp/runtime/pats_ccomp_instrset.h:276:35: error: assignment to expression with array type
 
#define ATSINSmove(tmp, val) (tmp = val)
                                   
^
triples_dats
.c:1062:1: note: in expansion of macro ATSINSmove
 
ATSINSmove(tmp19, ATSSELcon(env0, postiats_tysum_3, atslab__0)) ;
 
^~~~~~~~~~
/home/deech/ATS/test/ATS/ATS2/ccomp/runtime/pats_ccomp_instrset.h:276:35: error: assignment to expression with array type
 
#define ATSINSmove(tmp, val) (tmp = val)
                                   
^
triples_dats
.c:1070:1: note: in expansion of macro ATSINSmove
 
ATSINSmove(tmp21, ATSSELcon(env1, postiats_tysum_4, atslab__0)) ;
 
^~~~~~~~~~
/home/deech/ATS/test/ATS/ATS2/ccomp/runtime/pats_ccomp_instrset.h:327:65: error: assignment to expression with array type
 
#define ATSINSstore_fltrec_ofs(tmp, tyrec, lab, val) ((tmp).lab = val)
                                                                 
^
triples_dats
.c:1093:1: note: in expansion of macro ATSINSstore_fltrec_ofs
 
ATSINSstore_fltrec_ofs(tmp23, postiats_tyrec_2, atslab__0, tmp19) ;
 
^~~~~~~~~~~~~~~~~~~~~~
/home/deech/ATS/test/ATS/ATS2/ccomp/runtime/pats_ccomp_instrset.h:327:65: error: assignment to expression with array type
 
#define ATSINSstore_fltrec_ofs(tmp, tyrec, lab, val) ((tmp).lab = val)
                                                                 
^
triples_dats
.c:1094:1: note: in expansion of macro ATSINSstore_fltrec_ofs
 
ATSINSstore_fltrec_ofs(tmp23, postiats_tyrec_2, atslab__1, tmp21) ;
 
^~~~~~~~~~~~~~~~~~~~~~

Artyom Shalkhakov

unread,
Jan 2, 2019, 3:00:17 AM1/2/19
to ats-lang-users
ср, 2 янв. 2019 г. в 09:41, aditya siram <aditya...@gmail.com>:
Awesome. That does typecheck but when I try to build it with:

"$PATSHOME/bin/patscc" -O3 -flto -s -D_GNU_SOURCE -DATS_MEMALLOC_LIBC -I${PATSHOME}/contrib -O3 -o triples triples.dats -latslib



I don't have a compiler with me here but it seems like the function should be made into a template (i.e. the type parameters should be put prior to the function's name in the declaration).

This typechecks and the online compiler will accept it:

fun test (): void = {
  val xs = stream_vt_make_cons ((g0ofg1)1, stream_vt_make_cons ((g0ofg1)2, stream_vt_make_nil{int} ()))
 
  val ys = stream_vt_make_cons ((g0ofg1)"x", stream_vt_make_cons ((g0ofg1)"y", stream_vt_make_nil{string} ()))

  val zs = merge<int><string> (xs, ys)
 
  val l = stream2list_vt (zs)
  val () = println!(l) // should print something like: 1,x; 2,y

  val () = list_vt_free(l)
}

Unfortunately I can't run it (the glot.io-provided compiler doesn't support stream_vt_make_* functions).



--
Cheers,
Artyom Shalkhakov

Richard

unread,
Jan 2, 2019, 10:34:59 AM1/2/19
to ats-lang-users
Yes, this compiles and runs as expected. Although, I am not sure that I have ever used 'stream_vt_con_free'. A slightly different way to do the same without using stream_vt_con_free,



#include "share/atspre_staload.hats"

extern fun {a:t@ype}{b:t@ype}
  merge
: (stream_vt a, stream_vt b) -> stream_vt(@(a,b))

implement
{a}{b} merge (xs, ys) = let
fun
auxmain
(xs: stream_vt a, ys: stream_vt b) : stream_vt(@(a,b)) = $ldelay
(
(
 
case+ !xs of
 
| ~stream_vt_nil() => (~ys; stream_vt_nil())
 
| ~stream_vt_cons(x, xs) =>
   
case+ !ys of
   
| ~stream_vt_nil() => (~xs; stream_vt_nil())
   
| ~stream_vt_cons(y, ys) =>
      stream_vt_cons
(@(x, y), auxmain(xs, ys))
) ,
(
 
~xs; ~ys
)
)
in
  auxmain
(xs, ys)
end




To test,

implement main0() =
{
  val xs
= streamize_list_vt_elt($list_vt{int}(1, 2))
  val ys
= streamize_list_vt_elt($list_vt{string}("x", "y"))

Artyom Shalkhakov

unread,
Jan 3, 2019, 2:52:36 AM1/3/19
to ats-lang-users
Hi Richard,

ср, 2 янв. 2019 г. в 17:35, Richard <qpo...@gmail.com>:
Yes, this compiles and runs as expected. Although, I am not sure that I have ever used 'stream_vt_con_free'. A slightly different way to do the same without using stream_vt_con_free,



#include "share/atspre_staload.hats"

extern fun {a:t@ype}{b:t@ype}
  merge
: (stream_vt a, stream_vt b) -> stream_vt(@(a,b))

implement
{a}{b} merge (xs, ys) = let
fun
auxmain
(xs: stream_vt a, ys: stream_vt b) : stream_vt(@(a,b)) = $ldelay
(
(
 
case+ !xs of
 
| ~stream_vt_nil() => (~ys; stream_vt_nil())
 
| ~stream_vt_cons(x, xs) =>
   
case+ !ys of
   
| ~stream_vt_nil() => (~xs; stream_vt_nil())
   
| ~stream_vt_cons(y, ys) =>
      stream_vt_cons
(@(x, y), auxmain(xs, ys))
) ,
(
 
~xs; ~ys
)
)
in
  auxmain
(xs, ys)
end




My guess is that the prefix ~ operator is the "stream_vt_free". :-) I did not know it has an operator attached to it, thanks for pointing it out!
 


--
Cheers,
Artyom Shalkhakov
Reply all
Reply to author
Forward
0 new messages