1
0
Fork 0
cl-sites/HyperSpec-7-0/HyperSpec/Body/m_define.htm
2024-04-01 10:24:07 +02:00

163 lines
11 KiB
HTML

<!-- Common Lisp HyperSpec (TM), version 7.0 generated by Kent M. Pitman on Mon, 11-Apr-2005 2:31am EDT -->
<HTML>
<HEAD>
<TITLE>CLHS: Macro DEFINE-COMPILER-MACRO</TITLE>
<LINK HREF="../Data/clhs.css" REL="stylesheet" TYPE="text/css" />
<META HTTP-EQUIV="Author" CONTENT="Kent M. Pitman">
<META HTTP-EQUIV="Organization" CONTENT="LispWorks Ltd.">
<LINK REL=TOP HREF="../Front/index.htm">
<LINK REL=COPYRIGHT HREF="../Front/Help.htm#Legal">
<LINK REL=DISCLAIMER HREF="../Front/Help.htm#Disclaimer">
<LINK REL=PREV HREF="f_cmp_ma.htm">
<LINK REL=UP HREF="c_evalua.htm">
<LINK REL=NEXT HREF="m_defmac.htm">
</HEAD>
<BODY>
<H1><A REV=MADE HREF="http://www.lispworks.com/"><IMG WIDTH=80 HEIGHT=65 ALT="[LISPWORKS]" SRC="../Graphics/LWSmall.gif" ALIGN=Bottom></A><A REL=TOP HREF="../Front/index.htm"><IMG WIDTH=237 HEIGHT=65 ALT="[Common Lisp HyperSpec (TM)]" SRC="../Graphics/CLHS_Sm.gif" ALIGN=Bottom></A> <A REL=PREV HREF="f_cmp_ma.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="c_evalua.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="m_defmac.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
<HR>
<A NAME="define-compiler-macro"><I>Macro</I> <B>DEFINE-COMPILER-MACRO</B></A> <P>
<P>
<P><B>Syntax:</B><P>
<P>
<B>define-compiler-macro</B> <I>name lambda-list [[<I>declaration</I><B>*</B> | <I>documentation</I>]] <I>form</I><B>*</B></I><P> =&gt; <I>name</I><P>
<P>
<P><B>Arguments and Values:</B><P>
<P>
<I>name</I>---a <A REL=DEFINITION HREF="26_glo_f.htm#function_name"><I>function name</I></A>. <P>
<I>lambda-list</I>---a <A REL=DEFINITION HREF="26_glo_m.htm#macro_lambda_list"><I>macro lambda list</I></A>. <P>
<I>declaration</I>---a <A REL=DEFINITION HREF="s_declar.htm#declare"><B>declare</B></A> <A REL=DEFINITION HREF="26_glo_e.htm#expression"><I>expression</I></A>; not evaluated. <P>
<I>documentation</I>---a <A REL=DEFINITION HREF="26_glo_s.htm#string"><I>string</I></A>; not evaluated. <P>
<I>form</I>---a <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A>. <P>
<P><B>Description:</B><P>
<P>
<P>
This is the normal mechanism for defining a <A REL=DEFINITION HREF="26_glo_c.htm#compiler_macro_function"><I>compiler macro function</I></A>. Its manner of definition is the same as for <A REL=DEFINITION HREF="m_defmac.htm#defmacro"><B>defmacro</B></A>; the only differences are: <P>
<P><DL><DT>* The <I>name</I> can be a <A REL=DEFINITION HREF="26_glo_f.htm#function_name"><I>function name</I></A> naming any <A REL=DEFINITION HREF="26_glo_f.htm#function"><I>function</I></A> or <A REL=DEFINITION HREF="26_glo_m.htm#macro"><I>macro</I></A>. <P><DD>
<DT>* The expander function is installed as a <A REL=DEFINITION HREF="26_glo_c.htm#compiler_macro_function"><I>compiler macro function</I></A> for the <I>name</I>, rather than as a <A REL=DEFINITION HREF="26_glo_m.htm#macro_function"><I>macro function</I></A>. <P><DD>
<DT>* The <TT>&amp;whole</TT> argument is bound to the form argument that is passed to the <A REL=DEFINITION HREF="26_glo_c.htm#compiler_macro_function"><I>compiler macro function</I></A>. The remaining lambda-list parameters are specified as if this form contained the function name in the <A REL=DEFINITION HREF="26_glo_c.htm#car"><I>car</I></A> and the actual arguments in the <A REL=DEFINITION HREF="26_glo_c.htm#cdr"><I>cdr</I></A>, but if the <A REL=DEFINITION HREF="26_glo_c.htm#car"><I>car</I></A> of the actual form is the symbol <A REL=DEFINITION HREF="f_funcal.htm#funcall"><B>funcall</B></A>, then the destructuring of the arguments is actually performed using its <A REL=DEFINITION HREF="26_glo_c.htm#cddr"><I>cddr</I></A> instead. <P><DD>
<DT>* <I>Documentation</I> is attached as a <A REL=DEFINITION HREF="26_glo_d.htm#documentation_string"><I>documentation string</I></A> to <I>name</I> (as kind <B>compiler-macro</B>) and to the <A REL=DEFINITION HREF="26_glo_c.htm#compiler_macro_function"><I>compiler macro function</I></A>. <P><DD>
<DT>* Unlike an ordinary <A REL=DEFINITION HREF="26_glo_m.htm#macro"><I>macro</I></A>, a <A REL=DEFINITION HREF="26_glo_c.htm#compiler_macro"><I>compiler macro</I></A> can decline to provide an expansion merely by returning a form that is the <A REL=DEFINITION HREF="26_glo_s.htm#same"><I>same</I></A> as the original (which can be obtained by using <TT>&amp;whole</TT>). <P><DD></DL><P>
<P><B>Examples:</B><P>
<P>
<PRE>
(defun square (x) (expt x 2)) =&gt; SQUARE
(define-compiler-macro square (&amp;whole form arg)
(if (atom arg)
`(expt ,arg 2)
(case (car arg)
(square (if (= (length arg) 2)
`(expt ,(nth 1 arg) 4)
form))
(expt (if (= (length arg) 3)
(if (numberp (nth 2 arg))
`(expt ,(nth 1 arg) ,(* 2 (nth 2 arg)))
`(expt ,(nth 1 arg) (* 2 ,(nth 2 arg))))
form))
(otherwise `(expt ,arg 2))))) =&gt; SQUARE
(square (square 3)) =&gt; 81
(macroexpand '(square x)) =&gt; (SQUARE X), <A REL=DEFINITION HREF="26_glo_f.htm#false">false</A>
(funcall (compiler-macro-function 'square) '(square x) nil)
=&gt; (EXPT X 2)
(funcall (compiler-macro-function 'square) '(square (square x)) nil)
=&gt; (EXPT X 4)
(funcall (compiler-macro-function 'square) '(funcall #'square x) nil)
=&gt; (EXPT X 2)
(defun distance-positional (x1 y1 x2 y2)
(sqrt (+ (expt (- x2 x1) 2) (expt (- y2 y1) 2))))
=&gt; DISTANCE-POSITIONAL
(defun distance (&amp;key (x1 0) (y1 0) (x2 x1) (y2 y1))
(distance-positional x1 y1 x2 y2))
=&gt; DISTANCE
(define-compiler-macro distance (&amp;whole form
&amp;rest key-value-pairs
&amp;key (x1 0 x1-p)
(y1 0 y1-p)
(x2 x1 x2-p)
(y2 y1 y2-p)
&amp;allow-other-keys
&amp;environment env)
(flet ((key (n) (nth (* n 2) key-value-pairs))
(arg (n) (nth (1+ (* n 2)) key-value-pairs))
(simplep (x)
(let ((expanded-x (macroexpand x env)))
(or (constantp expanded-x env)
(symbolp expanded-x)))))
(let ((n (/ (length key-value-pairs) 2)))
(multiple-value-bind (x1s y1s x2s y2s others)
(loop for (key) on key-value-pairs by #'cddr
count (eq key ':x1) into x1s
count (eq key ':y1) into y1s
count (eq key ':x2) into x2s
count (eq key ':y1) into y2s
count (not (member key '(:x1 :x2 :y1 :y2)))
into others
finally (return (values x1s y1s x2s y2s others)))
(cond ((and (= n 4)
(eq (key 0) :x1)
(eq (key 1) :y1)
(eq (key 2) :x2)
(eq (key 3) :y2))
`(distance-positional ,x1 ,y1 ,x2 ,y2))
((and (if x1-p (and (= x1s 1) (simplep x1)) t)
(if y1-p (and (= y1s 1) (simplep y1)) t)
(if x2-p (and (= x2s 1) (simplep x2)) t)
(if y2-p (and (= y2s 1) (simplep y2)) t)
(zerop others))
`(distance-positional ,x1 ,y1 ,x2 ,y2))
((and (&lt; x1s 2) (&lt; y1s 2) (&lt; x2s 2) (&lt; y2s 2)
(zerop others))
(let ((temps (loop repeat n collect (gensym))))
`(let ,(loop for i below n
collect (list (nth i temps) (arg i)))
(distance
,@(loop for i below n
append (list (key i) (nth i temps)))))))
(t form))))))
=&gt; DISTANCE
(dolist (form
'((distance :x1 (setq x 7) :x2 (decf x) :y1 (decf x) :y2 (decf x))
(distance :x1 (setq x 7) :y1 (decf x) :x2 (decf x) :y2 (decf x))
(distance :x1 (setq x 7) :y1 (incf x))
(distance :x1 (setq x 7) :y1 (incf x) :x1 (incf x))
(distance :x1 a1 :y1 b1 :x2 a2 :y2 b2)
(distance :x1 a1 :x2 a2 :y1 b1 :y2 b2)
(distance :x1 a1 :y1 b1 :z1 c1 :x2 a2 :y2 b2 :z2 c2)))
(print (funcall (compiler-macro-function 'distance) form nil)))
&gt;&gt; (LET ((#:G6558 (SETQ X 7))
&gt;&gt; (#:G6559 (DECF X))
&gt;&gt; (#:G6560 (DECF X))
&gt;&gt; (#:G6561 (DECF X)))
&gt;&gt; (DISTANCE :X1 #:G6558 :X2 #:G6559 :Y1 #:G6560 :Y2 #:G6561))
&gt;&gt; (DISTANCE-POSITIONAL (SETQ X 7) (DECF X) (DECF X) (DECF X))
&gt;&gt; (LET ((#:G6567 (SETQ X 7))
&gt;&gt; (#:G6568 (INCF X)))
&gt;&gt; (DISTANCE :X1 #:G6567 :Y1 #:G6568))
&gt;&gt; (DISTANCE :X1 (SETQ X 7) :Y1 (INCF X) :X1 (INCF X))
&gt;&gt; (DISTANCE-POSITIONAL A1 B1 A2 B2)
&gt;&gt; (DISTANCE-POSITIONAL A1 B1 A2 B2)
&gt;&gt; (DISTANCE :X1 A1 :Y1 B1 :Z1 C1 :X2 A2 :Y2 B2 :Z2 C2)
=&gt; NIL
</PRE>
</TT> <P>
<P><B>Affected By:</B> None.
<P>
<P><B>Exceptional Situations:</B> None.
<P>
<P><B>See Also:</B><P>
<P>
<A REL=DEFINITION HREF="f_cmp_ma.htm#compiler-macro-function"><B>compiler-macro-function</B></A>, <A REL=DEFINITION HREF="m_defmac.htm#defmacro"><B>defmacro</B></A>, <A REL=DEFINITION HREF="f_docume.htm#documentation"><B>documentation</B></A>, <A REL=CHILD HREF="03_dk.htm">Section 3.4.11 (Syntactic Interaction of Documentation Strings and Declarations)</A> <P>
<P><B>Notes:</B><P>
<P>
The consequences of writing a <A REL=DEFINITION HREF="26_glo_c.htm#compiler_macro"><I>compiler macro</I></A> definition for a function in the <TT>COMMON-LISP</TT> package are undefined; it is quite possible that in some <A REL=DEFINITION HREF="26_glo_i.htm#implementation"><I>implementations</I></A> such an attempt would override an equivalent or equally important definition. In general, it is recommended that a programmer only write <A REL=DEFINITION HREF="26_glo_c.htm#compiler_macro"><I>compiler macro</I></A> definitions for <A REL=DEFINITION HREF="26_glo_f.htm#function"><I>functions</I></A> he or she personally maintains--writing a <A REL=DEFINITION HREF="26_glo_c.htm#compiler_macro"><I>compiler macro</I></A> definition for a function maintained elsewhere is normally considered a violation of traditional rules of modularity and data abstraction. <P>
<P>
<P><HR>The following <A REL=META HREF="../Front/X3J13Iss.htm">X3J13 cleanup issues</A>, <I>not part of the specification</I>, apply to this section:<P><UL><LI> <A REL=CHILD HREF="../Issues/iss135.htm">DOCUMENTATION-FUNCTION-BUGS:FIX</A><LI> <A REL=CHILD HREF="../Issues/iss097.htm">DECLS-AND-DOC</A><P></UL><HR>
<A REL=NAVIGATOR HREF="../Front/StartPts.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Starting Points]" SRC="../Graphics/StartPts.gif" ALIGN=Bottom></A><A REL=TOC HREF="../Front/Contents.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Contents]" SRC="../Graphics/Contents.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Master.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Index]" SRC="../Graphics/Index.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Symbol.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Symbols]" SRC="../Graphics/Symbols.gif" ALIGN=Bottom></A><A REL=GLOSSARY HREF="../Body/26_a.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Glossary]" SRC="../Graphics/Glossary.gif" ALIGN=Bottom></A><A HREF="../Front/X3J13Iss.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Issues]" SRC="../Graphics/Issues.gif" ALIGN=Bottom></A><BR>
<A REL=COPYRIGHT HREF="../Front/Help.htm#Legal"><I>Copyright 1996-2005, LispWorks Ltd. All rights reserved.</I></A><P>
</BODY>
</HTML>