some questions about segment default and guide book

94 views
Skip to first unread message

jin

unread,
Jun 2, 2025, 8:23:32 PMJun 2
to ats-lang-users
when i t2025-06-03 07.57.11.pngry to use list0_rev (list0_tail (listo_rev lista)) as initlist function, there notice segment default at runtime, so i change function to the picture below, error disappear, i want to know what cause the error, is there any relation between the error and the "make stable" of the second picture, which seems like not nessesary, that's really confuse2025-06-03 07.57.11.png
2025-06-03 08.02.32.png2025-06-03 07.57.11.png

gmhwxi

unread,
Jun 3, 2025, 6:45:35 PMJun 3
to ats-lang-users

I see that you wrote some ATS2 code.

Unfortunately, there is not much written documentation on ATS2 (or ATS in general).
It is often difficult for one to immediately figure out how various features in ATS2 should
be used.

In your code, you have some recursive templates (e.g., your template list0RevHelper is
recursive).

In general, templates should *not* be recursive. In this case, list0RevHelper does not need
to be a template in the first place. Just turn it into an ordinary (recursive) function. Then I believe
your problem will go away.

--Hongwei

gmhwxi

unread,
Jun 3, 2025, 6:53:07 PMJun 3
to ats-lang-users
For instance, I see the following kind of recursive templates very often:

fun{a:t@ype}
list0_length(xs: list0(a)): int =
case+ xs of
| list0_nil() => 0
| list0_cons(x1, xs) => 1 + list0_length<a>(xs)

This kind of code may be working, but should be avoided. Instead,
one can replace it with the following code:

fun{a:t@ype}
list0_length(xs: list0(a)): int =
length(xs) where
{
fun length(xs: list0(a)): int =
case+ xs of
| list0_nil() => 0
| list0_cons(_, xs) => 1 + length(xs)
}

BTW, recursive templates, by default, are not supported in ATS3.

jin

unread,
Jun 3, 2025, 7:31:48 PMJun 3
to ats-lang-users
thanks a lot, in my understand, the function under a function can use its father's type and without convert to templates, and can you explain more about why there is a make sort stable commet at the second picture which is the code in the book i download from ats lang web

gmhwxi

unread,
Jun 3, 2025, 7:43:08 PMJun 3
to ats-lang-users

Stable sorting means the ordering of equals should not be changed after sorting is done.
For instance, if we sort the following list according to parity (0 < 1):

[3,2,5,4,1,6]

we should get: [2,4,6,3,5,1]

In the mergesort implementation you referred to, list_reverse needs to be called to ensure sorting
done is actually stable sorting. Without list_reverse, the ordering among some equals is reversed.

jin

unread,
Jun 3, 2025, 7:55:31 PMJun 3
to ats-lang-users
so, the reverse is apllied to make sure the result is what user expected? does it can be removed from function like quick sort, which seems like will not effect the result 

Hongwei Xi

unread,
Jun 3, 2025, 8:06:06 PMJun 3
to ats-lan...@googlegroups.com
You can also make list-based quicksort stable by calling list_reverse (twice).

However, list-based quicksort is not very practical; it is mostly used for illustration purpose (that is, teaching).
On the other hand, list-based mergesort is useful in practice. Unlike array-based mergesort, list-based version
does not need extra memory for merging. Anyway, this not particularly related to ATS.

--
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 view this discussion visit https://groups.google.com/d/msgid/ats-lang-users/1de3e545-af9b-4bcd-b636-1c29823a0447n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages