# Problem With Template Instantiation (ATSPMVenv())

**URL:** <https://discourse.ats-lang.org/t/problem-with-template-instantiation-atspmvenv/503>\
**Category:** Google Group\
**Created:** [January 7, 2015, 11:38pm UTC](https://discourse.ats-lang.org/t/problem-with-template-instantiation-atspmvenv/503 "2015-01-07T23:38:38Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![William\_Blair](https://avatars.discourse-cdn.com/v4/letter/w/c68b51/32.png) [@William\_Blair](https://discourse.ats-lang.org/u/William_Blair)\
**Post date:** [January 7, 2015, 11:38pm UTC](https://discourse.ats-lang.org/t/problem-with-template-instantiation-atspmvenv/503/1 "2015-01-07T23:38:38Z")

</div>

I’m having some trouble compiling code that makes heavy use of templates. I  
am using a library that parses JSON into an array of tokens. Each token  
refers to a value which can be retrieved from the original JSON string. I  
have a template function that returns this string. Before any information  
is taken from the tokens, the user must implement this function with the  
correct string. This avoids having to set up a global variable or add  
parameters to functions. When I try to compile the corresponding C code, I  
find many instances of the following error, but only for some template  
functions.

```
parsing_dats.c:86143:186: error: use of undeclared identifier 'data'
ATSINSmove(tmpret762 __4, parse_s2rt__ 131__4(ATSPMVenv(data), arg0)) ;

```

The highest level code looks like the following

```
let val (data, jsv) = jsonval_parse_from_stdin ()

implement
jsonval_src<> () = data

val c3nstrs = jsv["c3nstrbody"]

in
    parse_c3nstr (c3nstrs)
end

```

I made a simpler version that uses the same technique, but the C code  
produced by patsopt compiles without any errors. You can find this small  
example on github.

```
https://gist.github.com/wdblair/2b5e0bd054b91abfe71d

```

Is it possible I am using too many layers of templates in my code? You can  
see the whole picture in the following repo:

> **[wdblair/ATS-Postiats-contrib](https://github.com/wdblair/ATS-Postiats-contrib/tree/jsmn/projects/MEDIUM/ATS-extsolve/parsing)**
>
> ATS-Postiats-contrib is primarily for packages contributed to ATS-Postiats - wdblair/ATS-Postiats-contrib

Thanks,  
Will

---

<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 8, 2015, 7:39am UTC](https://discourse.ats-lang.org/t/problem-with-template-instantiation-atspmvenv/503/2 "2015-01-08T07:39:39Z")

</div>

This is a VERY tricky issue because this kind of use was _not_  
part of the design. On the other hand, it seems natural to use templates  
this way. So …On Thursday, January 8, 2015 12:59:43 AM UTC-5, gmhwxi wrote:

> I have managed to produce this kind of bug using the following simple code:
> 
> [https://github.com/githwxi/ATS-Postiats/blob/master/doc/BUGS/bug-2015-01-07.dats](https://github.com/githwxi/ATS-Postiats/blob/master/doc/BUGS/bug-2015-01-07.dats)
> 
> On Wednesday, January 7, 2015 10:08:49 PM UTC-5, gmhwxi wrote:
> 
> > I think I know the cause of this problem, but fixing it requires some  
> > thoughts.
> > 
> > Basically, you used some templates that are defined mutually recursively:
> > 
> > implement{} f () = g\<\> ()  
> > implement{} g () = f\<\> ()
> > 
> > Let me first investigate the issue a bit further.
> > 
> > On Wednesday, January 7, 2015 6:38:38 PM UTC-5, William Blair wrote:
> > 
> > > I’m having some trouble compiling code that makes heavy use of  
> > > templates. I am using a library that parses JSON into an array of tokens.  
> > > Each token refers to a value which can be retrieved from the original JSON  
> > > string. I have a template function that returns this string. Before any  
> > > information is taken from the tokens, the user must implement this function  
> > > with the correct string. This avoids having to set up a global variable or  
> > > add parameters to functions. When I try to compile the corresponding C  
> > > code, I find many instances of the following error, but only for some  
> > > template functions.
> > > 
> > > ```
> > > parsing_dats.c:86143:186: error: use of undeclared identifier 'data'
> > > ATSINSmove(tmpret762 __4, parse_s2rt__ 131__4(ATSPMVenv(data), arg0)) ;
> > > 
> > > ```
> > > 
> > > The highest level code looks like the following
> > > 
> > > ```
> > > let val (data, jsv) = jsonval_parse_from_stdin ()
> > > 
> > > implement
> > > jsonval_src<> () = data
> > > 
> > > val c3nstrs = jsv["c3nstrbody"]
> > > 
> > > in
> > > parse_c3nstr (c3nstrs)
> > > end
> > > 
> > > ```
> > > 
> > > I made a simpler version that uses the same technique, but the C code  
> > > produced by patsopt compiles without any errors. You can find this  
> > > small example on github.
> > > 
> > > ```
> > > https://gist.github.com/wdblair/2b5e0bd054b91abfe71d
> > > 
> > > ```
> > > 
> > > Is it possible I am using too many layers of templates in my code? You  
> > > can see the whole picture in the following repo:
> > > 
> > > [https://github.com/wdblair/ATS-Postiats-contrib/tree/jsmn/projects/MEDIUM/ATS-extsolve/parsing](https://github.com/wdblair/ATS-Postiats-contrib/tree/jsmn/projects/MEDIUM/ATS-extsolve/parsing)
> > > 
> > > Thanks,  
> > > Will

---

<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 10, 2015, 7:59am UTC](https://discourse.ats-lang.org/t/problem-with-template-instantiation-atspmvenv/503/3 "2015-01-10T07:59:02Z")

</div>

I came up with a fix for this issue.

Basically, the idea is to allow the programmer to indicate to the compiler  
that a particular  
dynamic variable should be included in the environment of a template  
instance. For instance,

implement  
main0 (argc, argv) = let  
//  
val () = $closurenv(argc) // [argc] is put into the environment of the  
following instance of [foo]  
//  
in  
… foo\<\> (…) …  
end // end of [main0]

For a running example, please see:

> <https://github.com/githwxi/ATS-Postiats/blob/master/doc/BUGS/bug-2015-01-07.dats>

You need ATS2-github to test this example.On Thursday, January 8, 2015 at 2:39:39 AM UTC-5, gmhwxi wrote:

> This is a VERY tricky issue because this kind of use was _not_  
> part of the design. On the other hand, it seems natural to use templates  
> this way. So …
> 
> On Thursday, January 8, 2015 12:59:43 AM UTC-5, gmhwxi wrote:
> 
> > I have managed to produce this kind of bug using the following simple  
> > code:
> > 
> > [https://github.com/githwxi/ATS-Postiats/blob/master/doc/BUGS/bug-2015-01-07.dats](https://github.com/githwxi/ATS-Postiats/blob/master/doc/BUGS/bug-2015-01-07.dats)
> > 
> > On Wednesday, January 7, 2015 10:08:49 PM UTC-5, gmhwxi wrote:
> > 
> > > I think I know the cause of this problem, but fixing it requires some  
> > > thoughts.
> > > 
> > > Basically, you used some templates that are defined mutually recursively:
> > > 
> > > implement{} f () = g\<\> ()  
> > > implement{} g () = f\<\> ()
> > > 
> > > Let me first investigate the issue a bit further.
> > > 
> > > On Wednesday, January 7, 2015 6:38:38 PM UTC-5, William Blair wrote:
> > > 
> > > > I’m having some trouble compiling code that makes heavy use of  
> > > > templates. I am using a library that parses JSON into an array of tokens.  
> > > > Each token refers to a value which can be retrieved from the original JSON  
> > > > string. I have a template function that returns this string. Before any  
> > > > information is taken from the tokens, the user must implement this function  
> > > > with the correct string. This avoids having to set up a global variable or  
> > > > add parameters to functions. When I try to compile the corresponding C  
> > > > code, I find many instances of the following error, but only for some  
> > > > template functions.
> > > > 
> > > > ```
> > > > parsing_dats.c:86143:186: error: use of undeclared identifier 'data'
> > > > ATSINSmove(tmpret762 __4, parse_s2rt__ 131__4(ATSPMVenv(data), arg0)) 
> > > > 
> > > > ```
> > > > 
> > > > ;
> > > > 
> > > > The highest level code looks like the following
> > > > 
> > > > ```
> > > > let val (data, jsv) = jsonval_parse_from_stdin ()
> > > > 
> > > > implement
> > > > jsonval_src<> () = data
> > > > 
> > > > val c3nstrs = jsv["c3nstrbody"]
> > > > 
> > > > in
> > > > parse_c3nstr (c3nstrs)
> > > > end
> > > > 
> > > > ```
> > > > 
> > > > I made a simpler version that uses the same technique, but the C code  
> > > > produced by patsopt compiles without any errors. You can find this  
> > > > small example on github.
> > > > 
> > > > ```
> > > > https://gist.github.com/wdblair/2b5e0bd054b91abfe71d
> > > > 
> > > > ```
> > > > 
> > > > Is it possible I am using too many layers of templates in my code? You  
> > > > can see the whole picture in the following repo:
> > > > 
> > > > [https://github.com/wdblair/ATS-Postiats-contrib/tree/jsmn/projects/MEDIUM/ATS-extsolve/parsing](https://github.com/wdblair/ATS-Postiats-contrib/tree/jsmn/projects/MEDIUM/ATS-extsolve/parsing)
> > > > 
> > > > Thanks,  
> > > > Will

---

<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 8, 2015, 5:59am UTC](https://discourse.ats-lang.org/t/problem-with-template-instantiation-atspmvenv/503/4 "2015-01-08T05:59:43Z")

</div>

I have managed to produce this kind of bug using the following simple code:

[https://github.com/githwxi/ATS-Postiats/blob/master/doc/BUGS/bug-2015-01-07.datsOn](https://github.com/githwxi/ATS-Postiats/blob/master/doc/BUGS/bug-2015-01-07.datsOn) Wednesday, January 7, 2015 10:08:49 PM UTC-5, gmhwxi wrote:

> I think I know the cause of this problem, but fixing it requires some  
> thoughts.
> 
> Basically, you used some templates that are defined mutually recursively:
> 
> implement{} f () = g\<\> ()  
> implement{} g () = f\<\> ()
> 
> Let me first investigate the issue a bit further.
> 
> On Wednesday, January 7, 2015 6:38:38 PM UTC-5, William Blair wrote:
> 
> > I’m having some trouble compiling code that makes heavy use of templates.  
> > I am using a library that parses JSON into an array of tokens. Each token  
> > refers to a value which can be retrieved from the original JSON string. I  
> > have a template function that returns this string. Before any information  
> > is taken from the tokens, the user must implement this function with the  
> > correct string. This avoids having to set up a global variable or add  
> > parameters to functions. When I try to compile the corresponding C code, I  
> > find many instances of the following error, but only for some template  
> > functions.
> > 
> > ```
> > parsing_dats.c:86143:186: error: use of undeclared identifier 'data'
> > ATSINSmove(tmpret762 __4, parse_s2rt__ 131__4(ATSPMVenv(data), arg0)) ;
> > 
> > ```
> > 
> > The highest level code looks like the following
> > 
> > ```
> > let val (data, jsv) = jsonval_parse_from_stdin ()
> > 
> > implement
> > jsonval_src<> () = data
> > 
> > val c3nstrs = jsv["c3nstrbody"]
> > 
> > in
> > parse_c3nstr (c3nstrs)
> > end
> > 
> > ```
> > 
> > I made a simpler version that uses the same technique, but the C code  
> > produced by patsopt compiles without any errors. You can find this small  
> > example on github.
> > 
> > ```
> > https://gist.github.com/wdblair/2b5e0bd054b91abfe71d
> > 
> > ```
> > 
> > Is it possible I am using too many layers of templates in my code? You  
> > can see the whole picture in the following repo:
> > 
> > [https://github.com/wdblair/ATS-Postiats-contrib/tree/jsmn/projects/MEDIUM/ATS-extsolve/parsing](https://github.com/wdblair/ATS-Postiats-contrib/tree/jsmn/projects/MEDIUM/ATS-extsolve/parsing)
> > 
> > Thanks,  
> > Will

---

<div class="post-metadata">

**Author:** ![William\_Blair](https://avatars.discourse-cdn.com/v4/letter/w/c68b51/32.png) [@William\_Blair](https://discourse.ats-lang.org/u/William_Blair)\
**Post date:** [January 8, 2015, 8:12pm UTC](https://discourse.ats-lang.org/t/problem-with-template-instantiation-atspmvenv/503/5 "2015-01-08T20:12:07Z")

</div>

Are mutually recursive templates only an issue when instantiating one of  
the templates requires bringing in variables from the environment? If so, I  
could just replace the jsonval\_src template with a global variable for now.

What would you have to do to support using templates in this way?Am Donnerstag, 8. Januar 2015 02:39:39 UTC-5 schrieb gmhwxi:

> This is a VERY tricky issue because this kind of use was _not_  
> part of the design. On the other hand, it seems natural to use templates  
> this way. So …
> 
> On Thursday, January 8, 2015 12:59:43 AM UTC-5, gmhwxi wrote:
> 
> > I have managed to produce this kind of bug using the following simple  
> > code:
> > 
> > [https://github.com/githwxi/ATS-Postiats/blob/master/doc/BUGS/bug-2015-01-07.dats](https://github.com/githwxi/ATS-Postiats/blob/master/doc/BUGS/bug-2015-01-07.dats)
> > 
> > On Wednesday, January 7, 2015 10:08:49 PM UTC-5, gmhwxi wrote:
> > 
> > > I think I know the cause of this problem, but fixing it requires some  
> > > thoughts.
> > > 
> > > Basically, you used some templates that are defined mutually recursively:
> > > 
> > > implement{} f () = g\<\> ()  
> > > implement{} g () = f\<\> ()
> > > 
> > > Let me first investigate the issue a bit further.
> > > 
> > > On Wednesday, January 7, 2015 6:38:38 PM UTC-5, William Blair wrote:
> > > 
> > > > I’m having some trouble compiling code that makes heavy use of  
> > > > templates. I am using a library that parses JSON into an array of tokens.  
> > > > Each token refers to a value which can be retrieved from the original JSON  
> > > > string. I have a template function that returns this string. Before any  
> > > > information is taken from the tokens, the user must implement this function  
> > > > with the correct string. This avoids having to set up a global variable or  
> > > > add parameters to functions. When I try to compile the corresponding C  
> > > > code, I find many instances of the following error, but only for some  
> > > > template functions.
> > > > 
> > > > ```
> > > > parsing_dats.c:86143:186: error: use of undeclared identifier 'data'
> > > > ATSINSmove(tmpret762 __4, parse_s2rt__ 131__4(ATSPMVenv(data), arg0)) 
> > > > 
> > > > ```
> > > > 
> > > > ;
> > > > 
> > > > The highest level code looks like the following
> > > > 
> > > > ```
> > > > let val (data, jsv) = jsonval_parse_from_stdin ()
> > > > 
> > > > implement
> > > > jsonval_src<> () = data
> > > > 
> > > > val c3nstrs = jsv["c3nstrbody"]
> > > > 
> > > > in
> > > > parse_c3nstr (c3nstrs)
> > > > end
> > > > 
> > > > ```
> > > > 
> > > > I made a simpler version that uses the same technique, but the C code  
> > > > produced by patsopt compiles without any errors. You can find this  
> > > > small example on github.
> > > > 
> > > > ```
> > > > https://gist.github.com/wdblair/2b5e0bd054b91abfe71d
> > > > 
> > > > ```
> > > > 
> > > > Is it possible I am using too many layers of templates in my code? You  
> > > > can see the whole picture in the following repo:
> > > > 
> > > > [https://github.com/wdblair/ATS-Postiats-contrib/tree/jsmn/projects/MEDIUM/ATS-extsolve/parsing](https://github.com/wdblair/ATS-Postiats-contrib/tree/jsmn/projects/MEDIUM/ATS-extsolve/parsing)
> > > > 
> > > > Thanks,  
> > > > Will

---

<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, 5:30pm UTC](https://discourse.ats-lang.org/t/problem-with-template-instantiation-atspmvenv/503/6 "2015-01-15T17:30:14Z")

</div>

For now, a template argument needs to be a non-dependent type.  
List0 is dependent. The problem can be fixed by changing List0 to list0:

fun{}  
print\_itmlstlst  
(iss: itmlstlst): void = let  
typedef itmlst0 = list0(itm)  
implement  
print\_t0ype  
(is) = print\_itmlst (g1ofg0(is))  
in  
print\_list ($UNSAFE.cast{List0(itmlst0)}(iss))  
end

You need the following line for list0:

staload "prelude/libats/ML/SATS/list0.sats"On Thursday, January 15, 2015 at 4:31:09 AM UTC-5, William Blair wrote:

> > > I changed the keyword $closurenv to $tempenver as this is only  
> > > related to compiling template instances.
> 
> Thanks! That fixed the environment variable issue.
> 
> Another problem I ran into was using templates to parse lists of lists.  
> I’ve made a small example to demonstrate the issue I’m running into.
> 
> ```
> https://gist.github.com/wdblair/33010d8544d79c263ccf
> 
> ```
> 
> If you try to compile the above gist using
> 
> ```
> patscc -c -o printlists_dats.o printlists.dats
> 
> ```
> 
> The C compiler complains about the following line:
> 
> /\*  
> emit\_instr: loc0 = /Users/wdblair/tmp/printlists.dats: 437(line=33,  
> offs=13) – 454(line=33, offs=30)  
> \*/  
> ATSINSmove\_void(tmp3\_\_2,  
> PMVtmpltcstmat[0](print\_t0ype\<S2Eexi(n$8536$8538(13787); S2Eapp(S2Ecst(\>=);  
> S2Evar(n$8536$8538(13787)), S2Eintinf(0)); S2Eapp(S2Ecst(list);  
> S2Ecst(itm), S2Evar(n$8536$8538(13787))))\>)(tmp1\_\_2)) ;
> 
> It seems that it isn’t finding the implementation of print\_t0ype  
> given inside print\_itmlstlst. Does it look like I am using templates  
> incorrectly here?
> 
> Thanks,  
> Will
> 
> Am Samstag, 10. Januar 2015 13:50:36 UTC-5 schrieb gmhwxi:
> 
> > I changed the keyword $closurenv to $tempenver as this is only  
> > related to compiling template instances.
> > 
> > On Saturday, January 10, 2015 at 2:59:02 AM UTC-5, gmhwxi wrote:
> > 
> > > I came up with a fix for this issue.
> > > 
> > > Basically, the idea is to allow the programmer to indicate to the  
> > > compiler that a particular  
> > > dynamic variable should be included in the environment of a template  
> > > instance. For instance,
> > > 
> > > implement  
> > > main0 (argc, argv) = let  
> > > //  
> > > val () = $closurenv(argc) // [argc] is put into the environment of the  
> > > following instance of [foo]  
> > > //  
> > > in  
> > > … foo\<\> (…) …  
> > > end // end of [main0]
> > > 
> > > For a running example, please see:
> > > 
> > > [https://github.com/githwxi/ATS-Postiats/blob/master/doc/BUGS/bug-2015-01-07.dats](https://github.com/githwxi/ATS-Postiats/blob/master/doc/BUGS/bug-2015-01-07.dats)
> > > 
> > > You need ATS2-github to test this example.
> > > 
> > > On Thursday, January 8, 2015 at 2:39:39 AM UTC-5, gmhwxi wrote:
> > > 
> > > > This is a VERY tricky issue because this kind of use was _not_  
> > > > part of the design. On the other hand, it seems natural to use templates  
> > > > this way. So …
> > > > 
> > > > On Thursday, January 8, 2015 12:59:43 AM UTC-5, gmhwxi wrote:
> > > > 
> > > > > I have managed to produce this kind of bug using the following simple  
> > > > > code:
> > > > > 
> > > > > [https://github.com/githwxi/ATS-Postiats/blob/master/doc/BUGS/bug-2015-01-07.dats](https://github.com/githwxi/ATS-Postiats/blob/master/doc/BUGS/bug-2015-01-07.dats)
> > > > > 
> > > > > On Wednesday, January 7, 2015 10:08:49 PM UTC-5, gmhwxi wrote:
> > > > > 
> > > > > > I think I know the cause of this problem, but fixing it requires some  
> > > > > > thoughts.
> > > > > > 
> > > > > > Basically, you used some templates that are defined mutually  
> > > > > > recursively:
> > > > > > 
> > > > > > implement{} f () = g\<\> ()  
> > > > > > implement{} g () = f\<\> ()
> > > > > > 
> > > > > > Let me first investigate the issue a bit further.
> > > > > > 
> > > > > > On Wednesday, January 7, 2015 6:38:38 PM UTC-5, William Blair wrote:
> > > > > > 
> > > > > > > I’m having some trouble compiling code that makes heavy use of  
> > > > > > > templates. I am using a library that parses JSON into an array of tokens.  
> > > > > > > Each token refers to a value which can be retrieved from the original JSON  
> > > > > > > string. I have a template function that returns this string. Before any  
> > > > > > > information is taken from the tokens, the user must implement this function  
> > > > > > > with the correct string. This avoids having to set up a global variable or  
> > > > > > > add parameters to functions. When I try to compile the corresponding C  
> > > > > > > code, I find many instances of the following error, but only for some  
> > > > > > > template functions.
> > > > > > > 
> > > > > > > ```
> > > > > > > parsing_dats.c:86143:186: error: use of undeclared identifier 
> > > > > > > 
> > > > > > > ```
> > > > > > > 
> > > > > > > ‘data’  
> > > > > > > ATSINSmove(tmpret762\_\_4, parse\_s2rt\_\_131\_\_4(ATSPMVenv(data),  
> > > > > > > arg0)) ;
> > > > > > > 
> > > > > > > The highest level code looks like the following
> > > > > > > 
> > > > > > > ```
> > > > > > > let val (data, jsv) = jsonval_parse_from_stdin ()
> > > > > > > 
> > > > > > > implement
> > > > > > > jsonval_src<> () = data
> > > > > > > 
> > > > > > > val c3nstrs = jsv["c3nstrbody"]
> > > > > > > 
> > > > > > > in
> > > > > > > parse_c3nstr (c3nstrs)
> > > > > > > end
> > > > > > > 
> > > > > > > ```
> > > > > > > 
> > > > > > > I made a simpler version that uses the same technique, but the C  
> > > > > > > code produced by patsopt compiles without any errors. You can find  
> > > > > > > this small example on github.
> > > > > > > 
> > > > > > > ```
> > > > > > > https://gist.github.com/wdblair/2b5e0bd054b91abfe71d
> > > > > > > 
> > > > > > > ```
> > > > > > > 
> > > > > > > Is it possible I am using too many layers of templates in my code?  
> > > > > > > You can see the whole picture in the following repo:
> > > > > > > 
> > > > > > > [https://github.com/wdblair/ATS-Postiats-contrib/tree/jsmn/projects/MEDIUM/ATS-extsolve/parsing](https://github.com/wdblair/ATS-Postiats-contrib/tree/jsmn/projects/MEDIUM/ATS-extsolve/parsing)
> > > > > > > 
> > > > > > > Thanks,  
> > > > > > > Will

---

<div class="post-metadata">

**Author:** ![William\_Blair](https://avatars.discourse-cdn.com/v4/letter/w/c68b51/32.png) [@William\_Blair](https://discourse.ats-lang.org/u/William_Blair)\
**Post date:** [January 15, 2015, 8:40pm UTC](https://discourse.ats-lang.org/t/problem-with-template-instantiation-atspmvenv/503/7 "2015-01-15T20:40:47Z")

</div>

> > Or just do:
> > 
> > #include “share/HATS/atspre\_staload\_libats\_ML.hats”

Thanks! That worked well. I’ve run into this issue before, and it’s good to  
know what the cause of it is.Am Donnerstag, 15. Januar 2015 12:33:55 UTC-5 schrieb gmhwxi:

> The following two lines are needed:
> 
> staload “libats/ML/SATS/basis.sats”  
> staload “libats/ML/SATS/list0.sats”
> 
> Or just do:
> 
> #include “share/HATS/atspre\_staload\_libats\_ML.hats”
> 
> On Thursday, January 15, 2015 at 12:30:14 PM UTC-5, gmhwxi wrote:
> 
> > For now, a template argument needs to be a non-dependent type.  
> > List0 is dependent. The problem can be fixed by changing List0 to list0:
> > 
> > fun{}  
> > print\_itmlstlst  
> > (iss: itmlstlst): void = let  
> > typedef itmlst0 = list0(itm)  
> > implement  
> > print\_t0ype  
> > (is) = print\_itmlst (g1ofg0(is))  
> > in  
> > print\_list ($UNSAFE.cast{List0(itmlst0)}(iss))  
> > end
> > 
> > You need the following line for list0:
> > 
> > staload “prelude/libats/ML/SATS/list0.sats”
> > 
> > On Thursday, January 15, 2015 at 4:31:09 AM UTC-5, William Blair wrote:
> > 
> > > > > I changed the keyword $closurenv to $tempenver as this is only  
> > > > > related to compiling template instances.
> > > 
> > > Thanks! That fixed the environment variable issue.
> > > 
> > > Another problem I ran into was using templates to parse lists of lists.  
> > > I’ve made a small example to demonstrate the issue I’m running into.
> > > 
> > > ```
> > > https://gist.github.com/wdblair/33010d8544d79c263ccf
> > > 
> > > ```
> > > 
> > > If you try to compile the above gist using
> > > 
> > > ```
> > > patscc -c -o printlists_dats.o printlists.dats
> > > 
> > > ```
> > > 
> > > The C compiler complains about the following line:
> > > 
> > > /\*  
> > > emit\_instr: loc0 = /Users/wdblair/tmp/printlists.dats: 437(line=33,  
> > > offs=13) – 454(line=33, offs=30)  
> > > \*/  
> > > ATSINSmove\_void(tmp3\_\_2,  
> > > PMVtmpltcstmat[0](print\_t0ype\<S2Eexi(n$8536$8538(13787); S2Eapp(S2Ecst(\>=);  
> > > S2Evar(n$8536$8538(13787)), S2Eintinf(0)); S2Eapp(S2Ecst(list);  
> > > S2Ecst(itm), S2Evar(n$8536$8538(13787))))\>)(tmp1\_\_2)) ;
> > > 
> > > It seems that it isn’t finding the implementation of print\_t0ype  
> > > given inside print\_itmlstlst. Does it look like I am using templates  
> > > incorrectly here?
> > > 
> > > Thanks,  
> > > Will
> > > 
> > > Am Samstag, 10. Januar 2015 13:50:36 UTC-5 schrieb gmhwxi:
> > > 
> > > > I changed the keyword $closurenv to $tempenver as this is only  
> > > > related to compiling template instances.
> > > > 
> > > > On Saturday, January 10, 2015 at 2:59:02 AM UTC-5, gmhwxi wrote:
> > > > 
> > > > > I came up with a fix for this issue.
> > > > > 
> > > > > Basically, the idea is to allow the programmer to indicate to the  
> > > > > compiler that a particular  
> > > > > dynamic variable should be included in the environment of a template  
> > > > > instance. For instance,
> > > > > 
> > > > > implement  
> > > > > main0 (argc, argv) = let  
> > > > > //  
> > > > > val () = $closurenv(argc) // [argc] is put into the environment of  
> > > > > the following instance of [foo]  
> > > > > //  
> > > > > in  
> > > > > … foo\<\> (…) …  
> > > > > end // end of [main0]
> > > > > 
> > > > > For a running example, please see:
> > > > > 
> > > > > [https://github.com/githwxi/ATS-Postiats/blob/master/doc/BUGS/bug-2015-01-07.dats](https://github.com/githwxi/ATS-Postiats/blob/master/doc/BUGS/bug-2015-01-07.dats)
> > > > > 
> > > > > You need ATS2-github to test this example.
> > > > > 
> > > > > On Thursday, January 8, 2015 at 2:39:39 AM UTC-5, gmhwxi wrote:
> > > > > 
> > > > > > This is a VERY tricky issue because this kind of use was _not_  
> > > > > > part of the design. On the other hand, it seems natural to use  
> > > > > > templates  
> > > > > > this way. So …
> > > > > > 
> > > > > > On Thursday, January 8, 2015 12:59:43 AM UTC-5, gmhwxi wrote:
> > > > > > 
> > > > > > > I have managed to produce this kind of bug using the following  
> > > > > > > simple code:
> > > > > > > 
> > > > > > > [https://github.com/githwxi/ATS-Postiats/blob/master/doc/BUGS/bug-2015-01-07.dats](https://github.com/githwxi/ATS-Postiats/blob/master/doc/BUGS/bug-2015-01-07.dats)
> > > > > > > 
> > > > > > > On Wednesday, January 7, 2015 10:08:49 PM UTC-5, gmhwxi wrote:
> > > > > > > 
> > > > > > > > I think I know the cause of this problem, but fixing it requires  
> > > > > > > > some thoughts.
> > > > > > > > 
> > > > > > > > Basically, you used some templates that are defined mutually  
> > > > > > > > recursively:
> > > > > > > > 
> > > > > > > > implement{} f () = g\<\> ()  
> > > > > > > > implement{} g () = f\<\> ()
> > > > > > > > 
> > > > > > > > Let me first investigate the issue a bit further.
> > > > > > > > 
> > > > > > > > On Wednesday, January 7, 2015 6:38:38 PM UTC-5, William Blair wrote:
> > > > > > > > 
> > > > > > > > > I’m having some trouble compiling code that makes heavy use of  
> > > > > > > > > templates. I am using a library that parses JSON into an array of tokens.  
> > > > > > > > > Each token refers to a value which can be retrieved from the original JSON  
> > > > > > > > > string. I have a template function that returns this string. Before any  
> > > > > > > > > information is taken from the tokens, the user must implement this function  
> > > > > > > > > with the correct string. This avoids having to set up a global variable or  
> > > > > > > > > add parameters to functions. When I try to compile the corresponding C  
> > > > > > > > > code, I find many instances of the following error, but only for some  
> > > > > > > > > template functions.
> > > > > > > > > 
> > > > > > > > > ```
> > > > > > > > > parsing_dats.c:86143:186: error: use of undeclared identifier 
> > > > > > > > > 
> > > > > > > > > ```
> > > > > > > > > 
> > > > > > > > > ‘data’  
> > > > > > > > > ATSINSmove(tmpret762\_\_4, parse\_s2rt\_\_131\_\_4(ATSPMVenv(data),  
> > > > > > > > > arg0)) ;
> > > > > > > > > 
> > > > > > > > > The highest level code looks like the following
> > > > > > > > > 
> > > > > > > > > ```
> > > > > > > > > let val (data, jsv) = jsonval_parse_from_stdin ()
> > > > > > > > > 
> > > > > > > > > implement
> > > > > > > > > jsonval_src<> () = data
> > > > > > > > > 
> > > > > > > > > val c3nstrs = jsv["c3nstrbody"]
> > > > > > > > > 
> > > > > > > > > in
> > > > > > > > > parse_c3nstr (c3nstrs)
> > > > > > > > > end
> > > > > > > > > 
> > > > > > > > > ```
> > > > > > > > > 
> > > > > > > > > I made a simpler version that uses the same technique, but the C  
> > > > > > > > > code produced by patsopt compiles without any errors. You can  
> > > > > > > > > find this small example on github.
> > > > > > > > > 
> > > > > > > > > ```
> > > > > > > > > https://gist.github.com/wdblair/2b5e0bd054b91abfe71d
> > > > > > > > > 
> > > > > > > > > ```
> > > > > > > > > 
> > > > > > > > > Is it possible I am using too many layers of templates in my code?  
> > > > > > > > > You can see the whole picture in the following repo:
> > > > > > > > > 
> > > > > > > > > [https://github.com/wdblair/ATS-Postiats-contrib/tree/jsmn/projects/MEDIUM/ATS-extsolve/parsing](https://github.com/wdblair/ATS-Postiats-contrib/tree/jsmn/projects/MEDIUM/ATS-extsolve/parsing)
> > > > > > > > > 
> > > > > > > > > Thanks,  
> > > > > > > > > Will

---

<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, 8:51pm UTC](https://discourse.ats-lang.org/t/problem-with-template-instantiation-atspmvenv/503/8 "2015-01-15T20:51:05Z")

</div>

> > I’ve run into this issue before, …

Maybe you should have reported it earlier 🙂

I plan to add Part V to my book on Intro to ATS. It will be about  
template-based programming.On Thursday, January 15, 2015 at 3:40:47 PM UTC-5, William Blair wrote:

> > > Or just do:
> > > 
> > > #include “share/HATS/atspre\_staload\_libats\_ML.hats”
> 
> Thanks! That worked well. I’ve run into this issue before, and it’s good  
> to know what the cause of it is.
> 
> Am Donnerstag, 15. Januar 2015 12:33:55 UTC-5 schrieb gmhwxi:
> 
> > The following two lines are needed:
> > 
> > staload “libats/ML/SATS/basis.sats”  
> > staload “libats/ML/SATS/list0.sats”
> > 
> > Or just do:
> > 
> > #include “share/HATS/atspre\_staload\_libats\_ML.hats”
> > 
> > On Thursday, January 15, 2015 at 12:30:14 PM UTC-5, gmhwxi wrote:
> > 
> > > For now, a template argument needs to be a non-dependent type.  
> > > List0 is dependent. The problem can be fixed by changing List0 to list0:
> > > 
> > > fun{}  
> > > print\_itmlstlst  
> > > (iss: itmlstlst): void = let  
> > > typedef itmlst0 = list0(itm)  
> > > implement  
> > > print\_t0ype  
> > > (is) = print\_itmlst (g1ofg0(is))  
> > > in  
> > > print\_list ($UNSAFE.cast{List0(itmlst0)}(iss))  
> > > end
> > > 
> > > You need the following line for list0:
> > > 
> > > staload “prelude/libats/ML/SATS/list0.sats”
> > > 
> > > On Thursday, January 15, 2015 at 4:31:09 AM UTC-5, William Blair wrote:
> > > 
> > > > > > I changed the keyword $closurenv to $tempenver as this is only  
> > > > > > related to compiling template instances.
> > > > 
> > > > Thanks! That fixed the environment variable issue.
> > > > 
> > > > Another problem I ran into was using templates to parse lists of lists.  
> > > > I’ve made a small example to demonstrate the issue I’m running into.
> > > > 
> > > > ```
> > > > https://gist.github.com/wdblair/33010d8544d79c263ccf
> > > > 
> > > > ```
> > > > 
> > > > If you try to compile the above gist using
> > > > 
> > > > ```
> > > > patscc -c -o printlists_dats.o printlists.dats
> > > > 
> > > > ```
> > > > 
> > > > The C compiler complains about the following line:
> > > > 
> > > > /\*  
> > > > emit\_instr: loc0 = /Users/wdblair/tmp/printlists.dats: 437(line=33,  
> > > > offs=13) – 454(line=33, offs=30)  
> > > > \*/  
> > > > ATSINSmove\_void(tmp3\_\_2,  
> > > > PMVtmpltcstmat[0](print\_t0ype\<S2Eexi(n$8536$8538(13787); S2Eapp(S2Ecst(\>=);  
> > > > S2Evar(n$8536$8538(13787)), S2Eintinf(0)); S2Eapp(S2Ecst(list);  
> > > > S2Ecst(itm), S2Evar(n$8536$8538(13787))))\>)(tmp1\_\_2)) ;
> > > > 
> > > > It seems that it isn’t finding the implementation of  
> > > > print\_t0ype given inside print\_itmlstlst. Does it look like I am  
> > > > using templates incorrectly here?
> > > > 
> > > > Thanks,  
> > > > Will
> > > > 
> > > > Am Samstag, 10. Januar 2015 13:50:36 UTC-5 schrieb gmhwxi:
> > > > 
> > > > > I changed the keyword $closurenv to $tempenver as this is only  
> > > > > related to compiling template instances.
> > > > > 
> > > > > On Saturday, January 10, 2015 at 2:59:02 AM UTC-5, gmhwxi wrote:
> > > > > 
> > > > > > I came up with a fix for this issue.
> > > > > > 
> > > > > > Basically, the idea is to allow the programmer to indicate to the  
> > > > > > compiler that a particular  
> > > > > > dynamic variable should be included in the environment of a template  
> > > > > > instance. For instance,
> > > > > > 
> > > > > > implement  
> > > > > > main0 (argc, argv) = let  
> > > > > > //  
> > > > > > val () = $closurenv(argc) // [argc] is put into the environment of  
> > > > > > the following instance of [foo]  
> > > > > > //  
> > > > > > in  
> > > > > > … foo\<\> (…) …  
> > > > > > end // end of [main0]
> > > > > > 
> > > > > > For a running example, please see:
> > > > > > 
> > > > > > [https://github.com/githwxi/ATS-Postiats/blob/master/doc/BUGS/bug-2015-01-07.dats](https://github.com/githwxi/ATS-Postiats/blob/master/doc/BUGS/bug-2015-01-07.dats)
> > > > > > 
> > > > > > You need ATS2-github to test this example.
> > > > > > 
> > > > > > On Thursday, January 8, 2015 at 2:39:39 AM UTC-5, gmhwxi wrote:
> > > > > > 
> > > > > > > This is a VERY tricky issue because this kind of use was _not_  
> > > > > > > part of the design. On the other hand, it seems natural to use  
> > > > > > > templates  
> > > > > > > this way. So …
> > > > > > > 
> > > > > > > On Thursday, January 8, 2015 12:59:43 AM UTC-5, gmhwxi wrote:
> > > > > > > 
> > > > > > > > I have managed to produce this kind of bug using the following  
> > > > > > > > simple code:
> > > > > > > > 
> > > > > > > > [https://github.com/githwxi/ATS-Postiats/blob/master/doc/BUGS/bug-2015-01-07.dats](https://github.com/githwxi/ATS-Postiats/blob/master/doc/BUGS/bug-2015-01-07.dats)
> > > > > > > > 
> > > > > > > > On Wednesday, January 7, 2015 10:08:49 PM UTC-5, gmhwxi wrote:
> > > > > > > > 
> > > > > > > > > I think I know the cause of this problem, but fixing it requires  
> > > > > > > > > some thoughts.
> > > > > > > > > 
> > > > > > > > > Basically, you used some templates that are defined mutually  
> > > > > > > > > recursively:
> > > > > > > > > 
> > > > > > > > > implement{} f () = g\<\> ()  
> > > > > > > > > implement{} g () = f\<\> ()
> > > > > > > > > 
> > > > > > > > > Let me first investigate the issue a bit further.
> > > > > > > > > 
> > > > > > > > > On Wednesday, January 7, 2015 6:38:38 PM UTC-5, William Blair wrote:
> > > > > > > > > 
> > > > > > > > > > I’m having some trouble compiling code that makes heavy use of  
> > > > > > > > > > templates. I am using a library that parses JSON into an array of tokens.  
> > > > > > > > > > Each token refers to a value which can be retrieved from the original JSON  
> > > > > > > > > > string. I have a template function that returns this string. Before any  
> > > > > > > > > > information is taken from the tokens, the user must implement this function  
> > > > > > > > > > with the correct string. This avoids having to set up a global variable or  
> > > > > > > > > > add parameters to functions. When I try to compile the corresponding C  
> > > > > > > > > > code, I find many instances of the following error, but only for some  
> > > > > > > > > > template functions.
> > > > > > > > > > 
> > > > > > > > > > ```
> > > > > > > > > > parsing_dats.c:86143:186: error: use of undeclared identifier 
> > > > > > > > > > 
> > > > > > > > > > ```
> > > > > > > > > > 
> > > > > > > > > > ‘data’  
> > > > > > > > > > ATSINSmove(tmpret762\_\_4, parse\_s2rt\_\_131\_\_4(ATSPMVenv(data),  
> > > > > > > > > > arg0)) ;
> > > > > > > > > > 
> > > > > > > > > > The highest level code looks like the following
> > > > > > > > > > 
> > > > > > > > > > ```
> > > > > > > > > > let val (data, jsv) = jsonval_parse_from_stdin ()
> > > > > > > > > > 
> > > > > > > > > > implement
> > > > > > > > > > jsonval_src<> () = data
> > > > > > > > > > 
> > > > > > > > > > val c3nstrs = jsv["c3nstrbody"]
> > > > > > > > > > 
> > > > > > > > > > in
> > > > > > > > > > parse_c3nstr (c3nstrs)
> > > > > > > > > > end
> > > > > > > > > > 
> > > > > > > > > > ```
> > > > > > > > > > 
> > > > > > > > > > I made a simpler version that uses the same technique, but the C  
> > > > > > > > > > code produced by patsopt compiles without any errors. You can  
> > > > > > > > > > find this small example on github.
> > > > > > > > > > 
> > > > > > > > > > ```
> > > > > > > > > > https://gist.github.com/wdblair/2b5e0bd054b91abfe71d
> > > > > > > > > > 
> > > > > > > > > > ```
> > > > > > > > > > 
> > > > > > > > > > Is it possible I am using too many layers of templates in my  
> > > > > > > > > > code? You can see the whole picture in the following repo:
> > > > > > > > > > 
> > > > > > > > > > [https://github.com/wdblair/ATS-Postiats-contrib/tree/jsmn/projects/MEDIUM/ATS-extsolve/parsing](https://github.com/wdblair/ATS-Postiats-contrib/tree/jsmn/projects/MEDIUM/ATS-extsolve/parsing)
> > > > > > > > > > 
> > > > > > > > > > Thanks,  
> > > > > > > > > > Will

---

<div class="post-metadata">

**Author:** ![William\_Blair](https://avatars.discourse-cdn.com/v4/letter/w/c68b51/32.png) [@William\_Blair](https://discourse.ats-lang.org/u/William_Blair)\
**Post date:** [January 15, 2015, 9:31am UTC](https://discourse.ats-lang.org/t/problem-with-template-instantiation-atspmvenv/503/9 "2015-01-15T09:31:09Z")

</div>

> > I changed the keyword $closurenv to $tempenver as this is only  
> > related to compiling template instances.

Thanks! That fixed the environment variable issue.

Another problem I ran into was using templates to parse lists of lists.  
I’ve made a small example to demonstrate the issue I’m running into.

```
https://gist.github.com/wdblair/33010d8544d79c263ccf

```

If you try to compile the above gist using

```
patscc -c -o printlists_dats.o printlists.dats

```

The C compiler complains about the following line:

/\*  
emit\_instr: loc0 = /Users/wdblair/tmp/printlists.dats: 437(line=33,  
offs=13) – 454(line=33, offs=30)  
\*/  
ATSINSmove\_void(tmp3\_\_2,  
PMVtmpltcstmat[0](print\_t0ype\<S2Eexi(n$8536$8538(13787); S2Eapp(S2Ecst(\>=);  
S2Evar(n$8536$8538(13787)), S2Eintinf(0)); S2Eapp(S2Ecst(list);  
S2Ecst(itm), S2Evar(n$8536$8538(13787))))\>)(tmp1\_\_2)) ;

It seems that it isn’t finding the implementation of print\_t0ype  
given inside print\_itmlstlst. Does it look like I am using templates  
incorrectly here?

Thanks,  
WillAm Samstag, 10. Januar 2015 13:50:36 UTC-5 schrieb gmhwxi:

> I changed the keyword $closurenv to $tempenver as this is only  
> related to compiling template instances.
> 
> On Saturday, January 10, 2015 at 2:59:02 AM UTC-5, gmhwxi wrote:
> 
> > I came up with a fix for this issue.
> > 
> > Basically, the idea is to allow the programmer to indicate to the  
> > compiler that a particular  
> > dynamic variable should be included in the environment of a template  
> > instance. For instance,
> > 
> > implement  
> > main0 (argc, argv) = let  
> > //  
> > val () = $closurenv(argc) // [argc] is put into the environment of the  
> > following instance of [foo]  
> > //  
> > in  
> > … foo\<\> (…) …  
> > end // end of [main0]
> > 
> > For a running example, please see:
> > 
> > [https://github.com/githwxi/ATS-Postiats/blob/master/doc/BUGS/bug-2015-01-07.dats](https://github.com/githwxi/ATS-Postiats/blob/master/doc/BUGS/bug-2015-01-07.dats)
> > 
> > You need ATS2-github to test this example.
> > 
> > On Thursday, January 8, 2015 at 2:39:39 AM UTC-5, gmhwxi wrote:
> > 
> > > This is a VERY tricky issue because this kind of use was _not_  
> > > part of the design. On the other hand, it seems natural to use templates  
> > > this way. So …
> > > 
> > > On Thursday, January 8, 2015 12:59:43 AM UTC-5, gmhwxi wrote:
> > > 
> > > > I have managed to produce this kind of bug using the following simple  
> > > > code:
> > > > 
> > > > [https://github.com/githwxi/ATS-Postiats/blob/master/doc/BUGS/bug-2015-01-07.dats](https://github.com/githwxi/ATS-Postiats/blob/master/doc/BUGS/bug-2015-01-07.dats)
> > > > 
> > > > On Wednesday, January 7, 2015 10:08:49 PM UTC-5, gmhwxi wrote:
> > > > 
> > > > > I think I know the cause of this problem, but fixing it requires some  
> > > > > thoughts.
> > > > > 
> > > > > Basically, you used some templates that are defined mutually  
> > > > > recursively:
> > > > > 
> > > > > implement{} f () = g\<\> ()  
> > > > > implement{} g () = f\<\> ()
> > > > > 
> > > > > Let me first investigate the issue a bit further.
> > > > > 
> > > > > On Wednesday, January 7, 2015 6:38:38 PM UTC-5, William Blair wrote:
> > > > > 
> > > > > > I’m having some trouble compiling code that makes heavy use of  
> > > > > > templates. I am using a library that parses JSON into an array of tokens.  
> > > > > > Each token refers to a value which can be retrieved from the original JSON  
> > > > > > string. I have a template function that returns this string. Before any  
> > > > > > information is taken from the tokens, the user must implement this function  
> > > > > > with the correct string. This avoids having to set up a global variable or  
> > > > > > add parameters to functions. When I try to compile the corresponding C  
> > > > > > code, I find many instances of the following error, but only for some  
> > > > > > template functions.
> > > > > > 
> > > > > > ```
> > > > > > parsing_dats.c:86143:186: error: use of undeclared identifier 
> > > > > > 
> > > > > > ```
> > > > > > 
> > > > > > ‘data’  
> > > > > > ATSINSmove(tmpret762\_\_4, parse\_s2rt\_\_131\_\_4(ATSPMVenv(data),  
> > > > > > arg0)) ;
> > > > > > 
> > > > > > The highest level code looks like the following
> > > > > > 
> > > > > > ```
> > > > > > let val (data, jsv) = jsonval_parse_from_stdin ()
> > > > > > 
> > > > > > implement
> > > > > > jsonval_src<> () = data
> > > > > > 
> > > > > > val c3nstrs = jsv["c3nstrbody"]
> > > > > > 
> > > > > > in
> > > > > > parse_c3nstr (c3nstrs)
> > > > > > end
> > > > > > 
> > > > > > ```
> > > > > > 
> > > > > > I made a simpler version that uses the same technique, but the C code  
> > > > > > produced by patsopt compiles without any errors. You can find this  
> > > > > > small example on github.
> > > > > > 
> > > > > > ```
> > > > > > https://gist.github.com/wdblair/2b5e0bd054b91abfe71d
> > > > > > 
> > > > > > ```
> > > > > > 
> > > > > > Is it possible I am using too many layers of templates in my code?  
> > > > > > You can see the whole picture in the following repo:
> > > > > > 
> > > > > > [https://github.com/wdblair/ATS-Postiats-contrib/tree/jsmn/projects/MEDIUM/ATS-extsolve/parsing](https://github.com/wdblair/ATS-Postiats-contrib/tree/jsmn/projects/MEDIUM/ATS-extsolve/parsing)
> > > > > > 
> > > > > > Thanks,  
> > > > > > Will

---

<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 16, 2015, 3:52am UTC](https://discourse.ats-lang.org/t/problem-with-template-instantiation-atspmvenv/503/10 "2015-01-16T03:52:38Z")

</div>

Hello Hongwei,

> For now, a template argument needs to be a non-dependent type.

What do you mean here by ‘non-dependent’?

List0 is dependent because it is indexed? What about parameterized types?  
Can a parameterized type be a template argument?

---

<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 8, 2015, 3:08am UTC](https://discourse.ats-lang.org/t/problem-with-template-instantiation-atspmvenv/503/11 "2015-01-08T03:08:49Z")

</div>

I think I know the cause of this problem, but fixing it requires some  
thoughts.

Basically, you used some templates that are defined mutually recursively:

implement{} f () = g\<\> ()  
implement{} g () = f\<\> ()

Let me first investigate the issue a bit further.On Wednesday, January 7, 2015 6:38:38 PM UTC-5, William Blair wrote:

> I’m having some trouble compiling code that makes heavy use of templates.  
> I am using a library that parses JSON into an array of tokens. Each token  
> refers to a value which can be retrieved from the original JSON string. I  
> have a template function that returns this string. Before any information  
> is taken from the tokens, the user must implement this function with the  
> correct string. This avoids having to set up a global variable or add  
> parameters to functions. When I try to compile the corresponding C code, I  
> find many instances of the following error, but only for some template  
> functions.
> 
> ```
> parsing_dats.c:86143:186: error: use of undeclared identifier 'data'
> ATSINSmove(tmpret762 __4, parse_s2rt__ 131__4(ATSPMVenv(data), arg0)) ;
> 
> ```
> 
> The highest level code looks like the following
> 
> ```
> let val (data, jsv) = jsonval_parse_from_stdin ()
> 
> implement
> jsonval_src<> () = data
> 
> val c3nstrs = jsv["c3nstrbody"]
> 
> in
> parse_c3nstr (c3nstrs)
> end
> 
> ```
> 
> I made a simpler version that uses the same technique, but the C code  
> produced by patsopt compiles without any errors. You can find this small  
> example on github.
> 
> ```
> https://gist.github.com/wdblair/2b5e0bd054b91abfe71d
> 
> ```
> 
> Is it possible I am using too many layers of templates in my code? You can  
> see the whole picture in the following repo:
> 
> [https://github.com/wdblair/ATS-Postiats-contrib/tree/jsmn/projects/MEDIUM/ATS-extsolve/parsing](https://github.com/wdblair/ATS-Postiats-contrib/tree/jsmn/projects/MEDIUM/ATS-extsolve/parsing)
> 
> Thanks,  
> Will

---

<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 16, 2015, 5:32am UTC](https://discourse.ats-lang.org/t/problem-with-template-instantiation-atspmvenv/503/12 "2015-01-16T05:32:43Z")

</div>

I should have said non-quantified types (instead of non-dependent types).

List0(a) is quantified:

List0(a) = [n:int] list(a, n)On Thu, Jan 15, 2015 at 10:52 PM, Artyom Shalkhakov \< [artyom.s...@gmail.com](mailto:artyom.s...@gmail.com)\> wrote:

> Hello Hongwei,
> 
> On Thursday, January 15, 2015 at 11:30:14 PM UTC+6, gmhwxi wrote:
> 
> > For now, a template argument needs to be a non-dependent type.
> 
> What do you mean here by ‘non-dependent’?
> 
> List0 is dependent because it is indexed? What about parameterized types?  
> Can a parameterized type be a template argument?
> 
> > List0 is dependent. The problem can be fixed by changing List0 to list0:
> > 
> > fun{}  
> > print\_itmlstlst  
> > (iss: itmlstlst): void = let  
> > typedef itmlst0 = list0(itm)  
> > implement  
> > print\_t0ype  
> > (is) = print\_itmlst (g1ofg0(is))  
> > in  
> > print\_list ($UNSAFE.cast{List0(itmlst0)}(iss))  
> > end
> > 
> > You need the following line for list0:
> > 
> > staload “prelude/libats/ML/SATS/list0.sats”
> > 
> > On Thursday, January 15, 2015 at 4:31:09 AM UTC-5, William Blair wrote:
> > 
> > > > > I changed the keyword $closurenv to $tempenver as this is only  
> > > > > related to compiling template instances.
> > > 
> > > Thanks! That fixed the environment variable issue.
> > > 
> > > Another problem I ran into was using templates to parse lists of lists.  
> > > I’ve made a small example to demonstrate the issue I’m running into.
> > > 
> > > ```
> > > https://gist.github.com/wdblair/33010d8544d79c263ccf
> > > 
> > > ```
> > > 
> > > If you try to compile the above gist using
> > > 
> > > ```
> > > patscc -c -o printlists_dats.o printlists.dats
> > > 
> > > ```
> > > 
> > > The C compiler complains about the following line:
> > > 
> > > /\*  
> > > emit\_instr: loc0 = /Users/wdblair/tmp/printlists.dats: 437(line=33,  
> > > offs=13) – 454(line=33, offs=30)  
> > > \*/  
> > > ATSINSmove\_void(tmp3\_\_2, PMVtmpltcstmat[0](print\_t0ype\<S2Eexi(n$8536$8538(13787);  
> > > S2Eapp(S2Ecst(\>=); S2Evar(n$8536$8538(13787)), S2Eintinf(0));  
> > > S2Eapp(S2Ecst(list); S2Ecst(itm), S2Evar(n$8536$8538(13787))))\>)(tmp1\_\_2))  
> > > ;
> > > 
> > > It seems that it isn’t finding the implementation of print\_t0ype  
> > > given inside print\_itmlstlst. Does it look like I am using templates  
> > > incorrectly here?
> > > 
> > > Thanks,  
> > > Will
> > > 
> > > Am Samstag, 10. Januar 2015 13:50:36 UTC-5 schrieb gmhwxi:
> > > 
> > > > I changed the keyword $closurenv to $tempenver as this is only  
> > > > related to compiling template instances.
> > > > 
> > > > On Saturday, January 10, 2015 at 2:59:02 AM UTC-5, gmhwxi wrote:
> > > > 
> > > > > I came up with a fix for this issue.
> > > > > 
> > > > > Basically, the idea is to allow the programmer to indicate to the  
> > > > > compiler that a particular  
> > > > > dynamic variable should be included in the environment of a template  
> > > > > instance. For instance,
> > > > > 
> > > > > implement  
> > > > > main0 (argc, argv) = let  
> > > > > //  
> > > > > val () = $closurenv(argc) // [argc] is put into the environment of  
> > > > > the following instance of [foo]  
> > > > > //  
> > > > > in  
> > > > > … foo\<\> (…) …  
> > > > > end // end of [main0]
> > > > > 
> > > > > For a running example, please see:
> > > > > 
> > > > > [https://github.com/githwxi/ATS-Postiats/blob/master/doc/](https://github.com/githwxi/ATS-Postiats/blob/master/doc/)  
> > > > > BUGS/bug-2015-01-07.dats
> > > > > 
> > > > > You need ATS2-github to test this example.
> > > > > 
> > > > > On Thursday, January 8, 2015 at 2:39:39 AM UTC-5, gmhwxi wrote:
> > > > > 
> > > > > > This is a VERY tricky issue because this kind of use was _not_  
> > > > > > part of the design. On the other hand, it seems natural to use  
> > > > > > templates  
> > > > > > this way. So …
> > > > > > 
> > > > > > On Thursday, January 8, 2015 12:59:43 AM UTC-5, gmhwxi wrote:
> > > > > > 
> > > > > > > I have managed to produce this kind of bug using the following  
> > > > > > > simple code:
> > > > > > > 
> > > > > > > [https://github.com/githwxi/ATS-Postiats/blob/master/doc/](https://github.com/githwxi/ATS-Postiats/blob/master/doc/)  
> > > > > > > BUGS/bug-2015-01-07.dats
> > > > > > > 
> > > > > > > On Wednesday, January 7, 2015 10:08:49 PM UTC-5, gmhwxi wrote:
> > > > > > > 
> > > > > > > > I think I know the cause of this problem, but fixing it requires  
> > > > > > > > some thoughts.
> > > > > > > > 
> > > > > > > > Basically, you used some templates that are defined mutually  
> > > > > > > > recursively:
> > > > > > > > 
> > > > > > > > implement{} f () = g\<\> ()  
> > > > > > > > implement{} g () = f\<\> ()
> > > > > > > > 
> > > > > > > > Let me first investigate the issue a bit further.
> > > > > > > > 
> > > > > > > > On Wednesday, January 7, 2015 6:38:38 PM UTC-5, William Blair wrote:
> > > > > > > > 
> > > > > > > > > I’m having some trouble compiling code that makes heavy use of  
> > > > > > > > > templates. I am using a library that parses JSON into an array of tokens.  
> > > > > > > > > Each token refers to a value which can be retrieved from the original JSON  
> > > > > > > > > string. I have a template function that returns this string. Before any  
> > > > > > > > > information is taken from the tokens, the user must implement this function  
> > > > > > > > > with the correct string. This avoids having to set up a global variable or  
> > > > > > > > > add parameters to functions. When I try to compile the corresponding C  
> > > > > > > > > code, I find many instances of the following error, but only for some  
> > > > > > > > > template functions.
> > > > > > > > > 
> > > > > > > > > ```
> > > > > > > > > parsing_dats.c:86143:186: error: use of undeclared identifier
> > > > > > > > > 
> > > > > > > > > ```
> > > > > > > > > 
> > > > > > > > > ‘data’  
> > > > > > > > > ATSINSmove(tmpret762\_\_4, parse\_s2rt\_\_131\_\_4(ATSPMVenv(data),  
> > > > > > > > > arg0)) ;
> > > > > > > > > 
> > > > > > > > > The highest level code looks like the following
> > > > > > > > > 
> > > > > > > > > ```
> > > > > > > > > let val (data, jsv) = jsonval_parse_from_stdin ()
> > > > > > > > > 
> > > > > > > > > implement
> > > > > > > > > jsonval_src<> () = data
> > > > > > > > > 
> > > > > > > > > val c3nstrs = jsv["c3nstrbody"]
> > > > > > > > > 
> > > > > > > > > in
> > > > > > > > > parse_c3nstr (c3nstrs)
> > > > > > > > > end
> > > > > > > > > 
> > > > > > > > > ```
> > > > > > > > > 
> > > > > > > > > I made a simpler version that uses the same technique, but the C  
> > > > > > > > > code produced by patsopt compiles without any errors. You can  
> > > > > > > > > find this small example on github.
> > > > > > > > > 
> > > > > > > > > ```
> > > > > > > > > https://gist.github.com/wdblair/2b5e0bd054b91abfe71d
> > > > > > > > > 
> > > > > > > > > ```
> > > > > > > > > 
> > > > > > > > > Is it possible I am using too many layers of templates in my code?  
> > > > > > > > > You can see the whole picture in the following repo:
> > > > > > > > > 
> > > > > > > > > ```
> > > > > > > > > https://github.com/wdblair/ATS-Postiats-contrib/tree/
> > > > > > > > > 
> > > > > > > > > ```
> > > > > > > > > 
> > > > > > > > > jsmn/projects/MEDIUM/ATS-extsolve/parsing
> > > > > > > > > 
> > > > > > > > > Thanks,  
> > > > > > > > > Will
> > > > > > > > > 
> > > > > > > > > –  
> > > > > > > > > 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/086278ab-75b2-498d-b8b4-09ccb274ef5d%40googlegroups.com](https://groups.google.com/d/msgid/ats-lang-users/086278ab-75b2-498d-b8b4-09ccb274ef5d%40googlegroups.com)  
> > > > > > > > > [https://groups.google.com/d/msgid/ats-lang-users/086278ab-75b2-498d-b8b4-09ccb274ef5d%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/ats-lang-users/086278ab-75b2-498d-b8b4-09ccb274ef5d%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > > > > > > > .

---

<div class="post-metadata">

**Author:** ![William\_Blair](https://avatars.discourse-cdn.com/v4/letter/w/c68b51/32.png) [@William\_Blair](https://discourse.ats-lang.org/u/William_Blair)\
**Post date:** [January 8, 2015, 9:04pm UTC](https://discourse.ats-lang.org/t/problem-with-template-instantiation-atspmvenv/503/13 "2015-01-08T21:04:37Z")

</div>

Ah I see, that should be easy to implement. Thanks!Am Donnerstag, 8. Januar 2015 15:20:22 UTC-5 schrieb gmhwxi:

> > > Are mutually recursive templates only an issue when instantiating one of  
> > > the templates requires bringing in variables from the environment? If so, I  
> > > could just replace the jsonval\_src template with a global variable for now.
> 
> That should work.
> 
> I think that a better way for now is to break the mutual dependency:
> 
> implement  
> {}(_tmp_)  
> parse\_s2rt (…) = let
> 
> fun aux\_S2RTfun (…) = (code for parse\_S2RTfun)…
> 
> in  
> …  
> end
> 
> implement  
> {}(_tmp_)  
> parse\_S2RTfun (…) = … // if you still need it.
> 
> On Thu, Jan 8, 2015 at 3:12 PM, William Blair \<[william...@gmail.com](mailto:william...@gmail.com) \<javascript:\>\> wrote:
> 
> > Are mutually recursive templates only an issue when instantiating one of  
> > the templates requires bringing in variables from the environment? If so, I  
> > could just replace the jsonval\_src template with a global variable for now.
> > 
> > What would you have to do to support using templates in this way?
> > 
> > Am Donnerstag, 8. Januar 2015 02:39:39 UTC-5 schrieb gmhwxi:
> > 
> > > This is a VERY tricky issue because this kind of use was _not_  
> > > part of the design. On the other hand, it seems natural to use templates  
> > > this way. So …
> > > 
> > > On Thursday, January 8, 2015 12:59:43 AM UTC-5, gmhwxi wrote:
> > > 
> > > > I have managed to produce this kind of bug using the following simple  
> > > > code:
> > > > 
> > > > [https://github.com/githwxi/ATS-Postiats/blob/master/doc/](https://github.com/githwxi/ATS-Postiats/blob/master/doc/)  
> > > > BUGS/bug-2015-01-07.dats
> > > > 
> > > > On Wednesday, January 7, 2015 10:08:49 PM UTC-5, gmhwxi wrote:
> > > > 
> > > > > I think I know the cause of this problem, but fixing it requires some  
> > > > > thoughts.
> > > > > 
> > > > > Basically, you used some templates that are defined mutually  
> > > > > recursively:
> > > > > 
> > > > > implement{} f () = g\<\> ()  
> > > > > implement{} g () = f\<\> ()
> > > > > 
> > > > > Let me first investigate the issue a bit further.
> > > > > 
> > > > > On Wednesday, January 7, 2015 6:38:38 PM UTC-5, William Blair wrote:
> > > > > 
> > > > > > I’m having some trouble compiling code that makes heavy use of  
> > > > > > templates. I am using a library that parses JSON into an array of tokens.  
> > > > > > Each token refers to a value which can be retrieved from the original JSON  
> > > > > > string. I have a template function that returns this string. Before any  
> > > > > > information is taken from the tokens, the user must implement this function  
> > > > > > with the correct string. This avoids having to set up a global variable or  
> > > > > > add parameters to functions. When I try to compile the corresponding C  
> > > > > > code, I find many instances of the following error, but only for some  
> > > > > > template functions.
> > > > > > 
> > > > > > ```
> > > > > > parsing_dats.c:86143:186: error: use of undeclared identifier 
> > > > > > 
> > > > > > ```
> > > > > > 
> > > > > > ‘data’  
> > > > > > ATSINSmove(tmpret762\_\_4, parse\_s2rt\_\_131\_\_4(ATSPMVenv(data),  
> > > > > > arg0)) ;
> > > > > > 
> > > > > > The highest level code looks like the following
> > > > > > 
> > > > > > ```
> > > > > > let val (data, jsv) = jsonval_parse_from_stdin ()
> > > > > > 
> > > > > > implement
> > > > > > jsonval_src<> () = data
> > > > > > 
> > > > > > val c3nstrs = jsv["c3nstrbody"]
> > > > > > 
> > > > > > in
> > > > > > parse_c3nstr (c3nstrs)
> > > > > > end
> > > > > > 
> > > > > > ```
> > > > > > 
> > > > > > I made a simpler version that uses the same technique, but the C code  
> > > > > > produced by patsopt compiles without any errors. You can find this  
> > > > > > small example on github.
> > > > > > 
> > > > > > ```
> > > > > > https://gist.github.com/wdblair/2b5e0bd054b91abfe71d
> > > > > > 
> > > > > > ```
> > > > > > 
> > > > > > Is it possible I am using too many layers of templates in my code?  
> > > > > > You can see the whole picture in the following repo:
> > > > > > 
> > > > > > ```
> > > > > > https://github.com/wdblair/ATS-Postiats-contrib/tree/
> > > > > > 
> > > > > > ```
> > > > > > 
> > > > > > jsmn/projects/MEDIUM/ATS-extsolve/parsing
> > > > > > 
> > > > > > Thanks,  
> > > > > > Will
> > > > > > 
> > > > > > –  
> > > > > > 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/562bff4d-d74e-4d6f-9f07-a0adb027e0df%40googlegroups.com](https://groups.google.com/d/msgid/ats-lang-users/562bff4d-d74e-4d6f-9f07-a0adb027e0df%40googlegroups.com)  
> > > > > > [https://groups.google.com/d/msgid/ats-lang-users/562bff4d-d74e-4d6f-9f07-a0adb027e0df%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/ats-lang-users/562bff4d-d74e-4d6f-9f07-a0adb027e0df%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > > > > .

---

<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, 5:33pm UTC](https://discourse.ats-lang.org/t/problem-with-template-instantiation-atspmvenv/503/14 "2015-01-15T17:33:55Z")

</div>

The following two lines are needed:

staload “libats/ML/SATS/basis.sats”  
staload “libats/ML/SATS/list0.sats”

Or just do:

#include "share/HATS/atspre\_staload\_libats\_ML.hats"On Thursday, January 15, 2015 at 12:30:14 PM UTC-5, gmhwxi wrote:

> For now, a template argument needs to be a non-dependent type.  
> List0 is dependent. The problem can be fixed by changing List0 to list0:
> 
> fun{}  
> print\_itmlstlst  
> (iss: itmlstlst): void = let  
> typedef itmlst0 = list0(itm)  
> implement  
> print\_t0ype  
> (is) = print\_itmlst (g1ofg0(is))  
> in  
> print\_list ($UNSAFE.cast{List0(itmlst0)}(iss))  
> end
> 
> You need the following line for list0:
> 
> staload “prelude/libats/ML/SATS/list0.sats”
> 
> On Thursday, January 15, 2015 at 4:31:09 AM UTC-5, William Blair wrote:
> 
> > > > I changed the keyword $closurenv to $tempenver as this is only  
> > > > related to compiling template instances.
> > 
> > Thanks! That fixed the environment variable issue.
> > 
> > Another problem I ran into was using templates to parse lists of lists.  
> > I’ve made a small example to demonstrate the issue I’m running into.
> > 
> > ```
> > https://gist.github.com/wdblair/33010d8544d79c263ccf
> > 
> > ```
> > 
> > If you try to compile the above gist using
> > 
> > ```
> > patscc -c -o printlists_dats.o printlists.dats
> > 
> > ```
> > 
> > The C compiler complains about the following line:
> > 
> > /\*  
> > emit\_instr: loc0 = /Users/wdblair/tmp/printlists.dats: 437(line=33,  
> > offs=13) – 454(line=33, offs=30)  
> > \*/  
> > ATSINSmove\_void(tmp3\_\_2,  
> > PMVtmpltcstmat[0](print\_t0ype\<S2Eexi(n$8536$8538(13787); S2Eapp(S2Ecst(\>=);  
> > S2Evar(n$8536$8538(13787)), S2Eintinf(0)); S2Eapp(S2Ecst(list);  
> > S2Ecst(itm), S2Evar(n$8536$8538(13787))))\>)(tmp1\_\_2)) ;
> > 
> > It seems that it isn’t finding the implementation of print\_t0ype  
> > given inside print\_itmlstlst. Does it look like I am using templates  
> > incorrectly here?
> > 
> > Thanks,  
> > Will
> > 
> > Am Samstag, 10. Januar 2015 13:50:36 UTC-5 schrieb gmhwxi:
> > 
> > > I changed the keyword $closurenv to $tempenver as this is only  
> > > related to compiling template instances.
> > > 
> > > On Saturday, January 10, 2015 at 2:59:02 AM UTC-5, gmhwxi wrote:
> > > 
> > > > I came up with a fix for this issue.
> > > > 
> > > > Basically, the idea is to allow the programmer to indicate to the  
> > > > compiler that a particular  
> > > > dynamic variable should be included in the environment of a template  
> > > > instance. For instance,
> > > > 
> > > > implement  
> > > > main0 (argc, argv) = let  
> > > > //  
> > > > val () = $closurenv(argc) // [argc] is put into the environment of the  
> > > > following instance of [foo]  
> > > > //  
> > > > in  
> > > > … foo\<\> (…) …  
> > > > end // end of [main0]
> > > > 
> > > > For a running example, please see:
> > > > 
> > > > [https://github.com/githwxi/ATS-Postiats/blob/master/doc/BUGS/bug-2015-01-07.dats](https://github.com/githwxi/ATS-Postiats/blob/master/doc/BUGS/bug-2015-01-07.dats)
> > > > 
> > > > You need ATS2-github to test this example.
> > > > 
> > > > On Thursday, January 8, 2015 at 2:39:39 AM UTC-5, gmhwxi wrote:
> > > > 
> > > > > This is a VERY tricky issue because this kind of use was _not_  
> > > > > part of the design. On the other hand, it seems natural to use  
> > > > > templates  
> > > > > this way. So …
> > > > > 
> > > > > On Thursday, January 8, 2015 12:59:43 AM UTC-5, gmhwxi wrote:
> > > > > 
> > > > > > I have managed to produce this kind of bug using the following simple  
> > > > > > code:
> > > > > > 
> > > > > > [https://github.com/githwxi/ATS-Postiats/blob/master/doc/BUGS/bug-2015-01-07.dats](https://github.com/githwxi/ATS-Postiats/blob/master/doc/BUGS/bug-2015-01-07.dats)
> > > > > > 
> > > > > > On Wednesday, January 7, 2015 10:08:49 PM UTC-5, gmhwxi wrote:
> > > > > > 
> > > > > > > I think I know the cause of this problem, but fixing it requires  
> > > > > > > some thoughts.
> > > > > > > 
> > > > > > > Basically, you used some templates that are defined mutually  
> > > > > > > recursively:
> > > > > > > 
> > > > > > > implement{} f () = g\<\> ()  
> > > > > > > implement{} g () = f\<\> ()
> > > > > > > 
> > > > > > > Let me first investigate the issue a bit further.
> > > > > > > 
> > > > > > > On Wednesday, January 7, 2015 6:38:38 PM UTC-5, William Blair wrote:
> > > > > > > 
> > > > > > > > I’m having some trouble compiling code that makes heavy use of  
> > > > > > > > templates. I am using a library that parses JSON into an array of tokens.  
> > > > > > > > Each token refers to a value which can be retrieved from the original JSON  
> > > > > > > > string. I have a template function that returns this string. Before any  
> > > > > > > > information is taken from the tokens, the user must implement this function  
> > > > > > > > with the correct string. This avoids having to set up a global variable or  
> > > > > > > > add parameters to functions. When I try to compile the corresponding C  
> > > > > > > > code, I find many instances of the following error, but only for some  
> > > > > > > > template functions.
> > > > > > > > 
> > > > > > > > ```
> > > > > > > > parsing_dats.c:86143:186: error: use of undeclared identifier 
> > > > > > > > 
> > > > > > > > ```
> > > > > > > > 
> > > > > > > > ‘data’  
> > > > > > > > ATSINSmove(tmpret762\_\_4, parse\_s2rt\_\_131\_\_4(ATSPMVenv(data),  
> > > > > > > > arg0)) ;
> > > > > > > > 
> > > > > > > > The highest level code looks like the following
> > > > > > > > 
> > > > > > > > ```
> > > > > > > > let val (data, jsv) = jsonval_parse_from_stdin ()
> > > > > > > > 
> > > > > > > > implement
> > > > > > > > jsonval_src<> () = data
> > > > > > > > 
> > > > > > > > val c3nstrs = jsv["c3nstrbody"]
> > > > > > > > 
> > > > > > > > in
> > > > > > > > parse_c3nstr (c3nstrs)
> > > > > > > > end
> > > > > > > > 
> > > > > > > > ```
> > > > > > > > 
> > > > > > > > I made a simpler version that uses the same technique, but the C  
> > > > > > > > code produced by patsopt compiles without any errors. You can find  
> > > > > > > > this small example on github.
> > > > > > > > 
> > > > > > > > ```
> > > > > > > > https://gist.github.com/wdblair/2b5e0bd054b91abfe71d
> > > > > > > > 
> > > > > > > > ```
> > > > > > > > 
> > > > > > > > Is it possible I am using too many layers of templates in my code?  
> > > > > > > > You can see the whole picture in the following repo:
> > > > > > > > 
> > > > > > > > [https://github.com/wdblair/ATS-Postiats-contrib/tree/jsmn/projects/MEDIUM/ATS-extsolve/parsing](https://github.com/wdblair/ATS-Postiats-contrib/tree/jsmn/projects/MEDIUM/ATS-extsolve/parsing)
> > > > > > > > 
> > > > > > > > Thanks,  
> > > > > > > > Will

---

<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 8, 2015, 8:27pm UTC](https://discourse.ats-lang.org/t/problem-with-template-instantiation-atspmvenv/503/15 "2015-01-08T20:27:41Z")

</div>

> > What would you have to do to support using templates in this way?

Not really sure yet.

This kind of thing is inevitable as it is impossible to plan/design  
everything  
ahead.On Thursday, January 8, 2015 at 3:12:07 PM UTC-5, William Blair wrote:

> Are mutually recursive templates only an issue when instantiating one of  
> the templates requires bringing in variables from the environment? If so, I  
> could just replace the jsonval\_src template with a global variable for now.
> 
> What would you have to do to support using templates in this way?
> 
> Am Donnerstag, 8. Januar 2015 02:39:39 UTC-5 schrieb gmhwxi:
> 
> > This is a VERY tricky issue because this kind of use was _not_  
> > part of the design. On the other hand, it seems natural to use templates  
> > this way. So …
> > 
> > On Thursday, January 8, 2015 12:59:43 AM UTC-5, gmhwxi wrote:
> > 
> > > I have managed to produce this kind of bug using the following simple  
> > > code:
> > > 
> > > [https://github.com/githwxi/ATS-Postiats/blob/master/doc/BUGS/bug-2015-01-07.dats](https://github.com/githwxi/ATS-Postiats/blob/master/doc/BUGS/bug-2015-01-07.dats)
> > > 
> > > On Wednesday, January 7, 2015 10:08:49 PM UTC-5, gmhwxi wrote:
> > > 
> > > > I think I know the cause of this problem, but fixing it requires some  
> > > > thoughts.
> > > > 
> > > > Basically, you used some templates that are defined mutually  
> > > > recursively:
> > > > 
> > > > implement{} f () = g\<\> ()  
> > > > implement{} g () = f\<\> ()
> > > > 
> > > > Let me first investigate the issue a bit further.
> > > > 
> > > > On Wednesday, January 7, 2015 6:38:38 PM UTC-5, William Blair wrote:
> > > > 
> > > > > I’m having some trouble compiling code that makes heavy use of  
> > > > > templates. I am using a library that parses JSON into an array of tokens.  
> > > > > Each token refers to a value which can be retrieved from the original JSON  
> > > > > string. I have a template function that returns this string. Before any  
> > > > > information is taken from the tokens, the user must implement this function  
> > > > > with the correct string. This avoids having to set up a global variable or  
> > > > > add parameters to functions. When I try to compile the corresponding C  
> > > > > code, I find many instances of the following error, but only for some  
> > > > > template functions.
> > > > > 
> > > > > ```
> > > > > parsing_dats.c:86143:186: error: use of undeclared identifier 
> > > > > 
> > > > > ```
> > > > > 
> > > > > ‘data’  
> > > > > ATSINSmove(tmpret762\_\_4, parse\_s2rt\_\_131\_\_4(ATSPMVenv(data),  
> > > > > arg0)) ;
> > > > > 
> > > > > The highest level code looks like the following
> > > > > 
> > > > > ```
> > > > > let val (data, jsv) = jsonval_parse_from_stdin ()
> > > > > 
> > > > > implement
> > > > > jsonval_src<> () = data
> > > > > 
> > > > > val c3nstrs = jsv["c3nstrbody"]
> > > > > 
> > > > > in
> > > > > parse_c3nstr (c3nstrs)
> > > > > end
> > > > > 
> > > > > ```
> > > > > 
> > > > > I made a simpler version that uses the same technique, but the C code  
> > > > > produced by patsopt compiles without any errors. You can find this  
> > > > > small example on github.
> > > > > 
> > > > > ```
> > > > > https://gist.github.com/wdblair/2b5e0bd054b91abfe71d
> > > > > 
> > > > > ```
> > > > > 
> > > > > Is it possible I am using too many layers of templates in my code? You  
> > > > > can see the whole picture in the following repo:
> > > > > 
> > > > > [https://github.com/wdblair/ATS-Postiats-contrib/tree/jsmn/projects/MEDIUM/ATS-extsolve/parsing](https://github.com/wdblair/ATS-Postiats-contrib/tree/jsmn/projects/MEDIUM/ATS-extsolve/parsing)
> > > > > 
> > > > > Thanks,  
> > > > > Will

---

<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 10, 2015, 6:50pm UTC](https://discourse.ats-lang.org/t/problem-with-template-instantiation-atspmvenv/503/16 "2015-01-10T18:50:36Z")

</div>

I changed the keyword $closurenv to $tempenver as this is only  
related to compiling template instances.On Saturday, January 10, 2015 at 2:59:02 AM UTC-5, gmhwxi wrote:

> I came up with a fix for this issue.
> 
> Basically, the idea is to allow the programmer to indicate to the compiler  
> that a particular  
> dynamic variable should be included in the environment of a template  
> instance. For instance,
> 
> implement  
> main0 (argc, argv) = let  
> //  
> val () = $closurenv(argc) // [argc] is put into the environment of the  
> following instance of [foo]  
> //  
> in  
> … foo\<\> (…) …  
> end // end of [main0]
> 
> For a running example, please see:
> 
> [https://github.com/githwxi/ATS-Postiats/blob/master/doc/BUGS/bug-2015-01-07.dats](https://github.com/githwxi/ATS-Postiats/blob/master/doc/BUGS/bug-2015-01-07.dats)
> 
> You need ATS2-github to test this example.
> 
> On Thursday, January 8, 2015 at 2:39:39 AM UTC-5, gmhwxi wrote:
> 
> > This is a VERY tricky issue because this kind of use was _not_  
> > part of the design. On the other hand, it seems natural to use templates  
> > this way. So …
> > 
> > On Thursday, January 8, 2015 12:59:43 AM UTC-5, gmhwxi wrote:
> > 
> > > I have managed to produce this kind of bug using the following simple  
> > > code:
> > > 
> > > [https://github.com/githwxi/ATS-Postiats/blob/master/doc/BUGS/bug-2015-01-07.dats](https://github.com/githwxi/ATS-Postiats/blob/master/doc/BUGS/bug-2015-01-07.dats)
> > > 
> > > On Wednesday, January 7, 2015 10:08:49 PM UTC-5, gmhwxi wrote:
> > > 
> > > > I think I know the cause of this problem, but fixing it requires some  
> > > > thoughts.
> > > > 
> > > > Basically, you used some templates that are defined mutually  
> > > > recursively:
> > > > 
> > > > implement{} f () = g\<\> ()  
> > > > implement{} g () = f\<\> ()
> > > > 
> > > > Let me first investigate the issue a bit further.
> > > > 
> > > > On Wednesday, January 7, 2015 6:38:38 PM UTC-5, William Blair wrote:
> > > > 
> > > > > I’m having some trouble compiling code that makes heavy use of  
> > > > > templates. I am using a library that parses JSON into an array of tokens.  
> > > > > Each token refers to a value which can be retrieved from the original JSON  
> > > > > string. I have a template function that returns this string. Before any  
> > > > > information is taken from the tokens, the user must implement this function  
> > > > > with the correct string. This avoids having to set up a global variable or  
> > > > > add parameters to functions. When I try to compile the corresponding C  
> > > > > code, I find many instances of the following error, but only for some  
> > > > > template functions.
> > > > > 
> > > > > ```
> > > > > parsing_dats.c:86143:186: error: use of undeclared identifier 
> > > > > 
> > > > > ```
> > > > > 
> > > > > ‘data’  
> > > > > ATSINSmove(tmpret762\_\_4, parse\_s2rt\_\_131\_\_4(ATSPMVenv(data),  
> > > > > arg0)) ;
> > > > > 
> > > > > The highest level code looks like the following
> > > > > 
> > > > > ```
> > > > > let val (data, jsv) = jsonval_parse_from_stdin ()
> > > > > 
> > > > > implement
> > > > > jsonval_src<> () = data
> > > > > 
> > > > > val c3nstrs = jsv["c3nstrbody"]
> > > > > 
> > > > > in
> > > > > parse_c3nstr (c3nstrs)
> > > > > end
> > > > > 
> > > > > ```
> > > > > 
> > > > > I made a simpler version that uses the same technique, but the C code  
> > > > > produced by patsopt compiles without any errors. You can find this  
> > > > > small example on github.
> > > > > 
> > > > > ```
> > > > > https://gist.github.com/wdblair/2b5e0bd054b91abfe71d
> > > > > 
> > > > > ```
> > > > > 
> > > > > Is it possible I am using too many layers of templates in my code? You  
> > > > > can see the whole picture in the following repo:
> > > > > 
> > > > > [https://github.com/wdblair/ATS-Postiats-contrib/tree/jsmn/projects/MEDIUM/ATS-extsolve/parsing](https://github.com/wdblair/ATS-Postiats-contrib/tree/jsmn/projects/MEDIUM/ATS-extsolve/parsing)
> > > > > 
> > > > > Thanks,  
> > > > > Will

---

<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 8, 2015, 8:20pm UTC](https://discourse.ats-lang.org/t/problem-with-template-instantiation-atspmvenv/503/17 "2015-01-08T20:20:22Z")

</div>

> > Are mutually recursive templates only an issue when instantiating one of  
> > the templates requires bringing in variables from the environment? If so, I  
> > could just replace the jsonval\_src template with a global variable for now.

That should work.

I think that a better way for now is to break the mutual dependency:

implement  
{}(_tmp_)  
parse\_s2rt (…) = let

fun aux\_S2RTfun (…) = (code for parse\_S2RTfun)…

in  
…  
end

implement  
{}(_tmp_)  
parse\_S2RTfun (…) = … // if you still need it.On Thu, Jan 8, 2015 at 3:12 PM, William Blair \< [william.dou...@gmail.com](mailto:william.dou...@gmail.com)\> wrote:

> Are mutually recursive templates only an issue when instantiating one of  
> the templates requires bringing in variables from the environment? If so, I  
> could just replace the jsonval\_src template with a global variable for now.
> 
> What would you have to do to support using templates in this way?
> 
> Am Donnerstag, 8. Januar 2015 02:39:39 UTC-5 schrieb gmhwxi:
> 
> > This is a VERY tricky issue because this kind of use was _not_  
> > part of the design. On the other hand, it seems natural to use templates  
> > this way. So …
> > 
> > On Thursday, January 8, 2015 12:59:43 AM UTC-5, gmhwxi wrote:
> > 
> > > I have managed to produce this kind of bug using the following simple  
> > > code:
> > > 
> > > [https://github.com/githwxi/ATS-Postiats/blob/master/doc/](https://github.com/githwxi/ATS-Postiats/blob/master/doc/)  
> > > BUGS/bug-2015-01-07.dats
> > > 
> > > On Wednesday, January 7, 2015 10:08:49 PM UTC-5, gmhwxi wrote:
> > > 
> > > > I think I know the cause of this problem, but fixing it requires some  
> > > > thoughts.
> > > > 
> > > > Basically, you used some templates that are defined mutually  
> > > > recursively:
> > > > 
> > > > implement{} f () = g\<\> ()  
> > > > implement{} g () = f\<\> ()
> > > > 
> > > > Let me first investigate the issue a bit further.
> > > > 
> > > > On Wednesday, January 7, 2015 6:38:38 PM UTC-5, William Blair wrote:
> > > > 
> > > > > I’m having some trouble compiling code that makes heavy use of  
> > > > > templates. I am using a library that parses JSON into an array of tokens.  
> > > > > Each token refers to a value which can be retrieved from the original JSON  
> > > > > string. I have a template function that returns this string. Before any  
> > > > > information is taken from the tokens, the user must implement this function  
> > > > > with the correct string. This avoids having to set up a global variable or  
> > > > > add parameters to functions. When I try to compile the corresponding C  
> > > > > code, I find many instances of the following error, but only for some  
> > > > > template functions.
> > > > > 
> > > > > ```
> > > > > parsing_dats.c:86143:186: error: use of undeclared identifier
> > > > > 
> > > > > ```
> > > > > 
> > > > > ‘data’  
> > > > > ATSINSmove(tmpret762\_\_4, parse\_s2rt\_\_131\_\_4(ATSPMVenv(data),  
> > > > > arg0)) ;
> > > > > 
> > > > > The highest level code looks like the following
> > > > > 
> > > > > ```
> > > > > let val (data, jsv) = jsonval_parse_from_stdin ()
> > > > > 
> > > > > implement
> > > > > jsonval_src<> () = data
> > > > > 
> > > > > val c3nstrs = jsv["c3nstrbody"]
> > > > > 
> > > > > in
> > > > > parse_c3nstr (c3nstrs)
> > > > > end
> > > > > 
> > > > > ```
> > > > > 
> > > > > I made a simpler version that uses the same technique, but the C code  
> > > > > produced by patsopt compiles without any errors. You can find this  
> > > > > small example on github.
> > > > > 
> > > > > ```
> > > > > https://gist.github.com/wdblair/2b5e0bd054b91abfe71d
> > > > > 
> > > > > ```
> > > > > 
> > > > > Is it possible I am using too many layers of templates in my code? You  
> > > > > can see the whole picture in the following repo:
> > > > > 
> > > > > ```
> > > > > https://github.com/wdblair/ATS-Postiats-contrib/tree/
> > > > > 
> > > > > ```
> > > > > 
> > > > > jsmn/projects/MEDIUM/ATS-extsolve/parsing
> > > > > 
> > > > > Thanks,  
> > > > > Will
> > > > > 
> > > > > –  
> > > > > 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/562bff4d-d74e-4d6f-9f07-a0adb027e0df%40googlegroups.com](https://groups.google.com/d/msgid/ats-lang-users/562bff4d-d74e-4d6f-9f07-a0adb027e0df%40googlegroups.com)  
> > > > > [https://groups.google.com/d/msgid/ats-lang-users/562bff4d-d74e-4d6f-9f07-a0adb027e0df%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/ats-lang-users/562bff4d-d74e-4d6f-9f07-a0adb027e0df%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > > > > .
