Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

LISP: Macros in lisp


Published on

LISP: Macros in lisp

Published in: Technology
  • Login to see the comments

  • Be the first to like this

LISP: Macros in lisp

  1. 1. Macros in Lisp<br />
  2. 2. CL macro facility allows the user to define arbitrary functions that convert certain lisp forms into different forms before evaluating or compiling them.<br />Macros makes it possible to write codes that are clear and elegant.<br />In CL, macros are not functions<br />In particular, macros cannot be used as the functional arguments to functions such as apply, funcall, or map.<br />
  3. 3. overview<br />Macro definition<br />Macro expansion<br />Destructuring<br />Compiler macros<br />Environments<br />
  4. 4. Macro definition<br />defmacro construct is used to define a macro.<br />Macro-function determines whether the given function is the name of the macro.<br />macro-function symbol<br />The argument must be a symbol, if the function has a global function definition( macro definition) then the expansion function is returned else nil is returned.<br />Macro-function cannot work on local variables it can only determine the global definitions.<br />Setf is used as a macro function to install a macro as a symbols global function definition<br />To add an optional environment argument to macro-function:<br />macro-function symbol &optional env<br />(setf (macro-function symbol) fn)<br />
  5. 5. defmacro has the same syntax as the defun<br />Here, name is the symbol whose macro definition we are creating.<br />Lambda-list is similar in form to a lambda-list, <br />Forms constitute the body of the expander function.<br />The defmacro function returns the name.<br />defmacro name lambda-list [[ {declaration}* | doc-string ]] {form}* [Macro]<br />
  6. 6. Defmacro lambda-list keywords<br />Some of the lambda-list key words are: &optional, &rest, &key, &allow-other-keys, &aux<br />&body: this is identical in function to &rest, it informs certain output-formatting and editing functions that the remainder of the form is treated as a body and should be intended accordingly.<br />&whole: this is followed by a single variable that is bound to the entire macro-call form.<br />&environment: this is followed by a single variable that is bound to an environment representing the lexical environment in which the macro call is to be interpreted. <br />
  7. 7. Macro-expansion<br />If form is a macro call, then macroexpand-1 will expand the macro call once and return two values.( the expansion and t) if form is not macro call then two values ( form and nil) are returned.<br />A form is said to be a macro call only if it is a cons whose car is a symbol that names a macro.<br />The macro expansion function is used for expanding a macro call.<br />
  8. 8. Macro-expansion steps followed<br />Once the macroexpand-1 has determined that a symbol names a macro, it obtains the expansion function for that macro.<br />The value of the variable *macro-expand-hook* is then called as a function of three arguments: the expansion function, the form, and the environment env.<br />The value returned from this call is taken to be the expansion of the macro call.<br />The evaluator makes use of the macro calls as if through the use of macroexpand-1; eval also makes use of *macroexpand-hook*<br />Macroexpand is similar to macroexpand-1, but repeatedly expands forms until it is no longer a macro call.<br />
  9. 9. Destructing<br />To make the destructing feature of the defmacro available use:<br />This macro binds the variables specified in lambda-list to the corresponding values in the tree structure resulting from evaluating the expression, then executes the forms as the an implicit progn.<br />If the result of the evaluating expression does not match the destructuring pattern, an error should be signaled.<br />Destructuring-bind lambda-list expression {declaration}* {form}[Macro]<br />
  10. 10. Compiler macros<br />Compiler macros are used to transform the selective source code when the compiler is processing the code.<br />This is similar to defmacro except the definition is not stored in the symbol function cell of name and is not seen by macroexpand-1<br />The lambda-list may include &environment, &whole and may include destructuring.<br />Return-form is used to exit from the function.<br />define-compiler-macro name lambda-list <br /> {declaration | doc-string}* {form}*<br />
  11. 11. Name must be a symbol, if it has been defined as a compiler macro, then the compiler-macro-function returns the macro expansion function, else returns nil.<br />Expander function is obtained as if by a call to compiler-macro-function on the car of the form rather than by a call to macro-function.<br />Two values are returned, the expansion and the value if expansion is true else nil.<br />compiler macro function name &optional env<br />compiler-macroexpand from &optional env<br />compiler-macroexpand-1 from &optional env<br />
  12. 12. Three cases when no expansion occurs:<br />There is no compiler macro definition for the car of the form.<br />There is such a definition but there is also a not inline declaration, either globally or in lexical environment env.<br />A global compiler macro definition is shadowed by a local function or macro definition.<br />
  13. 13. Environments<br />There is a minimal set of accessors and a constructor for environments.<br />Define-declaration is used to define all the standard declaration specifiers, with the exception of special.<br />The function returns the interpretation of the symbol variable when it appears as a variable within the lexical environment env.<br />Three values are returned:<br />The first value indicates the type of definition in the env which may be :special, :lexical, :symbol-macro, :constant<br />Variable-information variable &optional env<br />
  14. 14. 2. The second value is true if there is a local binding else it returns nil.<br />3. The third value is an a-list containing information about the declarations that apply to the apparent binding of the variable.<br />Returns the information about the interpretation of the function when it appears in the functional position of the lexical environment env.<br />This function also returns three values:<br />Type of definition or binding of the function name which is apparent in env (nil, :function, :macro, :special-form) <br />The second value is true if the function is local else nil.<br />The third value is an a-list containing information about declarations that apply to the apparent binding of the function.<br />function-information function &optional env<br />
  15. 15. argument-environment env &key :variable :symbol-macro :fuction :macro :declare<br />returns a new environment containing the information present in enc augmented with the information provided by the key word arguments.<br />The arguments used are:<br /><ul><li>:variable list of symbols that will be visible as bound variable in the new environment,.
  16. 16. :symbol-macro  list of symbol macro definitions
  17. 17. :function  list of function-names that will be visible as local function bindings in the new environment.
  18. 18. :macro list of local macro definitions
  19. 19. :declare list of declaration specifiers</li></li></ul><li>define-declaration decl-name lambda-list {form}*<br />The macro defines a macro for the named declaration.<br />The function defined by this macro will be called with two augments, a declaration specifier whose car is decl-name and the env argument to augment-environment.<br />
  20. 20. The function must return two values:<br /><ul><li> the first value must be one of the following keywords :variable, :function, :declare
  21. 21. If the first value is :variable or :function, then the second value must be a list, the elements of which are of the form (binding-name key value)
  22. 22. If the first value is :declare, the second value must be a cons of the form ( key. Value)</li></li></ul><li>parse-macro name lambda-list body &optional env<br />The function is used to process a macro definition, it returns a lambda-expression that accepts two argument, a form and an environment.<br />The function returns an object of type function that is equivalent to what would be obtained by evaluating ‘(function, lambda-expression) in a syntactic environment env.<br />
  23. 23. Visit more self help tutorials<br />Pick a tutorial of your choice and browse through it at your own pace.<br />The tutorials section is free, self-guiding and will not involve any additional support.<br />Visit us at<br />