same linear variable in an argument

34 views
Skip to first unread message

chotu s

unread,
Mar 13, 2014, 10:01:35 AM3/13/14
to ats-lan...@googlegroups.com
Hello,

Can we pass same linear var in argument of a fun , for example :


#include "share/atspre_define.hats"
#include "share/atspre_staload.hats"


extern
fun{a:t@ype}
some_fun{n:nat} (v1 : !arrayptr(a,n) , v2 : !arrayptr(a,n)) : void
 

implement main0 () = {
  val v = arrayptr($arrpsz{int}(1,2,3,4,5))
  val () = some_fun<int> (v,v)
  val () = arrayptr_free (v)
}


Above code does not compile , and complain for 2nd arg , which is no more available

How to write above code in ATS2

Thanks

Brandon Barker

unread,
Mar 13, 2014, 10:25:14 AM3/13/14
to chotu s, ats-lan...@googlegroups.com
I think it makes sense that you can not pass (by value) the same linear argument more than once in the same function call (you would essentially have two "active views" for the same memory location); probably what you want is to make an explicit copy when needed and pass it as well.

Brandon Barker
brandon...@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.
To view this discussion on the web visit https://groups.google.com/d/msgid/ats-lang-users/CAAZxu4zLZDhBG02p%3D4d4hrYAMM1n1-x8CdyO07mGB_%3DLAVYYKg%40mail.gmail.com.

chotu s

unread,
Mar 13, 2014, 10:46:11 AM3/13/14
to Brandon Barker, ats-lan...@googlegroups.com
I thought so  . Is there any other way , without copy , say if copy is costly.


Brandon Barker

unread,
Mar 13, 2014, 11:15:58 AM3/13/14
to chotu s, ats-lan...@googlegroups.com
I can't think of a trivial (and safe) way to do it; one way might be to use an option type for the vectors, and v2 is none, just access it as v1 inside the function. I admit this is a rough idea and I have not tested it.

An unsafe way might be to just pass pointers and use unsafe functions to cast.


Brandon Barker
brandon...@gmail.com

Brandon Barker

unread,
Mar 13, 2014, 11:19:50 AM3/13/14
to chotu s, ats-lan...@googlegroups.com
Another thing that should work in principle is if we specify both vectors are read-only, and while possible, I think this is a little more involved in ATS (and I don't think I've tried it). 

Brandon Barker
brandon...@gmail.com

Brandon Barker

unread,
Mar 13, 2014, 11:24:48 AM3/13/14
to chotu s, ats-lan...@googlegroups.com
Here (I believe) is some not so light reading on the topic: http://www.cs.bu.edu/~hwxi/academic/papers/ictac10.pdf
It does have a few examples.

Unfortunately I can't recall if I've seen any other example code.

Brandon Barker
brandon...@gmail.com

chotu s

unread,
Mar 13, 2014, 11:25:51 AM3/13/14
to Brandon Barker, ats-lan...@googlegroups.com
Yes , they are read only in my original code they are not modified by the function.

chotu s

unread,
Mar 13, 2014, 11:26:26 AM3/13/14
to Brandon Barker, ats-lan...@googlegroups.com
Thanks Brandon , I will take a look at it.

gmhwxi

unread,
Mar 13, 2014, 12:04:51 PM3/13/14
to ats-lan...@googlegroups.com

Here is a way to do it. It is not type-safe but safe enough :)

//

extern
fun
{
a
:t@ype
} some_fun{n:nat}

 
(!arrayptr (a, n), !arrayptr (a, n)): void
//
(* ****** ****** *)

implement
main0
((*void*)) =
{
//
val A
=
(arrayptr)$arrpsz{int}(0, 1)
//
val
(fpf | A2) =
  decode
($vcopyenv_vt(A))
val
() = some_fun<int> (A, A2)
prval
((*void*)) = fpf (A2)
//
val
() = arrayptr_free (A)
//
} (* end of [main0] *)


Message has been deleted

gmhwxi

unread,
Mar 13, 2014, 12:13:12 PM3/13/14
to ats-lan...@googlegroups.com, Brandon Barker
I wrote plenty code involving read-only views.

However, it is very cumbersome to program with read-only views,
and the benefits from doing it seem rather limited. So there is no
longer built-in support for read-only views in ATS2.

However, at a higher-level, read-only views (and other data protection
concepts) can be very useful in system design.

Brandon Barker

unread,
Mar 13, 2014, 12:18:24 PM3/13/14
to gmhwxi, ats-lan...@googlegroups.com
Ah OK, I hadn't realized they were removed. I'm not sure what is meant by "at a higher level". 

Brandon Barker
brandon...@gmail.com

gmhwxi

unread,
Mar 13, 2014, 12:31:22 PM3/13/14
to ats-lan...@googlegroups.com, gmhwxi

Say you want to implement a read/write buffer shared by multiple threads.

Brandon Barker

unread,
Mar 13, 2014, 6:02:24 PM3/13/14
to Hongwei Xi, ats-lan...@googlegroups.com

So in ATS2, I presume this wouldn't be done with specialized  views, but rather read/write functions?

gmhwxi

unread,
Mar 13, 2014, 11:27:28 PM3/13/14
to ats-lan...@googlegroups.com, Hongwei Xi
One can define read-only views on one's own.
Reply all
Reply to author
Forward
0 new messages