packing a view and address in local scope

84 views
Skip to first unread message

Brandon Barker

unread,
May 13, 2014, 10:01:32 PM5/13/14
to ats-lan...@googlegroups.com
It seems that there are two basic ways to have an updated value: by using a 'var' (stack allocated variable), or using a reference ("an array with one element"). However, as noted, references have many downsides. I have seen the following kind of code:


(* Normally in a SATS file *)

abst@ype foo_abstype
= ptr
typedef foo_type = foo_abstype




(* Normally in the DATS file *)

typedef foo_concrete =
@{                                                                                                                                                    
, m = int                                                                                                                                              
, n = int                                                                                                                                              
}
assume foo_abstype
= foo_concrete


(* ****** ****** *)


local


var foo_data: foo_type
// have to initialize foo_data before making a ref
val
() = foo_data.m := 0
val
() = foo_data.n := 0
val foo
= ref_make_viewptr{foo_type}(view@foo_data | addr@foo_data)


in (*in-of-local*)

// We can use foo in functions implemented here, but not foo_data.

end (*end-of-local*)


Other than passing the 'var' to a function, is it possible to avoid using refs?

I am a bit rusty: are there other ways to pack/unpack (fold/unfold?) views with addresses other than using references? 

Using unsafe.sats with casts seems to be another option, possibly better for non-GC environments (though I guess it is not much difference in this regard as long as the refs are not created inside of functions with multiple calls)?

Basically, I would like to avoid initialization of foo_data outside of a function. Another function could be created to do the initialization and return a 'var', but just curious what is generally advised.

gmhwxi

unread,
May 13, 2014, 10:21:03 PM5/13/14
to ats-lan...@googlegroups.com

foo_data is statically allocated. So GC is a non-issue. This style of
coding is even suitable for kernel programming.

All global variables in C are essentially statically allocated refs in ATS.
A 'var' is stack-allocated, so it cannot be returned.

What is your reason for avoiding initialization outside a function?
Actually, in the C code generated from the ATS source, the initialization
code is included in the body of a function (that is called by 'dynload').

Brandon Barker

unread,
May 13, 2014, 10:29:33 PM5/13/14
to ats-lan...@googlegroups.com


On Tuesday, May 13, 2014 10:21:03 PM UTC-4, gmhwxi wrote:

foo_data is statically allocated. So GC is a non-issue. This style of
coding is even suitable for kernel programming.

All global variables in C are essentially statically allocated refs in ATS.

That is good to know.
 
A 'var' is stack-allocated, so it cannot be returned.

Makes sense 
What is your reason for avoiding initialization outside a function?
Actually, in the C code generated from the ATS source, the initialization
code is included in the body of a function (that is called by 'dynload').

I am just trying to keep code more tidy and readable by placing it in an initialization function, that is all.

gmhwxi

unread,
May 13, 2014, 10:40:38 PM5/13/14
to ats-lan...@googlegroups.com

Brandon Barker

unread,
May 14, 2014, 12:02:28 AM5/14/14
to ats-lan...@googlegroups.com
It is definitely an interesting example, although based on my prior knowledge, I should have been able to do the relevant parts without it - somehow I am always hesitant to use pointers (or linear types in general) in ATS after a while away from them. In C, pointers are a necessity, and one need only use the syntax '*' (which admittedly is probably the source of countless troubles), so I tend to get used to pointers quickly in C.

gmhwxi

unread,
May 14, 2014, 12:45:43 AM5/14/14
to ats-lan...@googlegroups.com
Allow me to go back to your original question.

abst@ype foo_abstype = ptr // @ should be dropped
typedef foo_type = foo_abstype

Normally, there should be some functions (methods) involving 'foo_type'
that are declared after the introduction of foo_abstype. If you just want to have
a global value of the type foo_type, then there is really no need to introduce
foo_abstype in the first place.

Brandon Barker

unread,
May 14, 2014, 7:58:00 AM5/14/14
to ats-lang-users

