views:

240

answers:

1

In the chapter about function in the Oz tutorial, it says that:

similar to lazy functional languages Oz allows certain forms of tail-recursion optimizations that are not found in certain strict functional languages including Standard ML, Scheme, and the concurrent functional language Erlang. However, standard function definitions in Oz are not lazy.

It then goes on to show the following function which is tail-recursive in Oz:

fun {Map Xs F}
   case Xs
   of nil then nil
   [] X|Xr then {F X}|{Map Xr F}
   end 
end 

What this does is, it maps the empty list to the empty list and non-empty list, to the result of applying the function F to its head and then prepending that to the result of calling Map on the tail. In other languages this would not be tail recursive, because the last operation is the prepend, not the recursive call to Map.

So my question is: If "standard function definitions in Oz are not lazy", what does Oz do that languages like Scheme or Erlang can't (or won't?) to be able to perform tail-recursion optimization for this function? And exactly when is a function tail-recursive in Oz?

+2  A: 

I am not too familiar with lazy functional languages, but if you think about the function Map in your question, it is easy to translate to a tail-recursive implementation if temporarily incomplete values in the heap are allowed (muted into more complete values one call at a time).

I have to assume that they are talking about this transformation in Oz. Lispers used to do this optimization by hand -- all values were mutable, in this case a function called setcdr would be used -- but you had to know what you were doing. Computers did not always have gigabytes of memory. It was justified to do this by hand, it arguably no longer is.

Back to your question, others modern languages do not do it automatically probably because it would be possible to observe the incomplete value while it is being built, and this must be what Oz has found a solution to. What other differences are there in Oz as compared to other languages that would explain it?

Pascal Cuoq
I don't actually know too much about oz. As far as I can tell, it's not too different from lisp. I just played around a bit with it in the last couple of days and stumbled upon the fact that it optimized recursions, that I didn't expect it to.
sepp2k
I only know Oz from reading Peter Van Roy's _Concepts, Techniques, and Models of Computer Programming_, but it does in fact has incomplete values--they are used extensively in concurrent programming, because reading an incomplete value cause the current thread to block. So Pascal's guess is probably how it works.(Here is the book link: http://www.info.ucl.ac.be/~pvr/book.html He used to have a draft version online, but has removed it. :( )
Nathan Sanders
As Oz is also a logic language then it might have the concept of a logical variable in which case it would be tail-recursive in the same way as for other logic languages like prolog.
rvirding