types.scm 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. ;;;; This is needed to load particular types
  21. (declare (usual-integrations))
  22. (define (make-type type-tag abstract-type-tag
  23. quantity-predicate concrete-predicate abstract-predicate)
  24. (list type-tag abstract-type-tag
  25. quantity-predicate concrete-predicate abstract-predicate))
  26. (define (type-tag type)
  27. (car type))
  28. (define (abstract-type-tag type)
  29. (cadr type))
  30. (define (quantity-predicate type)
  31. (caddr type))
  32. (define (concrete-predicate type)
  33. (cadddr type))
  34. (define (abstract-predicate type)
  35. (car (cddddr type)))
  36. (define-integrable number-type-tag '*number*)
  37. (define-integrable with-units-type-tag '*with-units*)
  38. (define-integrable unit-type-tag '*unit*)
  39. (define-integrable vector-type-tag '*vector*)
  40. (define-integrable abstract-vector-type-tag '*vector*)
  41. (define quaternion-type-tag '*quaternion*)
  42. ;;; Up vectors are implemented as scheme vectors
  43. (define-integrable up-type-tag '*vector*)
  44. (define-integrable abstract-up-type-tag '*vector*)
  45. (define-integrable down-type-tag '*down*)
  46. (define-integrable abstract-down-type-tag '*abstract-down*)
  47. (define-integrable matrix-type-tag '*matrix*)
  48. (define-integrable abstract-matrix-type-tag '*abstract-matrix*)
  49. (define-integrable function-type-tag '*function*)
  50. (define-integrable abstract-function-type-tag '*function*)
  51. (define-integrable differential-type-tag '*diff*)
  52. (define operator-type-tag '*operator*)
  53. (define-integrable series-type-tag '*series*)
  54. (define type-tags
  55. (list number-type-tag
  56. unit-type-tag
  57. with-units-type-tag
  58. vector-type-tag
  59. quaternion-type-tag
  60. ;;abstract-vector-type-tag
  61. ;;up-type-tag
  62. ;;abstract-up-type-tag
  63. down-type-tag
  64. abstract-down-type-tag
  65. matrix-type-tag
  66. abstract-matrix-type-tag
  67. function-type-tag
  68. differential-type-tag
  69. operator-type-tag
  70. series-type-tag))
  71. (define compound-type-tags
  72. (list vector-type-tag
  73. ;;up-type-tag
  74. quaternion-type-tag
  75. down-type-tag
  76. matrix-type-tag
  77. series-type-tag
  78. abstract-matrix-type-tag))
  79. (define abstract-type-tags
  80. (list number-type-tag
  81. vector-type-tag
  82. ;;abstract-vector-type-tag
  83. ;;abstract-up-type-tag
  84. abstract-down-type-tag
  85. abstract-matrix-type-tag))
  86. (define (abstract-quantity? x)
  87. (memq (g:type x) abstract-type-tags))
  88. ;;; NUMBER? is defined by Scheme system
  89. (define (abstract-number? x)
  90. (or (literal-number? x)
  91. (symbol? x)))
  92. (define (literal-number? x)
  93. (and (pair? x)
  94. (eq? (car x) number-type-tag)))
  95. (define (literal-real? x)
  96. (and (literal-number? x)
  97. ((has-property? 'real) x)))
  98. (define (numerical-quantity? x)
  99. (or (number? x)
  100. (abstract-number? x)
  101. (and (differential? x)
  102. (numerical-quantity? (differential-of x)))
  103. (and (with-units? x)
  104. (numerical-quantity? (u:value x)))))
  105. (define (with-units? x)
  106. (and (pair? x)
  107. (eq? (car x) with-units-type-tag)))
  108. (define (units? x)
  109. (or (eq? x '&unitless)
  110. (and (pair? x)
  111. (eq? (car x) unit-type-tag))))
  112. (define *number*
  113. (make-type '*number* '*number* numerical-quantity? number? abstract-number?))
  114. (define (compound-type-tag? x)
  115. ;; Will need to add tensors, etc.
  116. (memq x compound-type-tags))
  117. (define (not-compound? x)
  118. (not (or (vector? x)
  119. (and (pair? x)
  120. (compound-type-tag? (car x))))))
  121. (define (scalar? x)
  122. (not (or (vector? x)
  123. (and (pair? x)
  124. (compound-type-tag? (car x)))
  125. (function? x))))
  126. ;;; Scheme vectors are used to represent concrete vectors.
  127. ;;; VECTOR? is defined by Scheme system
  128. (define (abstract-vector? x)
  129. (and (pair? x)
  130. (eq? (car x) vector-type-tag)))
  131. (define (vector-quantity? v)
  132. (or (vector? v)
  133. (abstract-vector? v)
  134. (and (differential? v)
  135. (vector-quantity? (differential-of v)))))
  136. (define *vector*
  137. (make-type vector-type-tag
  138. abstract-vector-type-tag
  139. vector-quantity? vector? abstract-vector?))
  140. (define (quaternion? v)
  141. (and (pair? v)
  142. (eq? (car v) quaternion-type-tag)))
  143. (define (quaternion-quantity? v)
  144. (quaternion? v))
  145. (define (up? x)
  146. ;;(and (pair? x) (eq? (car x) up-type-tag))
  147. (vector? x))
  148. (define (abstract-up? x)
  149. (and (pair? x) (eq? (car x) abstract-up-type-tag)))
  150. (define (up-quantity? v)
  151. (or (up? v)
  152. (abstract-up? v)
  153. (and (differential? v)
  154. (up-quantity? (differential-of v)))))
  155. (define *up*
  156. (make-type up-type-tag
  157. abstract-up-type-tag
  158. vector-quantity? up? abstract-up?))
  159. (define (down? x)
  160. (and (pair? x)
  161. (eq? (car x) down-type-tag)))
  162. (define (abstract-down? x)
  163. (and (pair? x) (eq? (car x) abstract-down-type-tag)))
  164. (define (down-quantity? v)
  165. (or (down? v)
  166. (abstract-down? v)
  167. (and (differential? v)
  168. (down-quantity? (differential-of v)))))
  169. (define *down*
  170. (make-type
  171. down-type-tag
  172. abstract-down-type-tag
  173. down-quantity? down? abstract-down?))
  174. (define (structure? x)
  175. (or (up? x) (down? x)))
  176. (define (abstract-structure? x)
  177. (or (abstract-up? x) (abstract-down? x)))
  178. (define (matrix? m)
  179. (and (pair? m)
  180. (eq? (car m) matrix-type-tag)))
  181. (define (matrix-quantity? m)
  182. (or (matrix? m)
  183. (abstract-matrix? m)
  184. (and (differential? m)
  185. (matrix-quantity? (differential-of m)))))
  186. (define (abstract-matrix? m)
  187. (and (pair? m)
  188. (eq? (car m) abstract-matrix-type-tag)))
  189. (define *matrix*
  190. (make-type matrix-type-tag
  191. abstract-matrix-type-tag
  192. matrix-quantity? matrix? abstract-matrix?))
  193. (define (square-matrix? matrix)
  194. (and (matrix? matrix)
  195. (fix:= (caadr matrix) (cdadr matrix))))
  196. (define (square-abstract-matrix? matrix)
  197. (and (pair? matrix)
  198. (eq? (car matrix) abstract-matrix-type-tag)
  199. ((has-property? 'square) matrix)))
  200. (define (operator? x)
  201. (and (apply-hook? x)
  202. (eq? (car (apply-hook-extra x))
  203. operator-type-tag)))
  204. (define (not-operator? x)
  205. (not (operator? x)))
  206. (define (function-quantity? f)
  207. (procedure? f)) ;apply hooks are procedures.
  208. (define (function? f)
  209. (and (procedure? f)
  210. (not (operator? f))))
  211. (define (cofunction? f) ;may be combined with a function
  212. (not (operator? f)))
  213. (define (abstract-function? f)
  214. (and (typed-or-abstract-function? f)
  215. (f:expression f)))
  216. (define (typed-function? f)
  217. (and (typed-or-abstract-function? f)
  218. (not (f:expression f))))
  219. (define (typed-or-abstract-function? f)
  220. (and (apply-hook? f)
  221. (eq? (car (apply-hook-extra f))
  222. function-type-tag)))
  223. (define *function*
  224. (make-type function-type-tag
  225. abstract-function-type-tag
  226. function-quantity? function? abstract-function?))
  227. (define (differential? obj)
  228. (and (pair? obj)
  229. (eq? (car obj) differential-type-tag)))
  230. (define (not-differential? obj)
  231. (not (differential? obj)))
  232. (define (series? s)
  233. (and (pair? s)
  234. (eq? (car s) series-type-tag)))
  235. (define (not-series? s)
  236. (not (series? s)))
  237. (define (not-differential-or-compound? x)
  238. (not (or (vector? x)
  239. (and (pair? x)
  240. (or (compound-type-tag? (car x))
  241. (eq? (car x) differential-type-tag))))))
  242. (define (not-d-c-u? x)
  243. (not (or (eq? x '&unitless)
  244. (vector? x)
  245. (and (pair? x)
  246. (or (compound-type-tag? (car x))
  247. (eq? (car x) differential-type-tag)
  248. (eq? (car x) with-units-type-tag)
  249. (eq? (car x) unit-type-tag))))))