Brandon Barker
brandon...@gmail.com


---------- Forwarded message ----------
From: Brandon Barker <brandon...@gmail.com>
Date: Wed, May 14, 2014 at 7:56 AM
Subject: Re: packing a view and address in local scope
To: gmhwxi <gmh...@gmail.com>




Brandon Barker
brandon...@gmail.com


On Wed, May 14, 2014 at 12:45 AM, gmhwxi <gmh...@gmail.com> wrote:
Allow me to go back to your original question.

abst@ype foo_abstype = ptr // @ should be dropped
typedef foo_type = foo_abstype

Dropping the '@' seems problematic in my example. It is located in game.sats and game.dats with types:
board_conf_abstype = board_conf_simple
 
Normally, there should be some functions (methods) involving 'foo_type'
that are declared after the introduction of foo_abstype. If you just want to have
a global value of the type foo_type, then there is really no need to introduce
foo_abstype in the first place.

This is a good point - I'd been porting some C functions that I'd made to explicitly not use globals and instead take a couple of function arguments, but with ATS's local scope feature, I wanted to drop the function arguments. I should no longer need the abstypes in these cases.
 

--
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/7340a848-ec98-4d8a-9673-13ac241bcd2e%40googlegroups.com.


Zhiqiang Ren

unread,
May 14, 2014, 10:40:04 AM5/14/14
to ats-lan...@googlegroups.com
If we drop the @, we should change the
==========================

typedef foo_concrete =
@{                                                                                                                                                    
, m = int                                                                                                                                              
, n = int                                                                                                                                              
}
assume foo_abstype = foo_concrete
=========================
to
=========================
typedef foo_concrete =
'{                                                                                                                                                     
, m = int                                                                                                                                              
, n = int                                                                                                                                              
}
assume foo_abstype 
=
 foo_concrete
==============================
as well.

Brandon Barker

unread,
May 14, 2014, 10:52:13 AM5/14/14
to ats-lan...@googlegroups.com
Thanks - I actually did try that after my last mail (should've tried earlier), but then I got errors along these lines when trying to assign to the record values - maybe I'm doing it wrong:

The 1st translation (fixity) of [abstractTypeReferenced.dats] is successfully completed!
The 2nd translation (binding) of [abstractTypeReferenced.dats] is successfully completed!
/home/brand_000/ATStest/initalizeBeforeMakingReference/abstractTypeReferenced.dats: 419(line=32, offs=18) -- 421(line=32, offs=20): error(3): the type [S2Ecst(ptr_type)] is expected to be a tyrec (record-type).
/home/brand_000/ATStest/initalizeBeforeMakingReference/abstractTypeReferenced.dats: 419(line=32, offs=18) -- 421(line=32, offs=20): error(3): the type [S2Ecst(ptr_type)] is expected to be a tyrec (record-type).



(* ****** ****** *)
//
#include
"share/atspre_define.hats"
#include
"share/atspre_staload.hats"
//
(* ****** ****** *)





(* Normally in a SATS file *)

abstype foo_abstype
= ptr
typedef foo_type = foo_abstype




(* Normally in the DATS file *)


(* ****** ****** *)


typedef foo_concrete =
'{
, m = int
, n = int
}
assume foo_abstype = foo_concrete


(* ****** ****** *)


local


var foo_data: foo_type
val () = foo_data.m := 0
val () = foo_data.n := 0
val foo = ref_make_viewptr{foo_type}(view@foo_data | addr@foo_data)


in (*in-of-local*)


end (*end-of-local*)

Zhiqiang Ren

unread,
May 14, 2014, 11:04:35 AM5/14/14
to ats-lan...@googlegroups.com
Drop the "= ptr" as well.

Zhiqiang Ren

unread,
May 14, 2014, 11:11:06 AM5/14/14
to ats-lan...@googlegroups.com
I tried running the make, and got the following error

