1
$\begingroup$

I am designing a DSL for writing numerical simulations. I am working on a specific issue related to returning values from functions, and I am wondering if any other existing language solves the same problem.

A toy example of a basic "simulation kernel" in my DSL looks something like this:

function MySimulationKernel()
    res1 = foo1()
    res2 = foo2()
    res3 = foo3()
    res4 = foo4()
end

Once the simulation kernel has been implemented, I would like to run it and collect the simulation results. The problem is that which result(s) need to be collected is not known by the function.

For example, one user of MySimulationKernel might only need the value of res1, however another user might need res1 and res4. In this example, we have 4 different such values, so there are 16 possible combinations of output values:

$16 = {4 \choose 0} + {4 \choose 1} + {4 \choose 2} + {4 \choose 3} + {4 \choose 4}$

In practice, we have hundreds of these possible output values from a simulation kernel. It does not make sense for the simulation kernel to list out its possible result values, it is safe to assume that any local variable of the function is a potential result that the simulation user needs to collect from a function call.

Essentially, it seems that what to return is a separate concern from what to compute. The simulation kernel is concerned with what to compute, and it would be preferable if that code was not cluttered at all with any logic about what to return since that is a separate concern. The function callee must somehow specify what to return, and that is properly a concern of the function callee.

Note that the type and size of these different result values can vary drastically. In some cases, a result is just a single floating-point value (and would be highly efficient to return from the function). But in other cases, the result value could be a multi-gigabyte array and outputting that array from the simulation has a very high computational cost, so we want to avoid returning values unless they are requested at the function call.

What I am looking for are example of how other languages might solve this problem. It seems like such a simple problem that others must have already encountered and solved it (I hope). Any pointers in the right direction would be helpful.

The closest I solutions I found are the Policy-Based Design / Strategy Pattern [1], and Aspect-Oriented programming [2]. These solutions are definitely overkill for the problem I am trying to solve. They also require a heavy-duty object-oriented language, which doesn't make a lot sense in this DSL.

[1] https://en.wikipedia.org/wiki/Policy-based_design

[2] https://en.wikipedia.org/wiki/Aspect-oriented_programming

$\endgroup$
  • 2
    $\begingroup$ You may want a lazy semantics. In a strict language, you could return a tuple of closures () => (compute the actual value here) and let the caller decide which ones to evaluate. Lazy languages (e.g. Haskell) would just return a tuple. $\endgroup$ – chi Jan 16 '17 at 18:01
  • $\begingroup$ @chi The interesting aspect of this, for me, is where you say "let the caller decide which ones to evaluate" ... How does that caller syntax look in Haskell? (sorry, I'm not fluent in Haskell) $\endgroup$ – n00b101 Jan 16 '17 at 18:12
  • $\begingroup$ In Haskell when you call e.g.f w = let x=10+w ; y=2*w ; z=w*w in (x,y,z) only a tuple is created, with unevaluated thunks inside. If the caller uses only two variables e.g. case f 12 of (x,_,z) -> x+z then y=2*w is not computed at all. Think of it as a "on demand" evaluation, which is similar to returning a closures tuple. There is no special syntax for this, it's the default in Haskell. You should look up lazy vs eager evaluation. The major downside is that lazy evaluation and side effects (e.g. network IO) do not mix very well, but it's great for pure computation. $\endgroup$ – chi Jan 16 '17 at 19:27
  • 1
    $\begingroup$ May I recommend, with all due respect, that before you design a language, you look at how languages are designed and maybe learn a couple of basic principles of programming languages? Many resources are available, such as cs.cmu.edu/~rwh/courses/ppl $\endgroup$ – Andrej Bauer Jan 16 '17 at 22:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.