default.scm 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. ;;;; Simplifier interface
  21. ;;; g:simplify expects typed expressions. produces untyped expressions
  22. ;;; Needs hashtable, record, etc.
  23. (declare (usual-integrations))
  24. (define (default-simplify expr)
  25. (define (bad? expr)
  26. (or (boolean? expr)
  27. (null? expr)
  28. ;;(pathname? expr) ; FBE
  29. (undefined-value? expr)
  30. (and (pair? expr)
  31. (not (list? expr)))))
  32. (cond ((list? expr)
  33. (let ((subs
  34. (map (lambda (x) (g:simplify x))
  35. expr)))
  36. (if (not (rexists bad? subs))
  37. (new-simplify subs)
  38. subs)))
  39. ((pair? expr)
  40. (cons (g:simplify (car expr))
  41. (g:simplify (cdr expr))))
  42. (else expr)))
  43. (define g:simplify
  44. (make-generic-operator 1 'simplify default-simplify))
  45. #|
  46. (define (simplify-undefined expr) '*undefined-value*)
  47. (assign-operation 'simplify simplify-undefined undefined-value?)
  48. |#
  49. (define %simplify-default-dummy-1
  50. (begin
  51. ;;; There are no simplifiers yet for compound abstract types.
  52. ;(assign-operation 'simplify expression abstract-vector?)
  53. (assign-operation 'simplify expression abstract-up?)
  54. (assign-operation 'simplify expression abstract-down?)
  55. (assign-operation 'simplify expression abstract-matrix?)
  56. ;;; Series cannot be simplified except term by term.
  57. (assign-operation 'simplify identity series?)
  58. ;;; The following simplify to themselves.
  59. (assign-operation 'simplify identity number?)
  60. (assign-operation 'simplify identity symbol?)
  61. (assign-operation 'simplify identity null?)
  62. (assign-operation 'simplify identity boolean?)
  63. ;;(assign-operation 'simplify identity pathname?) ; FBE
  64. (assign-operation 'simplify identity undefined-value?)))
  65. ;;; Here we have notrivial simplification
  66. #|
  67. (define (simplify-with-units num)
  68. (let ((value (g:* (unit-scale (u:units num)) (u:value num)))
  69. (vect (unit-exponents (u:units num)))
  70. (system (environment-lookup scmutils-base-environment
  71. (unit-system (u:units num)))))
  72. (make-unit-description (g:simplify value) vect system)))
  73. (assign-operation 'simplify simplify-with-units with-units?)
  74. |#
  75. (define (simplify-units num)
  76. (let ((system (environment-lookup scmutils-base-environment
  77. (unit-system (u:units num)))))
  78. (with-units->expression system num)))
  79. (define %simplify-default-dummy-2
  80. (begin
  81. (assign-operation 'simplify simplify-units with-units?)
  82. (assign-operation 'simplify simplify-units units?)))
  83. ;;; This must be the first handler (last in generic table)
  84. ;;; that triggers on PROCEDURE? because it is default for
  85. ;;; procedures. Operators and abstract functions must
  86. ;;; be checked first.
  87. (define (simplify-procedure expr)
  88. (procedure-expression expr))
  89. (define %simplify-default-dummy-3
  90. (assign-operation 'simplify simplify-procedure procedure?))
  91. (define (simplify-abstract-function expr)
  92. (g:simplify (f:expression expr)))
  93. (define %simplify-default-dummy-4
  94. (assign-operation 'simplify simplify-abstract-function abstract-function?))
  95. (define (simplify-operator expr)
  96. (g:simplify (operator-name expr)))
  97. (define %simplify-default-dummy-5
  98. (assign-operation 'simplify simplify-operator operator?))
  99. (define (simplify-quaternion expr)
  100. (cons 'quaternion
  101. (vector->list
  102. ((vector-elementwise g:simplify) (cadr expr)))))
  103. (define %simplify-default-dummy-6
  104. (assign-operation 'simplify simplify-quaternion quaternion?))
  105. (define (simplify-matrix expr)
  106. `(matrix-by-rows
  107. ,@(map (lambda (r)
  108. (cons 'list (vector->list r)))
  109. (vector->list
  110. (matrix->array ((m:elementwise g:simplify) expr))))))
  111. (define %simplify-default-dummy-7
  112. (assign-operation 'simplify simplify-matrix matrix?))
  113. (define (simplify-differential expr)
  114. `(make-differential-quantity
  115. (list ,@(map (lambda (term)
  116. `(make-differential-term
  117. ',(differential-tags term)
  118. ,(g:simplify (differential-coefficient term))))
  119. (differential-term-list expr)))))
  120. (define %simplify-default-dummy-8
  121. (assign-operation 'simplify simplify-differential differential?))
  122. (define (simplify-down expr)
  123. (cons down-constructor-name
  124. (let lp ((i 0))
  125. (if (fix:= i (s:length expr))
  126. '()
  127. (cons (g:simplify (s:ref expr i))
  128. (lp (fix:+ i 1)))))))
  129. (define %simplify-default-dummy-9
  130. (assign-operation 'simplify simplify-down down?))
  131. (define (simplify-up expr)
  132. (cons up-constructor-name
  133. (let lp ((i 0))
  134. (if (fix:= i (s:length expr))
  135. '()
  136. (cons (g:simplify (s:ref expr i))
  137. (lp (fix:+ i 1)))))))
  138. (define %simplify-default-dummy-10
  139. (assign-operation 'simplify simplify-up up?))
  140. ;;; Not quite right... Should only expressionize
  141. ;;; and simplify compound arguments to literal-function
  142. ;;; subexpressions.
  143. (define (simplify-literal-number expr)
  144. (new-simplify (expression expr)))
  145. (define %simplify-default-dummy-11
  146. (assign-operation 'simplify simplify-literal-number literal-number?))