coord.scm 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. ;; (define-values (v1 v2 v3 ...) values-expr)
  21. (define-syntax define-values
  22. (er-macro-transformer
  23. (lambda (exp r c)
  24. (let* ((names (cadr exp))
  25. (values-expr (caddr exp))
  26. (temps (map generate-uninterned-symbol names)))
  27. `(,(r 'begin)
  28. ,@(map (lambda (name)
  29. `(,(r 'define) ,name))
  30. names)
  31. (,(r 'call-with-values)
  32. (,(r 'lambda) () ,values-expr)
  33. (,(r 'lambda) ,temps
  34. ,@(map (lambda (name temp)
  35. `(,(r 'set!) ,name ,temp))
  36. names temps))))))))
  37. ;; Use:
  38. ;; (define-coordinates (up x y z (down p q) ...) coord-sys)
  39. ;; Should expand into
  40. ;;(begin
  41. ;; (define x <x-coordinate-fn>)
  42. ;; ...
  43. ;; (define d/dx <x-basis-vector>)
  44. ;; ...
  45. ;; (define dx <x-basis-one-form>))
  46. (define-syntax define-coordinates
  47. (er-macro-transformer
  48. (lambda (e r c)
  49. (define (quote-symbol-names symbs)
  50. (cond
  51. ((and (pair? symbs)
  52. (memq (car symbs) '(up down)))
  53. `(,(car symbs) ,@(map quote-symbol-names (cdr symbs))))
  54. ((symbol? symbs)
  55. `(quote ,symbs))
  56. (else
  57. (error "bad coordinate prototype" symbs))))
  58. (define (get-symbol-names symbs)
  59. (cond
  60. ((and (pair? symbs)
  61. (memq (car symbs) '(up down)))
  62. (apply append
  63. (map get-symbol-names
  64. (cdr symbs))))
  65. ((symbol? symbs) (list symbs))
  66. (else (error "bad coordinate prototype" symbs))))
  67. (let ((coord-proto-symbs (cadr e))
  68. (coord-proto (quote-symbol-names (cadr e)))
  69. (coord-sys-expr (caddr e))
  70. (coord-sys (generate-uninterned-symbol 'coord-sys))
  71. (chart-functions (generate-uninterned-symbol 'chart-fn))
  72. (proto (generate-uninterned-symbol 'coord-proto)))
  73. (let* ((coord-symbs (get-symbol-names coord-proto-symbs))
  74. (coord-vector-syms
  75. (map (lambda (sym) (symbol 'd/d sym)) coord-symbs))
  76. (coord-one-form-syms
  77. (map (lambda (sym) (symbol 'd sym)) coord-symbs)))
  78. `(,(r 'begin)
  79. (,(r 'define-values)
  80. ,(append coord-symbs coord-vector-syms coord-one-form-syms)
  81. (,(r 'let) ((,coord-sys ,coord-sys-expr)
  82. (,proto ,coord-proto))
  83. ((,coord-sys 'set-coordinate-prototype!) ,proto)
  84. (,(r 'let) ((,chart-functions
  85. (,(r 'append)
  86. (,(r 'map) ,(r 'cadr)
  87. (,(r 'ultra-flatten)
  88. (,coord-sys 'coordinate-function-specs)))
  89. (,(r 'map) ,(r 'cadr)
  90. (,(r 'ultra-flatten)
  91. (,coord-sys 'coordinate-basis-vector-field-specs)))
  92. (,(r 'map) ,(r 'cadr)
  93. (,(r 'ultra-flatten)
  94. (,coord-sys 'coordinate-basis-1form-field-specs))))))
  95. (,(r 'apply) ,(r 'values) ,chart-functions))))))))))
  96. #|
  97. (pec
  98. (let ()
  99. (define-coordinates (up x y) R2-rect)
  100. (x ((R2-rect '->point) (up 'a 'b)))))
  101. #| Result:
  102. a
  103. |#
  104. (pec
  105. (let ()
  106. (define-coordinates (up x y) R2-rect)
  107. ((d/dx x) ((R2-rect '->point) (up 'a 'b)))))
  108. #| Result:
  109. 1
  110. |#
  111. (pec
  112. (let ()
  113. (define-coordinates (up p q) R2-rect)
  114. (R2-rect 'coordinate-function-specs)))
  115. #| Result:
  116. (up (p (??? x)) (q (??? x)))
  117. |#
  118. |#
  119. (define-syntax using-coordinates
  120. (er-macro-transformer
  121. (lambda (x r c)
  122. (let ((coord-proto (cadr x))
  123. (coord-sys-expr (caddr x))
  124. (body (cdddr x)))
  125. `(,(r 'let) ()
  126. (,(r 'define-coordinates) ,coord-proto ,coord-sys-expr)
  127. ,@body)))))
  128. #|
  129. (using-coordinates (up x y) R2-rect
  130. (pec (x ((R2-rect '->point) (up 'a 'b)))))
  131. #| Result:
  132. a
  133. |#
  134. (using-coordinates (up x y) R2-rect
  135. (pec ((dx d/dx) ((R2-polar '->point) (up 'a 'b)))))
  136. #| Result:
  137. 1
  138. |#
  139. |#
  140. #|
  141. ;; Global definitions and shadowing:
  142. (define-coordinates (up x y) R2-rect)
  143. (pe (x ((R2-polar '->point) (up 'r 'theta))))
  144. (* r (cos theta))
  145. (using-coordinates (up x y) R2-polar ;; funky
  146. (pe (x ((R2-rect '->point) (up 'a 'b)))))
  147. (sqrt (+ (expt a 2) (expt b 2)))
  148. (pe (x ((R2-rect '->point) (up 'a 'b))))
  149. a
  150. |#