I'm seeking better way to use global struct

89 views
Skip to first unread message

Kiwamu Okabe

unread,
Aug 19, 2016, 8:56:08 PM8/19/16
to ats-lang-users
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

gmhwxi

unread,
Aug 19, 2016, 10:39:13 PM8/19/16
to ats-lang-users, kiw...@debian.or.jp

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

Kiwamu Okabe

unread,
Aug 19, 2016, 10:52:51 PM8/19/16
to ats-lang-users
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
```

Kiwamu Okabe

unread,
Aug 19, 2016, 11:02:23 PM8/19/16
to ats-lang-users
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?

Kiwamu Okabe

unread,
Aug 19, 2016, 11:13:20 PM8/19/16
to ats-lang-users

Hongwei Xi

unread,
Aug 20, 2016, 12:44:34 AM8/20/16
to ats-lan...@googlegroups.com

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.


--
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-users+unsubscribe@googlegroups.com.
To post to this group, send email to ats-lang-users@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.

Hongwei Xi

unread,
Aug 20, 2016, 12:49:30 AM8/20/16
to ats-lan...@googlegroups.com

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


--
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-users+unsubscribe@googlegroups.com.
To post to this group, send email to ats-lang-users@googlegroups.com.
Visit this group at https://groups.google.com/group/ats-lang-users.

gmhwxi

unread,
Aug 20, 2016, 12:52:24 AM8/20/16
to ats-lang-users, kiw...@debian.or.jp

You can have at-views inside a struct, but handing such views is quite involved.

I would not recommend it.

Kiwamu Okabe

unread,
Aug 20, 2016, 12:53:04 AM8/20/16
to ats-lang-users
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
```

Hongwei Xi

unread,
Aug 20, 2016, 12:54:42 AM8/20/16
to ats-lan...@googlegroups.com
In this case, buz is a 'var' (instead of a 'val'). A 'var' is always
a left-value.

--
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-users+unsubscribe@googlegroups.com.
To post to this group, send email to ats-lang-users@googlegroups.com.
Visit this group at https://groups.google.com/group/ats-lang-users.

Kiwamu Okabe

unread,
Aug 20, 2016, 12:58:18 AM8/20/16
to ats-lang-users
On Sat, Aug 20, 2016 at 1:54 PM, Hongwei Xi <gmh...@gmail.com> 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?

Kiwamu Okabe

unread,
Aug 20, 2016, 1:01:56 AM8/20/16
to ats-lang-users
Hi Hongwei,

On Sat, Aug 20, 2016 at 1:52 PM, gmhwxi <gmh...@gmail.com> wrote:
> You can have at-views inside a struct, but handing such views is quite
> involved.
> I would not recommend it.

Umm... It's bad news for me...

I'm writing a converter that translate C code into ATS.

https://github.com/metasepi/c2ats

Now this tool translates following C code:

```
struct {
void *(*traverse_func)(int a[10][20], void *f(int i));
int a[2][3][4];
int *b;
struct t *c;
};
```

into following ATS code:

```
typedef struct_c2ats_anon_7053 = $extype_struct"struct { void *
(*traverse_func)(int a[10][20], void * f(int i)); int a[2][3][4]; int
* b; struct t * c; }" of {
traverse_func = (@[@[int][20]][10], (int) -> ptr) -> ptr,
a = @[@[@[int][4]][3]][2],
b = cPtr0(int),
c = cPtr0(struct_c2ats_t)
}
```

However we can't use cPtr0 in struct...
What is best way to import struct from C language?

Best regards,
--
Kiwamu Okabe at METASEPI DESIGN

gmhwxi

unread,
Aug 20, 2016, 1:02:12 AM8/20/16
to ats-lang-users, kiw...@debian.or.jp
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:

Kiwamu Okabe

unread,
Aug 20, 2016, 1:18:15 AM8/20/16
to ats-lang-users
Hi Hongwei,

On Sat, Aug 20, 2016 at 2:02 PM, gmhwxi <gmh...@gmail.com> wrote:
> Yes, you can use 'extvar'. However, 'extvar' is usually for targeting a
> language
> that does support explicit pointers (e.g., Lisp (Scheme), JavaScript).

But 'extvar' is only for left-value...
I think I should pointer to touch global value.

Thanks a lot,
Reply all
Reply to author
Forward
0 new messages