dynamically defined functions? 
Author Message
 dynamically defined functions?

Thanks very much to all Dylanists, including ALMS and Lyman
Taylor, who've responded to my questions recently.  I'm still
thinking about what you've said, and part of that thinking
involves writing some of my homework in Dylan, which brings me
to today's question:

It's not possible to define methods dynamically in Dylan, is
it?

Example:  I'm writing a program in which multiple "players"
play a game.  In each round, each player submits a "move",
which is a "C enum", or whatever equivalent data type is
natural in Dylan.  (Anyone?)  The moves are used to key into
a multi-dimensional collection of methods, and then each player
has the resulting method invoked on her.

Now I'd like to define these methods dynamically, even though
currently they all just say "Score X points and then make a
record of this round of the contest", and the only difference
between them is the value of X.  Still I'd rather have them
defined dynamically rather than statically with a macro or just
by typing in each one separately.

Thanks!

Zooko

P.S.  If I don't hear back, I'll continue to believe you can't
dynamically define methods and I'll use macros or retyping.

P.P.S.  The game is currently the Iterated Prisoner's Dilemma.
I'm considering making my algorithm be some genetic programming
trick, but if dynamically defined functions aren't happening in
Dylan then I'll have to settle for genetic algorithm or even
something lesser.  :-)

P.P.P.S.  Feel free to show me the Dylan syntax for this
multi-dimensional collection of methods keyed by "enum"-like
things.  Don't worry-- this wouldn't be cheating on my
homework, as my homework is merely "investigate the behavior of
your favorite kinds of algorithms in the Iterated Prisoner's
Dilemma", and me writing a whole new IPD system in Dylan
instead of using the available C version is merely for my own
enjoyment.

P.P.P.P.S.  Actually, I was going to have N methods in each
element of the collection so that each player can receive a
different method invokation depending on which player she
was -- that is, which move she made...

P.P.P.P.P.S.  Okay, after writing this whole thing I've read
the DRM about methods and I see that I can generate the methods
I want dynamically (even, for example, accepting user input to
determine which scores go with which moves).  But unfortunately
I don't appear to be able to define methods in the "eval this
source" or otherwise perfectly flexible way.  Hm.  Except that
I _could_ make a bunch of pre-defined methods and mix-n-match
them... Yeah, that might do the trick...

P*6.S.  Don't forget to write to me and tell me how to make
this multidimensional table of methods...

:-)

--Zooko



Sun, 13 Aug 2000 03:00:00 GMT  
 dynamically defined functions?



Quote:
>It's not possible to define methods dynamically in Dylan, is
>it?

No, I don't think so.

Quote:
>Example:  I'm writing a program in which multiple "players"
>play a game.  In each round, each player submits a "move",
>which is a "C enum", or whatever equivalent data type is
>natural in Dylan.  (Anyone?)  The moves are used to key into
>a multi-dimensional collection of methods, and then each player
>has the resulting method invoked on her.

>Now I'd like to define these methods dynamically, even though
>currently they all just say "Score X points and then make a
>record of this round of the contest", and the only difference
>between them is the value of X.  Still I'd rather have them
>defined dynamically rather than statically with a macro or just
>by typing in each one separately.

You can do this with closures.  Here's the example of a closure from the
DRM:

define method make-score (points :: <number>)
  method (increase :: <number>)
    points := points + increase;
  end method;
end method make-score;
define constant score-david = make-score(100)
define constant score-diane = make-score(400)
score-david(0)
 ->   100
score-david(10)
 ->   110
score-david(10)
 ->   120
score-diane(10)
 ->   410
score-david(0)
 ->   120

--

GTE Internetworking, Powered by BBN, Cambridge, MA
Support the anti-spam movement; see <http://www.cauce.org/>
Please don't send technical questions directly to me, post them to newsgroups.



Sun, 13 Aug 2000 03:00:00 GMT  
 dynamically defined functions?



Quote:

....

>It's not possible to define methods dynamically in Dylan, is
>it?

    To define methods of upon "code" entered at runtime. No.
    To construct closures and/or make additions to generic
    functions, yes.

Quote:
>Example:  I'm writing a program in which multiple "players"
>play a game.  In each round, each player submits a "move",
>which is a "C enum", or whatever equivalent data type is
>natural in Dylan.  (Anyone?)

   Well Enums of sorts....

