Writing a macro to define methods

Discussion of Common Lisp

Writing a macro to define methods

Postby mgcheung » Tue Jul 03, 2012 6:57 am

Recently I asked about programmatically creating names for structs. I realized that I think I want to do something a little different. First, I'm not sure if the idea on how to do the task I have in mind is the right one. I was thinking of writing a macro that takes a name, creates two related structs and then uses the names of those structs as well as other parameters passed to the macro to then call functions which would generate the code to define methods that would specialize to those structs. So here is what pops in my head when trying to create such a macro:
Code: Select all
(defmacro my-macro (name param1 param2)
             (let ((struct1 (symb name 'const1))
                    (struct2 (symb name 'const2)))
                   (generate-method1 struct1 struct2 param1 param2)
                    ...
                   (generate-methodn struct1 struct2 param1 param2)))


Does this make sense? Part of the reason I want to do it this way is that I want to encode some information that could be in the structs, but would be constant among many instances, into the definitions of the methods as constants. I think the big problem I have is in understanding where backquotes and commas and everything need to be.

If what I'm saying doesn't make sense, I'll try to clarify.
mgcheung
 
Posts: 5
Joined: Tue Jun 26, 2012 6:21 am

Re: Writing a macro to define methods

Postby Ramarren » Tue Jul 03, 2012 9:53 am

It is technically doable. If you are confused about backquotes and commas then remember that they are purely syntax, you can always write it out using standard list manipulation functions, which is more verbose but sometimes more clear.

But, although that it is hard to tell without knowing in more detail what are you trying to build, you might have chosen a wrong level of abstraction. If there are structures similar enough that they and their methods can be generated programmatically, then perhaps there should only be one more general structure, or the data flow in the program should be constructed differently.
Ramarren
 
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland

Re: Writing a macro to define methods

Postby mgcheung » Tue Jul 03, 2012 12:05 pm

To elaborate on what I'm trying to do, I'm trying to write an elliptic curve library, but I wanted to design it so that it will compute operations as efficiently as possible. The structures are for representations of points and the computations will depend on information about the curve and the field. I didn't think it was a good idea to include this information in the structure and I wanted the determination of how to most efficiently do the computations to be done at compile time. So the idea is that somebody will specify the parameters of the curve, which will then determine how to implement some of the basic operations and in the methods, the data that is idependent of the point, but dependent on the curve and field will be fixed in the method definition.

Ultimately I want a lot of flexibility to do comparisons between curves. The hope is this will be at the very least a prototyping system for experimenting with elliptic curves over prime or binary fields. Perhaps some day I would expand it to work for curves over more general finite fields, but the cases mentioned would be the easiest.

Perhaps I should review some of my lisp books. It has been a while since I've done much lisp programming and I think I've forgotten quite a bit about how to write macros.
mgcheung
 
Posts: 5
Joined: Tue Jun 26, 2012 6:21 am

Re: Writing a macro to define methods

Postby Ramarren » Tue Jul 03, 2012 1:40 pm

Using macros seems to me like a premature optimization for this case. Although I know very little about specifics of elliptical curve algorithms, I doubt the cost of algorithm dispatch is that significant. You should probably write a version just using functions first.
Ramarren
 
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland


Return to Common Lisp

Who is online

Users browsing this forum: Google [Bot] and 3 guests