Haha yeah `val xs1 = xs1' is quite confusing coming from other languages.

Your explanation helps a lot, and I understand the concept much better now. 
Thanks! Time to finish the rest of the book :)


________________________________
From: [email protected] on behalf of gmhwxi <[email protected]>
Sent: Sunday, February 10, 2019 8:44 PM
To: ats-lang-users
Subject: Re: Need help understanding free@


A list node contains two cells: one holding the content and the other holding
the pointer to the next node (or null).

When xs matches the pattern @list_vt_cons(x, xs1), xs refers to a list node;
x refers to the content cell and xs1 refers to the pointer cell. In this case, 
the
content is non-linear (a:t@ype) and thus does not need to be moved out. But
the pointer needs to be moved out before xs can be freed:

val xs1_ = xs

The above code was written long time ago. It could be prettified a bit as 
follows:

fun
{a:t@ype}
list_vt_free
  {n:nat} .<n>.
  (xs: list_vt(a, n)): void =
(
  case+ xs of
  | ~list_vt_nil() => ()
  | @list_vt_cons(_, xs1) =>
    let
      val xs1 = xs1
    in
      free@{a}{0}(xs); list_vt_free<a>(xs1)
    end
)

People are often puzzled by a beautiful line like 'val xs1 = xs1' :)

On Sunday, February 10, 2019 at 6:17:03 PM UTC-5, rnagasam wrote:
Hi all,

I'm facing some difficulty understanding how `free@' is used. I'm going through 
the Introduction to Programming in ATS book, and the example I'm facing trouble 
with is this,

fun{
a:t@ype
} list_vt_free
  {n:nat}.<n>. (xs: list_vt (a, n)): void =
  case+ xs of
  | @list_vt_cons(x, xs1) => let
      val xs1_ = xs1
      val () = free@{a}{0}(xs) in list_vt_free (xs1_)
      end
  | @list_vt_nil () => free@{a} (xs)

Why is it `free@{a}{0} (xs)' and not `free@{a}{0} (x)'? It seems that `free@' 
requires an unfolded constructor (here, it is operating on 
`list_vt_cons_unfold'). However, that begs the question -- how does `free@' 
know to free only the first node of the list and not the rest of the list? Any 
help understanding this will be much appreciated!

Thanks,
Ramana

--
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 
[email protected]<mailto:[email protected]>.
To post to this group, send email to 
[email protected]<mailto:[email protected]>.
Visit this group at 
https://groups.google.com/group/ats-lang-users<https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fgroup%2Fats-lang-users&data=02%7C01%7Crnagasam%40stevens.edu%7C7f5a4fdb1d3344d60b6508d68fc24fdc%7C8d1a69ec03b54345ae21dad112f5fb4f%7C0%7C0%7C636854462502737541&sdata=6Kt5VVrR1%2BDeDl7kNycffcDSldByecFzi8ROWP%2BL4GU%3D&reserved=0>.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ats-lang-users/e02a34b7-8150-482a-9f9b-93b89e063042%40googlegroups.com<https://na01.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgroups.google.com%2Fd%2Fmsgid%2Fats-lang-users%2Fe02a34b7-8150-482a-9f9b-93b89e063042%2540googlegroups.com%3Futm_medium%3Demail%26utm_source%3Dfooter&data=02%7C01%7Crnagasam%40stevens.edu%7C7f5a4fdb1d3344d60b6508d68fc24fdc%7C8d1a69ec03b54345ae21dad112f5fb4f%7C0%7C0%7C636854462502737541&sdata=L9vNbu0a8d6lX%2BY9qiEvx0CgD17mSdOpjZ31KA9xdQs%3D&reserved=0>.

-- 
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 [email protected].
To post to this group, send email to [email protected].
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/SN6PR10MB3086986809956FE1E30B3DE1C8640%40SN6PR10MB3086.namprd10.prod.outlook.com.

Reply via email to