# Dependent type is hard

**URL:** <https://discourse.ats-lang.org/t/dependent-type-is-hard/699>\
**Category:** Google Group\
**Created:** [January 12, 2015, 5:41pm UTC](https://discourse.ats-lang.org/t/dependent-type-is-hard/699 "2015-01-12T17:41:15Z")\
**Posts on this page:** 9\
**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:** [January 12, 2015, 5:41pm UTC](https://discourse.ats-lang.org/t/dependent-type-is-hard/699/1 "2015-01-12T17:41:15Z")

</div>

hi,  
I am still trying to implement my parser combinator and I have big troubles  
with dependent types because some structures are mutated and some closures  
are reused.  
Code is at :  
[http://www.ats-lang.org/SERVER/MYCODE/Patsoptaas\_serve.php?mycode\_url=http://pastebin.com/raw.php?i=avXpjG91](http://www.ats-lang.org/SERVER/MYCODE/Patsoptaas_serve.php?mycode_url=http://pastebin.com/raw.php?i=avXpjG91)

Basically in this sample, the count\_dvt acts like a stream and can be  
modified when some data consumed. (follows this pattern  
: [https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/ATS-QA-LIST/qa-list-2015-01-03-2.dats](https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/ATS-QA-LIST/qa-list-2015-01-03-2.dats))

And action\_vt acts like a combinator. It defines some actions to be done on  
count\_dvt.

What is hard is I want action\_vt to be used any number of times and  
count\_dvt to be mutable at any point.

Any advices to make it works ?

---

<div class="post-metadata">

**Author:** ![Artyom\_Shalkhakov](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@Artyom\_Shalkhakov](https://discourse.ats-lang.org/u/Artyom_Shalkhakov)\
**Post date:** [January 13, 2015, 9:29am UTC](https://discourse.ats-lang.org/t/dependent-type-is-hard/699/2 "2015-01-13T09:29:30Z")

</div>

Hello Cyrille,

> hi,  
> I am still trying to implement my parser combinator and I have big  
> troubles with dependent types because some structures are mutated and some  
> closures are reused.  
> Code is at :  
> [http://www.ats-lang.org/SERVER/MYCODE/Patsoptaas\_serve.php?mycode\_url=http://pastebin.com/raw.php?i=avXpjG91](http://www.ats-lang.org/SERVER/MYCODE/Patsoptaas_serve.php?mycode_url=http://pastebin.com/raw.php?i=avXpjG91)  
> [http://www.google.com/url?q=http%3A%2F%2Fwww.ats-lang.org%2FSERVER%2FMYCODE%2FPatsoptaas\_serve.php%3Fmycode\_url%3Dhttp%3A%2F%2Fpastebin.com%2Fraw.php%3Fi%3DavXpjG91&sa=D&sntz=1&usg=AFQjCNHSco7SiAo2MGLGd2qwXit8kq0H0w](http://www.google.com/url?q=http%3A%2F%2Fwww.ats-lang.org%2FSERVER%2FMYCODE%2FPatsoptaas_serve.php%3Fmycode_url%3Dhttp%3A%2F%2Fpastebin.com%2Fraw.php%3Fi%3DavXpjG91&sa=D&sntz=1&usg=AFQjCNHSco7SiAo2MGLGd2qwXit8kq0H0w)
> 
> Basically in this sample, the count\_dvt acts like a stream and can be  
> modified when some data consumed. (follows this pattern :  
> [https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/ATS-QA-LIST/qa-list-2015-01-03-2.dats](https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/ATS-QA-LIST/qa-list-2015-01-03-2.dats)  
> )
> 
> And action\_vt acts like a combinator. It defines some actions to be done  
> on count\_dvt.

Why are you using indexed datatypes here? Could you drop the indices? I  
guess you’ve extracted the code from a larger program where the indices  
were necessary for something.

Here’s the same thing sans indexes:  
[http://www.ats-lang.org/SERVER/MYCODE/Patsoptaas\_serve.php?mycode\_url=http://pastebin.com/raw.php?i=3Sr2W1qk](http://www.ats-lang.org/SERVER/MYCODE/Patsoptaas_serve.php?mycode_url=http://pastebin.com/raw.php?i=3Sr2W1qk)

> What is hard is I want action\_vt to be used any number of times and  
> count\_dvt to be mutable at any point.

You could implement a function for explicitly duplicating/sharing the  
linear value:

extern  
fun dup\_count\_vt (count: !count\_dvt): count\_dvt

And use it any time you want to get one more reference to the data  
structure.

Internally, such a function would either do a deep copy of the passed in  
argument (no refcounting is needed in this case), or it would bump the  
refcount (if refcounting is used). Note that in the former case, mutation  
doesn’t “propagate” to the copies (which is probably not what you want).

Hope this helps.

---

<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:** [January 17, 2015, 8:22pm UTC](https://discourse.ats-lang.org/t/dependent-type-is-hard/699/3 "2015-01-17T20:22:55Z")

</div>

I took a quick look.

The style you use is the standard functional style.

Once you are done, you could try something along the following line.

In ATS, a simpler and more efficient way is to use the following type  
for a parser:

typedef parser (t(_token_), a(_ast_)) = (&pstate(t) \>\> \_) - a

where pstate is for the current state of parsing. You could imagine that  
pstate contains the input and also information for reporting error messages.  
In the case of a parsing failure, an exception is raised. I have an  
unfinished  
project following what is outlined above:

[https://github.com/githwxi/ATS-Postiats-contrib/tree/master/contrib/libats-/hwxi/parcomb](https://github.com/githwxi/ATS-Postiats-contrib/tree/master/contrib/libats-/hwxi/parcomb)

If you do not like exceptions, then use

typedef parser (t(_token_), a(_ast_)) = (&pstate(t) \>\> \_) -  
Option\_vt(a)

So a parser is very much like a state monad.

Later, we could have linear parsers:

datatype  
parser(a, t) = PARSER(int(_ref-count_), &pstate(t) \>\> \_) - a)

This makes it fairly straightforward to free parsers constructed during  
parsing.

An example of reference counting can be found in the Locking and Unlocking  
section  
at the bottom of the ATS Intro book:

[http://ats-lang.sourceforge.net/DOCUMENT/INT2PROGINATS/HTML/INT2PROGINATS-BOOK.htmlOn](http://ats-lang.sourceforge.net/DOCUMENT/INT2PROGINATS/HTML/INT2PROGINATS-BOOK.htmlOn) Saturday, January 17, 2015 at 12:08:59 PM UTC-5, Cyrille Duret wrote:

> I see how long is the path to ATS enlightenment 🙂
> 
> My first attempt to define a simple parser combinator without any  
> dependent nor linear feature : [#include "share/atspre\_staload.hats"#include "share/HATS/atspre\_staload\_libats - Pastebin.com](http://pastebin.com/vBbDFxzJ)
> 
> By now I just defined string and seq combinators.  
> I will add some useful ones such as regex (using pcre), many, any, etc…
> 
> One can compile it with :  
> $ curl “[http://pastebin.com/raw.php?i=vBbDFxzJ](http://pastebin.com/raw.php?i=vBbDFxzJ)” \> test\_parser\_ML.dats  
> $ patscc -DATS\_MEMALLOC\_GCBDW -o test\_parser\_ML test\_parser\_ML.dats -lgc  
> -latslib
> 
> My inspiration are both from C++ and javascript world :
> 
> - [C++ Parser Combinator library](http://www.cs.hs-rm.de/~panitz/paper/cpppl/index.html)
> - [GitHub - jneen/parsimmon: A monadic LL(infinity) parser combinator library for javascript](https://github.com/jneen/parsimmon)
> 
> I understand now it is better to create the “ML” version first, then  
> modify it progressively to add linear types and dependent types step by  
> step.
> 
> My question is : can we mix some code using garbage collection and linear  
> type in the same program ?  
> In that case what would be the compilation command line ?
> 
> Le jeudi 15 janvier 2015 20:58:34 UTC+1, gmhwxi a écrit :
> 
> > Well, I think this is a bit like playing a music instrument.  
> > Practice is needed. Before one could play, say, Chopin, one needs to try  
> > playing a lot of simpler pieces 🙂
> > 
> > There is no “magic” in typechecking in ATS. To use advanced features  
> > in ATS effectively, one really needs to know _how_ a program is  
> > typechecked.
> > 
> > On Wednesday, January 14, 2015 at 11:08:39 AM UTC-5, Cyrille Duret wrote:
> > 
> > > thanks Artyom,  
> > > I see dependent types can be really hard with imperative code.
> > > 
> > > Le mardi 13 janvier 2015 10:29:30 UTC+1, Artyom Shalkhakov a écrit :
> > > 
> > > > Hello Cyrille,
> > > > 
> > > > On Monday, January 12, 2015 at 11:41:15 PM UTC+6, Cyrille Duret wrote:
> > > > 
> > > > > hi,  
> > > > > I am still trying to implement my parser combinator and I have big  
> > > > > troubles with dependent types because some structures are mutated and some  
> > > > > closures are reused.  
> > > > > Code is at :  
> > > > > [http://www.ats-lang.org/SERVER/MYCODE/Patsoptaas\_serve.php?mycode\_url=http://pastebin.com/raw.php?i=avXpjG91](http://www.ats-lang.org/SERVER/MYCODE/Patsoptaas_serve.php?mycode_url=http://pastebin.com/raw.php?i=avXpjG91)  
> > > > > [http://www.google.com/url?q=http%3A%2F%2Fwww.ats-lang.org%2FSERVER%2FMYCODE%2FPatsoptaas\_serve.php%3Fmycode\_url%3Dhttp%3A%2F%2Fpastebin.com%2Fraw.php%3Fi%3DavXpjG91&sa=D&sntz=1&usg=AFQjCNHSco7SiAo2MGLGd2qwXit8kq0H0w](http://www.google.com/url?q=http%3A%2F%2Fwww.ats-lang.org%2FSERVER%2FMYCODE%2FPatsoptaas_serve.php%3Fmycode_url%3Dhttp%3A%2F%2Fpastebin.com%2Fraw.php%3Fi%3DavXpjG91&sa=D&sntz=1&usg=AFQjCNHSco7SiAo2MGLGd2qwXit8kq0H0w)
> > > > > 
> > > > > Basically in this sample, the count\_dvt acts like a stream and can be  
> > > > > modified when some data consumed. (follows this pattern :  
> > > > > [https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/ATS-QA-LIST/qa-list-2015-01-03-2.dats](https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/ATS-QA-LIST/qa-list-2015-01-03-2.dats)  
> > > > > )
> > > > > 
> > > > > And action\_vt acts like a combinator. It defines some actions to be  
> > > > > done on count\_dvt.
> > > > 
> > > > Why are you using indexed datatypes here? Could you drop the indices? I  
> > > > guess you’ve extracted the code from a larger program where the indices  
> > > > were necessary for something.
> > > > 
> > > > Here’s the same thing sans indexes:  
> > > > [http://www.ats-lang.org/SERVER/MYCODE/Patsoptaas\_serve.php?mycode\_url=http://pastebin.com/raw.php?i=3Sr2W1qk](http://www.ats-lang.org/SERVER/MYCODE/Patsoptaas_serve.php?mycode_url=http://pastebin.com/raw.php?i=3Sr2W1qk)
> > > > 
> > > > > What is hard is I want action\_vt to be used any number of times and  
> > > > > count\_dvt to be mutable at any point.
> > > > 
> > > > You could implement a function for explicitly duplicating/sharing the  
> > > > linear value:
> > > > 
> > > > extern  
> > > > fun dup\_count\_vt (count: !count\_dvt): count\_dvt
> > > > 
> > > > And use it any time you want to get one more reference to the data  
> > > > structure.
> > > > 
> > > > Internally, such a function would either do a deep copy of the passed  
> > > > in argument (no refcounting is needed in this case), or it would bump the  
> > > > refcount (if refcounting is used). Note that in the former case, mutation  
> > > > doesn’t “propagate” to the copies (which is probably not what you want).
> > > > 
> > > > Hope this helps.
> > > > 
> > > > > Any advices to make 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:** [January 12, 2015, 7:06pm UTC](https://discourse.ats-lang.org/t/dependent-type-is-hard/699/4 "2015-01-12T19:06:10Z")

</div>

Implementing a linear version of parsing combinators can be challenging.  
It may make sense if you try to do a non-linear version in ATS first.

Due to the need for backtracking, tokens need to be saved into a buffers  
so that they may be used later. For unlimited backtracking, all the tokens  
need to be saved. So one may simply read all the tokens into an array  
(you can use libats/dynarray for this) before parsing.

To have linear parsers, you are likely in need of reference counting:

> <https://github.com/githwxi/ATS-Postiats/blob/master/libats/SATS/refcount.sats>

because parsers may be shared in the construction of other parsers.

So it is somewhat involved. I have yet to see a package of linear parsing  
combinators.  
If there is already one, then it is probably done in C++.On Monday, January 12, 2015 at 12:41:15 PM UTC-5, Cyrille Duret wrote:

> hi,  
> I am still trying to implement my parser combinator and I have big  
> troubles with dependent types because some structures are mutated and some  
> closures are reused.  
> Code is at :  
> [http://www.ats-lang.org/SERVER/MYCODE/Patsoptaas\_serve.php?mycode\_url=http://pastebin.com/raw.php?i=avXpjG91](http://www.ats-lang.org/SERVER/MYCODE/Patsoptaas_serve.php?mycode_url=http://pastebin.com/raw.php?i=avXpjG91)
> 
> Basically in this sample, the count\_dvt acts like a stream and can be  
> modified when some data consumed. (follows this pattern :  
> [https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/ATS-QA-LIST/qa-list-2015-01-03-2.dats](https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/ATS-QA-LIST/qa-list-2015-01-03-2.dats)  
> )
> 
> And action\_vt acts like a combinator. It defines some actions to be done  
> on count\_dvt.
> 
> What is hard is I want action\_vt to be used any number of times and  
> count\_dvt to be mutable at any point.
> 
> Any advices to make it works ?

---

<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:** [January 17, 2015, 5:08pm UTC](https://discourse.ats-lang.org/t/dependent-type-is-hard/699/5 "2015-01-17T17:08:59Z")

</div>

I see how long is the path to ATS enlightenment 🙂

My first attempt to define a simple parser combinator without any dependent  
nor linear feature : [#include "share/atspre\_staload.hats"#include "share/HATS/atspre\_staload\_libats - Pastebin.com](http://pastebin.com/vBbDFxzJ)

By now I just defined string and seq combinators.  
I will add some useful ones such as regex (using pcre), many, any, etc…

One can compile it with :  
$ curl “[http://pastebin.com/raw.php?i=vBbDFxzJ](http://pastebin.com/raw.php?i=vBbDFxzJ)” \> test\_parser\_ML.dats  
$ patscc -DATS\_MEMALLOC\_GCBDW -o test\_parser\_ML test\_parser\_ML.dats -lgc  
-latslib

My inspiration are both from C++ and javascript world :

- [C++ Parser Combinator library](http://www.cs.hs-rm.de/~panitz/paper/cpppl/index.html)
- [GitHub - jneen/parsimmon: A monadic LL(infinity) parser combinator library for javascript](https://github.com/jneen/parsimmon)

I understand now it is better to create the “ML” version first, then modify  
it progressively to add linear types and dependent types step by step.

My question is : can we mix some code using garbage collection and linear  
type in the same program ?  
In that case what would be the compilation command line ?Le jeudi 15 janvier 2015 20:58:34 UTC+1, gmhwxi a écrit :

> Well, I think this is a bit like playing a music instrument.  
> Practice is needed. Before one could play, say, Chopin, one needs to try  
> playing a lot of simpler pieces 🙂
> 
> There is no “magic” in typechecking in ATS. To use advanced features  
> in ATS effectively, one really needs to know _how_ a program is  
> typechecked.
> 
> On Wednesday, January 14, 2015 at 11:08:39 AM UTC-5, Cyrille Duret wrote:
> 
> > thanks Artyom,  
> > I see dependent types can be really hard with imperative code.
> > 
> > Le mardi 13 janvier 2015 10:29:30 UTC+1, Artyom Shalkhakov a écrit :
> > 
> > > Hello Cyrille,
> > > 
> > > On Monday, January 12, 2015 at 11:41:15 PM UTC+6, Cyrille Duret wrote:
> > > 
> > > > hi,  
> > > > I am still trying to implement my parser combinator and I have big  
> > > > troubles with dependent types because some structures are mutated and some  
> > > > closures are reused.  
> > > > Code is at :  
> > > > [http://www.ats-lang.org/SERVER/MYCODE/Patsoptaas\_serve.php?mycode\_url=http://pastebin.com/raw.php?i=avXpjG91](http://www.ats-lang.org/SERVER/MYCODE/Patsoptaas_serve.php?mycode_url=http://pastebin.com/raw.php?i=avXpjG91)  
> > > > [http://www.google.com/url?q=http%3A%2F%2Fwww.ats-lang.org%2FSERVER%2FMYCODE%2FPatsoptaas\_serve.php%3Fmycode\_url%3Dhttp%3A%2F%2Fpastebin.com%2Fraw.php%3Fi%3DavXpjG91&sa=D&sntz=1&usg=AFQjCNHSco7SiAo2MGLGd2qwXit8kq0H0w](http://www.google.com/url?q=http%3A%2F%2Fwww.ats-lang.org%2FSERVER%2FMYCODE%2FPatsoptaas_serve.php%3Fmycode_url%3Dhttp%3A%2F%2Fpastebin.com%2Fraw.php%3Fi%3DavXpjG91&sa=D&sntz=1&usg=AFQjCNHSco7SiAo2MGLGd2qwXit8kq0H0w)
> > > > 
> > > > Basically in this sample, the count\_dvt acts like a stream and can be  
> > > > modified when some data consumed. (follows this pattern :  
> > > > [https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/ATS-QA-LIST/qa-list-2015-01-03-2.dats](https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/ATS-QA-LIST/qa-list-2015-01-03-2.dats)  
> > > > )
> > > > 
> > > > And action\_vt acts like a combinator. It defines some actions to be  
> > > > done on count\_dvt.
> > > 
> > > Why are you using indexed datatypes here? Could you drop the indices? I  
> > > guess you’ve extracted the code from a larger program where the indices  
> > > were necessary for something.
> > > 
> > > Here’s the same thing sans indexes:  
> > > [http://www.ats-lang.org/SERVER/MYCODE/Patsoptaas\_serve.php?mycode\_url=http://pastebin.com/raw.php?i=3Sr2W1qk](http://www.ats-lang.org/SERVER/MYCODE/Patsoptaas_serve.php?mycode_url=http://pastebin.com/raw.php?i=3Sr2W1qk)
> > > 
> > > > What is hard is I want action\_vt to be used any number of times and  
> > > > count\_dvt to be mutable at any point.
> > > 
> > > You could implement a function for explicitly duplicating/sharing the  
> > > linear value:
> > > 
> > > extern  
> > > fun dup\_count\_vt (count: !count\_dvt): count\_dvt
> > > 
> > > And use it any time you want to get one more reference to the data  
> > > structure.
> > > 
> > > Internally, such a function would either do a deep copy of the passed in  
> > > argument (no refcounting is needed in this case), or it would bump the  
> > > refcount (if refcounting is used). Note that in the former case, mutation  
> > > doesn’t “propagate” to the copies (which is probably not what you want).
> > > 
> > > Hope this helps.
> > > 
> > > > Any advices to make 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:** [January 15, 2015, 7:58pm UTC](https://discourse.ats-lang.org/t/dependent-type-is-hard/699/6 "2015-01-15T19:58:34Z")

</div>

Well, I think this is a bit like playing a music instrument.  
Practice is needed. Before one could play, say, Chopin, one needs to try  
playing a lot of simpler pieces 🙂

There is no “magic” in typechecking in ATS. To use advanced features  
in ATS effectively, one really needs to know _how_ a program is typechecked.On Wednesday, January 14, 2015 at 11:08:39 AM UTC-5, Cyrille Duret wrote:

> thanks Artyom,  
> I see dependent types can be really hard with imperative code.
> 
> Le mardi 13 janvier 2015 10:29:30 UTC+1, Artyom Shalkhakov a écrit :
> 
> > Hello Cyrille,
> > 
> > On Monday, January 12, 2015 at 11:41:15 PM UTC+6, Cyrille Duret wrote:
> > 
> > > hi,  
> > > I am still trying to implement my parser combinator and I have big  
> > > troubles with dependent types because some structures are mutated and some  
> > > closures are reused.  
> > > Code is at :  
> > > [http://www.ats-lang.org/SERVER/MYCODE/Patsoptaas\_serve.php?mycode\_url=http://pastebin.com/raw.php?i=avXpjG91](http://www.ats-lang.org/SERVER/MYCODE/Patsoptaas_serve.php?mycode_url=http://pastebin.com/raw.php?i=avXpjG91)  
> > > [http://www.google.com/url?q=http%3A%2F%2Fwww.ats-lang.org%2FSERVER%2FMYCODE%2FPatsoptaas\_serve.php%3Fmycode\_url%3Dhttp%3A%2F%2Fpastebin.com%2Fraw.php%3Fi%3DavXpjG91&sa=D&sntz=1&usg=AFQjCNHSco7SiAo2MGLGd2qwXit8kq0H0w](http://www.google.com/url?q=http%3A%2F%2Fwww.ats-lang.org%2FSERVER%2FMYCODE%2FPatsoptaas_serve.php%3Fmycode_url%3Dhttp%3A%2F%2Fpastebin.com%2Fraw.php%3Fi%3DavXpjG91&sa=D&sntz=1&usg=AFQjCNHSco7SiAo2MGLGd2qwXit8kq0H0w)
> > > 
> > > Basically in this sample, the count\_dvt acts like a stream and can be  
> > > modified when some data consumed. (follows this pattern :  
> > > [https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/ATS-QA-LIST/qa-list-2015-01-03-2.dats](https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/ATS-QA-LIST/qa-list-2015-01-03-2.dats)  
> > > )
> > > 
> > > And action\_vt acts like a combinator. It defines some actions to be done  
> > > on count\_dvt.
> > 
> > Why are you using indexed datatypes here? Could you drop the indices? I  
> > guess you’ve extracted the code from a larger program where the indices  
> > were necessary for something.
> > 
> > Here’s the same thing sans indexes:  
> > [http://www.ats-lang.org/SERVER/MYCODE/Patsoptaas\_serve.php?mycode\_url=http://pastebin.com/raw.php?i=3Sr2W1qk](http://www.ats-lang.org/SERVER/MYCODE/Patsoptaas_serve.php?mycode_url=http://pastebin.com/raw.php?i=3Sr2W1qk)
> > 
> > > What is hard is I want action\_vt to be used any number of times and  
> > > count\_dvt to be mutable at any point.
> > 
> > You could implement a function for explicitly duplicating/sharing the  
> > linear value:
> > 
> > extern  
> > fun dup\_count\_vt (count: !count\_dvt): count\_dvt
> > 
> > And use it any time you want to get one more reference to the data  
> > structure.
> > 
> > Internally, such a function would either do a deep copy of the passed in  
> > argument (no refcounting is needed in this case), or it would bump the  
> > refcount (if refcounting is used). Note that in the former case, mutation  
> > doesn’t “propagate” to the copies (which is probably not what you want).
> > 
> > Hope this helps.
> > 
> > > Any advices to make it works ?

---

<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:** [January 14, 2015, 4:08pm UTC](https://discourse.ats-lang.org/t/dependent-type-is-hard/699/7 "2015-01-14T16:08:38Z")

</div>

thanks Artyom,  
I see dependent types can be really hard with imperative code.Le mardi 13 janvier 2015 10:29:30 UTC+1, Artyom Shalkhakov a écrit :

> Hello Cyrille,
> 
> On Monday, January 12, 2015 at 11:41:15 PM UTC+6, Cyrille Duret wrote:
> 
> > hi,  
> > I am still trying to implement my parser combinator and I have big  
> > troubles with dependent types because some structures are mutated and some  
> > closures are reused.  
> > Code is at :  
> > [http://www.ats-lang.org/SERVER/MYCODE/Patsoptaas\_serve.php?mycode\_url=http://pastebin.com/raw.php?i=avXpjG91](http://www.ats-lang.org/SERVER/MYCODE/Patsoptaas_serve.php?mycode_url=http://pastebin.com/raw.php?i=avXpjG91)  
> > [http://www.google.com/url?q=http%3A%2F%2Fwww.ats-lang.org%2FSERVER%2FMYCODE%2FPatsoptaas\_serve.php%3Fmycode\_url%3Dhttp%3A%2F%2Fpastebin.com%2Fraw.php%3Fi%3DavXpjG91&sa=D&sntz=1&usg=AFQjCNHSco7SiAo2MGLGd2qwXit8kq0H0w](http://www.google.com/url?q=http%3A%2F%2Fwww.ats-lang.org%2FSERVER%2FMYCODE%2FPatsoptaas_serve.php%3Fmycode_url%3Dhttp%3A%2F%2Fpastebin.com%2Fraw.php%3Fi%3DavXpjG91&sa=D&sntz=1&usg=AFQjCNHSco7SiAo2MGLGd2qwXit8kq0H0w)
> > 
> > Basically in this sample, the count\_dvt acts like a stream and can be  
> > modified when some data consumed. (follows this pattern :  
> > [https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/ATS-QA-LIST/qa-list-2015-01-03-2.dats](https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/ATS-QA-LIST/qa-list-2015-01-03-2.dats)  
> > )
> > 
> > And action\_vt acts like a combinator. It defines some actions to be done  
> > on count\_dvt.
> 
> Why are you using indexed datatypes here? Could you drop the indices? I  
> guess you’ve extracted the code from a larger program where the indices  
> were necessary for something.
> 
> Here’s the same thing sans indexes:  
> [http://www.ats-lang.org/SERVER/MYCODE/Patsoptaas\_serve.php?mycode\_url=http://pastebin.com/raw.php?i=3Sr2W1qk](http://www.ats-lang.org/SERVER/MYCODE/Patsoptaas_serve.php?mycode_url=http://pastebin.com/raw.php?i=3Sr2W1qk)
> 
> > What is hard is I want action\_vt to be used any number of times and  
> > count\_dvt to be mutable at any point.
> 
> You could implement a function for explicitly duplicating/sharing the  
> linear value:
> 
> extern  
> fun dup\_count\_vt (count: !count\_dvt): count\_dvt
> 
> And use it any time you want to get one more reference to the data  
> structure.
> 
> Internally, such a function would either do a deep copy of the passed in  
> argument (no refcounting is needed in this case), or it would bump the  
> refcount (if refcounting is used). Note that in the former case, mutation  
> doesn’t “propagate” to the copies (which is probably not what you want).
> 
> Hope this helps.
> 
> > Any advices to make 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:** [January 17, 2015, 6:25pm UTC](https://discourse.ats-lang.org/t/dependent-type-is-hard/699/8 "2015-01-17T18:25:01Z")

</div>

> > My question is : can we mix some code using garbage collection and linear  
> > type in the same program?

Yes. I myself do this all the time. A programmer can often tell which part  
of his or her program is a bottleneck  
in terms of memory usage. Using linear types can be a very effective way to  
address this kind of bottleneck issue.  
For instance, I used linear types in my implementation of the  
constraint-solving for ATS. Also, the part for testing  
whether pattern matching is exhaustive was first done using non-linear  
types; due to heavy GC, one often experienced  
pause during compilation. Using linear type to re-implement this part  
improved the performance very significantly.

> > In that case what would be the compilation command line?

You can add -DATS\_MEMALLOC\_GCBDW and -lgc. This means that malloc/free is  
handled my libgc (Boehm-GC).On Saturday, January 17, 2015 at 12:08:59 PM UTC-5, Cyrille Duret wrote:

> I see how long is the path to ATS enlightenment 🙂
> 
> My first attempt to define a simple parser combinator without any  
> dependent nor linear feature : [#include "share/atspre\_staload.hats"#include "share/HATS/atspre\_staload\_libats - Pastebin.com](http://pastebin.com/vBbDFxzJ)
> 
> By now I just defined string and seq combinators.  
> I will add some useful ones such as regex (using pcre), many, any, etc…
> 
> One can compile it with :  
> $ curl “[http://pastebin.com/raw.php?i=vBbDFxzJ](http://pastebin.com/raw.php?i=vBbDFxzJ)” \> test\_parser\_ML.dats  
> $ patscc -DATS\_MEMALLOC\_GCBDW -o test\_parser\_ML test\_parser\_ML.dats -lgc  
> -latslib
> 
> My inspiration are both from C++ and javascript world :
> 
> - [C++ Parser Combinator library](http://www.cs.hs-rm.de/~panitz/paper/cpppl/index.html)
> - [GitHub - jneen/parsimmon: A monadic LL(infinity) parser combinator library for javascript](https://github.com/jneen/parsimmon)
> 
> I understand now it is better to create the “ML” version first, then  
> modify it progressively to add linear types and dependent types step by  
> step.
> 
> My question is : can we mix some code using garbage collection and linear  
> type in the same program ?  
> In that case what would be the compilation command line ?
> 
> Le jeudi 15 janvier 2015 20:58:34 UTC+1, gmhwxi a écrit :
> 
> > Well, I think this is a bit like playing a music instrument.  
> > Practice is needed. Before one could play, say, Chopin, one needs to try  
> > playing a lot of simpler pieces 🙂
> > 
> > There is no “magic” in typechecking in ATS. To use advanced features  
> > in ATS effectively, one really needs to know _how_ a program is  
> > typechecked.
> > 
> > On Wednesday, January 14, 2015 at 11:08:39 AM UTC-5, Cyrille Duret wrote:
> > 
> > > thanks Artyom,  
> > > I see dependent types can be really hard with imperative code.
> > > 
> > > Le mardi 13 janvier 2015 10:29:30 UTC+1, Artyom Shalkhakov a écrit :
> > > 
> > > > Hello Cyrille,
> > > > 
> > > > On Monday, January 12, 2015 at 11:41:15 PM UTC+6, Cyrille Duret wrote:
> > > > 
> > > > > hi,  
> > > > > I am still trying to implement my parser combinator and I have big  
> > > > > troubles with dependent types because some structures are mutated and some  
> > > > > closures are reused.  
> > > > > Code is at :  
> > > > > [http://www.ats-lang.org/SERVER/MYCODE/Patsoptaas\_serve.php?mycode\_url=http://pastebin.com/raw.php?i=avXpjG91](http://www.ats-lang.org/SERVER/MYCODE/Patsoptaas_serve.php?mycode_url=http://pastebin.com/raw.php?i=avXpjG91)  
> > > > > [http://www.google.com/url?q=http%3A%2F%2Fwww.ats-lang.org%2FSERVER%2FMYCODE%2FPatsoptaas\_serve.php%3Fmycode\_url%3Dhttp%3A%2F%2Fpastebin.com%2Fraw.php%3Fi%3DavXpjG91&sa=D&sntz=1&usg=AFQjCNHSco7SiAo2MGLGd2qwXit8kq0H0w](http://www.google.com/url?q=http%3A%2F%2Fwww.ats-lang.org%2FSERVER%2FMYCODE%2FPatsoptaas_serve.php%3Fmycode_url%3Dhttp%3A%2F%2Fpastebin.com%2Fraw.php%3Fi%3DavXpjG91&sa=D&sntz=1&usg=AFQjCNHSco7SiAo2MGLGd2qwXit8kq0H0w)
> > > > > 
> > > > > Basically in this sample, the count\_dvt acts like a stream and can be  
> > > > > modified when some data consumed. (follows this pattern :  
> > > > > [https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/ATS-QA-LIST/qa-list-2015-01-03-2.dats](https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/ATS-QA-LIST/qa-list-2015-01-03-2.dats)  
> > > > > )
> > > > > 
> > > > > And action\_vt acts like a combinator. It defines some actions to be  
> > > > > done on count\_dvt.
> > > > 
> > > > Why are you using indexed datatypes here? Could you drop the indices? I  
> > > > guess you’ve extracted the code from a larger program where the indices  
> > > > were necessary for something.
> > > > 
> > > > Here’s the same thing sans indexes:  
> > > > [http://www.ats-lang.org/SERVER/MYCODE/Patsoptaas\_serve.php?mycode\_url=http://pastebin.com/raw.php?i=3Sr2W1qk](http://www.ats-lang.org/SERVER/MYCODE/Patsoptaas_serve.php?mycode_url=http://pastebin.com/raw.php?i=3Sr2W1qk)
> > > > 
> > > > > What is hard is I want action\_vt to be used any number of times and  
> > > > > count\_dvt to be mutable at any point.
> > > > 
> > > > You could implement a function for explicitly duplicating/sharing the  
> > > > linear value:
> > > > 
> > > > extern  
> > > > fun dup\_count\_vt (count: !count\_dvt): count\_dvt
> > > > 
> > > > And use it any time you want to get one more reference to the data  
> > > > structure.
> > > > 
> > > > Internally, such a function would either do a deep copy of the passed  
> > > > in argument (no refcounting is needed in this case), or it would bump the  
> > > > refcount (if refcounting is used). Note that in the former case, mutation  
> > > > doesn’t “propagate” to the copies (which is probably not what you want).
> > > > 
> > > > Hope this helps.
> > > > 
> > > > > Any advices to make 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:** [January 12, 2015, 6:27pm UTC](https://discourse.ats-lang.org/t/dependent-type-is-hard/699/9 "2015-01-12T18:27:43Z")

</div>

What I see here is something I call the complexity trap of dependent types.  
One can so easily get into unmanageable complexity when using dependent  
types.

In general, please think of dependent types as a form of refinement. You  
start out without dependent types. You add dependent types later to do  
so-called  
static debugging. The same can also be said about linear types.

In your case, I think the abstract type ‘count’ should be non-dependent:

absvtype count\_vtype  
vtypedef count = count\_vtype

When you implement ‘count’, you could use a dependent version.

Similarly, ‘action’ should probably be non-dependent as well:

absvtype action\_vtype  
vtypedef action = action\_vtype

It is not yet clear to me at this point how ‘action’ is supposed to be used.On Monday, January 12, 2015 at 12:41:15 PM UTC-5, Cyrille Duret wrote:

> hi,  
> I am still trying to implement my parser combinator and I have big  
> troubles with dependent types because some structures are mutated and some  
> closures are reused.  
> Code is at :  
> [http://www.ats-lang.org/SERVER/MYCODE/Patsoptaas\_serve.php?mycode\_url=http://pastebin.com/raw.php?i=avXpjG91](http://www.ats-lang.org/SERVER/MYCODE/Patsoptaas_serve.php?mycode_url=http://pastebin.com/raw.php?i=avXpjG91)
> 
> Basically in this sample, the count\_dvt acts like a stream and can be  
> modified when some data consumed. (follows this pattern :  
> [https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/ATS-QA-LIST/qa-list-2015-01-03-2.dats](https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/ATS-QA-LIST/qa-list-2015-01-03-2.dats)  
> )
> 
> And action\_vt acts like a combinator. It defines some actions to be done  
> on count\_dvt.
> 
> What is hard is I want action\_vt to be used any number of times and  
> count\_dvt to be mutable at any point.
> 
> Any advices to make it works ?
