fbe-coord.scm 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. ;;; Copyright © 2016 Federico Beffa <beffa@fbengineering.ch>
  2. ;;;
  3. ;;; This program is free software; you can redistribute it and/or modify it
  4. ;;; under the terms of the GNU General Public License as published by
  5. ;;; the Free Software Foundation; either version 3 of the License, or (at
  6. ;;; your option) any later version.
  7. ;;;
  8. ;;; This program is distributed in the hope that it will be useful, but
  9. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;;; GNU General Public License for more details.
  12. ;;;
  13. ;;; You should have received a copy of the GNU General Public License
  14. ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Comments:
  16. ;;;
  17. ;;; This is a port to Guile of the equivalent macros found in the
  18. ;;; original MIT/GNU Scheme Scmutils library.
  19. ;; (define-values (v1 v2 v3 ...) values-expr)
  20. (define-syntax define-values
  21. (lambda (stx)
  22. (syntax-case stx ()
  23. ((_ (name ...) v-expr)
  24. (with-syntax (((t ...) (generate-temporaries #'(name ...))))
  25. #`(begin
  26. (define name) ...
  27. (call-with-values
  28. (lambda () v-expr)
  29. (lambda (t ...)
  30. (set! name t) ...))))))))
  31. ;; Use:
  32. ;; (define-coordinates (up x y z (down p q) ...) coord-sys)
  33. (define-syntax define-coordinates
  34. (lambda (stx)
  35. (syntax-case stx ()
  36. ((k coord-proto-symb coord-sys-expr)
  37. (identifier? #'coord-proto-symb)
  38. (with-syntax (((coord-sys) (generate-temporaries #'(coord-sys-expr)))
  39. (coord-vector-sym
  40. (datum->syntax #'k
  41. (symbol-append
  42. 'd/d
  43. (syntax->datum #'coord-proto-symb))))
  44. (coord-one-form-sym
  45. (datum->syntax #'k
  46. (symbol-append
  47. 'd
  48. (syntax->datum #'coord-proto-symb)))))
  49. #'(begin
  50. (define-values (coord-proto-symb coord-vector-sym coord-one-form-sym)
  51. (let ((coord-sys coord-sys-expr))
  52. ((coord-sys 'set-coordinate-prototype!) 'coord-proto-symb)
  53. (let ((chart-functions
  54. (list
  55. (cadar (ultra-flatten
  56. (coord-sys 'coordinate-function-specs)))
  57. (cadar (ultra-flatten
  58. (coord-sys 'coordinate-basis-vector-field-specs)))
  59. (cadar (ultra-flatten
  60. (coord-sys 'coordinate-basis-1form-field-specs))))))
  61. (apply values chart-functions)))))))
  62. ((k (up/down coord-proto-symb ...) coord-sys-expr)
  63. (with-syntax (((coord-sys)
  64. (generate-temporaries #'(coord-sys-expr)))
  65. ((coord-vector-sym ...)
  66. (map (lambda (cs)
  67. (datum->syntax #'k
  68. (symbol-append
  69. 'd/d
  70. (syntax->datum cs))))
  71. #'(coord-proto-symb ...)))
  72. ((coord-one-form-sym ...)
  73. (map (lambda (cs)
  74. (datum->syntax #'k
  75. (symbol-append
  76. 'd
  77. (syntax->datum cs))))
  78. #'(coord-proto-symb ...)))
  79. )
  80. #'(begin
  81. (define-values (coord-proto-symb ...
  82. coord-vector-sym ...
  83. coord-one-form-sym ...)
  84. (let ((coord-sys coord-sys-expr))
  85. ((coord-sys 'set-coordinate-prototype!)
  86. (up/down 'coord-proto-symb ...))
  87. (let ((chart-functions
  88. (append
  89. (map cadr
  90. (ultra-flatten
  91. (coord-sys 'coordinate-function-specs)))
  92. (map cadr
  93. (ultra-flatten
  94. (coord-sys 'coordinate-basis-vector-field-specs)))
  95. (map cadr
  96. (ultra-flatten
  97. (coord-sys 'coordinate-basis-1form-field-specs))))))
  98. (apply values chart-functions))))))))))
  99. ;; (using-coordinates (up x y) R2-rect
  100. ;; (pec (x ((R2-rect '->point) (up 'a 'b))))
  101. (define-syntax using-coordinates
  102. (lambda (stx)
  103. (syntax-case stx ()
  104. ((_ (coord ...) coord-sys-expr body ...)
  105. #'(let ()
  106. (define-coordinates (coord ...) coord-sys-expr)
  107. body ...)))))