views:

595

answers:

10

I was wondering... why memoization is not provided natively as a language feature by any language I know about?

Edit: to clarify, what I mean is that the language provides a keyword to specify a given function as memoizable, not that every function is automatically memoized "by default", unless specified otherwise. For example, fortran provides the keyword PURE to specify a specific function as such. I guess that the compiler can take advantage of this information to memoize the call, but I ignore what happens if you declare PURE a function with side effects.

+1  A: 

Not all the languages natively support function decorators. I guess it would be a more general approach to support rather than supporting just memoization.

Li0liQ
+6  A: 

Because you shouldn't implement something as a language feature when it can easily be implemented in the language itself. A memoization feature belongs in a library, which is exactly where most languages put it.

Confusion
you can implement object oriented programming in C through virtual tables etc, but that did not prevent the creation of C++...
Stefano Borini
The word 'easily' in my answer is not there by accident.If you intend to copy the actual features of C++, and not just create shallow mimics, then you can not simply extend C with these C++ capabilities 'through virtual tables'. You would effectively be rewriting half of a C++ compiler. In contrast, a general memoization function is a few lines in any decent language. Adding a keyword for that is a bad idea. I'll leave it to the Lisp community to explain why.
Confusion
One of the key concepts of C++ (even more than OO, I'd say, since it's a special brand of "OO" that doesn't meet Kay's definition) is "only pay for what you use", which is difficult-to-impossible to do on top of C, at least with any reasonable syntax (since its macro language is so weak). In contrast, memoization is easy to do in almost any language, and adds minimal syntactic overhead.
Ken
Do "most languages" really have this in libraries? Offhand I can't think of a language that has a standard library facility for memoization.
Jason Orendorff
+4  A: 

A) Memoization trades space for time. I imagine that this can turn out to a fairly unbound property, in the sense, that the amount of data programs or libraries would have to store could consume large parts of memory really quick.

For a couple of languages, memoization is easy to implement and easy to customize for the given requirements.

As an example take some natural language processing on large bodies of text, where you don't want to compute basic properties of texts (word count, frequency, cooccurrences, ...) over and over again. In that case a memoization in combination with object serialization can be useful as opposed to memory caching, since you may run your application multiple times on unchanged corpora.

B) Another aspect: It's not true, that all functions or methods yield the same output for a same given input. Anyway some keyword or syntax for memoization would be necessary, along with configuration (memory limits, invalidation policy, ...) ...

The MYYN
+1  A: 

Hi

Your question also leaves open the solution of your learning more languages. I think that Lisp supports memoization, and I know that Mathematica does.

Regards

Mark

High Performance Mark
I know six or seven, but I don't like pure functional ones (hence no lisp) nor something you have to pay big bucks to get access to (and in any case I would not need it) ;)
Stefano Borini
+11  A: 

Because compilers have to emit semantically correct programs. You can't memoize a function without changing program semantics unless it is referentially transparent. In most programming languages not all functions are referentially transparent (pure functional programming languages are an exception) so you can't memoize everything. But then a mechanism is needed for detecting referential transparency and that is too hard.

Jason
@Downvoter: Give a reason.
Jason
sometimes people downvote for no reason at all, I guess it's just for fun, or they are bots.
Stefano Borini
You have to have some rep to be able to downvote - unlikely to be a bot.
JBRWilkinson
+1  A: 

Reverse the question. Why it should? As someone has said, it can be put in a library so no need of add syntax to the language, it's only usable on pure functions which are hard to identify automatically(unless you force the programmer to annotate them). It's also very hard to determine if memoization is going to speed up things or not. I don't think it's a desirable feature for a programming language.

Samuel
+5  A: 

Clojure has a memoize function (http://richhickey.github.com/clojure/clojure.core-api.html#clojure.core/memoize):

memoize
function

Usage: (memoize f)

Returns a memoized version of a referentially transparent function. The
memoized version of the function keeps a cache of the mapping from arguments
to results and, when calls with the same arguments are repeated often, has
higher performance at the expense of higher memory use.
peter.murray.rust
Also note that it's fewer than 10 lines of code.
Ken
I'm not sure this counts. He specifically said "language feature" not "implemented in a library". Although in any Lisp, the difference between language features and library implementations is slight, so +1 anyway.
A. Levy
@A. Levy: Also, it's from `clojure.core`, which basically means it's a builtin language feature, although not as fundamental as one of the 10 or so "special forms". As you say, the rules are different in Lisp :)
intuited
+3  A: 

In Haskell, memoization is automatic for (pure) functions you've defined that take no arguments. And the Fibonacci example in that Wiki is really about the simplest demonstrable example I would be able to think of either.

Haskell can do this because your pure functions are defined to produce the same results every time; of course, monadic functions that depend on side effects won't be memoized.

I'm not sure what the upper limits are -- obviously, it won't memoize more than the available memory. And I'm also not sure offhand if the memoization occurs at compile-time (if the values can be determined at compile-time), or if it always occurs the first time the function is called.

Mark Rushakoff
You'd certainly hope so...
Matt Joiner
+22  A: 

What YOU want from memoization may not be the same as what the compiler memoization option would provide.

You may know that it is only profitable to memoize the last 10 or so distinct values computed, because you know how the function will be used.

You may know that it only makes sense to memoize the last 2 or 3 values, because you will never use values older than that. (Fibonacci's Sequence comes to mind.)

You may be generating a LOT of values on some runs, and just a few on others.

You may want to "throw away" some of the memoized values and start over. (I memoized a random number generator this way, so I could replay the sequence of random numbers that built a certain structure, while some other parameters of the structure had been changed.)

Memoization as an optimization depends on the search for the memoized value being a lot cheaper than recomputation of the value. This in turn depends on the ordering of the input requests. This has implications for the memoization database: Does it use a stack, an array of all possible input values (which may be very large), a bucket hash, or a b-tree?

The memoizing compiler has to either provide a "one size fits all" memoization, or it has to provide lots of possible alternatives, and parameters to control the alternatives. At some point, it becomes easier for everyone to require the user to provide his own memoization.

John R. Strohm
I like your answer. You made a very good point.
Stefano Borini
+1. Very underrated answer.
Jason Orendorff
A: 

I really think such an option should be.

In data processing tasks there is an immutable input data (as time series, for example, where for a given time as soon as a value is known, it can never change). Taking in mind today RAM affordability, if a function result only depends on such immutable data, it is rational to memoize it rather than reread every time it's needed. Currently I have (in Scala and C#) to manually introduce an in-memory storage table and write 3 functions instead of one - one reading a value from file/db/ws, one storing it into an in-memory table, one to wrap them and read from memory if available or call the raw function if not. I think this could and should be implemented as a keyword and done behind the scenes.

Ivan