Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.lisp > #60861 > unrolled thread

implementing scheme - implementing macros

Started byMario Rosell <mario@mariorosell.es>
First post2026-06-12 21:15 +0000
Last post2026-08-29 17:15 -0300
Articles 9 — 8 participants

Back to article view | Back to comp.lang.lisp


Contents

  implementing scheme - implementing macros Mario Rosell <mario@mariorosell.es> - 2026-06-12 21:15 +0000
    Re: implementing scheme - implementing macros steve g <Sgonedes1977@gmail.com> - 2026-06-16 21:50 -0400
      Re: implementing scheme - implementing macros Lawrence D’Oliveiro <ldo@nz.invalid> - 2026-06-17 03:14 +0000
      Re: implementing scheme - implementing macros Paul Rubin <no.email@nospam.invalid> - 2026-06-16 21:45 -0700
        Re: implementing scheme - implementing macros steve g <Sgonedes1977@gmail.com> - 2026-06-19 23:15 -0400
    Re: implementing scheme - implementing macros antispam@fricas.org (Waldek Hebisch) - 2026-06-17 14:26 +0000
    Re: implementing scheme - implementing macros Kaz Kylheku <046-301-5902@kylheku.com> - 2026-06-23 20:59 +0000
      Re: implementing scheme - implementing macros Stefan Monnier <monnier@iro.umontreal.ca> - 2026-06-25 10:59 -0400
      Re: implementing scheme - implementing macros Kragen Javier Sitaker <kragen@canonical.org> - 2026-08-29 17:15 -0300

#60861 — implementing scheme - implementing macros

