# Packing a view and address in local scope

**URL:** <https://discourse.ats-lang.org/t/packing-a-view-and-address-in-local-scope/218>\
**Category:** Google Group\
**Created:** [May 14, 2014, 2:01am UTC](https://discourse.ats-lang.org/t/packing-a-view-and-address-in-local-scope/218 "2014-05-14T02:01:32Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brandon\_Barker](https://sea1.discourse-cdn.com/flex019/user_avatar/discourse.ats-lang.org/brandon_barker/32/61_2.png) [@Brandon\_Barker](https://discourse.ats-lang.org/u/Brandon_Barker)\
**Post date:** [May 14, 2014, 2:01am UTC](https://discourse.ats-lang.org/t/packing-a-view-and-address-in-local-scope/218/1 "2014-05-14T02:01:32Z")

</div>

It seems that there are two basic ways to have an updated value: by using a ‘var’  
(stack allocated variable)[http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x3377.html](http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x3377.html),  
or using a reference (“an array with one element”)[http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x1476.html](http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x1476.html).  
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.

---

<div class="post-metadata">

**Author:** ![Zhiqiang\_Ren](https://avatars.discourse-cdn.com/v4/letter/z/9fc29f/32.png) [@Zhiqiang\_Ren](https://discourse.ats-lang.org/u/Zhiqiang_Ren)\
**Post date:** [May 14, 2014, 3:11pm UTC](https://discourse.ats-lang.org/t/packing-a-view-and-address-in-local-scope/218/2 "2014-05-14T15:11:06Z")

</div>

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](http://www.ats-lang.org/DOWNLOAD/#installation_atscntrb))On Tuesday, May 13, 2014 10:40:38 PM UTC-4, gmhwxi wrote:

> I suggest that you try:
> 
> [https://github.com/githwxi/ATS-Postiats-contrib/blob/master/contrib/libats-/hwxi/globals/HATS/globvar.hats](https://github.com/githwxi/ATS-Postiats-contrib/blob/master/contrib/libats-/hwxi/globals/HATS/globvar.hats)
> 
> Here is an example of using globvar:
> 
> [https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/GTK3/TextProcessing/](https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/GTK3/TextProcessing/)
> 
> I think you will like it 🙂
> 
> > >

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [May 14, 2014, 4:45am UTC](https://discourse.ats-lang.org/t/packing-a-view-and-address-in-local-scope/218/3 "2014-05-14T04:45:43Z")

</div>

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.On Wednesday, May 14, 2014 12:02:28 AM UTC-4, Brandon Barker wrote:

> 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.
> 
> On Tuesday, May 13, 2014 10:40:38 PM UTC-4, gmhwxi wrote:
> 
> > I suggest that you try:
> > 
> > [https://github.com/githwxi/ATS-Postiats-contrib/blob/master/contrib/libats-/hwxi/globals/HATS/globvar.hats](https://github.com/githwxi/ATS-Postiats-contrib/blob/master/contrib/libats-/hwxi/globals/HATS/globvar.hats)
> > 
> > Here is an example of using globvar:
> > 
> > [https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/GTK3/TextProcessing/](https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/GTK3/TextProcessing/)
> > 
> > I think you will like it 🙂
> > 
> > On Tuesday, May 13, 2014 10:29:33 PM UTC-4, Brandon Barker wrote:
> > 
> > > 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.
> > > 
> > > > On Tuesday, May 13, 2014 10:01:32 PM UTC-4, Brandon Barker wrote:
> > > > 
> > > > > It seems that there are two basic ways to have an updated value: by  
> > > > > using a ‘var’ (stack allocated variable)[http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x3377.html](http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x3377.html),  
> > > > > or using a reference (“an array with one element”)[http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x1476.html](http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x1476.html).  
> > > > > 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.

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [May 14, 2014, 2:21am UTC](https://discourse.ats-lang.org/t/packing-a-view-and-address-in-local-scope/218/4 "2014-05-14T02:21:03Z")

</div>

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’).On Tuesday, May 13, 2014 10:01:32 PM UTC-4, Brandon Barker wrote:

> It seems that there are two basic ways to have an updated value: by using  
> a ‘var’ (stack allocated variable)[http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x3377.html](http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x3377.html),  
> or using a reference (“an array with one element”)[http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x1476.html](http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x1476.html).  
> 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.

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [May 19, 2015, 11:03pm UTC](https://discourse.ats-lang.org/t/packing-a-view-and-address-in-local-scope/218/5 "2015-05-19T23:03:03Z")

</div>

Yes, toplevel linear resources are not tracked.On Tuesday, May 19, 2015 at 7:01:22 PM UTC-4, Yannick Duchêne wrote:

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

---

<div class="post-metadata">

**Author:** ![Zhiqiang\_Ren](https://avatars.discourse-cdn.com/v4/letter/z/9fc29f/32.png) [@Zhiqiang\_Ren](https://discourse.ats-lang.org/u/Zhiqiang_Ren)\
**Post date:** [May 14, 2014, 2:40pm UTC](https://discourse.ats-lang.org/t/packing-a-view-and-address-in-local-scope/218/6 "2014-05-14T14:40:04Z")

</div>

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.On Wednesday, May 14, 2014 12:45:43 AM UTC-4, gmhwxi wrote:

> 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.
> 
> On Wednesday, May 14, 2014 12:02:28 AM UTC-4, Brandon Barker wrote:
> 
> > 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.
> > 
> > On Tuesday, May 13, 2014 10:40:38 PM UTC-4, gmhwxi wrote:
> > 
> > > I suggest that you try:
> > > 
> > > [https://github.com/githwxi/ATS-Postiats-contrib/blob/master/contrib/libats-/hwxi/globals/HATS/globvar.hats](https://github.com/githwxi/ATS-Postiats-contrib/blob/master/contrib/libats-/hwxi/globals/HATS/globvar.hats)
> > > 
> > > Here is an example of using globvar:
> > > 
> > > [https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/GTK3/TextProcessing/](https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/GTK3/TextProcessing/)
> > > 
> > > I think you will like it 🙂
> > > 
> > > On Tuesday, May 13, 2014 10:29:33 PM UTC-4, Brandon Barker wrote:
> > > 
> > > > 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.
> > > > 
> > > > > On Tuesday, May 13, 2014 10:01:32 PM UTC-4, Brandon Barker wrote:
> > > > > 
> > > > > > It seems that there are two basic ways to have an updated value: by  
> > > > > > using a ‘var’ (stack allocated variable)[http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x3377.html](http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x3377.html),  
> > > > > > or using a reference (“an array with one element”)[http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x1476.html](http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x1476.html).  
> > > > > > 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.

---

<div class="post-metadata">

**Author:** ![Brandon\_Barker](https://sea1.discourse-cdn.com/flex019/user_avatar/discourse.ats-lang.org/brandon_barker/32/61_2.png) [@Brandon\_Barker](https://discourse.ats-lang.org/u/Brandon_Barker)\
**Post date:** [May 14, 2014, 3:38pm UTC](https://discourse.ats-lang.org/t/packing-a-view-and-address-in-local-scope/218/7 "2014-05-14T15:38:12Z")

</div>

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=footerOn](https://groups.google.com/d/msgid/ats-lang-users/dcde4b29-ca89-4d3f-abb0-768a0311587d%40googlegroups.com?utm_medium=email&utm_source=footerOn) 14 May 2014 11:11, “Zhiqiang Ren” [alex.r...@gmail.com](mailto:alex.r...@gmail.com) wrote:

> 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](http://www.ats-lang.org/DOWNLOAD/#installation_atscntrb))
> 
> On Tuesday, May 13, 2014 10:40:38 PM UTC-4, gmhwxi wrote:
> 
> > I suggest that you try:
> > 
> > [https://github.com/githwxi/ATS-Postiats-contrib/blob/](https://github.com/githwxi/ATS-Postiats-contrib/blob/)  
> > master/contrib/libats-/hwxi/globals/HATS/globvar.hats
> > 
> > Here is an example of using globvar:
> > 
> > [https://github.com/githwxi/ATS-Postiats/blob/master/doc/](https://github.com/githwxi/ATS-Postiats/blob/master/doc/)  
> > EXAMPLE/GTK3/TextProcessing/
> > 
> > I think you will like it 🙂
> > 
> > > > –  
> > > > 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](mailto:ats-lang-user...@googlegroups.com).  
> > > > To post to this group, send email to [ats-lan...@googlegroups.com](mailto:ats-lan...@googlegroups.com).  
> > > > To view this discussion on the web visit  
> > > > [https://groups.google.com/d/msgid/ats-lang-users/4ba1e8ab-8e55-4990-8fd9-c3d516fb49e6%40googlegroups.com](https://groups.google.com/d/msgid/ats-lang-users/4ba1e8ab-8e55-4990-8fd9-c3d516fb49e6%40googlegroups.com)[https://groups.google.com/d/msgid/ats-lang-users/4ba1e8ab-8e55-4990-8fd9-c3d516fb49e6%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/ats-lang-users/4ba1e8ab-8e55-4990-8fd9-c3d516fb49e6%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > > .

---

<div class="post-metadata">

**Author:** ![Zhiqiang\_Ren](https://avatars.discourse-cdn.com/v4/letter/z/9fc29f/32.png) [@Zhiqiang\_Ren](https://discourse.ats-lang.org/u/Zhiqiang_Ren)\
**Post date:** [May 14, 2014, 3:04pm UTC](https://discourse.ats-lang.org/t/packing-a-view-and-address-in-local-scope/218/8 "2014-05-14T15:04:35Z")

</div>

Drop the “= ptr” as well.On Wednesday, May 14, 2014 10:52:13 AM UTC-4, Brandon Barker wrote:

> 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_)
> 
> On Wednesday, May 14, 2014 10:40:04 AM UTC-4, Zhiqiang Ren wrote:
> 
> > # 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.
> > 
> > On Wednesday, May 14, 2014 12:45:43 AM UTC-4, gmhwxi wrote:
> > 
> > > 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.
> > > 
> > > On Wednesday, May 14, 2014 12:02:28 AM UTC-4, Brandon Barker wrote:
> > > 
> > > > 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.
> > > > 
> > > > On Tuesday, May 13, 2014 10:40:38 PM UTC-4, gmhwxi wrote:
> > > > 
> > > > > I suggest that you try:
> > > > > 
> > > > > [https://github.com/githwxi/ATS-Postiats-contrib/blob/master/contrib/libats-/hwxi/globals/HATS/globvar.hats](https://github.com/githwxi/ATS-Postiats-contrib/blob/master/contrib/libats-/hwxi/globals/HATS/globvar.hats)
> > > > > 
> > > > > Here is an example of using globvar:
> > > > > 
> > > > > [https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/GTK3/TextProcessing/](https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/GTK3/TextProcessing/)
> > > > > 
> > > > > I think you will like it 🙂
> > > > > 
> > > > > On Tuesday, May 13, 2014 10:29:33 PM UTC-4, Brandon Barker wrote:
> > > > > 
> > > > > > 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.
> > > > > > 
> > > > > > > On Tuesday, May 13, 2014 10:01:32 PM UTC-4, Brandon Barker wrote:
> > > > > > > 
> > > > > > > > It seems that there are two basic ways to have an updated value: by  
> > > > > > > > using a ‘var’ (stack allocated variable)[http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x3377.html](http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x3377.html),  
> > > > > > > > or using a reference (“an array with one element”)[http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x1476.html](http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x1476.html).  
> > > > > > > > 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\_dataoutside of a function. Another function could be created to do the  
> > > > > > > > initialization and return a ‘var’, but just curious what is generally  
> > > > > > > > advised.

---

<div class="post-metadata">

**Author:** ![Brandon\_Barker](https://sea1.discourse-cdn.com/flex019/user_avatar/discourse.ats-lang.org/brandon_barker/32/61_2.png) [@Brandon\_Barker](https://discourse.ats-lang.org/u/Brandon_Barker)\
**Post date:** [May 14, 2014, 11:58am UTC](https://discourse.ats-lang.org/t/packing-a-view-and-address-in-local-scope/218/9 "2014-05-14T11:58:00Z")

</div>

Brandon Barker  
brandon…@gmail.comFrom: Brandon Barker [brandon...@gmail.com](mailto: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](mailto:gmh...@gmail.com)

Brandon Barker  
[brandon...@gmail.com](mailto:brandon...@gmail.com)

> 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[https://github.com/bbarker/M-N-K-Tic-Tac-Toe/blob/master/game.sats](https://github.com/bbarker/M-N-K-Tic-Tac-Toe/blob/master/game.sats)and  
> game.dats[https://github.com/bbarker/M-N-K-Tic-Tac-Toe/blob/master/game.sats](https://github.com/bbarker/M-N-K-Tic-Tac-Toe/blob/master/game.sats)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.

> 

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

> > > I suggest that you try:
> > > 
> > > [https://github.com/githwxi/ATS-Postiats-contrib/blob/](https://github.com/githwxi/ATS-Postiats-contrib/blob/)  
> > > master/contrib/libats-/hwxi/globals/HATS/globvar.hats
> > > 
> > > Here is an example of using globvar:
> > > 
> > > [https://github.com/githwxi/ATS-Postiats/blob/master/doc/](https://github.com/githwxi/ATS-Postiats/blob/master/doc/)  
> > > EXAMPLE/GTK3/TextProcessing/
> > > 
> > > I think you will like it 🙂

> > > >

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

> > > > > > It seems that there are two basic ways to have an updated value: by  
> > > > > > using a ‘var’ (stack allocated variable)[http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x3377.html](http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x3377.html),  
> > > > > > or using a reference (“an array with one element”)[http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x1476.html](http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x1476.html).  
> > > > > > 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.
> > > > > > 
> > > > > > –  
> > > > > > 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](mailto:ats-lang-user...@googlegroups.com).  
> > > > > > To post to this group, send email to [ats-lan...@googlegroups.com](mailto: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](https://groups.google.com/d/msgid/ats-lang-users/7340a848-ec98-4d8a-9673-13ac241bcd2e%40googlegroups.com)[https://groups.google.com/d/msgid/ats-lang-users/7340a848-ec98-4d8a-9673-13ac241bcd2e%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/ats-lang-users/7340a848-ec98-4d8a-9673-13ac241bcd2e%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > > > > .

---

<div class="post-metadata">

**Author:** ![Brandon\_Barker](https://sea1.discourse-cdn.com/flex019/user_avatar/discourse.ats-lang.org/brandon_barker/32/61_2.png) [@Brandon\_Barker](https://discourse.ats-lang.org/u/Brandon_Barker)\
**Post date:** [May 14, 2014, 2:52pm UTC](https://discourse.ats-lang.org/t/packing-a-view-and-address-in-local-scope/218/10 "2014-05-14T14:52:13Z")

</div>

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_)On Wednesday, May 14, 2014 10:40:04 AM UTC-4, Zhiqiang Ren wrote:

> # 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.
> 
> On Wednesday, May 14, 2014 12:45:43 AM UTC-4, gmhwxi wrote:
> 
> > 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.
> > 
> > On Wednesday, May 14, 2014 12:02:28 AM UTC-4, Brandon Barker wrote:
> > 
> > > 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.
> > > 
> > > On Tuesday, May 13, 2014 10:40:38 PM UTC-4, gmhwxi wrote:
> > > 
> > > > I suggest that you try:
> > > > 
> > > > [https://github.com/githwxi/ATS-Postiats-contrib/blob/master/contrib/libats-/hwxi/globals/HATS/globvar.hats](https://github.com/githwxi/ATS-Postiats-contrib/blob/master/contrib/libats-/hwxi/globals/HATS/globvar.hats)
> > > > 
> > > > Here is an example of using globvar:
> > > > 
> > > > [https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/GTK3/TextProcessing/](https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/GTK3/TextProcessing/)
> > > > 
> > > > I think you will like it 🙂
> > > > 
> > > > On Tuesday, May 13, 2014 10:29:33 PM UTC-4, Brandon Barker wrote:
> > > > 
> > > > > 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.
> > > > > 
> > > > > > On Tuesday, May 13, 2014 10:01:32 PM UTC-4, Brandon Barker wrote:
> > > > > > 
> > > > > > > It seems that there are two basic ways to have an updated value: by  
> > > > > > > using a ‘var’ (stack allocated variable)[http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x3377.html](http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x3377.html),  
> > > > > > > or using a reference (“an array with one element”)[http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x1476.html](http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x1476.html).  
> > > > > > > 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.

---

<div class="post-metadata">

**Author:** ![Brandon\_Barker](https://sea1.discourse-cdn.com/flex019/user_avatar/discourse.ats-lang.org/brandon_barker/32/61_2.png) [@Brandon\_Barker](https://discourse.ats-lang.org/u/Brandon_Barker)\
**Post date:** [May 14, 2014, 4:42pm UTC](https://discourse.ats-lang.org/t/packing-a-view-and-address-in-local-scope/218/11 "2014-05-14T16:42:06Z")

</div>

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.comOn Wed, May 14, 2014 at 12:38 PM, gmhwxi [gmh...@gmail.com](mailto:gmh...@gmail.com) wrote:

> 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_)
> 
> On Wednesday, May 14, 2014 10:52:13 AM UTC-4, Brandon Barker wrote:
> 
> > 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_)
> > 
> > On Wednesday, May 14, 2014 10:40:04 AM UTC-4, Zhiqiang Ren wrote:
> > 
> > > # 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.
> > > 
> > > On Wednesday, May 14, 2014 12:45:43 AM UTC-4, gmhwxi wrote:
> > > 
> > > > 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.
> > > > 
> > > > On Wednesday, May 14, 2014 12:02:28 AM UTC-4, Brandon Barker wrote:
> > > > 
> > > > > 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.
> > > > > 
> > > > > On Tuesday, May 13, 2014 10:40:38 PM UTC-4, gmhwxi wrote:
> > > > > 
> > > > > > I suggest that you try:
> > > > > > 
> > > > > > [https://github.com/githwxi/ATS-Postiats-contrib/blob/](https://github.com/githwxi/ATS-Postiats-contrib/blob/)  
> > > > > > master/contrib/libats-/hwxi/globals/HATS/globvar.hats
> > > > > > 
> > > > > > Here is an example of using globvar:
> > > > > > 
> > > > > > [https://github.com/githwxi/ATS-Postiats/blob/master/doc/](https://github.com/githwxi/ATS-Postiats/blob/master/doc/)  
> > > > > > EXAMPLE/GTK3/TextProcessing/
> > > > > > 
> > > > > > I think you will like it 🙂
> > > > > > 
> > > > > > On Tuesday, May 13, 2014 10:29:33 PM UTC-4, Brandon Barker wrote:
> > > > > > 
> > > > > > > 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.
> > > > > > > 
> > > > > > > > On Tuesday, May 13, 2014 10:01:32 PM UTC-4, Brandon Barker wrote:
> > > > > > > > 
> > > > > > > > > It seems that there are two basic ways to have an updated value:  
> > > > > > > > > by using a ‘var’ (stack allocated variable)[http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x3377.html](http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x3377.html),  
> > > > > > > > > or using a reference (“an array with one element”)[http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x1476.html](http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x1476.html).  
> > > > > > > > > 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\_dataoutside of a function. Another function could be created to do the  
> > > > > > > > > initialization and return a ‘var’, but just curious what is generally  
> > > > > > > > > advised.
> > > > > > > > > 
> > > > > > > > > –  
> > > > > > > > > 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](mailto:ats-lang-user...@googlegroups.com).  
> > > > > > > > > To post to this group, send email to [ats-lan...@googlegroups.com](mailto:ats-lan...@googlegroups.com).  
> > > > > > > > > To view this discussion on the web visit  
> > > > > > > > > [https://groups.google.com/d/msgid/ats-lang-users/84f7ed4f-8558-42ae-b0f7-641944bb6749%40googlegroups.com](https://groups.google.com/d/msgid/ats-lang-users/84f7ed4f-8558-42ae-b0f7-641944bb6749%40googlegroups.com)[https://groups.google.com/d/msgid/ats-lang-users/84f7ed4f-8558-42ae-b0f7-641944bb6749%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/ats-lang-users/84f7ed4f-8558-42ae-b0f7-641944bb6749%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > > > > > > > .

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [May 14, 2014, 4:38pm UTC](https://discourse.ats-lang.org/t/packing-a-view-and-address-in-local-scope/218/12 "2014-05-14T16:38:55Z")

</div>

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_)On Wednesday, May 14, 2014 10:52:13 AM UTC-4, Brandon Barker wrote:

> 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_)
> 
> On Wednesday, May 14, 2014 10:40:04 AM UTC-4, Zhiqiang Ren wrote:
> 
> > # 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.
> > 
> > On Wednesday, May 14, 2014 12:45:43 AM UTC-4, gmhwxi wrote:
> > 
> > > 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.
> > > 
> > > On Wednesday, May 14, 2014 12:02:28 AM UTC-4, Brandon Barker wrote:
> > > 
> > > > 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.
> > > > 
> > > > On Tuesday, May 13, 2014 10:40:38 PM UTC-4, gmhwxi wrote:
> > > > 
> > > > > I suggest that you try:
> > > > > 
> > > > > [https://github.com/githwxi/ATS-Postiats-contrib/blob/master/contrib/libats-/hwxi/globals/HATS/globvar.hats](https://github.com/githwxi/ATS-Postiats-contrib/blob/master/contrib/libats-/hwxi/globals/HATS/globvar.hats)
> > > > > 
> > > > > Here is an example of using globvar:
> > > > > 
> > > > > [https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/GTK3/TextProcessing/](https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/GTK3/TextProcessing/)
> > > > > 
> > > > > I think you will like it 🙂
> > > > > 
> > > > > On Tuesday, May 13, 2014 10:29:33 PM UTC-4, Brandon Barker wrote:
> > > > > 
> > > > > > 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.
> > > > > > 
> > > > > > > On Tuesday, May 13, 2014 10:01:32 PM UTC-4, Brandon Barker wrote:
> > > > > > > 
> > > > > > > > It seems that there are two basic ways to have an updated value: by  
> > > > > > > > using a ‘var’ (stack allocated variable)[http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x3377.html](http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x3377.html),  
> > > > > > > > or using a reference (“an array with one element”)[http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x1476.html](http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x1476.html).  
> > > > > > > > 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\_dataoutside of a function. Another function could be created to do the  
> > > > > > > > initialization and return a ‘var’, but just curious what is generally  
> > > > > > > > advised.

---

<div class="post-metadata">

**Author:** ![Brandon\_Barker](https://sea1.discourse-cdn.com/flex019/user_avatar/discourse.ats-lang.org/brandon_barker/32/61_2.png) [@Brandon\_Barker](https://discourse.ats-lang.org/u/Brandon_Barker)\
**Post date:** [May 14, 2014, 2:29am UTC](https://discourse.ats-lang.org/t/packing-a-view-and-address-in-local-scope/218/13 "2014-05-14T02:29:33Z")

</div>

> 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.\> On Tuesday, May 13, 2014 10:01:32 PM UTC-4, Brandon Barker wrote:

> > It seems that there are two basic ways to have an updated value: by using  
> > a ‘var’ (stack allocated variable)[http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x3377.html](http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x3377.html),  
> > or using a reference (“an array with one element”)[http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x1476.html](http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x1476.html).  
> > 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.

---

<div class="post-metadata">

**Author:** ![Zhiqiang\_Ren](https://avatars.discourse-cdn.com/v4/letter/z/9fc29f/32.png) [@Zhiqiang\_Ren](https://discourse.ats-lang.org/u/Zhiqiang_Ren)\
**Post date:** [May 18, 2014, 1:55am UTC](https://discourse.ats-lang.org/t/packing-a-view-and-address-in-local-scope/218/14 "2014-05-18T01:55:52Z")

</div>

Not exactly. But I got clarification and documented it in  
[http://ats-documentation.readthedocs.org/en/latest/ats\_blog/staload\_var/content.htmlOn](http://ats-documentation.readthedocs.org/en/latest/ats_blog/staload_var/content.htmlOn) Wednesday, May 14, 2014 11:38:12 AM UTC-4, Brandon Barker wrote:

> 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](https://groups.google.com/d/msgid/ats-lang-users/dcde4b29-ca89-4d3f-abb0-768a0311587d%40googlegroups.com?utm_medium=email&utm_source=footer)  
> On 14 May 2014 11:11, “Zhiqiang Ren” \<[alex...@gmail.com](mailto:alex...@gmail.com) \<javascript:\>\> wrote:
> 
> > 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](http://www.ats-lang.org/DOWNLOAD/#installation_atscntrb))
> > 
> > On Tuesday, May 13, 2014 10:40:38 PM UTC-4, gmhwxi wrote:
> > 
> > > I suggest that you try:
> > > 
> > > [https://github.com/githwxi/ATS-Postiats-contrib/blob/](https://github.com/githwxi/ATS-Postiats-contrib/blob/)  
> > > master/contrib/libats-/hwxi/globals/HATS/globvar.hats
> > > 
> > > Here is an example of using globvar:
> > > 
> > > [https://github.com/githwxi/ATS-Postiats/blob/master/doc/](https://github.com/githwxi/ATS-Postiats/blob/master/doc/)  
> > > EXAMPLE/GTK3/TextProcessing/
> > > 
> > > I think you will like it 🙂
> > > 
> > > > > –  
> > > > > 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...@googlegroups.com](mailto:ats-lang...@googlegroups.com) \<javascript:\>.  
> > > > > To post to this group, send email to [ats-l...@googlegroups.com](mailto:ats-l...@googlegroups.com)\<javascript:\>  
> > > > > .  
> > > > > To view this discussion on the web visit  
> > > > > [https://groups.google.com/d/msgid/ats-lang-users/4ba1e8ab-8e55-4990-8fd9-c3d516fb49e6%40googlegroups.com](https://groups.google.com/d/msgid/ats-lang-users/4ba1e8ab-8e55-4990-8fd9-c3d516fb49e6%40googlegroups.com)[https://groups.google.com/d/msgid/ats-lang-users/4ba1e8ab-8e55-4990-8fd9-c3d516fb49e6%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/ats-lang-users/4ba1e8ab-8e55-4990-8fd9-c3d516fb49e6%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > > > .

---

<div class="post-metadata">

**Author:** ![Brandon\_Barker](https://sea1.discourse-cdn.com/flex019/user_avatar/discourse.ats-lang.org/brandon_barker/32/61_2.png) [@Brandon\_Barker](https://discourse.ats-lang.org/u/Brandon_Barker)\
**Post date:** [May 14, 2014, 4:02am UTC](https://discourse.ats-lang.org/t/packing-a-view-and-address-in-local-scope/218/15 "2014-05-14T04:02:28Z")

</div>

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.On Tuesday, May 13, 2014 10:40:38 PM UTC-4, gmhwxi wrote:

> I suggest that you try:
> 
> [https://github.com/githwxi/ATS-Postiats-contrib/blob/master/contrib/libats-/hwxi/globals/HATS/globvar.hats](https://github.com/githwxi/ATS-Postiats-contrib/blob/master/contrib/libats-/hwxi/globals/HATS/globvar.hats)
> 
> Here is an example of using globvar:
> 
> [https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/GTK3/TextProcessing/](https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/GTK3/TextProcessing/)
> 
> I think you will like it 🙂
> 
> On Tuesday, May 13, 2014 10:29:33 PM UTC-4, Brandon Barker wrote:
> 
> > 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.
> > 
> > > On Tuesday, May 13, 2014 10:01:32 PM UTC-4, Brandon Barker wrote:
> > > 
> > > > It seems that there are two basic ways to have an updated value: by  
> > > > using a ‘var’ (stack allocated variable)[http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x3377.html](http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x3377.html),  
> > > > or using a reference (“an array with one element”)[http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x1476.html](http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x1476.html).  
> > > > 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.

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [May 14, 2014, 4:42pm UTC](https://discourse.ats-lang.org/t/packing-a-view-and-address-in-local-scope/218/16 "2014-05-14T16:42:00Z")

</div>

No, this won’t solve the problem.On Wednesday, May 14, 2014 11:04:35 AM UTC-4, Zhiqiang Ren wrote:

> Drop the “= ptr” as well.
> 
> On Wednesday, May 14, 2014 10:52:13 AM UTC-4, Brandon Barker wrote:
> 
> > 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_)
> > 
> > On Wednesday, May 14, 2014 10:40:04 AM UTC-4, Zhiqiang Ren wrote:
> > 
> > > # 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.
> > > 
> > > On Wednesday, May 14, 2014 12:45:43 AM UTC-4, gmhwxi wrote:
> > > 
> > > > 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.
> > > > 
> > > > On Wednesday, May 14, 2014 12:02:28 AM UTC-4, Brandon Barker wrote:
> > > > 
> > > > > 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.
> > > > > 
> > > > > On Tuesday, May 13, 2014 10:40:38 PM UTC-4, gmhwxi wrote:
> > > > > 
> > > > > > I suggest that you try:
> > > > > > 
> > > > > > [https://github.com/githwxi/ATS-Postiats-contrib/blob/master/contrib/libats-/hwxi/globals/HATS/globvar.hats](https://github.com/githwxi/ATS-Postiats-contrib/blob/master/contrib/libats-/hwxi/globals/HATS/globvar.hats)
> > > > > > 
> > > > > > Here is an example of using globvar:
> > > > > > 
> > > > > > [https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/GTK3/TextProcessing/](https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/GTK3/TextProcessing/)
> > > > > > 
> > > > > > I think you will like it 🙂
> > > > > > 
> > > > > > On Tuesday, May 13, 2014 10:29:33 PM UTC-4, Brandon Barker wrote:
> > > > > > 
> > > > > > > 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.
> > > > > > > 
> > > > > > > > On Tuesday, May 13, 2014 10:01:32 PM UTC-4, Brandon Barker wrote:
> > > > > > > > 
> > > > > > > > > It seems that there are two basic ways to have an updated value:  
> > > > > > > > > by using a ‘var’ (stack allocated variable)[http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x3377.html](http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x3377.html),  
> > > > > > > > > or using a reference (“an array with one element”)[http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x1476.html](http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x1476.html).  
> > > > > > > > > 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\_dataoutside of a function. Another function could be created to do the  
> > > > > > > > > initialization and return a ‘var’, but just curious what is generally  
> > > > > > > > > advised.

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [May 14, 2014, 2:40am UTC](https://discourse.ats-lang.org/t/packing-a-view-and-address-in-local-scope/218/17 "2014-05-14T02:40:38Z")

</div>

I suggest that you try:

[https://github.com/githwxi/ATS-Postiats-contrib/blob/master/contrib/libats-/hwxi/globals/HATS/globvar.hats](https://github.com/githwxi/ATS-Postiats-contrib/blob/master/contrib/libats-/hwxi/globals/HATS/globvar.hats)

Here is an example of using globvar:

[https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/GTK3/TextProcessing/](https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/GTK3/TextProcessing/)

I think you will like it :)On Tuesday, May 13, 2014 10:29:33 PM UTC-4, Brandon Barker wrote:

> 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.
> 
> > On Tuesday, May 13, 2014 10:01:32 PM UTC-4, Brandon Barker wrote:
> > 
> > > It seems that there are two basic ways to have an updated value: by  
> > > using a ‘var’ (stack allocated variable)[http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x3377.html](http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x3377.html),  
> > > or using a reference (“an array with one element”)[http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x1476.html](http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x1476.html).  
> > > 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.

---

<div class="post-metadata">

**Author:** ![Brandon\_Barker](https://sea1.discourse-cdn.com/flex019/user_avatar/discourse.ats-lang.org/brandon_barker/32/61_2.png) [@Brandon\_Barker](https://discourse.ats-lang.org/u/Brandon_Barker)\
**Post date:** [May 14, 2014, 3:56pm UTC](https://discourse.ats-lang.org/t/packing-a-view-and-address-in-local-scope/218/18 "2014-05-14T15:56:15Z")

</div>

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](https://groups.google.com/d/msgid/ats-lang-users/6174399b-fe88-43cb-b935-2ec008f1a196%40googlegroups.com)  
Drop the “= ptr” as well.On Wednesday, May 14, 2014 10:52:13 AM UTC-4, Brandon Barker wrote:

> 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_)
> 
> On Wednesday, May 14, 2014 10:40:04 AM UTC-4, Zhiqiang Ren wrote:
> 
> > # 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.
> > 
> > On Wednesday, May 14, 2014 12:45:43 AM UTC-4, gmhwxi wrote:
> > 
> > > 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.
> > > 
> > > On Wednesday, May 14, 2014 12:02:28 AM UTC-4, Brandon Barker wrote:
> > > 
> > > > 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.
> > > > 
> > > > On Tuesday, May 13, 2014 10:40:38 PM UTC-4, gmhwxi wrote:
> > > > 
> > > > > I suggest that you try:
> > > > > 
> > > > > [https://github.com/githwxi/ATS-Postiats-contrib/blob/](https://github.com/githwxi/ATS-Postiats-contrib/blob/)  
> > > > > master/contrib/libats-/hwxi/globals/HATS/globvar.hats
> > > > > 
> > > > > Here is an example of using globvar:
> > > > > 
> > > > > [https://github.com/githwxi/ATS-Postiats/blob/master/doc/](https://github.com/githwxi/ATS-Postiats/blob/master/doc/)  
> > > > > EXAMPLE/GTK3/TextProcessing/
> > > > > 
> > > > > I think you will like it 🙂
> > > > > 
> > > > > On Tuesday, May 13, 2014 10:29:33 PM UTC-4, Brandon Barker wrote:
> > > > > 
> > > > > > 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.
> > > > > > 
> > > > > > > On Tuesday, May 13, 2014 10:01:32 PM UTC-4, Brandon Barker wrote:
> > > > > > > 
> > > > > > > > It seems that there are two basic ways to have an updated value: by  
> > > > > > > > using a ‘var’ (stack allocated variable)[http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x3377.html](http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x3377.html),  
> > > > > > > > or using a reference (“an array with one element”)[http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x1476.html](http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x1476.html).  
> > > > > > > > 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\_dataoutside of a function. Another function could be created to do the  
> > > > > > > > initialization and return a ‘var’, but just curious what is generally  
> > > > > > > > advised.
> > > > > > > > 
> > > > > > > > –  
> > > > > > > > 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](mailto:ats-lang-user...@googlegroups.com).  
> > > > > > > > To post to this group, send email to [ats-lan...@googlegroups.com](mailto:ats-lan...@googlegroups.com).  
> > > > > > > > To view this discussion on the web visit  
> > > > > > > > [https://groups.google.com/d/msgid/ats-lang-users/a812e25b-a329-4ca0-98b4-a479bf408698%40googlegroups.com](https://groups.google.com/d/msgid/ats-lang-users/a812e25b-a329-4ca0-98b4-a479bf408698%40googlegroups.com)[https://groups.google.com/d/msgid/ats-lang-users/a812e25b-a329-4ca0-98b4-a479bf408698%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/ats-lang-users/a812e25b-a329-4ca0-98b4-a479bf408698%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > > > > > > .

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [May 14, 2014, 4:17pm UTC](https://discourse.ats-lang.org/t/packing-a-view-and-address-in-local-scope/218/19 "2014-05-14T16:17:38Z")

</div>

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}On Wednesday, May 14, 2014 11:11:06 AM UTC-4, Zhiqiang Ren wrote:

> 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](http://www.ats-lang.org/DOWNLOAD/#installation_atscntrb))
> 
> On Tuesday, May 13, 2014 10:40:38 PM UTC-4, gmhwxi wrote:
> 
> > I suggest that you try:
> > 
> > [https://github.com/githwxi/ATS-Postiats-contrib/blob/master/contrib/libats-/hwxi/globals/HATS/globvar.hats](https://github.com/githwxi/ATS-Postiats-contrib/blob/master/contrib/libats-/hwxi/globals/HATS/globvar.hats)
> > 
> > Here is an example of using globvar:
> > 
> > [https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/GTK3/TextProcessing/](https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/GTK3/TextProcessing/)
> > 
> > I think you will like it 🙂
> > 
> > > >

---

<div class="post-metadata">

**Author:** ![Yannick\_Duchene](https://avatars.discourse-cdn.com/v4/letter/y/b77776/32.png) [@Yannick\_Duchene](https://discourse.ats-lang.org/u/Yannick_Duchene)\
**Post date:** [May 19, 2015, 11:01pm UTC](https://discourse.ats-lang.org/t/packing-a-view-and-address-in-local-scope/218/20 "2015-05-19T23:01:22Z")

</div>

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