terminal-auspicious

Topics

programming

php drupal scheme scheming macros design patterns da la

design

design css

random thoughts

scribbles

alter ego

other me 'em that link us my space me linked in

Collections

Programmable web
PHP design patterns

guild

Partial evaluation and generics in php

Submitted by vlado on Mon, 2006-07-03 13:11.php | programming | scheming | very shorts

This is a followup of a few of my previous posts (lisp-like programming, aspects and honey, patterns & languages, closures), which describe some programming idioms typical for lisp-like languages.

simple partial evaluation

Here I will add some very short examples for how to do partial evaluation in php. The most straight forward approach would be something like:
function peval() {
$symbol = gen_symbol( $args = function_get_args() );
$f_ind = count($symbol);
return create_function('',"
global \$symbol;
return call_user_function_array(array_pop(\$symbol[$f_ind]), \$symbol);
');";
}

gen_symbol() is a utility function which generates a unique global symbol. And before you start, yes, this technique does pollute the global scope with extra symbols. So what? As long as we don't notice it why should we care. Just imagine it's hidden somewhere by magic.

generic functions and algorithms

If we presume a definition of generic functions that they implement an algorithm, which later might be specialised for a type we might get something like:
function generic_function_1($func, $arg1, $arg2) {
.... do something ....
$func( $arg1, $arg2 );
.... do something ....
}

It is not something new, quite a few functions in php are using this idiom anyway. the type specialiastion is done via the $func variable which must hold a callable value. The only 'strict' requirements are for the arguments of the famililyt of $func - like functions. Using this techniques, we could for example, implement algebraic datatypes in php. For an example usage have a look at aspects and honey

read more | vlado's blog | add new comment

Reply

Please solve the math problem above and type in the result. e.g. for 1+1, type 2
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <br /> <br> <div> <a> <em> <strong> <cite> <pre> <code> <ul> <ol> <li> <dl> <dt> <dd> <h3> <img> <blockquote> <q> <strike> <small> <h4> <h5> <h6>
  • Link to content with [[some text]], where "some text" is the title of existing content or the title of a new piece of content to create. You can also link text to a different title by using [[link to this title|show this text]]. Link to outside URLs with [[http://www.example.com|some text]], or even [[http://www.example.com]].
  • Lines and paragraphs break automatically.
More information about formatting options
Home ยป Partial evaluation and generics in php

dikini.net

spreading confusion by accident since 1970