((section 2 "Outdated egg!" (p "This is an egg for CHICKEN 4, the unsupported old release.  You're almost certainly looking for " (int-link "/eggref/5/varsubst" "the CHICKEN 5 version of this egg") ", if it exists.") (p "If it does not exist, there may be equivalent functionality provided by another egg; have a look at the " (link "https://wiki.call-cc.org/chicken-projects/egg-index-5.html" "egg index") ". Otherwise, please consider porting this egg to the current version of CHICKEN.") (tags "egg")) (section 2 "varsubst" (section 3 "Introduction" (p "The " (tt "varsubst") " library provides support for variable substition in user-defined languages. It contains a collection of routines for managing substitution environments and a parameterized substitution procedure.")) (section 3 "Library procedures" (def (sig (procedure "subst?:: OBJECT -> BOOL" (id subst?))) (p "A predicate that returns true if the given argument is a substitution environment.")) (def (sig (procedure "subst-empty:: () -> SUBST" (id subst-empty))) (p "Returns an empty substitution environment.")) (def (sig (procedure "subst-empty?:: SUBST -> BOOL" (id subst-empty?))) (p "Returns true if the given argument is an empty substitution environment, false otherwise.")) (def (sig (procedure "subst-includes?:: K * SUBST -> BOOL" (id subst-includes?))) (p "Returns true if the given symbol " (tt "K") " is contained in the given substitution environment, false otherwise.")) (def (sig (procedure "subst-lookup:: K * SUBST -> TERM" (id subst-lookup))) (p "Looks up symbol " (tt "K") " in the given substitution environment, and returns the term associated with it.")) (def (sig (procedure "subst-extend:: K * V * SUBST -> SUBST" (id subst-extend))) (p "Adds the binding " (tt "K * V") " to the given substitution environment, and returns the resulting substitution environment.")) (def (sig (procedure "subst-remove:: K * SUBST -> SUBST" (id subst-remove))) (p "Removes all bindings with key " (tt "K") " from the given substitution environment and returns the resulting substitution environment.")) (def (sig (procedure "subst-map:: PROC * SUBST -> SUBST" (id subst-map))) (p "For each binding " (tt "K * V") " in the given substitution environment, applies procedure " (tt "PROC") " to " (tt "V") " and adds the new binding " (tt "K * (PROC V)") " to the environment. Returns the resulting substitution environment.")) (def (sig (procedure "subst-driver:: VAR? * BIND? * VAR-PROC * BIND-PROC * SUBST-PROC [* PREFIX] -> (LAMBDA T SUBST)" (id subst-driver))) (p "Generalized substitution procedure. Predicates " (tt "VAR?") " and " (tt "BIND?") " are used to determine if a given term is a variable or a binding, respectively. Procedure " (tt "VAR-PROC") " takes a symbol and creates a variable term. Procedure " (tt "SUBST-PROC") " substitues variables in the terms of the user-defined language. Optional variable " (tt "PREFIX") " specifies the prefix for fresh variable names."))) (section 3 "Example" (pre "(use syntax-case matchable varsubst)\n\n(define (subst-term t subst k)\n (match t\n (('if c t e)\n  `(if ,(k c subst) ,(k t subst) ,(k e subst)))\n (('let bs e)\n  (k `(let ,(map (lambda (b) `(,(car b) ,(k (cadr b) subst))) bs) ,e) subst))\n ((f . es)\n  (cons (k f subst) (map (lambda (e) (k e subst)) es)))\n ((? atom? ) t)))\n\n(define (binding? t) (and (list? t) (eq? 'let (car t)) (cdr t)))\n\n(define (bind ks vs e) `(let ,(zip ks vs) ,e))\n\n(define driver  (subst-driver (lambda (x) (and (symbol? x) x)) binding? identity bind subst-term))\n\n(print (driver `(let ((a 1) (b 2)) (+ a (let ((a (+ b 5))) (+ a b)))) subst-empty))")) (section 3 "Authors" (p "Ivan Raikov")) (section 3 "Version" (dl (dt "1.3") (dd "Added subst-remove") (dt "1.2") (dd "Ported to Chicken 4") (dt "1.1") (dd "Using string comparison because equal? on the same symbols sometimes fails.") (dt "1.0") (dd "Initial version"))) (section 3 "License" (pre "Copyright 2008-2012 Ivan Raikov and the Okinawa Institute of Science\nand Technology.\n\nThis program is free software: you can redistribute it and/or modify\nit under the terms of the GNU General Public License as published by\nthe Free Software Foundation, either version 3 of the License, or (at\nyour option) any later version.\n\nThis program is distributed in the hope that it will be useful, but\nWITHOUT ANY WARRANTY; without even the implied warranty of\nMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\nGeneral Public License for more details.\n\nA full copy of the GPL license can be found at\n<http://www.gnu.org/licenses/>."))))