- Destructuring sequence expressions with bindings
- The functor bind-functor
- The module bind-sequences
- The modules bindings and list-bindings
- The functor bind-functor
- Last update
- Version History
Destructuring sequence expressions with bindings
Automatic destructuring of expressions is a handy feature, which can be successfully used in writing procedural macros, for example. Some programming languages use it extensively, in particular ML and its descendents Haskell and Miranda. And Chicken offers an egg called matchable, which does it as well.
This library provides an alternative to matchable, a bunch of macros, all starting with the bind prefix, and all being derived from bind and related procedures. They all destructure arbitrary mixtures of (pseudo-) lists, vectors and strings, which match a pattern, and can be easily enhanced, to accept other sequence types as well, arrays, for example.
The syntax of the fundamental bind macro is as follows[syntax] (bind pat (where fender ...) .. seq xpr ....)
Here, a pattern, pat, is a nested psudolist of (mostly) symbols, seq a sequencce expression, i.e. a mixture of pseudolists, vectors and strings, fender a check expression on pattern variables, var, of the form (var ok? ...) and xpr .... constitute the body of the macro. Note the special use of dots here and below: Three dots repeat the expression to the left zero or many times, two dots zero or one times and four dots one or many times.
This macro binds pattern variables, i.e. symbols of pat, to corresponding sequenceds of seq, checks, if the fenders succeed and exectutes the body in this context.
The bind macro is patterned after Paul Graham's dbind, cf. his classic "On Lisp", p. 232. Here is a Chicken version for lists:
(define-syntax dbind (ir-macro-transformer (lambda (form inject compare?) (letrec ( (mappend (lambda (fn lists) (apply append (map fn lists)))) (destruc (lambda (pat seq) (let loop ((pat pat) (seq seq) (n 0)) (if (pair? pat) (let ((p (car pat)) (recu (loop (cdr pat) seq (+ n 1)))) (if (symbol? p) (cons `(,p (list-ref ,seq ,n)) recu) (let ((g (gensym))) (cons (cons `(,g (list-ref ,seq ,n)) (loop p g 0)) recu)))) (if (null? pat) '() `((,pat (list-tail ,seq ,n)))))))) (dbind-ex (lambda (binds body) (if (null? binds) `(begin ,@body) `(let ,(map (lambda (b) (if (pair? (car b)) (car b) b)) binds) ,(dbind-ex (mappend (lambda (b) (if (pair? (car b)) (cdr b) '())) binds) body))))) ) (let ((pat (cadr form)) (seq (caddr form)) (body (cdddr form)) (gseq 'seq)) `(let ((,gseq ,seq)) ,(dbind-ex (destruc pat gseq) body)))))))
This code works as follows: First, destruc traverses the pattern and groups each symbol with some list accessing code, using gensyms to step down the pattern while grouping the gensym bound object with all pairs depending on this gensym. So, for example,
(destruc '(a (b . c) . d) 'seq)
will result in
((a (list-ref seq 0)) ((#:g (list-ref seq 1)) (b (list-ref #:g 0)) (c (list-tail #:g 1))) (d (list-tail seq 2)))
This tree is then transformed via dbind-ex into a nested let
(let ((a (list-ref seq 0)) (#:g (list-ref seq 1)) (d (list-tail seq 2))) (let ((b (list-ref #:g 0)) (c (list-tail #:g 1))) body))
Note, that the destructuring procedures are local to this macro. This is necessary in Chicken for the macro to work, in particular in compiled code, unless you define them for-syntax, but that would implicitly export them. But since they are of no interest outside of the macro, local procedures are preferable.
Note further, that ir-macro-transformer does all the necessary renaming transparently behind the scene, even if the helpers where defined in another module. In particular, gseq needn't be a gensym.
And note, that Graham's code didn't check for seq's length, i.e. (dbind (a b) '(1 2 3) (list a b) would happily return '(1 2).
Graham's original code works on the sequence datatype, so vectors and strings are destructured as well. Sequences don't exist in Scheme, unless you import-for-syntax Felix' sequences egg. To make this module self-contained, I prefer to supply access-routines closed over a table, which provides sequence versions of list-ref and list-tail, the only sequence routines used by destruc above, as well as a sequence version of length, which is needed to do the length checks.
There are some features, which I would like to have and which are implemented as well. First wildcards, represented by the underscore symbol. It matches everything, but binds nothing. So it can appear multiple times in the same macro. Wildcard symbols are simply not collected in the destruc routine.
Second, non-symbol literals, which don't bind anything, of course, but match only expressions evaluating to themselves. This and the length checks are treated simply by pairing them as well with check-routines in destruc but separating the pairs with leading symbol from those with leading nil or literal in dbind-ex. The former are bound with lets as in Graham's code, the latter's cadrs being evaluated before the recursive call to dbind-ex.
The last feature missing is fenders, which is important in particular for bind-case and can easily be implemented with a where clause: A pattern matches successfully if only each pattern variable can be bound and the checks as well as the where clause are satisfied. If the where clause doesn't pass, the next pattern is tried in bind-case or a bind-exception is signalled in bind.
This version is implemented as a functor, so modules for different sequence types can be generated, generic sequences (cf. above) being the default (in the generated bindings module). But if you need only lists, you can use the generated list-bindings module and thus circumvent the method dispatch ...
The functor bind-functor
This functor of one module argument produces the bindings and the list-bindings module, depending on its module argument.
Argument modules must provide three procedures, len, ref and tail patterned after length, list-ref and list-tail.
bindings[procedure] (bindings sym ..)
documentation procedure. Shows the exported symbols and the syntax of such an exported symbol, respectively.
bind[syntax] (bind pat (where fender ...) .. seq xpr ....)
binds pattern variables of pat to subexpressions of seq and executes xpr .... in this context, provided all fenders return #t, if supplied.
bindable?[syntax] (bindable? pat (where fender ...) ..)
returns a unary predicate which checks, if its sequence argument matches the pattern argument, pat, of bindable? and passes all of its fenders (the syntax is slightly changed for consistency).
bind-case[syntax] (bind-case seq (pat (where fender ...) .. xpr ....) ....)
Matches seq against a series of patterns and executes the body of the first matching pattern satisfying fenders (if given).
bind-define[syntax] (bind-define pat (where fender ...) .. seq)
defines pattern variables of pat with values matching subexpressions of seq in one go
bind-set![syntax] (bind-set! pat (where fender ...) .. seq)
sets symbols of pat to corresponding subexpressions of seq
bind-lambda[syntax] (bind-lambda pat (where fender ...) .. xpr ....)
combination of lambda and bind, one pattern argument
bind-lambda*[syntax] (bind-lambda* pat (where fender ...) .. xpr ....)
combination of lambda and bind, multiple pattern arguments
bind-case-lambda[syntax] (bind-case-lambda (pat (where fender ...) .. xpr ....) ....)
Combination of bind-case and lambda with one pattern argument
bind-case-lambda*[syntax] (bind-case-lambda* (pat (where fender ...) .. xpr ....) ....)
Combination of bind-case and lambda with multiple pattern arguments
bind*[syntax] (bind* loop pat (where fender ...) .. seq xpr ....)
named version of bind. loop is bound to a one-parameter procedure, which can be used in the body xpr ....
bindrec[syntax] (bindrec pat (where fender ...) .. seq xpr ....)
bind pattern variables of pat to subsequences of seq recursively
bind-let[syntax] (bind-let loop .. ((pat (where fender ...) .. seq) ...) xpr ....)
like let, named and unnamed, but binds patterns to sequence templates. In the named case loop is bound to a one-parameter-procedure accessible in the body xpr ....
bind-let*[syntax] (bind-let* ((pat (where fender ...) .. seq) ...) xpr ....)
like let*, but binds patterns to sequence templates
bind-letrec[syntax] (bind-letrec ((pat (where fender ...) .. seq) ...) xpr ....)
like letrec, but binds patterns to sequence templates
bind/cc[syntax] (bind/cc cc xpr ....)
captures the current continuation in cc and executes xpr .... in this context.
bind-exception[procedure] (bind-exception loc msg . args)
generates a composite condition of type (exn bind) with location loc, message msg and arguments args.
bind-exception-handler[procedure] (bind-exception-handler var)
exception-handler to be passed to the parameter current-exception-handler
signal-bind-exception[procedure] (signal-bind-exception loc msg arg ...)
signals a bind-exception, can be used instead of error.
prints the contents of the table
bind-table-add![procedure] (bind-table-add! type? len ref tail)
adds a custom new list of sequence operators to the top of the table (in previous versions of the library named seq-length-ref-tail!)
symbol-dispatcher[procedure] (symbol-dispatcher alist)
creates a documentation procedure as used in all modules of this library.
list-of[procedure] (list-of ok? ...)
returns a predicate which checks, if its argument is a list which passes every predicate ok? ...
pseudo-list-of[procedure] (pseudo-list-of ok? ...)
returns a predicate which checks, if its argument is a pseudo-list, which passes every predicate ok? ...
vector-of[procedure] (vector-of ok? ...)
returns a predicate which checks, if its argument is a vector which passes every predicate ok? ...
The module bind-sequences
This is a helper module, which is used to supply an argument to the bind-functor to produce the bindings module. Additional procedures are reexported from the functor.
The three routines below provide access to generic sequences, i.e. lists, pseudo-lists, vectors and strings. Additional sequence types like arrays or finite lazy-lists can be added to the internal dispatch table by means of the reexported bind-table-add! routine above.
bind-seq-length[procedure] (bind-seq-length seq)
returns the length of the generic sequence seq (previously named seq-length)
bind-seq-ref[procedure] (bind-seq-ref seq n)
returns the nth item of the generic sequence seq (preveiously named seq-ref)
bind-seq-tail[procedure] (bind-seq-tail seq n)
returns the tail of the generic sequence seq, starting at n (previously named seq-tail)
The modules bindings and list-bindings
These modules are generated by the bind-functor.
(require-library bindings) (import bindings) ;; alternatively, if you want to destructure lists only ;; (import list-bindings)
(use bindings) (let ((stack #f) (push! #f) (pop! #f)) (bind-set! (stack (push! pop!)) (list '() (vector (lambda (xpr) (set! stack (cons xpr stack))) (lambda () (set! stack (cdr stack)))))) (push! 1) (push! 0) stack) ; -> '(0 1) (begin (bind-define (top push! pop!) (let ((lst '())) (vector (lambda () (car lst)) (lambda (xpr) (set! lst (cons xpr lst))) (lambda () (set! lst (cdr lst)))))) (push! 0) (push! 1) (pop!) (top)) ; -> 0 (bind a 1 a) ; -> 1 (bind (x y z w) '(1 2 3 4) (list x y z w)) ; -> '(1 2 3 4) (bind (x . y) '#(1 2 3 4) (list x y)) ; -> '(1 #(2 3 4)) (bind (x (y (z u . v)) w) '(1 #(2 "foo") 4) (list x y z u v w)) ; -> '(1 2 #\f #\o "o" 4) (bind (x (y (z . u)) v . w) (vector 1 (list 2 (cons 3 4)) 5 6) (list x y z u v w)) ; -> '(1 2 3 4 5 #(6)) ((bind-lambda (a (b . C) . d) (list a b C d)) '(1 #(20 30 40) 2 3)) ; -> '(1 20 #(30 40) (2 3)) ((bind-lambda* ((a (b . C) . d) (e . f)) (list a b C d e f)) '(1 #(20 30 40) 2 3) '#(4 5 6)) ; -> '(1 20 #(30 40) (2 3) 4 #(5 6)) (bind* loop (x (a . b) y) '(5 #(1) 0) (if (zero? x) (list x a b y) (loop (list (- x 1) (cons a (cons a b)) (+ y 1))))) ; -> '(0 1 (1 1 1 1 1 . #()) 5) (bind* loop (x y) '(5 0) (if (zero? x) (vector x y) (loop (vector (- x 1) (+ y 1))))) ; -> '#(0 5) (bind-let (((x y (z . w)) '(1 2 #(3 4 5)))) (list x y z w)) ; -> '(1 2 3 #(4 5)) (bind-let ( (((x y) z) '(#(1 2) 3)) (u (+ 2 2)) ((v w) '#(5 6)) ) (list x y z u v w)) ; -> '(1 2 3 4 5 6) (bind-let loop (((a b) '(5 0))) (if (zero? a) (list a b) (loop (list (list (- a 1) (+ b 1)))))) ; -> '(0 5) (bind-let loop ( ((x . y) '(1 2 3)) ((z) '#(10)) ) (if (zero? z) (list x y z) (loop (list (cons (+ x 1) (map add1 y)) (list (- z 1)))))) ; -> '(11 (12 13) 0) (bind-let* ( (((x y) z) '(#(1 2) 3)) (u (+ 1 2 x)) ((v w) (list (+ z 2) 6)) ) (list x y z u v w)) ; -> '(1 2 3 4 5 6) (bindrec ((o?) e?) (vector (list (lambda (m) (if (zero? m) #f (e? (- m 1))))) (lambda (n) (if (zero? n) #t (o? (- n 1))))) (list (o? 95) (e? 95))) ; -> '(#t #f) (bind-letrec ( ((o? (e?)) (list (lambda (m) (if (zero? m) #f (e? (- m 1)))) (vector (lambda (n) (if (zero? n) #t (o? (- n 1))))))) ) (list (o? 95) (e? 95))) ; -> '(#t #f) ((bindable? ()) '()) ; -> #t ((bindable? (a (b C) . d)) '(1 (2 3) . 4)) ; -> #t ((bindable? (a (b C) . d)) '(1 #(2 3) 4 5)) ; -> #t ((bindable? (a (b . C) . d)) '(1 (2 3) 4)) ; -> #t ((bindable? (a (b . C) . d)) '#(1 2 3 4 5)) ; -> #f ((bindable? (a (b C) d)) '(1 (2 3) 4 5)) ; -> #f ((bindable? (a b) (even? a)) '#(1 2)) ; -> #f (bind-case '#(1 2) (() '()) ((a) (list a)) ((a b) (list a b)) ((a b C) (list a b C))) ; -> '(1 2)) (define (my-map fn lst) (bind-case lst (() '()) ((x . xs) (cons (fn x) (my-map fn xs))))) (my-map add1 '(1 2 3))) ; -> '(2 3 4) (define (vector-reverse vec) (let ((result (make-vector (vector-length vec) #f))) (let loop ((vec vec)) (bind-case vec (() result) ((x . xs) (vector-set! result (vector-length xs) x) (loop (subvector vec 1))))))) (vector-reverse #(0 1 2 3)) ; -> #(3 2 1 0) ((bind-case-lambda ((a (b . C) . d) (list a b C d)) ((e . f) (where (e zero?)) e) ((e . f) (list e f))) '(1 2 3 4 5)) ; -> '(1 (2 3 4 5))) ((bind-case-lambda ((e . f) (where (e zero?)) f) ((e . f) (list e f))) '#(0 2 3 4 5)) ;-> '#(2 3 4 5)) ((bind-case-lambda ((a (b . C) . d) (list a b C d)) ((e . f) (list e f))) '(1 #(2 3 4) 5 6)) ; -> '(1 2 #(3 4) (5 6)) ((bind-case-lambda* (((a b C . d) (e . f)) (list a b C d e f))) '(1 2 3) '#(4 5 6)) ; -> '(1 2 3 () 4 #(5 6)) ((bind-case-lambda* (((a (b . C) . d) (e . f)) (list a b C d e f))) '(1 #(20 30 40) 2 3) '(4 5 6)) ; -> '(1 20 #(30 40) (2 3) 4 (5 6)) ;;adding arrays to generic sequence table (bind-table-add! array? array-length (lambda (seq k) (array-item k seq)) (lambda (seq k) (array-drop k seq))) (bind (x y z) (array 1 2 3) (list x y z)) ;-> '(1 2 3) (bind (x (y z)) (vector 0 (array 1 2)) (list x y z)) ;-> '(0 1 2) (bind (x (y . z)) (vector 0 (array 1 2 3 4)) (list x y z)) ;-> '(0 1 @(2 3 4))
Jan 15, 2016
Copyright (c) 2011-2016, 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.
- functor implementation, literal rest parameter bug fixed
- macro-rules and friends moved to procedural-macros, where clauses now follow patterns
- code completely rewritten,simplified and reorganized, some small syntax changes
- code reorganized again to fix compile problems
- code reorganized
- let-macro and letrec-macro added, list-of? replaced by list-of to accept zero to many predicates
- where and key clauses now work together in macro-rules, moreover, macro-rules can be used in define-macro, so it needn't be imported for-syntax
- bindings library split in two; fixed a compile-time bug, thanks to John Foerch and Moritz Heidkamp
- wild-card processing added
- support for once formal parameters, rename-prefix clause moved to parameter, let-macro and letrec-macro changed to er-macro and renamed
- bugfix in define-er-macro, bind/cc added, three macros moved to macro-helpers
- library now includes low-level-macros
- all macros are low-level. Module depends on low-level-macros
- documentation procedure beautified
- generic functions rewritten, records removed from tests
- internal generic-pair? added again bugfixed
- exception-handler added, internal generic-pair? removed
- bind-case improved, format replaced by print
- code partially rewritten, syntax of bindable? changed, matching records outsourced to the tests
- renamed bind? to bindable?, bind/cc moved to list-bindings
- generics (and hence bind and friends) now accept records, bind/cc added
- bind-matches? and bind-loop changed to bindable? and bind*, where clauses and generic functions added, syms->vars removed
- all binding macros can now destructure arbitrary nested sequences, i.e mixtures of lists, pseudo-lists, vectors and strings; dependency on contracts removed.
- initial import