Nested viewtype destructuring ...

61 views
Skip to first unread message

aditya siram

unread,
May 15, 2018, 9:04:32 PM5/15/18
to ats-lang-users
Hi all,
I'm sure there's an easy answer to this but I'm not having much luck. How do I destructure into a view type that nested in another viewtype? For example, I'd to pattern match on something of type 'Option_vt(Option_vt(list_vt int))'. I'm not having much luck doing it in one fell swoop like: ' | Some_vt(Some_vt(list_vt_cons(x, xs))) => ...' especially when the linear proof of some inner value (like 'x') needs to be preserved.

Thanks!

Hongwei Xi

unread,
May 15, 2018, 9:27:59 PM5/15/18
to ats-lan...@googlegroups.com
I am so clear as to what you need precisely.

Could you show me how to do it using pattern matching repeatly?
Then I may be able to do it using a nested pattern.

Here is an example that may be useful:

| ~Some_vt(~Some_vt(xs as list_vt_cons _)) => // using xs here

The two Some_vt constructors are freed but list_vt_cons is not.

--
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/e196b40c-7b12-45f1-abe6-4edb9d4bf3fd%40googlegroups.com.

Hongwei Xi

unread,
May 15, 2018, 9:28:33 PM5/15/18
to ats-lan...@googlegroups.com
Oops. I am NOT so clear ...

On Tue, May 15, 2018 at 9:27 PM, Hongwei Xi <gmh...@gmail.com> wrote:
I am so clear as to what you need precisely.

Could you show me how to do it using pattern matching repeatly?
Then I may be able to do it using a nested pattern.

Here is an example that may be useful:

| ~Some_vt(~Some_vt(xs as list_vt_cons _)) => // using xs here

The two Some_vt constructors are freed but list_vt_cons is not.
On Tue, May 15, 2018 at 9:04 PM, aditya siram <aditya...@gmail.com> wrote:
Hi all,
I'm sure there's an easy answer to this but I'm not having much luck. How do I destructure into a view type that nested in another viewtype? For example, I'd to pattern match on something of type 'Option_vt(Option_vt(list_vt int))'. I'm not having much luck doing it in one fell swoop like: ' | Some_vt(Some_vt(list_vt_cons(x, xs))) => ...' especially when the linear proof of some inner value (like 'x') needs to be preserved.

Thanks!

--
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-lan...@googlegroups.com.

aditya siram

unread,
May 15, 2018, 10:27:03 PM5/15/18
to ats-lang-users

#include "share/atspre_staload.hats"

datavtype nested
= nested of (Option_vt(List_vt(int)))

extern fun add_heads(
  a
: Option_vt(nested),
  b
: Option_vt(nested)
): Option_vt(nested)

implement add_heads
(a,b) =
 
case (a,b) of
   
| (~None_vt(),g) => g
   
| (l, ~None_vt()) => l
   
| (Some_vt(nested(Some_vt(list_vt_cons(x,_)))),
         
Some_vt(nested(Some_vt(list_vt_cons(y,ys))))) =>
       
Some_vt(nested(Some_vt(list_vt_cons(x+y,ys))))

implement main0
(argc,argv) = ()


The above is a minimal non-compiling example from a larger program. I even tried adding '@' to the pattern match with 'fold@' in the body but it didn't work.

Thanks!


On Tuesday, May 15, 2018 at 8:28:33 PM UTC-5, gmhwxi wrote:
Oops. I am NOT so clear ...
On Tue, May 15, 2018 at 9:27 PM, Hongwei Xi <gmh...@gmail.com> wrote:
I am so clear as to what you need precisely.

Could you show me how to do it using pattern matching repeatly?
Then I may be able to do it using a nested pattern.

Here is an example that may be useful:

| ~Some_vt(~Some_vt(xs as list_vt_cons _)) => // using xs here

The two Some_vt constructors are freed but list_vt_cons is not.
On Tue, May 15, 2018 at 9:04 PM, aditya siram <aditya...@gmail.com> wrote:
Hi all,
I'm sure there's an easy answer to this but I'm not having much luck. How do I destructure into a view type that nested in another viewtype? For example, I'd to pattern match on something of type 'Option_vt(Option_vt(list_vt int))'. I'm not having much luck doing it in one fell swoop like: ' | Some_vt(Some_vt(list_vt_cons(x, xs))) => ...' especially when the linear proof of some inner value (like 'x') needs to be preserved.

Thanks!

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

Hongwei Xi

unread,
May 15, 2018, 10:35:14 PM5/15/18
to ats-lan...@googlegroups.com
Here is some code that typechecks:

implement add_heads(a,b) =
  case (a,b) of
  | (~None_vt(),g) => g
  | (l, ~None_vt()) => l
  | (~Some_vt(~nested(~Some_vt(~list_vt_cons(x,xs)))),
     ~Some_vt(~nested(~Some_vt(~list_vt_cons(y,ys))))) =>
    (free(xs); Some_vt(nested(Some_vt(list_vt_cons(x+y,ys)))))

Some warnings are issued due to non-exhaustiveness of pattern matching involved.



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.

aditya siram

unread,
May 16, 2018, 9:56:27 AM5/16/18
to ats-lang-users
Awesome, thanks! I think my confusion was mostly that the error messages complained about needing to retain a linear variable instead of consuming it so I never tried to free them. Also why did 'xs' have to freed explicitly with 'free(xs)' instead of using the '~' syntax in the pattern match?

Hongwei Xi

unread,
May 16, 2018, 9:59:48 AM5/16/18
to ats-lan...@googlegroups.com
The pattern ~list_vt_cons(x, xs) means the list constructor/node (that matches it) is freed;
the head of the list is stored in 'x' and the tail in 'xs'.

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.

aditya siram

unread,
May 16, 2018, 11:54:38 AM5/16/18
to ats-lang-users
I meant why couldn't you still a '~' on the 'xs' like
| (~Some_vt(~nested(~Some_vt(~list_vt_cons(x,~xs)))), ... =>

gmhwxi

unread,
May 16, 2018, 11:58:36 AM5/16/18
to ats-lang-users

I see. A pattern like '~xs' is problematic as not every linear value can be
freed.
Reply all
Reply to author
Forward
0 new messages