This project is read-only.

NeturalMath Quick Start Guide

Part 5: Understanding Expressions

This document is not complete, and my represent features which are not yet implemented in the current version of NeturalMath.

Expressions represent one of the most fundamental concepts in NeturalMath. An expression is made of some combination of symbols and operations. Everything you have written in NeturalMath to date, or will every write in it is some form of an expression. The most visible form of an expression in NeturalMath is defining a variable. You learned how to do this in the previous section of this tutorial.
x = 12
y = x + 3

Both the number 12, and the sequence x + 3 are expressions. Technically, lines "x = 12" and "y = x + 3" are also expressions. These are expressions that are comprised of sub expressions. At the end of the previous example, we considered a case where one variable is defined in terms of another variable, but that variable was not itself defined at the time. The sample below is an example of one such case:
y = 3 * x

The variable y is defined without x first being defined. This is allowable in NeturalMath because y is not actually being defined as the value of three times x, but is instead defined as the expression three times x. When the actual value of y is required by the system, it first checks to see if x has been defined. If it has not, it will throw out and error. If the value has been defined in a later statement, then the computed value will be returned. This may be a very difficult concept to initially grasp. It is best represented in the example below:
y = x + 2
print y // returns an error

x = 2
print y // returns 4

x = 5
print y // returns 7

When the value for x has not been defined, trying to return the value of y will result in an error because we are trying to evaluate the value of something which does not exist. As soon as we set x equal to the value of two, we can return the correct value of y. This does not mean that y is actually assigned the value of four, however. What this means is that y is still defined as an expression representing the value of x plus the number two. Whenever the value of x changes, the result value of y changes as well.

Outputing Expressions

We know from previous examples that we can use the print command to output the value of a variable or operation. When we assign the variable x to some value, we can output that value using print, but what if we want to output the expression it is defined as instead? The def command is able to do this for us. See the example below:
y = x + 3
print y // returns an error because x is undefined

def y  // returns the x + 3

The def command outputs the expression that y was assigned rather than the result of evaluating that expression. Try experimenting with the samples below:
def 3

x = 3
def x

y = x + 12
def y

def z

Simplifying Expressions

NeturalMath will automatically attempt to simplify an expression as best as possible whenever they are used. What this means is that it will automatically try to reduce the number of operations present in an expression to the simplest possible form, thereby reducing the number of operations that need to be performed in order to evaluate the expression. Consider the example below:
x = 2 + 2
def x

The def statement returns that x is equal to four, not two plus two. This is because NeturalMath realizes that two plus two can be expressed more simplely as the number four rather than an addition of two numbers. NeturalMath inherently understands that constants and variables of the same type may be combined following typical mathematical rules to create a more compact expression that yields the same net result. For example:
y = x + 3 * x + 4
def y // (4 * x) + 4

Limitations on Expressions

Expressions are a very powerful and fundamental feature of NeturalMath, but there are some limitations. For example, a variable may not make use of an expression which uses its own variable definition. That is to say, the variable y cannot be assigned an expression containing the variable y because this would create a circular reference inside the expression, which would result in a logical paradox. This would also cause the computer to enter an infinite loop while trying to evaluate it.
y = y +2 // invalid assignment, throws an error

The next section on symbolic notation explains some ways to work around this issue.

A further look into expressions and how they work, as well as how to manipulate expressions directly is provided in the next section.
Part 6: Symbolic Manipulation

Last edited Nov 27, 2010 at 3:40 PM by zanethorn, version 7


No comments yet.