A demo of memorizing functions in ATS

44 views
Skip to first unread message

Steinway Wu

unread,
Jul 10, 2016, 9:15:10 PM7/10/16
to ats-lang-users
Hi, 

In a practice, I encountered a problem of memorizing functions. Given a function `f`, a memorized version of `f`, called `memo f` should cache the results of `f` applying on some `input`. I tried a demo here https://glot.io/snippets/egflakau5h, which shows a generic implementation of the function `memo0` (for memorizing a function of no arguments) and `memo1` (for memorizing a function with one argument). Comments are welcomed.

gmhwxi

unread,
Jul 10, 2016, 9:38:49 PM7/10/16
to ats-lang-users
Please change

memo1

to

memo1<int, int>

Also, please delete the line "staload .../gorder_int.dats"

Steinway Wu

unread,
Jul 10, 2016, 11:25:30 PM7/10/16
to ats-lang-users
Thanks, I forgot to save it after debug. Now it works. 

Artyom Shalkhakov

unread,
Jul 13, 2016, 9:29:42 AM7/13/16
to ats-lang-users
Hello Steinway,


On Monday, July 11, 2016 at 7:15:10 AM UTC+6, Steinway Wu wrote:
Hi, 

In a practice, I encountered a problem of memorizing functions. Given a function `f`, a memorized version of `f`, called `memo f` should cache the results of `f` applying on some `input`. I tried a demo here https://glot.io/snippets/egflakau5h, which shows a generic implementation of the function `memo0` (for memorizing a function of no arguments) and `memo1` (for memorizing a function with one argument). Comments are welcomed.

Thanks for the example! Reminds of hash-consing technique (say you memoize unary constructors for a datatype).

I've changed the code a tiny bit to make the [memo1] function return type fully generic. Here's the code:

https://glot.io/snippets/egid4pae37

gmhwxi

unread,
Jul 13, 2016, 10:40:48 PM7/13/16
to ats-lang-users

I would say that a hash table (instead of a binary search tree) is more
suitable for this form of memoization.

gmhwxi

unread,
Jul 14, 2016, 11:02:16 AM7/14/16
to ats-lang-users

Memoization is a very interesting topic. Here is an example involving
the handling of a recursively defined function:

https://github.com/githwxi/ATS-Postiats/blob/master/doc/EXAMPLE/MISC/memoization.dats

I will try to write about it once I get some free time.


On Wednesday, July 13, 2016 at 9:29:42 AM UTC-4, Artyom Shalkhakov wrote:
Reply all
Reply to author
Forward
0 new messages