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?
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…
[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?
[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…
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
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, are
broken. 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?
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?