From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Stefan Israelsson Tampe Newsgroups: gmane.lisp.guile.devel Subject: splicing macros Date: Sat, 26 Jan 2013 14:03:20 +0100 Message-ID: <5103d30a.6718700a.6306.350b@mx.google.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1359205136 1156 80.91.229.3 (26 Jan 2013 12:58:56 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 26 Jan 2013 12:58:56 +0000 (UTC) To: guile-devel@gnu.org Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Sat Jan 26 13:59:16 2013 Return-path: Envelope-to: guile-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Tz5Ln-0007xs-75 for guile-devel@m.gmane.org; Sat, 26 Jan 2013 13:59:15 +0100 Original-Received: from localhost ([::1]:33871 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tz5LV-0003DE-PM for guile-devel@m.gmane.org; Sat, 26 Jan 2013 07:58:57 -0500 Original-Received: from eggs.gnu.org ([208.118.235.92]:60410) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tz5LS-0003D9-Ee for guile-devel@gnu.org; Sat, 26 Jan 2013 07:58:55 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Tz5LR-0000l7-2o for guile-devel@gnu.org; Sat, 26 Jan 2013 07:58:54 -0500 Original-Received: from mail-lb0-f177.google.com ([209.85.217.177]:49181) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Tz5LQ-0000l3-NB for guile-devel@gnu.org; Sat, 26 Jan 2013 07:58:53 -0500 Original-Received: by mail-lb0-f177.google.com with SMTP id go11so2052740lbb.36 for ; Sat, 26 Jan 2013 04:58:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:from:to:subject:date:message-id:user-agent:mime-version :content-type; bh=BeR4Cd9CtZ4JoULBNTGdKkbxSGcCH3j/k28AT78NhJk=; b=KdWsJAnb3PUIodKDck7sXJ9T+npgE266eznvgQ2v8X2eIwC679aw9JDaSJRV2WwfLm 2odxJHeWAh+ev3zgnp6zTQAgbfUvr6AKe4tUoxhJL9Nx0nk+Tjzg3riYkDRHP7PEmjSX pw4VT+7jGUx+/jhUJFrRdbzFOdsQbMEoGbPDdOl92o1YyVaevojme4xiTZZ2pH8+54DC C/N4AnjMu31Uw5Z8NLGhqxiOQKLxWcCoas6B+rSS5jFP8KXMKTDv/yyoHCWbBQra2LiB wjLZz+85KlW4ZHa5/1PWiOllZ8VHMNN9PFeHDpRJvWtOU5rtjBc99N719MBfAG+y3Yah icFQ== X-Received: by 10.112.36.165 with SMTP id r5mr3414535lbj.112.1359205131533; Sat, 26 Jan 2013 04:58:51 -0800 (PST) Original-Received: from blackbone (1-1-1-39a.veo.vs.bostream.se. [82.182.254.46]) by mx.google.com with ESMTPS id t7sm1048672lbf.12.2013.01.26.04.58.49 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Sat, 26 Jan 2013 04:58:50 -0800 (PST) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 209.85.217.177 X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Original-Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.devel:15597 Archived-At: 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