unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* splicing macros
@ 2013-01-26 13:03 Stefan Israelsson Tampe
  2013-01-27 10:17 ` Andy Wingo
  0 siblings, 1 reply; 6+ messages in thread
From: Stefan Israelsson Tampe @ 2013-01-26 13:03 UTC (permalink / raw)
  To: guile-devel


Hi all,

This is something fun to implement using ck-macros and
local-syntax-value.

The idea is to patch guile's quasisyntax.scm in ice-9 to implement
splicing of macros. 

If we assume a reader macro #.code  ==> (macro-splicing code), with the
proposed hack I can now write:

  (define-syntax-rule (2x x) (x x))

and

  (define-syntax 4x
     (lambda (x)
        (syntax-case x ()
          ((_ x) #`(#.(2x x) #.(2x x))))))

and

 (define-syntax g (lambda (x) #`(#.(4x 1) #.(4x 2))))

$4 = (1 1 1 1 2 2 2 2)


------------------------------------------------------------
I will assume that you are familliar with th ck macro, included in
recent guile releases or else consider looking it up at

  http://okmij.org/ftp/Scheme/macros.html

It is now quite natural to use his c-append macro to do do the
transformation:
  (a b #.(f x) c d)
 -->
  (ck () (c-append '(a b) (c-append (ck-it '(f x)) '(c d))))

And assuming that ck-it can dispatch the macro (f x) the appending would 
be obvious. The second magic is ck-it, it is defined as

(use-modules (system syntax))

(define-syntax ck-it 
  (lambda (x)
    (syntax-case x (quote)
      ((_ s (quote f))
       (and (identifier? #'f) (eq? (syntax-local-binding #'f) 'macro))
       (call-with-values (lambda () (syntax-local-binding #'f))
         (lambda (m transformer)
           (list #'ck-it #'s (list #'quote (transformer #'f))))))

      ((_ s (quote (quote x))) 
       (list #'ck #'s #'(quote x)))

      ((_ s (quote (f . x)))
       (and (identifier? #'f) (eq? (syntax-local-binding #'f) 'macro))
       (call-with-values (lambda () (syntax-local-binding #'f))
         (lambda (m transformer)
           (list #'ck-it #'s (list #'quote (transformer #'(f . x)))))))
          
      ((_ s f)
       (list #'ck #'s #'f)))))


It will iterativelly apply macros until a non macro sexp is appearing,
To inhibit macro expeansion one need to quote it. This is a bit unclean
because if the macro returns (quote x), x any sexp, it will return x in 
stead. A better solution might be to introduce a special inhibit
macro. Also notice how splicing in already spliced syntaxes works as the
example above works. Another thing to note is how we use
syntax-local-binding to search find the macro transformer and use that
in order to make all this work.

Any thoughts? Should we add ck-it to guile's ck.scm? Should we add
splicing macros? 

/Stefan



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: splicing macros
  2013-01-26 13:03 splicing macros Stefan Israelsson Tampe
@ 2013-01-27 10:17 ` Andy Wingo
  2013-01-28 11:27   ` Stefan Israelsson Tampe
  2013-02-08 20:53   ` Stefan Israelsson Tampe
  0 siblings, 2 replies; 6+ messages in thread
From: Andy Wingo @ 2013-01-27 10:17 UTC (permalink / raw)
  To: Stefan Israelsson Tampe; +Cc: guile-devel

On Sat 26 Jan 2013 14:03, Stefan Israelsson Tampe <stefan.itampe@gmail.com> writes:

> I will assume that you are familliar with th ck macro, included in
> recent guile releases or else consider looking it up at
>
>   http://okmij.org/ftp/Scheme/macros.html

It does not seem to be documented in Guile's manual.  Want to write some
documentation? :)

Andy
-- 
http://wingolog.org/



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: splicing macros
  2013-01-27 10:17 ` Andy Wingo
@ 2013-01-28 11:27   ` Stefan Israelsson Tampe
  2013-02-08 20:53   ` Stefan Israelsson Tampe
  1 sibling, 0 replies; 6+ messages in thread
From: Stefan Israelsson Tampe @ 2013-01-28 11:27 UTC (permalink / raw)
  To: Andy Wingo; +Cc: guile-devel

Yes, I can add documentation for it.

/Stefan

On Sun, Jan 27, 2013 at 11:17 AM, Andy Wingo <wingo@pobox.com> wrote:
> On Sat 26 Jan 2013 14:03, Stefan Israelsson Tampe <stefan.itampe@gmail.com> writes:
>
>> I will assume that you are familliar with th ck macro, included in
>> recent guile releases or else consider looking it up at
>>
>>   http://okmij.org/ftp/Scheme/macros.html
>
> It does not seem to be documented in Guile's manual.  Want to write some
> documentation? :)
>
> Andy
> --
> http://wingolog.org/



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: splicing macros
  2013-01-27 10:17 ` Andy Wingo
  2013-01-28 11:27   ` Stefan Israelsson Tampe
@ 2013-02-08 20:53   ` Stefan Israelsson Tampe
  2013-04-29 20:46     ` Fwd: " Stefan Israelsson Tampe
  2014-03-20 20:32     ` Andy Wingo
  1 sibling, 2 replies; 6+ messages in thread
From: Stefan Israelsson Tampe @ 2013-02-08 20:53 UTC (permalink / raw)
  To: Andy Wingo; +Cc: guile-devel

[-- Attachment #1: Type: text/plain, Size: 543 bytes --]

Hi,

Here is a git-format-patch of some docs for the ck macro.
enjoy!




On Sun, Jan 27, 2013 at 11:17 AM, Andy Wingo <wingo@pobox.com> wrote:
> On Sat 26 Jan 2013 14:03, Stefan Israelsson Tampe <stefan.itampe@gmail.com> writes:
>
>> I will assume that you are familliar with th ck macro, included in
>> recent guile releases or else consider looking it up at
>>
>>   http://okmij.org/ftp/Scheme/macros.html
>
> It does not seem to be documented in Guile's manual.  Want to write some
> documentation? :)
>
> Andy
> --
> http://wingolog.org/

[-- Attachment #2: 0001-doc-ref-api-macros.texi-added-documentation-for-the-.patch --]
[-- Type: application/octet-stream, Size: 2564 bytes --]

From c0e4a806bda68dc3b645e17e2475929d2cb0dfad Mon Sep 17 00:00:00 2001
From: Stefan Israelsson Tampe <stefan.itampe@gmail.com>
Date: Fri, 8 Feb 2013 21:42:38 +0100
Subject: [PATCH] * doc/ref/api-macros.texi: added documentation for the ck
 macro

---
 doc/ref/api-macros.texi |   46 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)

diff --git a/doc/ref/api-macros.texi b/doc/ref/api-macros.texi
index 347d025..1f9e645 100644
--- a/doc/ref/api-macros.texi
+++ b/doc/ref/api-macros.texi
@@ -44,6 +44,7 @@ languages}, or EDSLs.}.
 * Syntax Parameters::           Syntax Parameters.
 * Eval When::                   Affecting the expand-time environment.
 * Internal Macros::             Macros as first-class values.
+* Ck macro::                    Applicative evaluation order macro facility.
 @end menu
 
 @node Defining Macros
@@ -1160,7 +1161,52 @@ which one may ask the docstring. That's the whole reason this section is
 documented. Actually a part of the result of @code{macro-binding}.
 @end deffn
 
+@node Ck macro
+@subsection The Ck macro
 
+@deffn {Syntax} ck state expression
+The @code{ck} macro is a facility that simulate function applicative expansion 
+order for the scheme macro system. It does so by introducing a few conventions. 
+First every expression of the form @code{(quote x)} will mark x as the result
+of the expansion of that argument and stop try expanding. Secondly every macro 
+have to use as first argument, the state and then concatenate the actual 
+arguments. Thirdly it have to explicitly invoke the @code{ck} macro at start 
+and also at every continuation when a resulting expansion have finished. To 
+start the expantion use @code{(ck () expr)}.
+
+@example
+(use-modules (system base ck))
+
+;;note
+;; 1) The use of '... to indicate a value
+;; 2) How we continue with issueing (ck s ...) at the end of expansion
+;; 3) How macros applications inside of the ck macro does not have explicit 
+;;    s (That's added later on)
+
+(define-syntax c-cons 
+  (syntax-rules (quote)
+    ((_ s 'x 'y) (ck s '(x . y)))))
+
+(define-syntax c-map
+  (syntax-rules (quote)
+    ((_ s 'c-f '(x . l))
+     (ck s (c-cons (c-f 'x) (c-map 'c-f 'l))))
+    ((_ s _ '())
+     (ck s '()))))
+
+(define-syntax c-f 
+  (syntax-rules (quote)
+     ((_ s 'x)
+      (ck s '(* x 10)))))
+
+
+;; And to show the macro in action write
+
+,exp (ck () (c-map 'c-f '(a b c)))
+
+$1 = ((* a 10) (* b 10) (* c 10))    
+@end example
+@end deffn
 @c Local Variables:
 @c TeX-master: "guile.texi"
 @c End:
-- 
1.7.9.5


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Fwd: splicing macros
  2013-02-08 20:53   ` Stefan Israelsson Tampe
@ 2013-04-29 20:46     ` Stefan Israelsson Tampe
  2014-03-20 20:32     ` Andy Wingo
  1 sibling, 0 replies; 6+ messages in thread
From: Stefan Israelsson Tampe @ 2013-04-29 20:46 UTC (permalink / raw)
  To: guile-devel, Mark H Weaver


[-- Attachment #1.1: Type: text/plain, Size: 819 bytes --]

I found the doc, maybe you can take a look!
---------- Forwarded message ----------
From: Stefan Israelsson Tampe <stefan.itampe@gmail.com>
Date: Fri, Feb 8, 2013 at 9:53 PM
Subject: Re: splicing macros
To: Andy Wingo <wingo@pobox.com>
Cc: guile-devel <guile-devel@gnu.org>


Hi,

Here is a git-format-patch of some docs for the ck macro.
enjoy!




On Sun, Jan 27, 2013 at 11:17 AM, Andy Wingo <wingo@pobox.com> wrote:
> On Sat 26 Jan 2013 14:03, Stefan Israelsson Tampe <stefan.itampe@gmail.com>
writes:
>
>> I will assume that you are familliar with th ck macro, included in
>> recent guile releases or else consider looking it up at
>>
>>   http://okmij.org/ftp/Scheme/macros.html
>
> It does not seem to be documented in Guile's manual.  Want to write some
> documentation? :)
>
> Andy
> --
> http://wingolog.org/

[-- Attachment #1.2: Type: text/html, Size: 1565 bytes --]

[-- Attachment #2: 0001-doc-ref-api-macros.texi-added-documentation-for-the-.patch --]
[-- Type: application/octet-stream, Size: 2564 bytes --]

From c0e4a806bda68dc3b645e17e2475929d2cb0dfad Mon Sep 17 00:00:00 2001
From: Stefan Israelsson Tampe <stefan.itampe@gmail.com>
Date: Fri, 8 Feb 2013 21:42:38 +0100
Subject: [PATCH] * doc/ref/api-macros.texi: added documentation for the ck
 macro

---
 doc/ref/api-macros.texi |   46 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 46 insertions(+)

diff --git a/doc/ref/api-macros.texi b/doc/ref/api-macros.texi
index 347d025..1f9e645 100644
--- a/doc/ref/api-macros.texi
+++ b/doc/ref/api-macros.texi
@@ -44,6 +44,7 @@ languages}, or EDSLs.}.
 * Syntax Parameters::           Syntax Parameters.
 * Eval When::                   Affecting the expand-time environment.
 * Internal Macros::             Macros as first-class values.
+* Ck macro::                    Applicative evaluation order macro facility.
 @end menu
 
 @node Defining Macros
@@ -1160,7 +1161,52 @@ which one may ask the docstring. That's the whole reason this section is
 documented. Actually a part of the result of @code{macro-binding}.
 @end deffn
 
+@node Ck macro
+@subsection The Ck macro
 
+@deffn {Syntax} ck state expression
+The @code{ck} macro is a facility that simulate function applicative expansion 
+order for the scheme macro system. It does so by introducing a few conventions. 
+First every expression of the form @code{(quote x)} will mark x as the result
+of the expansion of that argument and stop try expanding. Secondly every macro 
+have to use as first argument, the state and then concatenate the actual 
+arguments. Thirdly it have to explicitly invoke the @code{ck} macro at start 
+and also at every continuation when a resulting expansion have finished. To 
+start the expantion use @code{(ck () expr)}.
+
+@example
+(use-modules (system base ck))
+
+;;note
+;; 1) The use of '... to indicate a value
+;; 2) How we continue with issueing (ck s ...) at the end of expansion
+;; 3) How macros applications inside of the ck macro does not have explicit 
+;;    s (That's added later on)
+
+(define-syntax c-cons 
+  (syntax-rules (quote)
+    ((_ s 'x 'y) (ck s '(x . y)))))
+
+(define-syntax c-map
+  (syntax-rules (quote)
+    ((_ s 'c-f '(x . l))
+     (ck s (c-cons (c-f 'x) (c-map 'c-f 'l))))
+    ((_ s _ '())
+     (ck s '()))))
+
+(define-syntax c-f 
+  (syntax-rules (quote)
+     ((_ s 'x)
+      (ck s '(* x 10)))))
+
+
+;; And to show the macro in action write
+
+,exp (ck () (c-map 'c-f '(a b c)))
+
+$1 = ((* a 10) (* b 10) (* c 10))    
+@end example
+@end deffn
 @c Local Variables:
 @c TeX-master: "guile.texi"
 @c End:
-- 
1.7.9.5


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: splicing macros
  2013-02-08 20:53   ` Stefan Israelsson Tampe
  2013-04-29 20:46     ` Fwd: " Stefan Israelsson Tampe
@ 2014-03-20 20:32     ` Andy Wingo
  1 sibling, 0 replies; 6+ messages in thread
From: Andy Wingo @ 2014-03-20 20:32 UTC (permalink / raw)
  To: Stefan Israelsson Tampe; +Cc: guile-devel

Hi,

Top-posting as it's been so long.  You know, on second thought I'm
hesitant to document this macro.  I understood it at one point but it
has totally left my head -- I can only remember so many things.  It seem
to me that the best we can do is probably pointing to Oleg's web page,
and leaving it out of the manual.  If we do put it in, it's not really a
peer of "syntax-rules", which is where you put it...  dunno.  I'm open
to other thoughts.

Sorry for the negativity!

A

On Fri 08 Feb 2013 21:53, Stefan Israelsson Tampe <stefan.itampe@gmail.com> writes:

> Hi,
>
> Here is a git-format-patch of some docs for the ck macro.
> enjoy!
>
>
>
>
> On Sun, Jan 27, 2013 at 11:17 AM, Andy Wingo <wingo@pobox.com> wrote:
>> On Sat 26 Jan 2013 14:03, Stefan Israelsson Tampe <stefan.itampe@gmail.com> writes:
>>
>>> I will assume that you are familliar with th ck macro, included in
>>> recent guile releases or else consider looking it up at
>>>
>>>   http://okmij.org/ftp/Scheme/macros.html
>>
>> It does not seem to be documented in Guile's manual.  Want to write some
>> documentation? :)
>>
>> Andy
>> --
>> http://wingolog.org/
>
> From c0e4a806bda68dc3b645e17e2475929d2cb0dfad Mon Sep 17 00:00:00 2001
> From: Stefan Israelsson Tampe <stefan.itampe@gmail.com>
> Date: Fri, 8 Feb 2013 21:42:38 +0100
> Subject: [PATCH] * doc/ref/api-macros.texi: added documentation for the ck
>  macro
>
> ---
>  doc/ref/api-macros.texi |   46 ++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 46 insertions(+)
>
> diff --git a/doc/ref/api-macros.texi b/doc/ref/api-macros.texi
> index 347d025..1f9e645 100644
> --- a/doc/ref/api-macros.texi
> +++ b/doc/ref/api-macros.texi
> @@ -44,6 +44,7 @@ languages}, or EDSLs.}.
>  * Syntax Parameters::           Syntax Parameters.
>  * Eval When::                   Affecting the expand-time environment.
>  * Internal Macros::             Macros as first-class values.
> +* Ck macro::                    Applicative evaluation order macro facility.
>  @end menu
>  
>  @node Defining Macros
> @@ -1160,7 +1161,52 @@ which one may ask the docstring. That's the whole reason this section is
>  documented. Actually a part of the result of @code{macro-binding}.
>  @end deffn
>  
> +@node Ck macro
> +@subsection The Ck macro
>  
> +@deffn {Syntax} ck state expression
> +The @code{ck} macro is a facility that simulate function applicative expansion 
> +order for the scheme macro system. It does so by introducing a few conventions. 
> +First every expression of the form @code{(quote x)} will mark x as the result
> +of the expansion of that argument and stop try expanding. Secondly every macro 
> +have to use as first argument, the state and then concatenate the actual 
> +arguments. Thirdly it have to explicitly invoke the @code{ck} macro at start 
> +and also at every continuation when a resulting expansion have finished. To 
> +start the expantion use @code{(ck () expr)}.
> +
> +@example
> +(use-modules (system base ck))
> +
> +;;note
> +;; 1) The use of '... to indicate a value
> +;; 2) How we continue with issueing (ck s ...) at the end of expansion
> +;; 3) How macros applications inside of the ck macro does not have explicit 
> +;;    s (That's added later on)
> +
> +(define-syntax c-cons 
> +  (syntax-rules (quote)
> +    ((_ s 'x 'y) (ck s '(x . y)))))
> +
> +(define-syntax c-map
> +  (syntax-rules (quote)
> +    ((_ s 'c-f '(x . l))
> +     (ck s (c-cons (c-f 'x) (c-map 'c-f 'l))))
> +    ((_ s _ '())
> +     (ck s '()))))
> +
> +(define-syntax c-f 
> +  (syntax-rules (quote)
> +     ((_ s 'x)
> +      (ck s '(* x 10)))))
> +
> +
> +;; And to show the macro in action write
> +
> +,exp (ck () (c-map 'c-f '(a b c)))
> +
> +$1 = ((* a 10) (* b 10) (* c 10))    
> +@end example
> +@end deffn
>  @c Local Variables:
>  @c TeX-master: "guile.texi"
>  @c End:

-- 
http://wingolog.org/



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2014-03-20 20:32 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-01-26 13:03 splicing macros Stefan Israelsson Tampe
2013-01-27 10:17 ` Andy Wingo
2013-01-28 11:27   ` Stefan Israelsson Tampe
2013-02-08 20:53   ` Stefan Israelsson Tampe
2013-04-29 20:46     ` Fwd: " Stefan Israelsson Tampe
2014-03-20 20:32     ` Andy Wingo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).