postiats_learn/globalvar/TextProcessing.dats: 429(line=30, offs=1) -- 461(line=30, offs=33): error(1): the file [{$GLIB}/SATS/glib.sats] is not available for staloading.

I guess it has something to do with contrib. What's the best practice to use contrib currently? (Maybe there's already a thread discussing about it, please help me find the link. Thanks)

(I use two git repositories, one for ATS-Postiats, one for ATS-Postiats-contrib. Therefore I don't want to move one repository into another, which is suggested by http://www.ats-lang.org/DOWNLOAD/#installation_atscntrb)


On Tuesday, May 13, 2014 10:40:38 PM UTC-4, gmhwxi wrote:

Brandon Barker

unread,
May 14, 2014, 11:38:12 AM5/14/14
to Zhiqiang Ren, ats-lang-users

Does this one help? https://groups.google.com/d/msgid/ats-lang-users/dcde4b29-ca89-4d3f-abb0-768a0311587d%40googlegroups.com?utm_medium=email&utm_source=footer

--
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.

Brandon Barker

unread,
May 14, 2014, 11:56:15 AM5/14/14
to Zhiqiang Ren, ats-lang-users

I read in another thread that this could cause c compilation issues:

https://groups.google.com/d/msgid/ats-lang-users/6174399b-fe88-43cb-b935-2ec008f1a196%40googlegroups.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.

gmhwxi

unread,
May 14, 2014, 12:17:38 PM5/14/14
to ats-lan...@googlegroups.com
This is how I set up my environment variables for ATS and ATS-contrib:

export POSTIATS=${HOME}/research/Postiats/git
export POSTIATS_contrib=${HOME}/research/Postiats-contrib/git

######

export PATSHOME=${POSTIATS}
export PATSHOMERELOC=${POSTIATS_contrib}

gmhwxi

unread,
May 14, 2014, 12:38:55 PM5/14/14
to ats-lan...@googlegroups.com

Not sure what you wanted to do. As I said before, you declared
an abstract type but not any values or functions creating/using values
of that abstract type. So what is the point of declaring the abstract type
in the first place?

Here is my guess:

typedef foo_concrete =
@{
, m = int
, n = int
}
assume foo_abstype = ref(foo_concrete)

local


var foo_data: foo_concrete
val () = foo_data.m := 0
val () = foo_data.n := 0
val foo = ref_make_viewptr{foo_concrete}(view@foo_data | addr@foo_data)


in (*in-of-local*)


end (*end-of-local*)

gmhwxi

unread,
May 14, 2014, 12:42:00 PM5/14/14
to ats-lan...@googlegroups.com
No, this won't solve the problem.

Brandon Barker

unread,
May 14, 2014, 12:42:06 PM5/14/14
to gmhwxi, ats-lang-users
I agree with you. I am just trying to understand better how abstype/absvtype keywords work, but I'll save that for another thread.

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.

Zhiqiang Ren

unread,
May 17, 2014, 9:55:52 PM5/17/14
to ats-lan...@googlegroups.com, Zhiqiang Ren
Not exactly. But I got clarification and documented it in
http://ats-documentation.readthedocs.org/en/latest/ats_blog/staload_var/content.html

Yannick Duchêne

unread,
May 19, 2015, 7:01:22 PM5/19/15
to ats-lan...@googlegroups.com


Le mercredi 14 mai 2014 04:21:03 UTC+2, gmhwxi a écrit :

foo_data is statically allocated. So GC is a non-issue. This style of
coding is even suitable for kernel programming.

All global variables in C are essentially statically allocated refs in ATS.
A 'var' is stack-allocated, so it cannot be returned.

 
Is this the reason why I get no error from the checker when I don't consume a value of a linear type declared in the ATS global scope?

gmhwxi

unread,
May 19, 2015, 7:03:03 PM5/19/15
to ats-lan...@googlegroups.com

Yes, toplevel linear resources are not tracked.
Reply all
Reply to author
Forward
0 new messages