Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there whole-program-transforming macros in Lisp or Scheme?

I have seen one answer of How does Lisp let you redefine the language itself? Stack Overflow question (answered by Noah Lavine):

Macros aren't quite a complete redefinition of the language, at least as far as I know (I'm actually a Schemer; I could be wrong), because there is a restriction. A macro can only take a single subtree of your code, and generate a single subtree to replace it. Therefore you can't write whole-program-transforming macros, as cool as that would be.

After reading this I am curious about whether there are "whole-program-transforming macros" in Lisp or Scheme (or some other language).

If not then why?

  • It is not useful and never required?
  • Same thing could be achieved by some other ways?
  • It is not possible to implement it even in Lisp?
  • It is possible, but not tried or implemented ever?

Update

One kind of use case e.g.

As in stumpwm code here are some functions all in different lisp source files uses a dynamic/global defvar variable *screen-list* that is defined in primitives.lisp , but used in screen.lisp, user.lisp, window.lisp. (Here each files have functions, class, vars related to one aspect or object)

Now I wanted to define these functions under the closure where *screen-list* variable available by let form, it should not be dynamic/global variable, But without moving these all functions into one place (because I do not want these functions to lose place from their related file) So that this variable will be accessible to only these functions.

Above e.g. equally apply to label and flet, so that it will further possible that we could make it like that only required variable, function will be available, to those who require it.

Note one way might be implement and use some macro defun_with_context for defun where first argument is context where let, flet variables definend. But apart from it could it be achieved by reader-macro as Vatine and Gareth Rees answered.

like image 564
Sharad Avatar asked Jan 21 '11 12:01

Sharad


1 Answers

You quoted Noah Lavine as saying:

A macro can only take a single subtree of your code, and generate a single subtree to replace it

This is the case for ordinary macros, but reader macros get access to the input stream and can do whatever they like with it.

See the Hyperspec section 2.2 and the set-macro-character function.

like image 173
Gareth Rees Avatar answered Sep 19 '22 05:09

Gareth Rees