#|[ Author: Juergen Lorenz ju (at) jugilo (dot) de Copyright (c) 2013, Juergen Lorenz All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. This module provides simplyfied versions of some of the bindings macros, restricting destructuring to nested list expressions. ]|# (module list-bindings (export list-bindings bind-define bind-set! bind bindable? bind-lambda bind-case bind/cc bind-let bind-let*) (import scheme (only chicken condition-case error) (only extras format)) (define (list-bindings) '(bind-define bind-set! bind bind-lambda bind-let* bind-let bind-case bindable? bind/cc)) #|[ Binding macros ============== ]|# ;;; (bindable? pat) ;;; --------------- ;;; returns a procedure, which checks, if subexpressions of its only ;;; agument, tpl, are bindable to the pattern variables of pat. (define-syntax bindable? (syntax-rules () ((_ (a . b)) (lambda (seq) (and (pair? seq) ((bindable? a) (car seq)) ((bindable? b) (cdr seq))))) ((_ ()) (lambda (seq) (null? seq))) ((_ a) (lambda (seq) #t)))) ;;; (bind pat seq . body) ;;; -------------------------- ;;; Common Lisp's destructuring-bind: ;;; binds pattern variables of pat to corresponding subexpression of ;;; seq and evalueates body in this context (define-syntax bind (syntax-rules () ((_(a . b) lst xpr . xprs) (let ((seq lst)) (if (pair? seq) (bind a (car seq) (bind b (cdr seq) xpr . xprs)) (error 'bind (format #f "template ~s doesn't match pattern ~s" seq '(a . b)))))) ((_ () lst xpr . xprs) (let ((seq lst)) (if (null? seq) (let () xpr . xprs) (error 'bind (format #f "template ~s doesn't match pattern ~s" seq '()))))) ((_ a lst xpr . xprs) (let ((seq lst)) (let ((a seq)) xpr . xprs))))) ;;; (bind-lambda pat xpr . xprs) ;;; ------------------------------ ;;; Combination of bind and lambda (define-syntax bind-lambda (syntax-rules () ((_ pat xpr . xprs) (lambda (x) (bind pat x xpr . xprs))))) ;;; (bind-case seq (pat xpr . xprs) ....) ;;; ------------------------------------- ;;; Checks if seq matches pattern pat in sequence, binds the pattern ;;; variables of the first matching pattern to corresponding sublists of ;;; seq and executes corresponding body xpr . xprs (define-syntax bind-case (syntax-rules () ((_ seq (pat0 xpr0 . xprs0) (pat1 xpr1 . xprs1) ...) (cond (((bindable? pat0) seq) (bind pat0 seq xpr0 . xprs0)) (((bindable? pat1) seq) (bind pat1 seq xpr1 . xprs1)) ... (else (error 'bind-case (format #f "template ~s doesn't match any of the patterns ~s" seq '(pat0 pat1 ...)))))))) #|[ The next two macros provide simultaneous setting and defining of pattern variables to subexpressions of a template. The following first try would perfectly work, if seq is simply a list, but not if it is a list wrapped by a let storing common state. But the latter is the most often used case of bind-define. (define-syntax bind-define (syntax-rules () ((_ (a . b) seq) (begin (bind-define a (car seq)) (bind-define b (cdr seq)))) ((_ () seq) (if (null? seq) (void) (error 'bind-define "match error"))) ((_ a seq) (define a seq)))) What we need is one further indirection provided in the following versions, which first map the pattern, pat, to some auxiliary pattern, aux, of the same form with gensym. In bind-set! the template, seq, is then bound to some variable x which in turn can be bound to aux in the macro expansion. Since we used gensym instead of rename above, there will be no name-clash - rename is referentially transparent! We could have used implicit-renaming macros as well, but then the gensyms would be automatically renamed again, which isn't necessary. Note, that there is some code duplication in the two macros below, which could have been avoided by defining two helpers, pmap and pflatten, in a separate helper module which must be imported for syntax. I've done this in a former version. ]|# ;;; (bind-set! pat seq) ;;; ------------------- ;;; sets pattern variables of the pattern pat to corresponding ;;; subexpressions of the template seq (define-syntax bind-set! (er-macro-transformer (lambda (form rename compare?) (let ((pat (cadr form)) (seq (caddr form))) (let ((aux (let recur ((pat pat)) (cond ((null? pat) '()) ((symbol? pat) (gensym)) ((pair? pat) (cons (recur (car pat)) (recur (cdr pat))))))) ; rename would potentially clash with the %x below (%bind (rename 'bind)) (%set! (rename 'set!)) (%let (rename 'let)) (%x (rename 'x))) `(,%let ((,%x ,seq)) (,%bind ,aux ,%x ,@(let recur ((pat pat) (aux aux)) (cond ((null? pat) '()) ((symbol? pat) `((set! ,pat ,aux))) ((pair? pat) (append (recur (car pat) (car aux)) (recur (cdr pat) (cdr aux))))))))))))) ;(define-syntax bind-set! ; (ir-macro-transformer ; (lambda (form inject compare?) ; (let ((pat (cadr form)) (seq (caddr form))) ; (let ((aux (pmap gensym pat))) ; `(let ((x ,seq)) ; (bind ,aux x ; ,@(map (lambda (p a) `(set! ,p ,a)) ; (pflatten pat) (pflatten aux))))))))) ;;; (bind-define pat seq) ;;; --------------------- ;;; defines pattern variables of the pattern pat by setting them to ;;; corresponding subexpressions of the template seq (define-syntax bind-define (er-macro-transformer (lambda (form rename compare?) (let ((pat (cadr form)) (seq (caddr form))) (let ((aux (let recur ((pat pat)) (cond ((null? pat) '()) ((symbol? pat) (gensym)) ((pair? pat) (cons (recur (car pat)) (recur (cdr pat))))))) (%bind-set! (rename 'bind-set!)) (%define (rename 'define)) (%begin (rename 'begin))) `(,%begin (,%bind-set! ,aux ,seq) ,@(let recur ((pat pat) (aux aux)) (cond ((null? pat) '()) ((symbol? pat) `((set! ,pat ,aux))) ((pair? pat) (append (recur (car pat) (car aux)) (recur (cdr pat) (cdr aux)))))))))))) ;(define-syntax bind-define ; (ir-macro-transformer ; (lambda (form inject compare?) ; (let ((pat (cadr form)) (seq (caddr form))) ; (let ((aux (pmap gensym pat))) ; `(begin ; (bind-set! ,aux ,seq) ; ,@(map (lambda (p a) `(define ,p ,a)) ; (pflatten pat) (pflatten aux)))))))) ;;; (bind-let* ((pat seq) ...) xpr . xprs) ;;; -------------------------------------- (define-syntax bind-let* (syntax-rules () ((_ () xpr . xprs) (let () xpr . xprs)) ((_ ((pat0 seq0) (pat1 seq1) ...) xpr . xprs) (bind pat0 seq0 (bind-let* ((pat1 seq1) ...) xpr . xprs))))) ;;; (bind-let ((pat seq) ...) xpr . xprs) ;;; ------------------------------------- (define-syntax bind-let (ir-macro-transformer (lambda (form inject compare?) (let ((binds (cadr form)) (xpr (caddr form)) (xprs (cdddr form))) (let ((syms (map (lambda (x) (gensym)) binds))) `(bind ,syms ,(cons 'list (map cadr binds)) (bind-let* ,(map (lambda (p s) `(,p ,s)) (map car binds) syms) ,xpr ,@xprs))))))) ;;; (bind/cc k xpr . xprs) ;;; ---------------------- (define-syntax bind/cc (syntax-rules () ((_ k xpr . xprs) (call-with-current-continuation (lambda (k) xpr . xprs))))) ) ; module list-bindings