(courtesy of "Dylan Programming" Feinburg, et. al.  p.318 sect. 19.5 )

   define constant <latitude-direction>  
       = type-union ( singleton ( #"north" ) , singleton ( #"south" )) ;

only the symbols #"north" and  #"south" are members of the
defined type.  They have no implicit "order" though.
Unlike "C enums" these aren't symbolic stand-ins for implicitly
specified set of numbers. ( or perhaps not so implicitly specified
numbers).

You can index  a <table> with a symbol though. So

    define variable *my-move-table* = make (<table> , size: 4  );

    *my-move-table*[ #"up" ]    := "up" ;
    *my-move-table*[ #"down" ]  := "down" ;
    *my-move-table*[ #"left" ]  := "left" ;
    *my-move-table*[ #"right" ] := "right";

There is no reason why the right hand side of those latter expressions
cannot also be tables ( or anything else... ).

    *my-move-table*[ #"up" ]   :=  make ( <table> );

I didn't test this but I think...

    *my-move-table*[ #"up" ] [ 1 ]  :=  "foo"  ;

should work. which should be equiv. to

    element  (*my-move-table* , #"up") [ 1 ]  := "foo" ;

    element ( element ( *my-move-table* , #"up") , 1 ) := "foo" ;

    element-setter ( "foo" ,   element ( *my-move-table* , #"up"), 1 );

Or you could define some sort of  "enum to integer" mapper and
use arrays.

          aref   ( *my-move-array* , map-to-int ( #"up") , 1 )

While not as dynamic it seems like you could skip having the
multidimensional collection by just using mutlimethods.  For instance
instead of the expression:

                *my-move-table* [ #"up" ] [ 1 ]  

you could have something like

       define method  do-move  ( direction == #"up" , player-num == 1 )

        "foo";
       end method

I don't have my DRM handy so I certain if the below works...
[ precisely do the singleton parameters have to be literals??? ]

 define method generate-my-method (  new-direction :: <symbol> ,
                                     new-player-num :: <integer> )

    method ( direction == new-direction , player-num == new-player-num )
       "foo"
    end method

 end method generate-my-method ;

and then later say

    add-method (  do-move  , generate-my-method ( #"up" , 1 ) ) ;

 to dynamically add the "equiv." method that the "define method do-move..."
 above does.  This way you can change it dynamically also.  However,
 the generic DO-MOVE would have to be unsealed.   So is unsealed
 two arg singleton dispatching faster/slower than two table lookups? :-)

Quote:
>P.P.P.S.  Feel free to show me the Dylan syntax for this
>multi-dimensional collection of methods keyed by "enum"-like
>things.

  A  <table> can be indexed by "any old object". :-)

Quote:
>determine which scores go with which moves).  But unfortunately
>I don't appear to be able to define methods in the "eval this
>source" or otherwise perfectly flexible way.

    "eval" has been banished to the nether regions in Dylan.
    Saddling resulting applications with a built-in optimizing compiler
    for a general purpose programming language isn't very conducive
    for producing "smaller" runtime footprints. :-)

--

Lyman S. Taylor                 "Because no matter where you go,

                                                Buckaroo Banzai



Sun, 13 Aug 2000 03:00:00 GMT  
 dynamically defined functions?

[Untested sketch]

// Enumerations, Cf. Dylan Programming, p.318

define constant <move> = one-of(#"up", #"down", #"left", #"right");
define constant <position> = limited(<integer>, min: 0, max: 63);

// Dynamically created methods, Cf. Dylan Reference, p. 83, 352

define method make-score
    (position :: <position>, move :: <move>, points :: <number>)
  method (thepos :: singleton(position), themove :: singleton(move))
    points;
  end;
end;

//  Multi-dimensional collection of methods

define open generic score (position :: <position>, move :: <move>) => <number>;

define function build-game()
  add-method score make-scorer(0, #"up", 3);
  add-method score make-scorer(0, #"right", 42);
  add-method score make-scorer(1, #"up", 12);
  add-method score make-scorer(1, #"left", -42);
  add-method score make-scorer(1, #"right", 19);
  ...
  add-method score make-scorer(63, #"left", -6);
  add-method score make-scorer(63, #"down", -12);
end;

...
  // Calculate score for next move
  my-score := my-score + score(my-position, my-move);

...

---
P. Tucker Withington, Harlequin Inc., One Cambridge Center, Cambridge MA 02142
Phone: +1 617 374 2557       Fax: +1 617 252 6505      "Honk if you love GC's"
Dylan Programming:  <URL:http://www.harlequin.com/books/dylan-book/index.html>



Sun, 13 Aug 2000 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. memory leak with dynamically defined functions?

2. memory leak with dynamically defined functions?

3. Dynamically Defined Functions in Classes?

4. Dynamically defining columns in a dataset widget in VW2.0

5. problem defining a function using built in filter function

6. functions defined inside a function

7. define function in function

8. Recursive function defined within function => NameError

9. Dynamically adding a REXX function module

10. EVALUATE functions dynamically

11. benchmark cut for evaluation of dynamically created function

12. benchmark cut for evaluation of dynamically created function

 

 
Powered by phpBB® Forum Software