# Handling noreturn functions

**URL:** <https://discourse.ats-lang.org/t/handling-noreturn-functions/170>\
**Category:** Google Group\
**Created:** [September 12, 2014, 5:12pm UTC](https://discourse.ats-lang.org/t/handling-noreturn-functions/170 "2014-09-12T17:12:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shea\_Levy](https://avatars.discourse-cdn.com/v4/letter/s/b38774/32.png) [@Shea\_Levy](https://discourse.ats-lang.org/u/Shea_Levy)\
**Post date:** [September 12, 2014, 5:12pm UTC](https://discourse.ats-lang.org/t/handling-noreturn-functions/170/1 "2014-09-12T17:12:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [September 12, 2014, 5:53pm UTC](https://discourse.ats-lang.org/t/handling-noreturn-functions/170/2 "2014-09-12T17:53:04Z")

</div>

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.On Friday, September 12, 2014 1:40:08 PM UTC-4, Shea Levy wrote:

> 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
> 
> On Fri, Sep 12, 2014 at 10:22:57AM -0700, gmhwxi wrote:
> 
> > 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})
> > 
> > On Friday, September 12, 2014 1:12:51 PM UTC-4, Shea Levy wrote:
> > 
> > > 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
> > 
> > –  
> > 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...@googlegroups.com](mailto:ats-lang...@googlegroups.com) \<javascript:\>.  
> > To post to this group, send email to [ats-l...@googlegroups.com](mailto:ats-l...@googlegroups.com)  
> > \<javascript:\>.  
> > Visit this group at [http://groups.google.com/group/ats-lang-users](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](https://groups.google.com/d/msgid/ats-lang-users/633a0d8e-6d2a-476a-9727-ad3190a74372%40googlegroups.com).

---

<div class="post-metadata">

**Author:** ![Shea\_Levy](https://avatars.discourse-cdn.com/v4/letter/s/b38774/32.png) [@Shea\_Levy](https://discourse.ats-lang.org/u/Shea_Levy)\
**Post date:** [September 13, 2014, 3:11pm UTC](https://discourse.ats-lang.org/t/handling-noreturn-functions/170/3 "2014-09-13T15:11:54Z")

</div>

Perfect!On Fri, Sep 12, 2014 at 12:11:16PM -0700, gmhwxi wrote:

> How about
> 
> ptr0\_get(the\_null\_ptr)
> 
> On Friday, September 12, 2014 3:07:15 PM UTC-4, Shea Levy wrote:
> 
> > Ah, right. I wonder if something like:
> > 
> > > #define FAKE\_VAL(ty) \*((ty \*) NULL)
> > 
> > could be done in ATS?
> > 
> > ~Shea
> > 
> > On Fri, Sep 12, 2014 at 10:53:04AM -0700, gmhwxi wrote:
> > 
> > > 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.
> > > 
> > > On Friday, September 12, 2014 1:40:08 PM UTC-4, Shea Levy wrote:
> > > 
> > > > 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
> > > > 
> > > > On Fri, Sep 12, 2014 at 10:22:57AM -0700, gmhwxi wrote:
> > > > 
> > > > > 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})
> > > > > 
> > > > > On Friday, September 12, 2014 1:12:51 PM UTC-4, Shea Levy wrote:
> > > > > 
> > > > > > 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
> > > > > 
> > > > > –  
> > > > > 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...@googlegroups.com](mailto:ats-lang...@googlegroups.com) \<javascript:\>.  
> > > > > To post to this group, send email to [ats-l...@googlegroups.com](mailto:ats-l...@googlegroups.com)  
> > > > > \<javascript:\>.  
> > > > > Visit this group at [http://groups.google.com/group/ats-lang-users](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](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...@googlegroups.com](mailto:ats-lang...@googlegroups.com) \<javascript:\>.  
> > > To post to this group, send email to [ats-l...@googlegroups.com](mailto:ats-l...@googlegroups.com)  
> > > \<javascript:\>.  
> > > Visit this group at [http://groups.google.com/group/ats-lang-users](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](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](mailto:ats-lang-user...@googlegroups.com).  
> To post to this group, send email to [ats-lan...@googlegroups.com](mailto:ats-lan...@googlegroups.com).  
> Visit this group at [http://groups.google.com/group/ats-lang-users](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](https://groups.google.com/d/msgid/ats-lang-users/0fb3ee84-6d02-4a4f-b835-0de39a8db74d%40googlegroups.com).

---

<div class="post-metadata">

**Author:** ![Shea\_Levy](https://avatars.discourse-cdn.com/v4/letter/s/b38774/32.png) [@Shea\_Levy](https://discourse.ats-lang.org/u/Shea_Levy)\
**Post date:** [September 12, 2014, 7:07pm UTC](https://discourse.ats-lang.org/t/handling-noreturn-functions/170/4 "2014-09-12T19:07:11Z")

</div>

Ah, right. I wonder if something like:

> #define FAKE\_VAL(ty) \*((ty \*) NULL)

could be done in ATS?

~SheaOn Fri, Sep 12, 2014 at 10:53:04AM -0700, gmhwxi wrote:

> 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.
> 
> On Friday, September 12, 2014 1:40:08 PM UTC-4, Shea Levy wrote:
> 
> > 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
> > 
> > On Fri, Sep 12, 2014 at 10:22:57AM -0700, gmhwxi wrote:
> > 
> > > 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})
> > > 
> > > On Friday, September 12, 2014 1:12:51 PM UTC-4, Shea Levy wrote:
> > > 
> > > > 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
> > > 
> > > –  
> > > 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...@googlegroups.com](mailto:ats-lang...@googlegroups.com) \<javascript:\>.  
> > > To post to this group, send email to [ats-l...@googlegroups.com](mailto:ats-l...@googlegroups.com)  
> > > \<javascript:\>.  
> > > Visit this group at [http://groups.google.com/group/ats-lang-users](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](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](mailto:ats-lang-user...@googlegroups.com).  
> To post to this group, send email to [ats-lan...@googlegroups.com](mailto:ats-lan...@googlegroups.com).  
> Visit this group at [http://groups.google.com/group/ats-lang-users](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](https://groups.google.com/d/msgid/ats-lang-users/0041ef17-fbf3-4fa1-9be7-cf3dd4d59fd8%40googlegroups.com).

---

<div class="post-metadata">

**Author:** ![Shea\_Levy](https://avatars.discourse-cdn.com/v4/letter/s/b38774/32.png) [@Shea\_Levy](https://discourse.ats-lang.org/u/Shea_Levy)\
**Post date:** [September 12, 2014, 5:40pm UTC](https://discourse.ats-lang.org/t/handling-noreturn-functions/170/5 "2014-09-12T17:40:05Z")

</div>

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?

~SheaOn Fri, Sep 12, 2014 at 10:22:57AM -0700, gmhwxi wrote:

> 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})
> 
> On Friday, September 12, 2014 1:12:51 PM UTC-4, Shea Levy wrote:
> 
> > 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
> 
> –  
> 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](mailto:ats-lang-user...@googlegroups.com).  
> To post to this group, send email to [ats-lan...@googlegroups.com](mailto:ats-lan...@googlegroups.com).  
> Visit this group at [http://groups.google.com/group/ats-lang-users](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](https://groups.google.com/d/msgid/ats-lang-users/633a0d8e-6d2a-476a-9727-ad3190a74372%40googlegroups.com).

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [September 12, 2014, 5:22pm UTC](https://discourse.ats-lang.org/t/handling-noreturn-functions/170/6 "2014-09-12T17:22:57Z")

</div>

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})On Friday, September 12, 2014 1:12:51 PM UTC-4, Shea Levy wrote:

> 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

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [September 12, 2014, 7:11pm UTC](https://discourse.ats-lang.org/t/handling-noreturn-functions/170/7 "2014-09-12T19:11:16Z")

</div>

How about

ptr0\_get(the\_null\_ptr)On Friday, September 12, 2014 3:07:15 PM UTC-4, Shea Levy wrote:

> Ah, right. I wonder if something like:
> 
> > #define FAKE\_VAL(ty) \*((ty \*) NULL)
> 
> could be done in ATS?
> 
> ~Shea
> 
> On Fri, Sep 12, 2014 at 10:53:04AM -0700, gmhwxi wrote:
> 
> > 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.
> > 
> > On Friday, September 12, 2014 1:40:08 PM UTC-4, Shea Levy wrote:
> > 
> > > 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
> > > 
> > > On Fri, Sep 12, 2014 at 10:22:57AM -0700, gmhwxi wrote:
> > > 
> > > > 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})
> > > > 
> > > > On Friday, September 12, 2014 1:12:51 PM UTC-4, Shea Levy wrote:
> > > > 
> > > > > 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
> > > > 
> > > > –  
> > > > 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...@googlegroups.com](mailto:ats-lang...@googlegroups.com) \<javascript:\>.  
> > > > To post to this group, send email to [ats-l...@googlegroups.com](mailto:ats-l...@googlegroups.com)  
> > > > \<javascript:\>.  
> > > > Visit this group at [http://groups.google.com/group/ats-lang-users](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](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...@googlegroups.com](mailto:ats-lang...@googlegroups.com) \<javascript:\>.  
> > To post to this group, send email to [ats-l...@googlegroups.com](mailto:ats-l...@googlegroups.com)  
> > \<javascript:\>.  
> > Visit this group at [http://groups.google.com/group/ats-lang-users](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](https://groups.google.com/d/msgid/ats-lang-users/0041ef17-fbf3-4fa1-9be7-cf3dd4d59fd8%40googlegroups.com).
