; Author: Juergen Lorenz ; ju (at) jugilo (dot) de ; ; Copyright (c) 2015-2019, 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 dispasser. ; ; Redistributions in binary form must reproduce the above copyright ; notice, this list of conditions and the following dispasser 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. #|[ Besides the documentation procedure, this module exports three macros, local, local* and localrec, in parallel to the standard macros let, let* and letrec. Contrary to the latter, the body of locals consists of definitions, not expressions. So, one is able to define procedures with a common state, something what define-values can do as well. ]|# (module locals (locals local local* localrec) (import scheme (only (chicken base) print case-lambda error)) ;;; (local ((x a) ...) (define y val) (define (z . args) body) ...) ;;; -> ;;; (begin (define y #f) (define z #f) ... ;;; (let ((x a) ...) ;;; (set! y val) ;;; (set! z (lambda args body) ;;; ...))) (define-syntax %local ; internal (er-macro-transformer (lambda (form rename compare?) (let ((letter (cadr form)) (locals (caddr form)) (globals (cdddr form)) (%set! (rename 'set!)) (%begin (rename 'begin)) (%define (rename 'define)) (%lambda (rename 'lambda)) (filter (lambda (ok? xs) (let recur ((xs xs)) (cond ((null? xs) xs) ((ok? (car xs)) (cons (car xs) (recur (cdr xs)))) (else (recur (cdr xs))))))) (define? (lambda (p) (and (pair? p) (compare? (car p) (rename 'define))))) (local? (lambda (p) (and (pair? p) (compare? (car p) (rename 'local))))) (local*? (lambda (p) (and (pair? p) (compare? (car p) (rename 'local*))))) (localrec? (lambda (p) (and (pair? p) (compare? (car p) (rename 'localrec))))) ) (let ((defs (filter define? globals)) (locs (filter local? globals)) (def->pair (lambda (def) (if (pair? (cadr def)) (let ((name (caadr def)) (args (cdadr def))) `(,name (,%lambda ,args ,(caddr def) ,@(cdddr def)))) (cdr def)))) (pairs->defs (lambda (ps) (map (lambda (p) `(,%define ,(car p) #f)) ps))) (pairs->sets (lambda (ps) (map (lambda (p) `(,%set! ,(car p) ,(cadr p))) ps))) ) (let ((defpairs (map def->pair defs))) `(,%begin ,@(pairs->defs defpairs) (,letter ,locals ,@(pairs->sets defpairs))))) )))) (define-syntax local (syntax-rules () ((_ locals def def1 ...) (%local let locals def def1 ...)))) (define-syntax local* (syntax-rules () ((_ locals def def1 ...) (%local let* locals def def1 ...)))) (define-syntax localrec (syntax-rules () ((_ locals def def1 ...) (%local letrec locals def def1 ...)))) ;;; (locals [sym]) ;;; ------------- ;;; documentation procderue (define locals (let ((als '( (locals procedure: (locals sym ..) "documentation procedure") (local macro: (local ((x x-val) ...) def....) "exports definitions def...., which all depend on a" "common state x ..." "The state variables x ... are bound in parallel.") (local* macro: (local* ((x x-val) ...) def....) "exports definitions def...., which all depend on a" "common state x ..." "The state variables x ... are bound sequentially.") (localrec macro: (localrec ((x x-val) ...) def....) "exports definitions def...., which all depend on a" "common state x ..." "The state variables x ... are bound recursively.") ))) (case-lambda (() (map car als)) ((sym) (let ((pair (assq sym als))) (if pair (for-each print (cdr pair)) (error "Not in list" sym (map car als)))))))) ) ; module locals