home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / gnu / info / calc.info-21 (.txt) < prev    next >
GNU Info File  |  1994-12-22  |  50KB  |  841 lines

  1. This is Info file calc.info, produced by Makeinfo-1.55 from the input
  2. file calc.texinfo.
  3.    This file documents Calc, the GNU Emacs calculator.
  4.    Copyright (C) 1990, 1991 Free Software Foundation, Inc.
  5.    Permission is granted to make and distribute verbatim copies of this
  6. manual provided the copyright notice and this permission notice are
  7. preserved on all copies.
  8.    Permission is granted to copy and distribute modified versions of
  9. this manual under the conditions for verbatim copying, provided also
  10. that the section entitled "GNU General Public License" is included
  11. exactly as in the original, and provided that the entire resulting
  12. derived work is distributed under the terms of a permission notice
  13. identical to this one.
  14.    Permission is granted to copy and distribute translations of this
  15. manual into another language, under the above conditions for modified
  16. versions, except that the section entitled "GNU General Public License"
  17. may be included in a translation approved by the author instead of in
  18. the original English.
  19. File: calc.info,  Node: Conditional Rewrite Rules,  Next: Algebraic Properties of Rewrite Rules,  Prev: Basic Rewrite Rules,  Up: Rewrite Rules
  20. Conditional Rewrite Rules
  21. -------------------------
  22. A rewrite rule can also be "conditional", written in the form `OLD :=
  23. NEW :: COND'.  (There is also the obsolete form `[OLD, NEW, COND]'.)
  24. If a COND part is present in the rule, this is an additional condition
  25. that must be satisfied before the rule is accepted.  Once OLD has been
  26. successfully matched to the target expression, COND is evaluated (with
  27. all the meta-variables substituted for the values they matched) and
  28. simplified with `a s' (`calc-simplify').  If the result is a nonzero
  29. number or any other object known to be nonzero (*note Declarations::.),
  30. the rule is accepted.  If the result is zero or if it is a symbolic
  31. formula that is not known to be nonzero, the rule is rejected.  *Note
  32. Logical Operations::, for a number of functions that return 1 or 0
  33. according to the results of various tests.
  34.    For example, the formula `n > 0' simplifies to 1 or 0 if `n' is
  35. replaced by a positive or nonpositive number, respectively (or if `n'
  36. has been declared to be positive or nonpositive).  Thus, the rule
  37. `f(x,y) := g(y+x,x) :: x+y > 0' would apply to `f(0, 4)' but not to
  38. `f(-3, 2)' or `f(12, a+1)' (assuming no outstanding declarations for
  39. `a').  In the case of `f(-3, 2)', the condition can be shown not to be
  40. satisfied; in the case of `f(12, a+1)', the condition merely cannot be
  41. shown to be satisfied, but that is enough to reject the rule.
  42.    While Calc will use declarations to reason about variables in the
  43. formula being rewritten, declarations do not apply to meta-variables.
  44. For example, the rule `f(a) := g(a+1)' will match for any values of
  45. `a', such as complex numbers, vectors, or formulas, even if `a' has
  46. been declared to be real or scalar.  If you want the meta-variable `a'
  47. to match only literal real numbers, use `f(a) := g(a+1) :: real(a)'.
  48. If you want `a' to match only reals and formulas which are provably
  49. real, use `dreal(a)' as the condition.
  50.    The `::' operator is a shorthand for the `condition' function; `OLD
  51. := NEW :: COND' is equivalent to the formula `condition(assign(OLD,
  52. NEW), COND)'.
  53.    If you have several conditions, you can use `... :: c1 :: c2 :: c3'
  54. or `... :: c1 && c2 && c3'.  The two are entirely equivalent.
  55.    It is also possible to embed conditions inside the pattern: `f(x ::
  56. x>0, y) := g(y+x, x)'.  This is purely a notational convenience,
  57. though; where a condition appears in a rule has no effect on when it is
  58. tested.  The rewrite-rule compiler automatically decides when it is
  59. best to test each condition while a rule is being matched.
  60.    Certain conditions are handled as special cases by the rewrite rule
  61. system and are tested very efficiently:  Where `x' is any
  62. meta-variable, these conditions are `integer(x)', `real(x)',
  63. `constant(x)', `negative(x)', `x >= y' where `y' is either a constant
  64. or another meta-variable and `>=' may be replaced by any of the six
  65. relational operators, and `x % a = b' where `a' and `b' are constants.
  66. Other conditions, like `x >= y+1' or `dreal(x)', will be less efficient
  67. to check since Calc must bring the whole evaluator and simplifier into
  68. play.
  69.    An interesting property of `::' is that neither of its arguments
  70. will be touched by Calc's default simplifications.  This is important
  71. because conditions often are expressions that cannot safely be
  72. evaluated early.  For example, the `typeof' function never remains in
  73. symbolic form; entering `typeof(a)' will put the number 100 (the type
  74. code for variables like `a') on the stack.  But putting the condition
  75. `... :: typeof(a) = 6' on the stack is safe since `::' prevents the
  76. `typeof' from being evaluated until the condition is actually used by
  77. the rewrite system.
  78.    Since `::' protects its lefthand side, too, you can use a dummy
  79. condition to protect a rule that must itself not evaluate early.  For
  80. example, it's not safe to put `a(f,x) := apply(f, [x])' on the stack
  81. because it will immediately evaluate to `a(f,x) := f(x)', where the
  82. meta-variable-ness of `f' on the righthand side has been lost.  But
  83. `a(f,x) := apply(f, [x]) :: 1' is safe, and of course the condition `1'
  84. is always true (nonzero) so it has no effect on the functioning of the
  85. rule.  (The rewrite compiler will ensure that it doesn't even impact
  86. the speed of matching the rule.)
  87. File: calc.info,  Node: Algebraic Properties of Rewrite Rules,  Next: Other Features of Rewrite Rules,  Prev: Conditional Rewrite Rules,  Up: Rewrite Rules
  88. Algebraic Properties of Rewrite Rules
  89. -------------------------------------
  90. The rewrite mechanism understands the algebraic properties of functions
  91. like `+' and `*'.  In particular, pattern matching takes the
  92. associativity and commutativity of the following functions into account:
  93.      + - *  = !=  && ||  and or xor  vint vunion vxor  gcd lcm  max min  beta
  94.    For example, the rewrite rule:
  95.      a x + b x  :=  (a + b) x
  96. will match formulas of the form,
  97.      a x + b x,  x a + x b,  a x + x b,  x a + b x
  98.    Rewrites also understand the relationship between the `+' and `-'
  99. operators.  The above rewrite rule will also match the formulas,
  100.      a x - b x,  x a - x b,  a x - x b,  x a - b x
  101. by matching `b' in the pattern to `-b' from the formula.
  102.    Applied to a sum of many terms like `r + a x + s + b x + t', this
  103. pattern will check all pairs of terms for possible matches.  The rewrite
  104. will take whichever suitable pair it discovers first.
  105.    In general, a pattern using an associative operator like `a + b'
  106. will try 2 n different ways to match a sum of n terms like `x + y + z -
  107. w'.  First, `a' is matched against each of `x', `y', `z', and `-w' in
  108. turn, with `b' being matched to the remainders `y + z - w', `x + z -
  109. w', etc.  If none of these succeed, then `b' is matched against each of
  110. the four terms with `a' matching the remainder.  Half-and-half matches,
  111. like `(x + y) + (z - w)', are not tried.
  112.    Note that `*' is not commutative when applied to matrices, but
  113. rewrite rules pretend that it is.  If you type `m v' to enable matrix
  114. mode (*note Matrix Mode::.), rewrite rules will match `*' literally,
  115. ignoring its usual commutativity property.  (In the current
  116. implementation, the associativity also vanishes--it is as if the
  117. pattern had been enclosed in a `plain' marker; see below.) If you are
  118. applying rewrites to formulas with matrices, it's best to enable matrix
  119. mode first to prevent algebraically incorrect rewrites from occurring.
  120.    The pattern `-x' will actually match any expression.  For example,
  121. the rule
  122.      f(-x)  :=  -f(x)
  123. will rewrite `f(a)' to `-f(-a)'.  To avoid this, either use a `plain'
  124. marker as described below, or add a `negative(x)' condition.  The
  125. `negative' function is true if its argument "looks" negative, for
  126. example, because it is a negative number or because it is a formula
  127. like `-x'.  The new rule using this condition is:
  128.      f(x)  :=  -f(-x)  :: negative(x)    or, equivalently,
  129.      f(-x)  :=  -f(x)  :: negative(-x)
  130.    In the same way, the pattern `x - y' will match the sum `a + b' by
  131. matching `y' to `-b'.
  132.    The pattern `a b' will also match the fo