ck.scm 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. ;;; ck, to facilitate applicative-order macro programming
  2. ;;; Copyright (C) 2012 Free Software Foundation, Inc
  3. ;;; Copyright (C) 2009, 2011 Oleg Kiselyov
  4. ;;;
  5. ;;; This library is free software; you can redistribute it and/or
  6. ;;; modify it under the terms of the GNU Lesser General Public
  7. ;;; License as published by the Free Software Foundation; either
  8. ;;; version 3 of the License, or (at your option) any later version.
  9. ;;;
  10. ;;; This library is distributed in the hope that it will be useful,
  11. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;;; Lesser General Public License for more details.
  14. ;;;
  15. ;;; You should have received a copy of the GNU Lesser General Public
  16. ;;; License along with this library; if not, write to the Free Software
  17. ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. ;;;
  19. ;;;
  20. ;;; Originally written by Oleg Kiselyov and later contributed to Guile.
  21. ;;;
  22. ;;; Based on the CK machine introduced in:
  23. ;;;
  24. ;;; Matthias Felleisen and Daniel P. Friedman: Control operators, the
  25. ;;; SECD machine, and the lambda-calculus. In Martin Wirsing, editor,
  26. ;;; Formal Description of Programming Concepts III, pages
  27. ;;; 193-217. Elsevier, Amsterdam, 1986.
  28. ;;;
  29. ;;; See http://okmij.org/ftp/Scheme/macros.html#ck-macros for details.
  30. ;;;
  31. (define-module (system base ck)
  32. #:export (ck))
  33. (define-syntax ck
  34. (syntax-rules (quote)
  35. ((ck () 'v) v) ; yield the value on empty stack
  36. ((ck (((op ...) ea ...) . s) 'v) ; re-focus on the other argument, ea
  37. (ck-arg s (op ... 'v) ea ...))
  38. ((ck s (op ea ...)) ; Focus: handling an application;
  39. (ck-arg s (op) ea ...)))) ; check if args are values
  40. (define-syntax ck-arg
  41. (syntax-rules (quote)
  42. ((ck-arg s (op va ...)) ; all arguments are evaluated,
  43. (op s va ...)) ; do the redex
  44. ((ck-arg s (op ...) 'v ea1 ...) ; optimization when the first ea
  45. (ck-arg s (op ... 'v) ea1 ...)) ; was already a value
  46. ((ck-arg s (op ...) ea ea1 ...) ; focus on ea, to evaluate it
  47. (ck (((op ...) ea1 ...) . s) ea))))