FromMario Rosell <mario@mariorosell.es>
Date2026-06-12 21:15 +0000
Subjectimplementing scheme - implementing macros
Message-ID<110hst1$2gjgb$1@dont-email.me>
[ Repost from Reddit. r/lisp: https://t.ly/Fldzk ]

Hello everyone.

I am making a Scheme R5RS implementation and it is going pretty well.

I am trying implement macros, and don't really know how to implement them.
Scheme has a pretty complex macro system, so for now I am trying to
implement defmacro and then implement define-syntax, syntax-case, etc.

Any tips?

Thanks in advance.

-- 
- mario

[toc] | [next] | [standalone]


#60931

Fromsteve g <Sgonedes1977@gmail.com>
Date2026-06-16 21:50 -0400
Message-ID<87ik7iq6jh.fsf@gmail.com>
In reply to#60861
Mario Rosell <mario@mariorosell.es> writes:

> [ Repost from Reddit. r/lisp: https://t.ly/Fldzk ]
>
> Hello everyone.
>
> I am making a Scheme R5RS implementation and it is going pretty well.
>
> I am trying implement macros, and don't really know how to implement them.
> Scheme has a pretty complex macro system, so for now I am trying to
> implement defmacro and then implement define-syntax, syntax-case, etc.
>
> Any tips?


using eval is your friend.


the old emacs way was hard to read.

the emacs lisp info page: "

14.1 A Simple Example of a Macro
================================

Suppose we would like to define a Lisp construct to increment a variable
value, much like the ‘++’ operator in C.  We would like to write ‘(inc
x)’ and have the effect of ‘(setq x (1+ x))’.  Here's a macro definition
that does the job:

     (defmacro inc (var)
        (list 'setq var (list '1+ var)))

 "

you need to ``reevaluate'' the macro twice.

the commmon lisp was uses symbols like @ , .  etc..


hope this is helpful.

[toc] | [prev] | [next] | [standalone]


#60936

FromLawrence D’Oliveiro <ldo@nz.invalid>
Date2026-06-17 03:14 +0000
Message-ID<110t3e2$1ijv4$10@dont-email.me>
In reply to#60931
On Tue, 16 Jun 2026 21:50:26 -0400, steve g wrote:

> the old emacs way was hard to read.
>
> the commmon lisp was uses symbols like @ , . etc..

That’s the traditional way of doing it, also supported by Emacs
(naturally).

[toc] | [prev] | [next] | [standalone]


#60943

FromPaul Rubin <no.email@nospam.invalid>
Date2026-06-16 21:45 -0700
Message-ID<87o6h94vxn.fsf@nightsong.com>
In reply to#60931
steve g <Sgonedes1977@gmail.com> writes:
> you need to ``reevaluate'' the macro twice.
> the commmon lisp was uses symbols like @ , .  etc..

That's been built into Emacs Lisp for quite a while too.  Before it was
built into the reader, it was available as a loadable macro package so
you'd write something like (` (setq (, var) (1+ (, var))) .  Now it's
like CL, `(setq ,var (1+ ,var)) .   

[toc] | [prev] | [next] | [standalone]


#61001

Fromsteve g <Sgonedes1977@gmail.com>
Date2026-06-19 23:15 -0400
Message-ID<871pe1j41q.fsf@gmail.com>
In reply to#60943
Paul Rubin <no.email@nospam.invalid> writes:

> steve g <Sgonedes1977@gmail.com> writes:
>> you need to ``reevaluate'' the macro twice.
>> the commmon lisp was uses symbols like @ , .  etc..
>
> That's been built into Emacs Lisp for quite a while too.  Before it was
> built into the reader, it was available as a loadable macro package so
> you'd write something like (` (setq (, var) (1+ (, var))) .  Now it's
> like CL, `(setq ,var (1+ ,var)) .   


yes, I do remeber this. very compilcated to read; much better now.

[toc] | [prev] | [next] | [standalone]


#60953

Fromantispam@fricas.org (Waldek Hebisch)
Date2026-06-17 14:26 +0000
Message-ID<110uapr$389la$1@paganini.bofh.team>
In reply to#60861
Mario Rosell <mario@mariorosell.es> wrote:
> [ Repost from Reddit. r/lisp: https://t.ly/Fldzk ]
> 
> Hello everyone.
> 
> I am making a Scheme R5RS implementation and it is going pretty well.
> 
> I am trying implement macros, and don't really know how to implement them.
> Scheme has a pretty complex macro system, so for now I am trying to
> implement defmacro and then implement define-syntax, syntax-case, etc.
> 
> Any tips?

Naive macros are easy: when you see macro definition you handle
it almost as a function, except for that you mark resulting
object as a macro.  In evaluator (or in compiler if you have one)
you first check if the form is a macro call (that is head is marked as
a macro), if it is than you execute corresponding macro function applying
it to unevaluated arguments.  When macro function returns you
handle result as new source code.

Once you have naive macros you can add niceties like quasiquote
by preprocessing the macro definition.  In other words, better
macro can be implemented as macro definition which processes
its argument and then passes it to naive macro.

I am not familiar with details of Scheme macros, but it is quite
likely that they could be implemented in similar way on top of
naive macros.

-- 
                              Waldek Hebisch

[toc] | [prev] | [next] | [standalone]


#61013

FromKaz Kylheku <046-301-5902@kylheku.com>
Date2026-06-23 20:59 +0000
Message-ID<20260623124622.64@kylheku.com>
In reply to#60861
On 2026-06-12, Mario Rosell <mario@mariorosell.es> wrote:
> [ Repost from Reddit. r/lisp: https://t.ly/Fldzk ]
>
> Hello everyone.
>
> I am making a Scheme R5RS implementation and it is going pretty well.
>
> I am trying implement macros, and don't really know how to implement them.
> Scheme has a pretty complex macro system, so for now I am trying to
> implement defmacro and then implement define-syntax, syntax-case, etc.
>
> Any tips?

There exists more than one algorithm for hygienic macros.

The "grand daddy" is Kohlbecker's algorithm (1986)

The offshoots:

- Syntactic closures (1988)
  - Alan Bawden paper from the proceedings of the 1988 ACM conference on Lisp
    and functional programming.
  - Chris Hanson 1991 paper, "A Syntactic Closure Macro Facility". 

- Modified Kohlbecker's algorithm:
  - William Clinger's 1990 paper, "Macros That Work"
    - claims to address issues with syntactic closures
    - reduces Kohlbecker from quadratic to linear time

-- 
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

[toc] | [prev] | [next] | [standalone]


#61015

FromStefan Monnier <monnier@iro.umontreal.ca>
Date2026-06-25 10:59 -0400
Message-ID<jwvechu7kq4.fsf-monnier+comp.lang.lisp@gnu.org>
In reply to#61013
> There exists more than one algorithm for hygienic macros.
>
> The "grand daddy" is Kohlbecker's algorithm (1986)
>
> The offshoots:
>
> - Syntactic closures (1988)
>   - Alan Bawden paper from the proceedings of the 1988 ACM conference on Lisp
>     and functional programming.
>   - Chris Hanson 1991 paper, "A Syntactic Closure Macro Facility". 
>
> - Modified Kohlbecker's algorithm:
>   - William Clinger's 1990 paper, "Macros That Work"
>     - claims to address issues with syntactic closures
>     - reduces Kohlbecker from quadratic to linear time

Might be worth mentioning the "sets of scopes" one used in Racket:

    Binding as sets of scopes
    Matthew Flatt. POPL 2016.
    doi: 10.1145/2837614.2837620


=== Stefan

[toc] | [prev] | [next] | [standalone]


#61616

FromKragen Javier Sitaker <kragen@canonical.org>
Date2026-08-29 17:15 -0300
Message-ID<87qzjg7kfc.fsf@debian>
In reply to#61013
Kaz Kylheku <046-301-5902@kylheku.com> writes:
> On 2026-06-12, Mario Rosell <mario@mariorosell.es> wrote:
>> I am trying implement macros, and don't really know how to implement them.
>> Scheme has a pretty complex macro system, so for now I am trying to
>> implement defmacro and then implement define-syntax, syntax-case, etc.
>
> There exists more than one algorithm for hygienic macros. (...)
> Kohlbecker's algorithm (1986) (...) William Clinger's 1990 paper,
> "Macros That Work"

This can be one of the hairiest parts of implementing Scheme, but there
are off-the-shelf free-software library implementations of Clinger’s
algorithm that you can “just” drop into your Scheme if you want.
They're several thousand lines of code, and you’ll likely have to
macroexpand them first on an existing Scheme.  The other respondents who
think they’re just quasiquotation plus eval, similar to Common Lisp
macros, are being far too optimistic; they will be in for a big surprise
if they ever try to implement Scheme macros themselves.

Kragen

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.lisp


csiph-web