Scrap Dump

Jul. 31st, 2016 06:56 pm
[personal profile] tangaroa

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.

From:
Anonymous( )Anonymous This account has disabled anonymous posting.
OpenID( )OpenID You can comment on this post while signed in with an account from many other sites, once you have confirmed your email address. Sign in using OpenID.
User
Account name:
Password:
If you don't have an account you can create one now.
Subject:
HTML doesn't work in the subject.

Message:

 
Notice: This account is set to log the IP addresses of everyone who comments.
Links will be displayed as unclickable URLs to help prevent spam.
Page generated Jun. 22nd, 2017 12:02 pm
Powered by Dreamwidth Studios