I'm seeking better way to use global struct


#1

Please try the following code:

#include “share/atspre_define.hats”
#include “share/atspre_staload.hats”

%{^
struct foo {
int a;
int *p;
};
struct foo foo_var;
%}

absvt@ype struct_foo
vtypedef struct_foo_impl = $extype_struct"struct foo" of {
a = int,
p = [l:addr] (int@l | ptr l)
}

vtypedef pstruct_foo = [l:addr] (struct_foo@l | ptr(l))

assume struct_foo = struct_foo_impl
macdef takeout_pstruct_foo = $extval(pstruct_foo, “&foo_var”)
extern praxi addback_pstruct_foo (pstruct_foo): void

implement main0 () = {
val (pf | p) = takeout_pstruct_foo
// val () = foo.a := 1 // error(3): a non-proof component is replaced of
the type[S2Ecst(int)].
val () = println! !p.a
// val () = println! foo.p // error(3): the symbol [print] cannot be
resolved as no match is found.
prval () = addback_pstruct_foo @(pf | p)
}

I put a running version here:

https://github.com/githwxi/ATS-Postiats-test/blob/master/contrib/hwxi/TEST30/test39.datsOn Friday, August 19, 2016 at 8:56:08 PM UTC-4, Kiwamu Okabe wrote:

Hi all,

I’m writing following code, because seeking better way to use global
struct.

https://github.com/jats-ug/practice-ats/blob/master/extval/main.dats

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

%{^ 
struct foo { 
  int a; 
  int *p; 
}; 
struct foo foo_var; 
%} 

absvt@ype struct_foo 
vtypedef struct_foo_impl = $extype_struct"struct foo" of { 
  a = int, 
  p = [l:addr] (int@l | ptr l) 
} 
assume struct_foo = struct_foo_impl 
macdef takeout_struct_foo = $extval(struct_foo, "foo_var") 
extern praxi addback_struct_foo (struct_foo): void 

implement main0 () = { 
  val foo = takeout_struct_foo 
//  val () = foo.a := 1 // error(3): a non-proof component is replaced 
of the type[S2Ecst(int)]. 
  val () = println! foo.a 
//  val () = println! foo.p // error(3): the symbol [print] cannot be 
resolved as no match is found. 
  prval () = addback_struct_foo foo 
} 

However, the code can’t touch the member of the struct.
Is it non-sense code?

Best regards,

Kiwamu Okabe at METASEPI DESIGN


#2

And also, we can’t use at-view in struct define? I should use cPtr0() in
struct?

You can have at-views in a struct, but handling such views is quite
involved. I would
not recommend it.On Fri, Aug 19, 2016 at 11:02 PM, Kiwamu Okabe kiw...@debian.or.jp wrote:

Hi Hongwei,

vtypedef struct_foo_impl = $extype_struct"struct foo" of {
  a = int,
  p = ptr
}

And also, we can’t use at-view in struct define? I should use cPtr0() in
struct?

Best regards,

Kiwamu Okabe


You received this message because you are subscribed to the Google Groups
"ats-lang-users" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to ats-lang-user...@googlegroups.com.
To post to this group, send email to ats-lan...@googlegroups.com.
Visit this group at https://groups.google.com/group/ats-lang-users.
To view this discussion on the web visit https://groups.google.com/d/
msgid/ats-lang-users/CAEvX6d%3Dw8Vfi4z7KOUTdUBpZi3kRm-
ab0gq0c7NCvPS_mHR6pw%40mail.gmail.com.


#3

You can have at-views inside a struct, but handing such views is quite
involved.
I would not recommend it.On Friday, August 19, 2016 at 11:02:23 PM UTC-4, Kiwamu Okabe wrote:

Hi Hongwei,

vtypedef struct_foo_impl = $extype_struct"struct foo" of { 
  a = int, 
  p = ptr 
} 

And also, we can’t use at-view in struct define? I should use cPtr0() in
struct?

Best regards,

Kiwamu Okabe


#4

In this case, buz is a ‘var’ (instead of a ‘val’). A ‘var’ is always
a left-value.On Sat, Aug 20, 2016 at 12:52 AM, Kiwamu Okabe kiw...@debian.or.jp wrote:

Hi Hongwei,

On Sat, Aug 20, 2016 at 1:44 PM, Hongwei Xi gmh...@gmail.com wrote:

If you do:

val foo = $extval(struct_foo, “foo”)

the foo is a value (a flat record); it cannot be used as a left-value.

Thanks. It means the answer for following:

Totally, $extval can only maintain viewtype as pointer. Is it right?

However, following code is using flat record with well-typed…

  var buz: @{ a = int, b = ptr }
  val () = buz.a := 1

Best regards,

Kiwamu Okabe at METASEPI DESIGN


You received this message because you are subscribed to the Google Groups
"ats-lang-users" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to ats-lang-user...@googlegroups.com.
To post to this group, send email to ats-lan...@googlegroups.com.
Visit this group at https://groups.google.com/group/ats-lang-users.
To view this discussion on the web visit https://groups.google.com/d/
msgid/ats-lang-users/CAEvX6dnRibuCXTDgv9gkLoYC92eRz
09eoEYi0cu42zBgJWd5mA%40mail.gmail.com.


#5

If you do:

val foo = $extval(struct_foo, “foo”)

the foo is a value (a flat record); it cannot be used as a left-value.On Fri, Aug 19, 2016 at 10:52 PM, Kiwamu Okabe kiw...@debian.or.jp wrote:

Hi Hongwei,

On Sat, Aug 20, 2016 at 11:39 AM, gmhwxi gmh...@gmail.com wrote:

Please try the following code:

Thanks a lot!
Totally, $extval can only maintain viewtype as pointer. Is it right?
I’m some confusing…

val () = println! !(!pfoo.p) // print 10

I like following style, better. The code is well-typed. My thinking is
a bad thing?

  val () = println! !(pfoo->p) // print 10

Best regards,

Kiwamu Okabe at METASEPI DESIGN


You received this message because you are subscribed to the Google Groups
"ats-lang-users" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to ats-lang-user...@googlegroups.com.
To post to this group, send email to ats-lan...@googlegroups.com.
Visit this group at https://groups.google.com/group/ats-lang-users.
To view this discussion on the web visit https://groups.google.com/d/
msgid/ats-lang-users/CAEvX6d%3DtzQTALLbZkLisbBOfR-VqD%
3DTQS4BXBGYj6WKnbcGPyA%40mail.gmail.com.


#6

Yes, you can use ‘extvar’. However, ‘extvar’ is usually for targeting a
language
that does support explicit pointers (e.g., Lisp (Scheme), JavaScript).On Saturday, August 20, 2016 at 12:58:18 AM UTC-4, Kiwamu Okabe wrote:

On Sat, Aug 20, 2016 at 1:54 PM, Hongwei Xi <…> wrote:

In this case, buz is a ‘var’ (instead of a ‘val’). A ‘var’ is always
a left-value.

Thanks. You mean $extval return ‘val’.
May I use extvar for this purpose?

Best regards,

Kiwamu Okabe at METASEPI DESIGN