Jul. 31st, 2016

Scrap Dump

Jul. 31st, 2016 06:56 pm

pattern for resolution of an abstraction to a meaningful value

A = an abstract something

function f(A) {
  if(A == "FOO")
    return FOO
  if (A == "BAR")
    return BAR 
  if (A matches /[A-Za-z0-9]+/)
    return TOKEN_ALNUM
  if (A has method "next")
    return I_SEQUENCE
  if ( not (A contains meat) )
    return VEGETARIAN

  return DEFAULT 
}

This can be generalized to a function taking input:

   f(A, DEFAULT, array of [f_comparator -> f_resolver])

where the comparator function runs a comparison or pattern match on A, and the resolver function takes A and produces an output.

Possible error conditions:

  • All comparators fail. Solution: force caller to provide a DEFAULT value, have the caller handle it.
  • Multiple comparators match. Possible solutions:
    1. Use the first matching comparator.
    2. Give different values to comparators. Use the comparator with the highest matching score.
    3. (combination of 1 and 2) Sort the comparator->resolver pairs by comparator value before passing them in.
    4. Throw an exception (nooooooo!)
    5. [Edit Aug 2] Perform ALL of the matching operations. Assume the function is a decorator.
    6. [Edit Aug 2] Perform all of the matching operations and return a set of results. Assume the function returns results, plural, rather than one result.

Possible optimizations:

  • Inline the function calls.

These are not particularly new ideas. All of this has to be well tread ground.


[Edit Aug 2] Thoughts on "inline the function calls"

The comparator functions may be very simple and easily optimizable in theory. My preference is to write code to pass them in as a set and then let the compiler somehow produce code that has the simple comparison functions inlined as in the original example. This might be possible in theory if the functions are defined at compile time. The optimizer would need to be smart enough to unroll the loop and examine its contents and understand them.

If the functions are defined at run time, the optimizer would need to be part of the run time environment. It seems that what I want is the ability to arbitrarily optimize away function calls at runtime.

Page generated Oct. 19th, 2017 07:05 am
Powered by Dreamwidth Studios