/usr/share/doc/yacas-doc/html/codingchapter2.html is in yacas-doc 1.3.3-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 | <html>
<head>
<title>Evaluation of expressions</title>
<link rel="stylesheet" href="yacas.css" TYPE="text/css" MEDIA="screen">
</head>
<body>
<a name="c2">
</a>
<h1>
2. Evaluation of expressions
</h1>
<p> </p>
When programming in some language, it helps to have a mental
model of what goes on behind the scenes when evaluating expressions,
or in this case simplifying expressions.
<p>
This section aims to explain how evaluation (and simplification)
of expressions works internally, in <b><tt>Yacas</tt></b>.
<p>
<a name="c2s1">
</a>
<h2>
<hr>2.1 The LISP heritage
</h2>
<h3>
<hr>Representation of expressions
</h3>
Much of the inner workings is based on how LISP-like languages
are built up. When an expression is entered, or composed in some
fashion, it is converted into a prefix form much like you get
in LISP:
<p>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
a+b -> (+ a b)
Sin(a) -> (Sin a)
</pre></tr>
</table>
Here the sub-expression is changed into a list of so-called "atoms",
where the first atom is a function name of the function to be
invoked, and the atoms following are the arguments to be passed in
as parameters to that function.
<p>
<b><tt>Yacas</tt></b> has the function <b><tt>FullForm</tt></b> to show the internal representation:
<p>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> FullForm(a+b)
(+ a b )
Out> a+b;
In> FullForm(Sin(a))
(Sin a )
Out> Sin(a);
In> FullForm(a+b+c)
(+ (+ a b )c )
Out> a+b+c;
</pre></tr>
</table>
<p>
The internal representation is very close to what <b><tt>FullForm</tt></b> shows
on screen. <b><tt>a+b+c</tt></b> would be <b><tt>(+ (+ a b )c )</tt></b> internally, or:
<p>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
()
|
|
+ -> () -> c
|
|
+ -> a -> b
</pre></tr>
</table>
<p>
<h3>
<hr>Evaluation
</h3>
An expression like described above is done in the following manner:
first the arguments are evaluated (if they need to be evaluated,
<b><tt>Yacas</tt></b> can be told to not evaluate certain parameters to functions),
and only then are these arguments passed in to the function for
evaluation. They are passed in by binding local variables to the
values, so these arguments are available as local values.
<p>
For instance, suppose we are evaluating <b><tt>2*3+4</tt></b>. This first gets
changed to the internal representation <b><tt>(+ (* 2 3 )4 )</tt></b>. Then,
during evaluation, the top expression refers to function "<b><tt>+</tt></b>".
Its arguments are <b><tt>(* 2 3)</tt></b> and <b><tt>4</tt></b>. First <b><tt>(* 2 3)</tt></b> gets evaluated.
This is a function call to the function "<b><tt>*</tt></b>" with arguments <b><tt>2</tt></b>
and <b><tt>3</tt></b>, which evaluate to themselves. Then the function "<b><tt>*</tt></b>" is
invoked with these arguments. The <b><tt>Yacas</tt></b> standard script library
has code that accepts numeric input and performs the multiplication
numerically, resulting in <b><tt>6</tt></b>.
<p>
The second argument to the top-level "<b><tt>+</tt></b>" is <b><tt>4</tt></b>, which evaluates
to itself.
<p>
Now, both arguments to the "<b><tt>+</tt></b>" function have been evaluated, and
the results are <b><tt>6</tt></b> and <b><tt>4</tt></b>. Now the "<b><tt>+</tt></b>" function is invoked.
This function also has code in the script library to actually
perform the addition when the arguments are numeric, so the result
is 10:
<p>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> FullForm(Hold(2*3+4))
(+ (* 2 3 )4 )
Out> 2*3+4;
In> 2*3+4
Out> 10;
</pre></tr>
</table>
<p>
Note that in <b><tt>Yacas</tt></b>, the script language does not define a "<b><tt>+</tt></b>" function
in the core. This and other functions are all implemented in the script library.
The feature "when the arguments to "<b><tt>+</tt></b>" are numeric, perform the numeric
addition" is considered to be a "policy" which should be configurable.
It should not be a part of the core language.
<p>
It is surprisingly difficult to keep in mind that evaluation is
bottom up, and that arguments are evaluated before the function call
is evaluated. In some sense, you might feel that the evaluation of the
arguments is part of evaluation of the function. It is not. Arguments
are evaluated before the function gets called.
<p>
Suppose we define the function <b><tt>f</tt></b>, which adds two numbers, and
traces itself, as:
<p>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> f(a,b):= \
In> [\
In> Local(result);\
In> Echo("Enter f with arguments ",a,b);\
In> result:=a+b;\
In> Echo("Leave f with result ",result);\
In> result;\
In> ];
Out> True;
</pre></tr>
</table>
<p>
Then the following interaction shows this principle:
<p>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> f(f(2,3),4)
Enter f with arguments 2 3
Leave f with result 5
Enter f with arguments 5 4
Leave f with result 9
Out> 9;
</pre></tr>
</table>
<p>
The first Enter/Leave combination is for <b><tt>f(2,3)</tt></b>, and only then is
the outer call to <b><tt>f</tt></b> entered.
<p>
This has important consequences for the way <b><tt>Yacas</tt></b> simplifies
expressions: the expression trees are traversed bottom up, as
the lowest parts of the expression trees are simplified first,
before being passed along up to the calling function.
<p>
<a name="c2s2">
</a>
<h2>
<hr>2.2 <b><tt>Yacas</tt></b>-specific extensions for CAS implementations
</h2>
<b><tt>Yacas</tt></b> has a few language features specifically designed for use
when implementing a CAS.
<p>
<h3>
<hr>The transformation rules
</h3>
Working with transformation rules is explained in the introduction
and tutorial book. This section mainly deals with how <b><tt>Yacas</tt></b>
works with transformation rules under the hood.
<p>
A transformation rule consists of two parts: a condition that
an expression should match, and a result to be substituted for
the expression if the condition holds. The most common way
to specify a condition is a pattern to be matched to an expression.
<p>
A pattern is again simply an expression, stored in internal format:
<p>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> FullForm(a_IsInteger+b_IsInteger*(_x))
(+ (_ a IsInteger )(* (_ b IsInteger )(_ x )))
Out> a _IsInteger+b _IsInteger*_x;
</pre></tr>
</table>
<p>
<b><tt>Yacas</tt></b> maintains structures of transformation rules, and tries
to match them to the expression being evaluated. It first tries
to match the structure of the pattern to the expression. In the above
case, it tries to match to <b><tt>a+b*x</tt></b>. If this matches, local variables
<b><tt>a</tt></b>, <b><tt>b</tt></b> and <b><tt>x</tt></b> are declared and assigned the sub-trees of the expression
being matched. Then the predicates are tried on each of them: in this
case, <b><tt>IsInteger(a)</tt></b> and <b><tt>IsInteger(b)</tt></b> should both return <b><tt>True</tt></b>.
<p>
Not shown in the above case, are post-predicates. They get evaluated
afterwards. This post-predicate must also evaluate to <b><tt>True</tt></b>.
If the structure of the expression matches the structure of the
pattern, and all predicates evaluate to <b><tt>True</tt></b>, the pattern matches
and the transformation rule is applied, meaning the right hand side
is evaluated, with the local variables mentioned in the pattern
assigned. This evaluation means all transformation rules are re-applied
to the right-hand side of the expression.
<p>
Note that the arguments to a function are evaluated first, and only
then is the function itself called. So the arguments are evaluated,
and then the transformation rules applied on it. The main function
defines its parameters also, so these get assigned to local variables
also, before trying the patterns with their associated local variables.
<p>
Here is an example making the fact that the names in a pattern are
local variables more explicit:
<p>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> f1(_x,_a) <-- x+a
Out> True;
In> f2(_x,_a) <-- [Local(a); x+a;];
Out> True;
In> f1(1,2)
Out> 3;
In> f2(1,2)
Out> a+1;
</pre></tr>
</table>
<p>
<h3>
<hr>Using different rules in different cases
</h3>
In a lot of cases, the algorithm to be invoked depends on the type
of the arguments. Or the result depends on the form of the input
expression. This results in the typical "case" or "switch" statement,
where the code to evaluate to determine the result depends on the
form of the input expression, or the type of the arguments, or some other conditions.
<p>
<b><tt>Yacas</tt></b> allows to define several transformation rules
for one and the same function, if the rules are to be applied under different conditions.
<p>
Suppose the function
<b><tt>f</tt></b> is defined, a factorial function:
<p>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
10 # f(0) <-- 1;
20 # f(n_IsPositiveInteger) <-- n*f(n-1);
</pre></tr>
</table>
<p>
Then interaction can look like:
<p>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> f(3)
Out> 6;
In> f(a)
Out> f(a);
</pre></tr>
</table>
<p>
If the left hand side is matched by the expression being considered,
then the right hand side is evaluated. A subtle but important thing
to note is that this means that the whole body of transformation rules
is thus re-applied to the right-hand side of the <b><tt><--</tt></b> operator.
<p>
Evaluation goes bottom-up, evaluating (simplifying) the lowest parts
of a tree first, but for a tree that matches a transformation rule,
the substitution essentially means return the result of evaluating the
right-hand side. Transformation rules are re-applied, on the right hand
side of the transformation rule, and the original expression can be thought
of as been substituted by the result of evaluating this right-hand side,
which is supposed to be a "simpler" expression, or a result closer to what
the user wants.
<p>
Internally, the function <b><tt>f</tt></b> is built up to resemble the following
pseudo-code:
<p>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
f(n)
{
if (n = 1)
return 1;
else if (IsPositiveInteger(n))
return n*f(n-1);
else return f(n) unevaluated;
}
</pre></tr>
</table>
<p>
The transformation rules are thus combined into one big
statement that gets executed, with each transformation
rule being a if-clause in the statement to be evaluated.
Transformation rules can be spread over different files,
and combined in functional groups. This adds to the readability.
The alternative is to write the full body of each function as
one big routine, which becomes harder to maintain as the function
becomes larger and larger, and hard or impossible to extend.
<p>
One nice feature is that functionality is easy to extend without
modifying the original source code:
<p>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> Ln(x*y)
Out> Ln(x*y);
In> Ln(_x*_y) <-- Ln(x) + Ln(y)
Out> True;
In> Ln(x*y)
Out> Ln(x)+Ln(y);
</pre></tr>
</table>
<p>
This is generally not advisable, due to the fact that it alters
the behavior of the entire system. But it can be useful in some
instances. For instance, when introducing a new function <b><tt>f(x)</tt></b>,
one can decide to define a derivative explicitly, and a way
to simplify it numerically:
<p>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> f(_x)_InNumericMode() <-- Exp(x)
Out> True;
In> (Deriv(_x)f(_y)) <-- f(y)*(Deriv(x)y);
Out> True;
In> f(2)
Out> f(2);
In> N(f(2))
Out> 7.3890560989;
In> Exp(2)
Out> Exp(2);
In> N(Exp(2))
Out> 7.3890560989;
In> D(x)f(a*x)
Out> f(a*x)*a;
</pre></tr>
</table>
<p>
<h3>
<hr>The "Evaluation is Simplification" hack
</h3>
One of the ideas behind the <b><tt>Yacas</tt></b> scripting language is that evaluation
is used for simplifying expressions.
One consequence of this is that
objects can be returned unevaluated when they can not be simplified
further. This happens to variables that are not assigned, functions that
are not defined, or function invocations where the arguments passed
in as parameters are not actually handled by any code in the scripts.
An integral that can not be performed by <b><tt>Yacas</tt></b> should be returned
unevaluated:
<p>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> 2+3
Out> 5;
In> a+b
Out> a+b;
In> Sin(a)
Out> Sin(a);
In> Sin(0)
Out> 0;
In> Integrate(x)Ln(x)
Out> x*Ln(x)-x;
In> Integrate(x)Ln(Sin(x))
Out> Integrate(x)Ln(Sin(x));
In> a!
Out> a!;
In> 3!
Out> 6;
</pre></tr>
</table>
<p>
Other languages usually do not allow evaluation of unbound variables,
or undefined functions. In <b><tt>Yacas</tt></b>, these are interpreted as some yet
undefined global variables or functions, and returned unevaluated.
<p>
<a name="c2s3">
</a>
<h2>
<hr>2.3 Destructive operations
</h2>
<b><tt>Yacas</tt></b> tries to keep as few copies of objects in memory as
possible. Thus when assigning the value of one variable to another,
a reference is copied, and both variables refer to the same memory,
physically. This is relevant for programming; for example, one
should use <b><tt>FlatCopy</tt></b> to actually make a new copy of an object.
Another feature relevant to reference semantics is "destructive
operations"; these are functions that modify their arguments rather than work on a copy. Destructive operations on lists are generally recognized
because their name starts with "Destructive",
e.g. <b><tt>DestructiveDelete</tt></b>. One other destructive
operation is assignment of a list element through <b><tt>list[index] := ...</tt></b>.
<p>
Some examples to illustrate destructive operations on lists:
<p>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> x1:={a,b,c}
Out> {a,b,c};
</pre></tr>
</table>
A list <b><tt>x1</tt></b> is created.
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> FullForm(x1)
(List a b c )
Out> {a,b,c};
In> x2:=z:x1
Out> {z,a,b,c};
</pre></tr>
</table>
A new list <b><tt>x2</tt></b> is <b><tt>z</tt></b> appended to <b><tt>x1</tt></b>. The <b><tt>:</tt></b> operation creates a copy of <b><tt>x1</tt></b> before appending, so <b><tt>x1</tt></b> is unchanged by this.
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> FullForm(x2)
(List z a b c )
Out> {z,a,b,c};
In> x2[1]:=y
Out> True;
</pre></tr>
</table>
We have modified the first element of <b><tt>x2</tt></b>, but <b><tt>x1</tt></b> is still the same.
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> x2
Out> {y,a,b,c};
In> x1
Out> {a,b,c};
In> x2[2]:=A
Out> True;
</pre></tr>
</table>
We have modified the second element of <b><tt>x2</tt></b>, but <b><tt>x1</tt></b> is still the same.
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> x2
Out> {y,A,b,c};
In> x1
Out> {a,b,c};
In> x2:=x1
Out> {A,b,c};
</pre></tr>
</table>
Now <b><tt>x2</tt></b> and <b><tt>x1</tt></b> refer to the same list.
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> x2[1]:=A
Out> True;
</pre></tr>
</table>
We have modified the first element of <b><tt>x2</tt></b>, and <b><tt>x1</tt></b> is also modified.
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> x2
Out> {A,b,c};
In> x1
Out> {A,b,c};
</pre></tr>
</table>
<p>
A programmer should always be cautious when dealing with
destructive operations. Sometimes it is not desirable to
change the original expression.
The language deals with it this way because of performance considerations.
Operations can be made non-destructive by using <b><tt>FlatCopy</tt></b>:
<p>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> x1:={a,b,c}
Out> {a,b,c};
In> DestructiveReverse(x1)
Out> {c,b,a};
In> x1
Out> {a};
In> x1:={a,b,c}
Out> {a,b,c};
In> DestructiveReverse(FlatCopy(x1))
Out> {c,b,a};
In> x1
Out> {a,b,c};
</pre></tr>
</table>
<p>
<b><tt>FlatCopy</tt></b> copies the elements of an expression only at the top level of nesting.
This means that if a list contains sub-lists, they are not copied, but
references to them are copied instead:
<p>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> dict1:={}
Out> {};
In> dict1["name"]:="John";
Out> True;
In> dict2:=FlatCopy(dict1)
Out> {{"name","John"}};
In> dict2["name"]:="Mark";
Out> True;
In> dict1
Out> {{"name","Mark"}};
</pre></tr>
</table>
<p>
A workaround for this is to use <b><tt>Subst</tt></b> to copy the entire tree:
<p>
<table cellpadding="0" width="100%">
<tr><td width=100% bgcolor="#DDDDEE"><pre>
In> dict1:={}
Out> {};
In> dict1["name"]:="John";
Out> True;
In> dict2:=Subst(a,a)(dict1)
Out> {{"name","John"}};
In> dict2["name"]:="Mark";
Out> True;
In> dict1
Out> {{"name","John"}};
In> dict2
Out> {{"name","Mark"}};
</pre></tr>
</table>
<p>
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2425144-1";
urchinTracker();
</script>
</body>
</html>
|