Tuesday, June 5, 2012

Using Wittgen

This post and the next one will present some simple examples showing how to use Wittgen. The purpose is to make the language definition easier to understand. More advanced examples will come later, but for now the goal is simply to understand what the language is.

Motivations for the creation of Wittgen and its structure will be explained in later posts. However, for now, it is important to emphasize that the motivation is not the creation of a high level language so that standard programming problems can be solved in a small number of highly intuitive code lines. It is also not designed as a low level language so that the code should run as fast as possible. In fact, simple problems, may take more code to program and, performance may be ridiculously worse than in typical environments - particularly for maths.

The first, introductory program has already been presented. (It is traditional to introduce programming languages  using “hello world!”). The next program is designed to produce an infinite series of a’s. It will demonstrate the use of Doing Now as well as how to create a simple loop.


1 prog1:=

2 say:=@say} a}
3 Doing Now:=@prog1}}
4 }
5 say:=a}
6 Doing Now:=@prog1}}


Ignore the line numbers, they should not be included in the source. They are here only to make explaining the code easier. 

How does the code above work? There is an intuitive way to think of this. Imagine a game with one rule and one box. The box starts off containing one letter a. The one rule in the game goes like this: “Whatever is in the box, add another “a” to the box and then do this rule again.” Such a procedure is natural and easy for a human being.

That is what the code above does. The rule and the box are held in memory. The box is the variable called “say” and the rule is the variable called “prog1” which refers to the text from line 2-4 inclusive. 

Now, the full details of how Wittgen actually processes this short piece of code will be presented:

To start Wittgen the entire text is assigned to the variable Doing Now. Wittgen’s memory will now contain one variable called Doing Now whose value will be the text string:

        “prog1:=say:=@say} a}Doing Now:=@prog1}}}say:=a}Doing Now:=@prog1}}”

From then on the program will continue until the next time Doing Now needs to be executed and there is no assign to perform.

The first assign is the text from line 1 to 4 inclusive. The first step that Wittgen will take is to set Doing Now to:

        “say:=a}Doing Now:=@prog1}}”

and now it must create a new variable called prog1 and assign to it a text:

        “say:=@say} a}Doing Now:=@prog1}}}” 

Now here is an interesting point. There are two retrieve instructions in this text string; @say} and @prog1}. Why were they not evaluated before assigning the text string to prog1? The answer is that both of these are inside assign statements. @say} is part of the assign say:=@say} a} and is therefore left as is. Similarly, @prog1} is part of the assign Doing Now:=@prog1}}. We are not up to doing those assigns because we are still doing the outer assign. The result is that the whole string is assigned without replacement to prog1.The first assign is done. Wittgen’s memory can be represented by the following table:


Doing Now
say:=a}Doing Now:=@prog1}}
prog1
say:=@say} a}Doing Now:=@prog1}}


The first assign to Doing Now from line 5. This is a simple assign to a new variable called "say". The memory now looks like:

Doing Now
Doing Now:=@prog1}}
prog1
say:=@say} a}Doing Now:=@prog1}}
say
a

Now Doing Now has only one assign left - the assign of line 6 in the original code. Wittgen needs to evaluate the string “@prog1}” and then assign it to Doing Now. This requires retrieving the value of prog1 which is the second entry in the table here. Careful attention must be paid to the order here. Before the evaluation is done the assign is removed from Doing Now, so Doing Now now refers to an empty text. However, the execution of the assign instruction will put a new value in Doing Now. So that at the end of the assign, Doing Now has a new activity. The memory will now look as follows:

Doing Now
say:=@say} a}Doing Now:=@prog1}}
prog1
say:=@say} a}Doing Now:=@prog1}}
say
a


Doing Now now has two assigns to do. The first is line 2 of the original code and the second is line 3. The first assign evaluates “@say} a” by retrieving the value of say, which happens to be “a”, and therefore replacing @say} with an a to get the new string “a a”. This value is now put in the variable say. The memory now looks like:


Doing Now
Doing Now:=@prog1}}
prog1
say:=@say} a}Doing Now:=@prog1}}
say
a a

Doing Now looks exactly the way it looked two steps ago. The difference is that now the value of say is “a a” and not “a”. Therefore in two steps time say will have the value “a a a”. Wittgen will run forever, making say have more and more a’s.

The explanation was a little lengthy but actually all that the code does is “Whatever is in the box, add another “a” to the box and then do this rule again.”. It was important to explore what is actually going on in memory during execution and how each of the instructions works.

The next post will go through a few more examples. After that it will be time to explain why Wittgen was designed the way it was and how all this relates to cognition and philosophy.

No comments:

Post a Comment