# Recursive datatype and gcc error

**URL:** <https://discourse.ats-lang.org/t/recursive-datatype-and-gcc-error/289>\
**Category:** Google Group\
**Created:** [July 16, 2013, 8:48am UTC](https://discourse.ats-lang.org/t/recursive-datatype-and-gcc-error/289 "2013-07-16T08:48:28Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cyrille\_Duret](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@Cyrille\_Duret](https://discourse.ats-lang.org/u/Cyrille_Duret)\
**Post date:** [July 16, 2013, 8:48am UTC](https://discourse.ats-lang.org/t/recursive-datatype-and-gcc-error/289/1 "2013-07-16T08:48:28Z")

</div>

hello I am trying to implement coroutines with ATS using recursive datatype  
and I have error from the C compilation stage.  
Here is my program :

datatype coroutine\_dt (i: t@ype, o: t@ype) = Coroutine(i, o) of (i -\<cloref1

> (o, coroutine\_dt(i,o)))

extern fun{a, b: t@ype} co\_run(co: coroutine\_dt(a, b), x: a): (b,  
coroutine\_dt(a,b))

implement{a,b} co\_run(co, x) = case+ co of  
| Coroutine (f) =\> f x

fun int\_from(n: int): coroutine\_dt(int,int) = Coroutine(lam x =\> (n,int\_from  
(n+1)))

implement main(argc, argv) = let  
val res = co\_run( int\_from 5, 0 )  
in  
printf(“result from coroutine : %i\n”, @(res.0))  
end

I am aware of the problem cited here (  
[[ats-lang-users] GCC compilation errors: 'tmpXX' undeclared | The ATS PL System](http://sourceforge.net/mailarchive/message.php?msg_id=23656176) ) but I  
cannot understand how to change the boxed types to unboxed ones and how to  
make my little sample program to work.  
Would that mean that I would have to use pointers everywhere ?

Any advices would be greatly appreciated 😉

thanks a lot  
cyrille

---

<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:** [July 27, 2013, 3:54am UTC](https://discourse.ats-lang.org/t/recursive-datatype-and-gcc-error/289/2 "2013-07-27T03:54:46Z")

</div>

I translated a couple of functions:

[https://github.com/githwxi/ATS-Postiats/tree/master/doc/PROJECT/SMALL/coroutine](https://github.com/githwxi/ATS-Postiats/tree/master/doc/PROJECT/SMALL/coroutine)

This should indicate a way to assign a linear type to co-routines. Please  
feel free to use  
the code to implement other functions.

If a coroutine is assigned a linear type, then calling co\_run on the  
coroutine also frees it.  
This way to manage memory can be much more efficient then using GC.

Also, I suggest to make ‘coroutine’ an abstract type; make ‘event’ an  
abstract type, too.

---

<div class="post-metadata">

**Author:** ![Cyrille\_Duret](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@Cyrille\_Duret](https://discourse.ats-lang.org/u/Cyrille_Duret)\
**Post date:** [August 5, 2013, 10:12pm UTC](https://discourse.ats-lang.org/t/recursive-datatype-and-gcc-error/289/3 "2013-08-05T22:12:02Z")

</div>

I try to port my frp functions from your sample code but I have a problem :

the event type is also a viewtype so a coroutine should use more that once  
a single resource.

A simple example :

extern fun{a,b: vt0p} co\_app\_pure(x: b): coroutine\_vt(a,b)

implement{a,b} co\_app\_pure(x) = coroutine\_of\_linc{a,b}(llam \_ =\> (x,co\_app\_pure  
\<a,b\>(x)))

By using viewt@ype as base types for my coroutines functions, I have an  
error because x is consumed twice so the compiler error :  
… there is no type for the dynamic variable [x]

My question is : how to overcome this ?  
thanks a lot 😉

---

<div class="post-metadata">

**Author:** ![Cyrille\_Duret](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@Cyrille\_Duret](https://discourse.ats-lang.org/u/Cyrille_Duret)\
**Post date:** [July 16, 2013, 7:55pm UTC](https://discourse.ats-lang.org/t/recursive-datatype-and-gcc-error/289/4 "2013-07-16T19:55:24Z")

</div>

This technique is quite straightforward to achieve expressive FRP.

Knowing that the haskell community is quite large, most of resources  
available on the internet does apply to haskell language.

As an intermediate haskell user, I would say that linear type and dependent  
type are quite different of all programming habits around, therefore the  
amount of efforts to get into linear and dependent thinking is high.

The haskell community has a great thing : The monad Reader  
( [http://themonadreader.wordpress.com/](http://themonadreader.wordpress.com/) ) It is an electronic magazine that  
demonstrate the power of underlying architecture of a haskell : monads.  
Maybe would be great to have The linear/dependent type Reader to show to  
the world the power of ATS 😉

---

<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:** [July 16, 2013, 6:29pm UTC](https://discourse.ats-lang.org/t/recursive-datatype-and-gcc-error/289/5 "2013-07-16T18:29:37Z")

</div>

Thanks for the interesting link about FRP; this is different than the  
notion of coroutine I was looking at, so what I said doesn’t apply.

---

<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:** [August 7, 2013, 12:59pm UTC](https://discourse.ats-lang.org/t/recursive-datatype-and-gcc-error/289/6 "2013-08-07T12:59:00Z")

</div>

> > last coroutine cannot be freed

That is exact my thought 🙂

You can always turn GC on so that the last coroutine will be eventually  
collected.

---

<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:** [July 16, 2013, 2:03pm UTC](https://discourse.ats-lang.org/t/recursive-datatype-and-gcc-error/289/7 "2013-07-16T14:03:52Z")

</div>

FYI, I just tried your example in ATS2 and also observed the C compiler  
problem.

I wasn’t familiar with coroutines, but after looking at them, I wonder if  
mutually tail recursive closures, at least in ATS, might have the same  
benefits as coroutines (as well as perhaps being easier to read) - aside  
from having to put the “yield” in the tail position.

---

<div class="post-metadata">

**Author:** ![Cyrille\_Duret](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@Cyrille\_Duret](https://discourse.ats-lang.org/u/Cyrille_Duret)\
**Post date:** [July 26, 2013, 9:23am UTC](https://discourse.ats-lang.org/t/recursive-datatype-and-gcc-error/289/8 "2013-07-26T09:23:33Z")

</div>

I have made a git repo for my sample  
: [https://github.com/cduret/ats\_samples/tree/master/frp](https://github.com/cduret/ats_samples/tree/master/frp)  
😉

---

<div class="post-metadata">

**Author:** ![Cyrille\_Duret](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@Cyrille\_Duret](https://discourse.ats-lang.org/u/Cyrille_Duret)\
**Post date:** [August 9, 2013, 9:53am UTC](https://discourse.ats-lang.org/t/recursive-datatype-and-gcc-error/289/9 "2013-08-09T09:53:40Z")

</div>

hi  
I have problem in implementing templates with viewtype as you explained me  
before.

Simple example :

staload "prelude/SATS/list\_vt.sats"  
staload \_(_anonymous_) = “prelude/DATS/list\_vt.dats”

(\* event signature \*)  
absviewtype event\_vt (a: t@ype+) = ptr

extern castfn event\_of\_list {a: t@ype}{n: nat} (l: list\_vt(a, n)):\<\>event\_vt a  
extern castfn list\_of\_event {a: t@ype} (e: event\_vt a):\<\> [n: nat] list\_vt(a  
, n)

// generic functions  
extern fun{a:vt0p} print\_resource(x: a): void  
extern fun{a:t@ype} print\_free\_list{n: nat} (l: list\_vt(a, n)): void

implement print\_resource(x) = printf("%d ", @(x))  
implement print\_resource\<event\_vt(int)\>(x) = let  
val l = list\_of\_event{int} x  
in  
print\_free\_list l  
end

implement print\_resource(x) = printf("%f ", @(double\_of\_float x))  
implement print\_resource\<event\_vt(float)\>(x) = let  
val l = list\_of\_event{float} x  
in  
print\_free\_list l  
end

implement{a} print\_free\_list(l) = case+ l of  
| ~list\_vt\_nil () =\> ()  
| ~list\_vt\_cons (x, xs) =\> let  
val () = print\_resource(x)  
val () = print\_free\_list xs  
in print("\n") end

implement main () = let  
val e = event\_of\_list{int}(list\_vt\_cons(1, list\_vt\_cons(2, list\_vt\_nil())))  
in  
print\_resource e  
end

this typecheck but at the C compilation an error occurs :  
bug\_v\_dats.c:238: error: redefinition of ‘print\_resource\_01812\_ats\_ptr\_type’  
bug\_v\_dats.c:172: error: previous definition of  
’print\_resource\_01812\_ats\_ptr\_type’ was here

if I comment the “implement print\_resource\<event\_vt(float)\>(x)” function it  
works ☹

---

<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:** [July 17, 2013, 1:26pm UTC](https://discourse.ats-lang.org/t/recursive-datatype-and-gcc-error/289/10 "2013-07-17T13:26:04Z")

</div>

> > As an intermediate haskell user, I would say that linear type and  
> > dependent type are quite different of all programming habits around,  
> > therefore the amount of efforts to get into linear and dependent thinking  
> > is high.

Dependent types are tied to quantification in logic. My own teaching  
experience tells me that a majority of programmers have difficulty dealing  
with quantifiers.  
This is a bit like in Calculus: most people learning calculus cannot  
precisely define what continuity really is; this concept involves two  
quantifies: forall … exists …;  
fewer people could understand uniform continuity: exists … forall …  
exists. Actually many mathematicians proved false theorems in 19th century  
because they messed up with the concept of uniform continuity (e.g., Dini  
theorem is a good example involving this concept)

Linear types are quite different. This concept is tied to our intuition  
about objects in the physical world. My own teaching experience indicates  
that most programmers have no difficulty using linear types.

> > The haskell community has a great thing : The monad Reader (  
> > [http://themonadreader.wordpress.com/](http://themonadreader.wordpress.com/) ) It is an electronic magazine that  
> > demonstrate the power of underlying architecture of a haskell : monads.

I took a glance. Very nice. However, we must not forget the basic  
philosophical point:

You can see the physical world through the lens of monad or just see it as  
it is (linear types).

> > Maybe would be great to have The linear/dependent type Reader to show to  
> > the world the power of ATS 😉

I could only do so much. It is up to the users of ATS to show the world the  
power of ATS. Of course, I will be happy to contribute.

Cheers!

---

<div class="post-metadata">

**Author:** ![Cyrille\_Duret](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@Cyrille\_Duret](https://discourse.ats-lang.org/u/Cyrille_Duret)\
**Post date:** [August 9, 2013, 2:28pm UTC](https://discourse.ats-lang.org/t/recursive-datatype-and-gcc-error/289/11 "2013-08-09T14:28:14Z")

</div>

by the way what is the status of ATS2 development ?

I heard you could make a release this summer ?

cheers 😉

---

<div class="post-metadata">

**Author:** ![Cyrille\_Duret](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@Cyrille\_Duret](https://discourse.ats-lang.org/u/Cyrille_Duret)\
**Post date:** [July 16, 2013, 3:36pm UTC](https://discourse.ats-lang.org/t/recursive-datatype-and-gcc-error/289/12 "2013-07-16T15:36:44Z")

</div>

I try to implement functional FRP through use of arrows as described here :

> <https://github.com/leonidas/codeblog/blob/master/2012/2012-01-08-streams-coroutines.md>

I already implemented that style of pull reactive architecture with dynamic  
languages and it works quite well.

If implementing coroutines with mutually recursive closures could result in  
a better code, I’d be happy to look that way too 😉

---

<div class="post-metadata">

**Author:** ![Cyrille\_Duret](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@Cyrille\_Duret](https://discourse.ats-lang.org/u/Cyrille_Duret)\
**Post date:** [August 9, 2013, 2:17pm UTC](https://discourse.ats-lang.org/t/recursive-datatype-and-gcc-error/289/13 "2013-08-09T14:17:04Z")

</div>

thanks,  
that’s a good argument to switch indeed 😉

---

<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:** [August 6, 2013, 1:14am UTC](https://discourse.ats-lang.org/t/recursive-datatype-and-gcc-error/289/14 "2013-08-06T01:14:24Z")

</div>

coroutine (a, b) was defined as: (a) -\<lin,cloptr1\> b

If you allow a and b to be linear types, then there are two possibilities  
for coroutine(a,b)

1. (a) -\<lin,cloptr1\> b
2. (!a) -\<lin,cloptr1\> b

I think that the latter is a more natural definition.  
Questions like this that rise naturally in a linear setting can be really  
interesting. However, they have not  
be carefully studied yet.

---

<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:** [August 6, 2013, 9:25pm UTC](https://discourse.ats-lang.org/t/recursive-datatype-and-gcc-error/289/15 "2013-08-06T21:25:07Z")

</div>

You went too far 🙂

You could introduce a function:

extern fun{a:vt@pye} co\_arr\_fanout$dup (x: a): (a, a)

When implementing the following one,

extern fun{a,b,c:vt@ype} co\_arr\_fanout(cof: coroutine\_vt(a, b), cog:coroutine\_vt  
(a, c)): coroutine\_vt(a, (b,c))

you make use of co\_arr\_fanout$dup. Then you implement a special version:

implement(a)  
co\_arr\_fanout$dup\<event(a)\> (x) = …

Now you can call co\_arr\_fanout\<event(…)\>

The syntax I used above only works in ATS2.

Cheers!

---

<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:** [August 9, 2013, 4:17pm UTC](https://discourse.ats-lang.org/t/recursive-datatype-and-gcc-error/289/16 "2013-08-09T16:17:15Z")

</div>

Yes, I am actively working on it. I want to use ATS2 for my class in Fall  
2013.

Cheers!

---

<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:** [August 9, 2013, 2:03pm UTC](https://discourse.ats-lang.org/t/recursive-datatype-and-gcc-error/289/17 "2013-08-09T14:03:25Z")

</div>

The template system of ATS1 is very primitive; it is an  
add-on feature after ATS1 is already implemented.

The following two instantiations are given the same name:

implement print\_resource\<event\_vt(int)\>(x) = …  
implement print\_resource\<event\_vt(float)\>(x) = …

So they cannot be used in the same file.

I suggest that you switch to ATS2.

---

<div class="post-metadata">

**Author:** ![Cyrille\_Duret](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@Cyrille\_Duret](https://discourse.ats-lang.org/u/Cyrille_Duret)\
**Post date:** [August 7, 2013, 12:46pm UTC](https://discourse.ats-lang.org/t/recursive-datatype-and-gcc-error/289/18 "2013-08-07T12:46:38Z")

</div>

thanks

So it means that only the last coroutine cannot be freed from my program.  
I can live with that for now 😉

---

<div class="post-metadata">

**Author:** ![Cyrille\_Duret](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@Cyrille\_Duret](https://discourse.ats-lang.org/u/Cyrille_Duret)\
**Post date:** [July 25, 2013, 2:59pm UTC](https://discourse.ats-lang.org/t/recursive-datatype-and-gcc-error/289/19 "2013-07-25T14:59:21Z")

</div>

hi I have made a fairly simple implementation of FRP system with coroutines.

Just in case of interest 😉

[http://pastie.org/8174621](http://pastie.org/8174621)

next challenge is to use linear type to define coroutines.

any clue ?

thanks !

---

<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:** [August 6, 2013, 1:56am UTC](https://discourse.ats-lang.org/t/recursive-datatype-and-gcc-error/289/20 "2013-08-06T01:56:37Z")

</div>

After studying the issue further, I realized that

1. (a) -\<lin,cloptr1\> b

should be the definition from coroutine(a, b) even when a and b are linear.

In the fanout case, a needs to be non-linear:

[Next page](https://discourse.ats-lang.org/t/recursive-datatype-and-gcc-error/289.md?page=2)
