eki / mathcraft

A mathtastic computer algebra system.
0 stars 0 forks source link

Add identifiers? #4

Open eki opened 4 years ago

eki commented 4 years ago

It would be nice if we could use identifiers like width or length or etc. How can we combine word like identifiers with the mathematical notation that uses single letter variables?

Is 2 * width a single variable named width or multiple single letter variables w, i, d, t, h multiplied together in a term?

One option here could be to white list certain letters as likely to be variables: x, y, z, n, m, a, b, c, t. Anything else would be treated as an identifier.

This could be okay because we'll also have some mathematical constants like Euler's number e, or the imaginary number i, right? Or, the d when doing derivatives.

In other words, when working with variables we can have things like 2x^2yz, but when working with identifiers we'd need 2 * width^2 / length.

Examples of identifiers: width, length, height, strength, str, hp, attack, atk, 3ptm (three pointer made!), pts, reb, ast, st. Basically, lots of things from math, video games, sports, etc.

It's just not possible to differentiate between identifiers and single letter variables when allowing for multiplication without the symbol. So, we either have to default to single letter variables or identifiers. Or, one of the two must be denoted in some way. Or, we guess, of course.

Declaring identifiers:

var width, length

width * length = 100

Or, using some kind of sigil: @width * @length = 100

Or, require some kind of symbol in the identifier that is not a letter: house.width * house.length = 100.

The dot notation isn't terrible because we could always do .width * .length = 100

Although, none of that feels nice.

Can we use inference in some way? Or, we'd just be guessing?

The safest option may be not to allow identifiers?

eki commented 4 years ago

Note: we're going to add functions and they will be parsed as identifiers and need to be interpreted as not being variables multiplied together.

eki commented 4 years ago

After more and more thought, I think it'd be most natural to introduce some kind of var keyword for declaring identifiers as variables. We'll already need some kind of statement separator for systems of equations, so this can benefit from that separator.

var width

width = x_1 - x_2

Or, (inline variable)

var width = x_1 - x_2

Or, (function definition?)

width(x_1, x_2) = x_1 - x_2

Side note: TeX uses the underscore to denote a subscript and that's a pretty nice idea to copy. In this case x_1 would be parsed as an identifier and used as a variable. Will there be confusion rendering, though? 2x_iy_i?