Tuesday, July 27, 2010

A reduced programming language

Not to bang on about the '=' operator, but this alternate definition described in previous posts seems like an interesting way to produce a reduced programming language... I'm not sure how useful it would be but it is a kind of interesting thought experiment.

If you have the equation 4 = x*x then its value is 4, but its value can only exist in a universe where x is 2 or x is -2. If we previously had the expression x = all reals; (by this I mean it is the contents of the set of reals, it isn't a set itself), then the later expression 4=x*x can only exist in the subuniverse of the first expression where x = 2,-2. It is not that the first expression x = all reals is now wrong exactly, but that for most elements e.g. x = 0, their existance has become infinitely unlikely.

Anyway what does it all mean, well it means that with a programming language that follows this '=' operator design we could write:
x = reals;
x = 3; <-- this expression evaluates to 3, and can only exist as a statement if x then becomes 3
but unlike most languages you could equally write
3 = x; <-- x becomes 3
and as eluded to in previous posts, you can use = in equations like so:
y = 2 * (x = 3); <-- x becomes 3, y becomes 6

'=' becomes like a filter on the previous expressions, for example:
x = reals;
x^2 = 4 <-- x becomes 2, -2
but if we write
x = reals;
x = -2;
x^2 = 4 <-- x will be -2, since it is a subnumber of values of its previous self

What if you write:
x = reals;
x = 4; <-- x becomes 4
x = 5;
In this case there is no subuniverse that can exist where 4=5, so the value of x=5 is void and x becomes void.

You can of course write very complicated equations and get answers, e.g.
(x^2.3 + x^-1.7)^(x+1) = 13;
The design will filter x appropriatly. However it is very hard for a programming language to solve arbitrary equations like this in general. Such a programming language would probably reference a large database of formula patterns on the net to provide a solution. This has the advantage that the user doesn't need to manually write its own solvers separately for every equation they have, and these complicated solvers can use stable, well trodden algorithms.
But also users should have syntax so they can write your own solver, for example:
(x^2 = 2*x)
<
// optional user defined solve function, can call a common routine:
solveQuadratic(...);
>

What if we want to perform some arithmetic on a 'number of' values? We can use the syntax:
(number of values)
{
}
which the block will be executed for each individual value, e.g:
(1, 2, 4)
{
print("value = ", value);
}
In fact, we can use 3 different notations for 3 different effects:
- a 'number of' something has no particular order, but can have more than 1 of the same value. 1,2,2,3 == 3,2,1,2. It optionally uses () to resolve ambiguity, so matches the use of () in functions and matches that (1,2),3 == 1,2,3 == 1,(2,3)
- a list of something has a specific order and can repeat values. It can use [] like arrays.
- a set of something has no specific order and number of repeated values is ignored. 1,2,2,3 == 3,2,1. It can use {}.

The result is that a list is executed in order, but a 'number of' and a set can be executed on separate threads.
(1,2,3)
{
// executed on separate threads
}
{1,2,2,3}
{
// executed on separate threads, only 3 executions
}


OK, so far so good. The thing I find interesting about such a language, is it has surprisingly few keywords...
- we can do an if statement like so:
(x=3)
{
print("x is ", x);
}
- we can do a while statement like so:
x = integers;
x >= 0;
[x < maxCount ]
{
...
}
- and hopefully you can see that a for statement and a do{}while can be written similarly.
- There is no ==, you just use the one = operator.
- Threading is done through the use of sets/'number of's rather than the serial lists.
- 'and' also can be used, you can write (x^2=4 and x<0) this evaluates to 'true' and x will be -2.

I'm not sure how it is supposed to cope with infinite expressions though:
x = reals
x >= 0;
(x<1)
{
y = y^0.9;
}
Perhaps an internal search algorithm can pick values and try to converge towards a solution... not very easy though... but an interesting and in some ways powerful language none-the-less.

No comments:

Post a Comment