comcon.scm 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #| -*-Scheme-*-
  2. Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
  3. 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
  4. 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Massachusetts
  5. Institute of Technology
  6. This file is part of MIT/GNU Scheme.
  7. MIT/GNU Scheme is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11. MIT/GNU Scheme is distributed in the hope that it will be useful, but
  12. WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with MIT/GNU Scheme; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
  18. USA.
  19. |#
  20. ;;;; COMCON.SCM
  21. (declare (usual-integrations))
  22. ;;; Useful utilities for programs that construct SCHEME programs.
  23. ;;; Needs UTILS.SCM
  24. (define (lambdafy n body-generator)
  25. (cond ((exact-integer? n)
  26. (let ((bvl (make-bound-variables n)))
  27. `(lambda ,bvl ,(expression (g:apply body-generator bvl)))))
  28. ((list? n)
  29. (let llp ((n n) (body-generator body-generator))
  30. (if (null? (cdr n))
  31. (let ((bvl (make-bound-variables (car n))))
  32. `(lambda ,bvl ,(expression (g:apply body-generator bvl))))
  33. (let ((bvl (make-bound-variables (car n))))
  34. `(lambda ,bvl
  35. ,(llp (cdr n) (g:apply body-generator bvl)))))))
  36. ((and (pair? n)
  37. (exact-integer? (car n))
  38. (exact-integer? (cdr n))
  39. (fix:= (car n) (cdr n)))
  40. (lambdafy (car n) body-generator))
  41. ((pair? n)
  42. ;; In Scheme 7.5 #f=() so (3) and (3 . #f) are not distinguished.
  43. (error "General arity is unimplemented -- LAMBDAFY"
  44. n))
  45. (else
  46. (error "Bad variable specification -- LAMBDAFY"
  47. n))))
  48. (define (make-bound-variables n)
  49. ;;n is a general arity
  50. (let do-loop ((i 0) (names '()))
  51. (if (fix:= i n)
  52. names
  53. (do-loop (fix:1+ i)
  54. (cons (generate-uninterned-symbol 'x) names)))))
  55. (define (letify vals body-generator)
  56. (if (null? vals)
  57. (body-generator '())
  58. (let ((names (map (lambda (x) (generate-uninterned-symbol 'y)) vals)))
  59. `(let ,(map list names vals) ,(body-generator names)))))
  60. (define (definify name definition-expression)
  61. (if (pair? definition-expression)
  62. (if (eq? (car definition-expression) 'lambda)
  63. `(define (,name . ,(cadr definition-expression))
  64. . ,(cddr definition-expression))
  65. `(define ,name ,definition-expression))
  66. `(define ,name ,definition-expression)))