Handling noreturn functions

21 views
Skip to first unread message

Shea Levy

unread,
Sep 12, 2014, 1:12:51 PM9/12/14
to ats-lan...@googlegroups.com
Hi all,

Some functions (like C's exit(3) or longjmp(3)) never return. Is there a
way to mark them as such in ATS so that a non-void function can call
them without having to construct a fake value to return after the call?

e.g. is there some way I can write:

> fn foo(bool b): int = if b then 1 else perror "bad value"; exit(1)

without sequencing the exit with some constructed value that I know will
never actually be returned?

~Shea

gmhwxi

unread,
Sep 12, 2014, 1:22:57 PM9/12/14
to ats-lan...@googlegroups.com

It works if you write:

fn foo{b:bool}(b: bool b): int =
 
if b then 1 else (perror "bad value"; exit(1))

For any type T, exit(1){T} is considered by the typechecker a value of the type T. So you can write:

fn foo{b:bool}(b: bool b): int =
 
if b then 1 else (perror "bad value"; exit(1){int})

Shea Levy

unread,
Sep 12, 2014, 1:40:08 PM9/12/14
to ats-lan...@googlegroups.com
Ah ha! I see that's implemented by making exit a template and
implementing it as a macro. My attempt to implement something similar
with an infinte loop failed:

> fun loop (): {a: t@ype}(a) = loop ()
> implement main () = loop ()

Gives:

> test_dats.c:119:1: error: ‘loop_0’ declared as function returning an array

during c compilation. Do I have to resort to a macro/function in C here?

~Shea
> --
> 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 http://groups.google.com/group/ats-lang-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ats-lang-users/633a0d8e-6d2a-476a-9727-ad3190a74372%40googlegroups.com.

gmhwxi

unread,
Sep 12, 2014, 1:53:04 PM9/12/14
to ats-lan...@googlegroups.com
Notice that the size of the type T = {a:t@ype}(a) is unknown, so
the compiler cannot generate proper C code for 'loop' (it is no problem
to generate JavaScript code, though). So you need a special way to implement
such a function for use in C.

Shea Levy

unread,
Sep 12, 2014, 3:07:15 PM9/12/14
to ats-lan...@googlegroups.com
Ah, right. I wonder if something like:

> #define FAKE_VAL(ty) *((ty *) NULL)

could be done in ATS?

~Shea
> > an email to ats-lang-user...@googlegroups.com <javascript:>.
> > > To post to this group, send email to ats-lan...@googlegroups.com
> > <javascript:>.
> > > Visit this group at http://groups.google.com/group/ats-lang-users.
> > > To view this discussion on the web visit
> > https://groups.google.com/d/msgid/ats-lang-users/633a0d8e-6d2a-476a-9727-ad3190a74372%40googlegroups.com.
> >
> >
>
> --
> 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 http://groups.google.com/group/ats-lang-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ats-lang-users/0041ef17-fbf3-4fa1-9be7-cf3dd4d59fd8%40googlegroups.com.

gmhwxi

unread,
Sep 12, 2014, 3:11:16 PM9/12/14
to ats-lan...@googlegroups.com
How about

ptr0_get<ty>(the_null_ptr)

Shea Levy

unread,
Sep 13, 2014, 11:11:58 AM9/13/14
to ats-lan...@googlegroups.com
Perfect!
> > https://groups.google.com/d/msgid/ats-lang-users/0041ef17-fbf3-4fa1-9be7-cf3dd4d59fd8%40googlegroups.com.
> >
> >
>
> --
> 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 http://groups.google.com/group/ats-lang-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ats-lang-users/0fb3ee84-6d02-4a4f-b835-0de39a8db74d%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages