# Defining a function with internal mutable state

**URL:** <https://discourse.ats-lang.org/t/defining-a-function-with-internal-mutable-state/810>\
**Category:** Google Group\
**Created:** [June 19, 2014, 8:01pm UTC](https://discourse.ats-lang.org/t/defining-a-function-with-internal-mutable-state/810 "2014-06-19T20:01:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Justin\_Smith](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@Justin\_Smith](https://discourse.ats-lang.org/u/Justin_Smith)\
**Post date:** [June 19, 2014, 8:01pm UTC](https://discourse.ats-lang.org/t/defining-a-function-with-internal-mutable-state/810/1 "2014-06-19T20:01:08Z")

</div>

I have the following function, which should return a function that gives  
successive pseudo-random numbers, returning a new one on each call (adapted  
from some example code from the pulseaudio project for making a pink noise  
generator):

fun make\_rand (): ulint = let  
var randState = 22222UL  
in  
lam (): ulint =\> let  
val old = randState  
val new = (old \* 196314165) + 907633515  
in  
randState := new;  
new  
end  
end

There is some unneeded verbosity here that I added in order to work out my  
understanding of the steps being done as I try to address the errors from  
the compiler.

The ats2 compiler says “error(3): dereference cannot be performed: the  
proof search for view located at [S2Evar(randState(3800))] failed to turn  
up a result.”.

What do I need to satisfy the compiler that this local var will be  
accessible when called at runtime, and more importantly, how would I  
anticipate and diagnose this sort of issue when writing code in the future?

---

<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:** [May 18, 2015, 3:32pm UTC](https://discourse.ats-lang.org/t/defining-a-function-with-internal-mutable-state/810/2 "2015-05-18T15:32:11Z")

</div>

make\_rand is essentially defined as

make\_rand = lam () =\> lam () =\> …

So make\_rand() gives you a closure: lam () =\> …

In JavaScript, ‘ref’ becomes a polymorphic function (instead of a template).  
The code for make\_rand still works (though the syntax needs to be changed  
a little bit).On Monday, May 18, 2015 at 10:57:09 AM UTC-4, Yannick Duchêne wrote:

> Le jeudi 19 juin 2014 23:42:03 UTC+2, gmhwxi a écrit :
> 
> > [var] introduces a stack-allocated variable. If it was  
> > put into a closure, then dangling pointer would be created.
> > 
> > Here is a running implementation:
> > 
> > //  
> > #include  
> > “share/atspre\_staload.hats”  
> > //  
> > // How to compile:  
> > // patscc -DATS\_MEMALLOC\_LIBC -o rand rand.dats  
> > //  
> > (\* \*\*\*\*\*\* \*\*\*\*\*\* \*)
> > 
> > fun  
> > make\_rand  
> > (  
> > // argumentless  
> > ) : () - ulint = let  
> > val state = ref (22222UL)  
> > in  
> > //  
> > lam() : ulint =\> let  
> > val old = !state  
> > val new = (old \* 196314165UL) + 907633515UL  
> > val ((_void_)) = !state := new  
> > in  
> > new  
> > end // end of [lam]  
> > //  
> > end // end of [make\_rand]
> > 
> > (\* \*\*\*\*\*\* \*\*\*\*\*\* \*)
> > 
> > implement  
> > main0 () =  
> > {  
> > val rand = make\_rand()  
> > //  
> > val () = println! ("rand() = ", rand())  
> > val () = println! ("rand() = ", rand())  
> > //  
> > } (\* end of [main0] \*)
> > 
> > (\* \*\*\*\*\*\* \*\*\*\*\*\* \*)
> 
> I’ve never used ATS closure, and I try to understand this: `make_rand`  
> seems to return a parameterless lambda expression, but `rand` is invoked as  
> `rand()` instead of `rand()()`. Is the lambda expression transparent,  
> `make_rand` being substituted for the lambda expression it returns,  
> transparently? Is that it?
> 
> Also `val state = ref<ulint> (22222UL)` automatically allocates  
> heap‑storage transparently? Can this be tweaked for target other than C?  
> I’m thinking about the JavaScript target, which have no explicit allocation  
> and deallocation…

---

<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:** [June 19, 2014, 9:42pm UTC](https://discourse.ats-lang.org/t/defining-a-function-with-internal-mutable-state/810/3 "2014-06-19T21:42:03Z")

</div>

[var] introduces a stack-allocated variable. If it was  
put into a closure, then dangling pointer would be created.

Here is a running implementation:

//  
#include  
“share/atspre\_staload.hats”  
//  
// How to compile:  
// patscc -DATS\_MEMALLOC\_LIBC -o rand rand.dats  
//  
(\* \*\*\*\*\*\* \*\*\*\*\*\* \*)

fun  
make\_rand  
(  
// argumentless  
) : () - ulint = let  
val state = ref (22222UL)  
in  
//  
lam() : ulint =\> let  
val old = !state  
val new = (old \* 196314165UL) + 907633515UL  
val ((_void_)) = !state := new  
in  
new  
end // end of [lam]  
//  
end // end of [make\_rand]

(\* \*\*\*\*\*\* \*\*\*\*\*\* \*)

implement  
main0 () =  
{  
val rand = make\_rand()  
//  
val () = println! ("rand() = ", rand())  
val () = println! ("rand() = ", rand())  
//  
} (\* end of [main0] \*)

(\* \*\*\*\*\*\* \*\*\*\*\*\* \*)On Thursday, June 19, 2014 4:01:08 PM UTC-4, Justin Smith wrote:

> I have the following function, which should return a function that gives  
> successive pseudo-random numbers, returning a new one on each call (adapted  
> from some example code from the pulseaudio project for making a pink noise  
> generator):
> 
> fun make\_rand (): ulint = let  
> var randState = 22222UL  
> in  
> lam (): ulint =\> let  
> val old = randState  
> val new = (old \* 196314165) + 907633515  
> in  
> randState := new;  
> new  
> end  
> end
> 
> There is some unneeded verbosity here that I added in order to work out my  
> understanding of the steps being done as I try to address the errors from  
> the compiler.
> 
> The ats2 compiler says “error(3): dereference cannot be performed: the  
> proof search for view located at [S2Evar(randState(3800))] failed to turn  
> up a result.”.
> 
> What do I need to satisfy the compiler that this local var will be  
> accessible when called at runtime, and more importantly, how would I  
> anticipate and diagnose this sort of issue when writing code in the future?

---

<div class="post-metadata">

**Author:** ![Yannick\_Duchene](https://avatars.discourse-cdn.com/v4/letter/y/b77776/32.png) [@Yannick\_Duchene](https://discourse.ats-lang.org/u/Yannick_Duchene)\
**Post date:** [May 18, 2015, 2:57pm UTC](https://discourse.ats-lang.org/t/defining-a-function-with-internal-mutable-state/810/4 "2015-05-18T14:57:09Z")

</div>

> [var] introduces a stack-allocated variable. If it was  
> put into a closure, then dangling pointer would be created.
> 
> Here is a running implementation:
> 
> //  
> #include  
> “share/atspre\_staload.hats”  
> //  
> // How to compile:  
> // patscc -DATS\_MEMALLOC\_LIBC -o rand rand.dats  
> //  
> (\* \*\*\*\*\*\* \*\*\*\*\*\* \*)
> 
> fun  
> make\_rand  
> (  
> // argumentless  
> ) : () - ulint = let  
> val state = ref (22222UL)  
> in  
> //  
> lam() : ulint =\> let  
> val old = !state  
> val new = (old \* 196314165UL) + 907633515UL  
> val ((_void_)) = !state := new  
> in  
> new  
> end // end of [lam]  
> //  
> end // end of [make\_rand]
> 
> (\* \*\*\*\*\*\* \*\*\*\*\*\* \*)
> 
> implement  
> main0 () =  
> {  
> val rand = make\_rand()  
> //  
> val () = println! ("rand() = ", rand())  
> val () = println! ("rand() = ", rand())  
> //  
> } (\* end of [main0] \*)
> 
> (\* \*\*\*\*\*\* \*\*\*\*\*\* \*)

I’ve never used ATS closure, and I try to understand this: `make_rand`  
seems to return a parameterless lambda expression, but `rand` is invoked as  
`rand()` instead of `rand()()`. Is the lambda expression transparent,  
`make_rand` being substituted for the lambda expression it returns,  
transparently? Is that it?

Also `val state = ref<ulint> (22222UL)` automatically allocates  
heap‑storage transparently? Can this be tweaked for target other than C?  
I’m thinking about the JavaScript target, which have no explicit allocation  
and deallocation…

---

<div class="post-metadata">

**Author:** ![Justin\_Smith](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@Justin\_Smith](https://discourse.ats-lang.org/u/Justin_Smith)\
**Post date:** [June 19, 2014, 9:53pm UTC](https://discourse.ats-lang.org/t/defining-a-function-with-internal-mutable-state/810/5 "2014-06-19T21:53:00Z")

</div>

Thanks for the assistance. I have been reading the ats programming book  
from the official website, and now that I know that “ref” was the keyword I  
was looking for I was able to find the following documentation:  
[http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x1499.html](http://www.ats-lang.org/DOCUMENT/INT2PROGINATS/HTML/x1499.html)

Thanks for this very interesting language, I’ll likely be back with more  
questions.

By the way, in trying to find the solution to this particular task, I  
discovered that many of the citation links from the ats wikipedia page,  
pointing to [ats-lang.org](http://ats-lang.org), are  
broken. [http://en.wikipedia.org/wiki/ATS\_(programming\_language)On](http://en.wikipedia.org/wiki/ATS_(programming_language)On) Thursday, June 19, 2014 2:43:51 PM UTC-7, gmhwxi wrote:

> > > how would I anticipate and diagnose this sort of issue when writing code  
> > > in the future?
> 
> You are welcome to raise your questions here.
> 
> On Thursday, June 19, 2014 4:01:08 PM UTC-4, Justin Smith wrote:
> 
> > I have the following function, which should return a function that gives  
> > successive pseudo-random numbers, returning a new one on each call (adapted  
> > from some example code from the pulseaudio project for making a pink noise  
> > generator):
> > 
> > fun make\_rand (): ulint = let  
> > var randState = 22222UL  
> > in  
> > lam (): ulint =\> let  
> > val old = randState  
> > val new = (old \* 196314165) + 907633515  
> > in  
> > randState := new;  
> > new  
> > end  
> > end
> > 
> > There is some unneeded verbosity here that I added in order to work out  
> > my understanding of the steps being done as I try to address the errors  
> > from the compiler.
> > 
> > The ats2 compiler says “error(3): dereference cannot be performed: the  
> > proof search for view located at [S2Evar(randState(3800))] failed to turn  
> > up a result.”.
> > 
> > What do I need to satisfy the compiler that this local var will be  
> > accessible when called at runtime, and more importantly, how would I  
> > anticipate and diagnose this sort of issue when writing code in the future?

---

<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:** [June 19, 2014, 9:43pm UTC](https://discourse.ats-lang.org/t/defining-a-function-with-internal-mutable-state/810/6 "2014-06-19T21:43:51Z")

</div>

> > how would I anticipate and diagnose this sort of issue when writing code  
> > in the future?

You are welcome to raise your questions here.On Thursday, June 19, 2014 4:01:08 PM UTC-4, Justin Smith wrote:

> I have the following function, which should return a function that gives  
> successive pseudo-random numbers, returning a new one on each call (adapted  
> from some example code from the pulseaudio project for making a pink noise  
> generator):
> 
> fun make\_rand (): ulint = let  
> var randState = 22222UL  
> in  
> lam (): ulint =\> let  
> val old = randState  
> val new = (old \* 196314165) + 907633515  
> in  
> randState := new;  
> new  
> end  
> end
> 
> There is some unneeded verbosity here that I added in order to work out my  
> understanding of the steps being done as I try to address the errors from  
> the compiler.
> 
> The ats2 compiler says “error(3): dereference cannot be performed: the  
> proof search for view located at [S2Evar(randState(3800))] failed to turn  
> up a result.”.
> 
> What do I need to satisfy the compiler that this local var will be  
> accessible when called at runtime, and more importantly, how would I  
> anticipate and diagnose this sort of issue when writing code in the future?
