Newsgroups: comp.lang.misc Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!ispd-newsserver!kodak!islsun!cok From: cok@islsun.Kodak.COM (David Cok) Subject: type checking problem Message-ID: <1991May16.182932.26327@kodak.kodak.com> Sender: news@kodak.kodak.com Organization: Eastman Kodak Co., Rochester, NY Date: Thu, 16 May 91 18:29:32 GMT Here is a real-life problem which advocates of various languages/typing systems are invited to comment on. I want to write a routine to optimize a function (numerically) over one of its arguments, returning the abscissa of the extremum. I'll settle for optimization over the first argument. I'll even restrict myself to that argument and the return value being doubles. So I want a function optimize() which returns a double and takes as an argument a pointer to a function which returns a double and takes as arguments a double *and other arguments*. It would be used like this optimum_x = optimize(f,...) The problem is what to do with the other arguments to f. One solution is to allow creation of functions on the fly. Given, say, myf of four arguments, one would write optimum_x = optimize(myf(#1,a,b,c)) in which myf(#1,a,b,c) is a function of one argument obtained by supplying values for three of the four arguments of f. This can be done in interpreted environments like Mathematica. Can it be done in any compiled programming languages? Smalltalk with blocks perhaps? Another approach which is similar is to write a wrapper function which takes only one argument -- the one to be optimized -- but then the other arguments would have to be passed in as global parameters, which is the sort of design I want to avoid. Instead I want to declare something like this: double optimize(double (*f)(double,...),...) but where the types of the two ...'s match up. Note that all optimize does with this list of arguments is to call the function f on them, i.e. double optimize(double (*f)(double,...),...) { double x; while (not converged) { pick a new x; result = f(x,...); } return x; } Given some syntax to indicate that the two ...'s denote the same number and sequence of types, both the implementation of optimize and the call of optimize could be statically type-checked. However, no statically type- checked language that I know of allows this. Are there any? There are no dynamically created inhomogeneous lists which require checking of type tags or other such examples proposed by dynamic-typing advocates. The problem also requires flexibility in number of arguments which is not common in dynamically typed languages. Would anyone care to point me to an example? One potential partial statically-typed solution is C++ with function templates: double optimize(double (*f)()) {... f() ... } template double optimize(double (*f)(T1), T1 a1) { ... f(a1) ...} template double optimize(double (*f)(T1,T2), T1 a1, T2 a2) { ... f(a1,a2) ...} This gives flexibility in type but not in number of arguments (without writing a template for each possible number of arguments). However, it sure would be a shame to have to have separate instantiations of the code for each possible combination of arguments. Comments and solutions are welcome. David R. Cok Eastman Kodak Company -- Imaging Science Lab cok@Kodak.COM