# Goto or coroutines close to automatons

**URL:** <https://discourse.ats-lang.org/t/goto-or-coroutines-close-to-automatons/750>\
**Category:** Google Group\
**Created:** [April 9, 2015, 2:16pm UTC](https://discourse.ats-lang.org/t/goto-or-coroutines-close-to-automatons/750 "2015-04-09T14:16:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yannick\_Duchene](https://avatars.discourse-cdn.com/v4/letter/y/b77776/32.png) [@Yannick\_Duchene](https://discourse.ats-lang.org/u/Yannick_Duchene)\
**Post date:** [April 9, 2015, 2:16pm UTC](https://discourse.ats-lang.org/t/goto-or-coroutines-close-to-automatons/750/1 "2015-04-09T14:16:27Z")

</div>

As far I know, and according to this newsgroup, ATS2 does not have the goto  
statement I though about to implement coroutines. The coroutines I have in  
mind do not invoke recursive functions (although they invoke functions),  
only iteration (terminal recursion, not the same as general recursion), and  
a discrete value would be enough to determine the point where to resume  
(that’s why I though about goto), beside a reference to the coroutines  
data, which would be a linear type.

Out of curiosity, as I believe coroutines may help me in some ways, is  
there a way to help supporting coroutine with no general recursion? The  
latter point implies obviously bounded an even fixed allocation, which is  
important, as it makes a coroutine implementation easier. Finally, a kind  
of coroutine which is very close to an automaton (with some differences).

I though about automated rewriting, but what I had in mind, involves goto.

For now, this question is just a prospective question, and I though it may  
interest future ATS2 users too.

---

<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:** [April 10, 2015, 2:18pm UTC](https://discourse.ats-lang.org/t/goto-or-coroutines-close-to-automatons/750/2 "2015-04-10T14:18:05Z")

</div>

Only self tail-calls by default.

To turn mutually recursive tail-calls into jumps, you need to use the  
keyword ‘fnx’ (instead of ‘fun’).

To turn all tail-calls into jumps, one usually needs to do CPS-style  
compilation.On Friday, April 10, 2015 at 9:24:06 AM UTC-4, Yannick Duchêne wrote:

> I should have asked this question instead: is ATS guaranteed to optimize  
> self tail calls only or all tail calls?

---

<div class="post-metadata">

**Author:** ![Brandon\_Barker](https://sea1.discourse-cdn.com/flex019/user_avatar/discourse.ats-lang.org/brandon_barker/32/61_2.png) [@Brandon\_Barker](https://discourse.ats-lang.org/u/Brandon_Barker)\
**Post date:** [April 10, 2015, 1:30pm UTC](https://discourse.ats-lang.org/t/goto-or-coroutines-close-to-automatons/750/3 "2015-04-10T13:30:03Z")

</div>

I don’t have much experience with coroutines … so i’m not sure if the  
example posted on the google group a couple of years ago was any use (see  
“leak my coroutine”). In ATS there is a way to have mutual tail call  
optimization  
[http://www.cs.bu.edu/~hwxi/ATS/DOCUMENT/TUTORIALATS/HTML/c244.htmlOn](http://www.cs.bu.edu/~hwxi/ATS/DOCUMENT/TUTORIALATS/HTML/c244.htmlOn) Fri, Apr 10, 2015 at 9:24 AM, ‘Yannick Duchêne’ via ats-lang-users \< [ats-lan...@googlegroups.com](mailto:ats-lan...@googlegroups.com)\> wrote:

> I should have asked this question instead: is ATS guaranteed to optimize  
> self tail calls only or all tail calls?
> 
> –  
> 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/ba812efd-49ad-40a7-a7ac-51cdfebea386%40googlegroups.com](https://groups.google.com/d/msgid/ats-lang-users/ba812efd-49ad-40a7-a7ac-51cdfebea386%40googlegroups.com)  
> [https://groups.google.com/d/msgid/ats-lang-users/ba812efd-49ad-40a7-a7ac-51cdfebea386%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/ats-lang-users/ba812efd-49ad-40a7-a7ac-51cdfebea386%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .

Brandon Barker  
[brandon...@gmail.com](mailto:brandon...@gmail.com)

---

<div class="post-metadata">

**Author:** ![Yannick\_Duchene](https://avatars.discourse-cdn.com/v4/letter/y/b77776/32.png) [@Yannick\_Duchene](https://discourse.ats-lang.org/u/Yannick_Duchene)\
**Post date:** [April 10, 2015, 6:13pm UTC](https://discourse.ats-lang.org/t/goto-or-coroutines-close-to-automatons/750/4 "2015-04-10T18:13:27Z")

</div>

> The original design is this:
> 
> Using ‘fnx’ means to require all the recursive calls in the body of the  
> defined  
> function to be tail-calls. It is yet to be fully enforced.

I like this idea in general, to underline noticeable cases with specific  
keywords or syntax.

---

<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:** [April 10, 2015, 3:53pm UTC](https://discourse.ats-lang.org/t/goto-or-coroutines-close-to-automatons/750/5 "2015-04-10T15:53:16Z")

</div>

The original design is this:

Using ‘fnx’ means to require all the recursive calls in the body of the  
defined  
function to be tail-calls. It is yet to be fully enforced.On Fri, Apr 10, 2015 at 11:33 AM, ‘Yannick Duchêne’ via ats-lang-users \< [ats-lan...@googlegroups.com](mailto:ats-lan...@googlegroups.com)\> wrote:

> Le vendredi 10 avril 2015 16:18:06 UTC+2, gmhwxi a écrit :
> 
> > Only self tail-calls by default.
> > 
> > To turn mutually recursive tail-calls into jumps, you need to use the  
> > keyword ‘fnx’ (instead of ‘fun’).
> 
> (I forgot a question) Why is this not the default? Is there a  
> technical/stability reason?
> 
> –  
> 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/5c652a42-8017-4f86-9a6e-dafa71205b87%40googlegroups.com](https://groups.google.com/d/msgid/ats-lang-users/5c652a42-8017-4f86-9a6e-dafa71205b87%40googlegroups.com)  
> [https://groups.google.com/d/msgid/ats-lang-users/5c652a42-8017-4f86-9a6e-dafa71205b87%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/ats-lang-users/5c652a42-8017-4f86-9a6e-dafa71205b87%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .

---

<div class="post-metadata">

**Author:** ![Yannick\_Duchene](https://avatars.discourse-cdn.com/v4/letter/y/b77776/32.png) [@Yannick\_Duchene](https://discourse.ats-lang.org/u/Yannick_Duchene)\
**Post date:** [April 10, 2015, 1:24pm UTC](https://discourse.ats-lang.org/t/goto-or-coroutines-close-to-automatons/750/6 "2015-04-10T13:24:06Z")

</div>

I should have asked this question instead: is ATS guaranteed to optimize  
self tail calls only or all tail calls?

---

<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:** [April 10, 2015, 4:04pm UTC](https://discourse.ats-lang.org/t/goto-or-coroutines-close-to-automatons/750/7 "2015-04-10T16:04:33Z")

</div>

> > If something is written in this style, how well do ATS support it?

There is currently no support for it in ATS.

If you are interested, you could think of a way to recompile the C code  
generated from ATS source.On Fri, Apr 10, 2015 at 11:31 AM, ‘Yannick Duchêne’ via ats-lang-users \< [ats-lan...@googlegroups.com](mailto:ats-lan...@googlegroups.com)\> wrote:

> Le vendredi 10 avril 2015 16:18:06 UTC+2, gmhwxi a écrit :
> 
> > Only self tail-calls by default.
> > 
> > To turn mutually recursive tail-calls into jumps, you need to use the  
> > keyword ‘fnx’ (instead of ‘fun’).
> 
> I did not knew `fnx`, thanks for the notice.
> 
> To turn all tail-calls into jumps, one usually needs to do CPS-style
> 
> > compilation.
> 
> If something is written in this style, how well do ATS support it?
> 
> –  
> 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/11d53eca-c214-4de0-9c54-549610084edb%40googlegroups.com](https://groups.google.com/d/msgid/ats-lang-users/11d53eca-c214-4de0-9c54-549610084edb%40googlegroups.com)  
> [https://groups.google.com/d/msgid/ats-lang-users/11d53eca-c214-4de0-9c54-549610084edb%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/ats-lang-users/11d53eca-c214-4de0-9c54-549610084edb%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .

---

<div class="post-metadata">

**Author:** ![Yannick\_Duchene](https://avatars.discourse-cdn.com/v4/letter/y/b77776/32.png) [@Yannick\_Duchene](https://discourse.ats-lang.org/u/Yannick_Duchene)\
**Post date:** [April 10, 2015, 3:31pm UTC](https://discourse.ats-lang.org/t/goto-or-coroutines-close-to-automatons/750/8 "2015-04-10T15:31:22Z")

</div>

> Only self tail-calls by default.
> 
> To turn mutually recursive tail-calls into jumps, you need to use the  
> keyword ‘fnx’ (instead of ‘fun’).

I did not knew `fnx`, thanks for the notice.

To turn all tail-calls into jumps, one usually needs to do CPS-style

> compilation.

If something is written in this style, how well do ATS support it?

---

<div class="post-metadata">

**Author:** ![Yannick\_Duchene](https://avatars.discourse-cdn.com/v4/letter/y/b77776/32.png) [@Yannick\_Duchene](https://discourse.ats-lang.org/u/Yannick_Duchene)\
**Post date:** [April 10, 2015, 3:33pm UTC](https://discourse.ats-lang.org/t/goto-or-coroutines-close-to-automatons/750/9 "2015-04-10T15:33:22Z")

</div>

> Only self tail-calls by default.
> 
> To turn mutually recursive tail-calls into jumps, you need to use the  
> keyword ‘fnx’ (instead of ‘fun’).

(I forgot a question) Why is this not the default? Is there a  
technical/stability reason?
