Models – 01 – Models, Definitions, Analysis and Design


This is the kind of text I write to help myself understand something better. I will also write a little bit about the driving reasons which led me to write about models, which were analysis and design.

It’s generally accepted that a model for something material resembles it. If I tell you I have a model of a plane, you’d usually think of a miniature plane, or some 3D interactive software which simulates the plane performing various movements. Maybe something else. The point is that you usually don’t think about the plane, but about something else which somehow looks like it.

More generally, a model is about something. We should always talk about models mentioning what they’re about. These things are of extreme importance because they form a context for the model.

For something as abstract and general as a model, it’s no surprise that there are many ways for defining one. Back when you were learning, say addition and subtraction, you probably had the “follow the model” kind of exercise. It goes as the following:

Follow the model
1 ? 4 = 5 (+)
7 ? 2 = 5 (-)
1 ? 1 = 2 ( )
9 ? 2 = 7 ( )
5 ? 3 = 8 ( )
2 ? 1 = 1 ( )

To perform the exercise, you’d have to fill in the blanks with the right operator symbols as in the first two cases.

There is a model definition in this exercise. However, as I said before, a model is about something. In this case, what is “the something”? It doesn’t seem obvious, but, in a sense, the model is about how you should solve each item in the exercise. It was defined by two examples of its application. Given the examples, your mind then filled in the blanks and “got it” somehow. It begs the question: which are “the blanks” your mind filled?

A model doesn’t always have to be material (in the sense that it exists outside one’s mind). There are mental models, and those are only thought of. So if you want to communicate them with other people, you must find ways to represent them. That is, you should find a way to communicate something (the representation) that, for your particular purposes, is as good as the actual thing you want to communicate (the model). Representations can be tricky things to understand, but we use them all the time. For example, an angle is different from its measurement, and almost all the time we can talk about one through the other. We can do so because for our purposes, most of the time, an angle is as good as its measurement.

Your professor, then, was expecting you’d interpret the representation of the model (the two examples) and realize the model yourself.

Now I think I can go back to the question I’ve asked before. What are the blanks your mind filled? The answer is the model itself. When you interpreted the representation of the model, you conceived it in your mind.


What is a model? A model is a set of properties about something.

A property of something is a statement which is always true for that thing. For example, “in my game, blood is always drawn with red like colors”. The statement “blood is always drawn with red like colors” is true about “my game”, which means this statement is a property of my game.

Some models are mathematical: their statements are always true, regardless of what (they’re exactly true). For example, part of the model for real numbers is that if you have three of them a,b,c and you have that a=b, then it’s also true that a+c=b+c.

Models Go Mental

You could now be wondering how is a miniature plane a model for a real plane? Is a metal miniature plane a set of properties? It surely doesn’t look like so. One way of looking at this is that models are always mental and “material models” are always representation of (mental) models. Which means that if you think of a model for a plane and construct a miniature plane which looks just like what you’ve thought, then that miniature plane represents the model you have in mind.

The distinction I will make, then, is that a model is restricted to what you think of, but its representation can material (it exists in the real world, outside one’s mind), but not necessarily.

In this sense, it’s more useful to look at a miniature plane as a representation of a model and not as the model itself. In practice, the definition of a model is wide open and includes cases like these too. For now, throughout this article, I am going to use that models are intrinsically mental because I believe that is a more useful way of looking at things (it’s kind of a useful lie for doing thinking on my own).

Usage, Analysis and Design

Curiously, you can have:

  • a geometric model of a plane;
  • a mechanical configuration model of a plane;
  • models for its electric configuration; and
  • many others.

In general, you could conceive several models for the various aspects of a plane. The same goes for laptop computers, software systems, and whatever else you can think of. You can take something S, look at a part P of it, ignore everything else, and build a model for S by only looking at P.
Being able to do such a think helps in analysis. You can, for example, analyze a plane by first looking at its geometry, then at its mechanics, and then at its electronics. Maybe you decide you’ll do this for each individual material part of the plane (whatever you choose those to be). Therefore, from analyzing the parts, you analyze the plane as a whole.

This activity of building a model requires you to think about something so to extract relevant properties out of it.

In general, the process of analyzing something and the process of building a model are connected. In both cases, you accumulate properties about something. For example, when you get the representation of a model, you have to look at it and extract relevant properties so you can build the model you’re expected to. It seems, then, that through analysis, one builds models. This leads us to a better definition of analysis.

Analysis is the process by which given S, you build a model for S. And some times, we talk about the analysis as being the model constructed from the analysis. The distinction should be clear from the context.

Analysis is being thought of extracting properties of something existing. But what about extracting properties of something which doesn’t exist? You can do so by analyzing how this non-existing entity would be. After that, you can derive a model of how to realize that non-existing entity so to make it real. And as it turns out, you can analyze existing things too (how nice!) in order to build models of how they could be realized.

The model of how you could realize/create something is called design. Designing is the process by which you construct a design. So it follows that designing is a creative process.

A good example of analysis and design is in engineering. Engineering is about problem solving as many other activities are. A team of engineers is given a problem. They’ll, then, analyze the problem to build a models (of requirements on the solution, of the environment in which the solution will be inserted in, and so forth). At some point, they have to design the solution or parts of it. Engineering teams are expected to do these analysis and design activities so to solve the problem. For them, analysis and design are major tools which they use to solve problems.

The design is just another model, which means that, by our definition of model, it’s mental. So a design needs representation(s) if you want to communicate it, store it somewhere, etc. In designing, engineers tend to build diagrams, texts, and all sorts of documents representing the design and/or its parts. The same goes for analysis by the way . And there are actually special tools for representing analysis and design models.

Representing models is often useful, but that is another topic. However, it’d be useful if there were documents representing all the produced analysis and designs so that others (and the original engineers themselves) can use those to learn about and/or remember what happened during the project development.

Final Thoughts

In my experience, and belief, it’s often the case that analysis form a cause for design, which means it’s very common to see analysis and designing as two connected activities. I believe it’s common, in the case there are problems in need of solutions, that there is also a need for good solutions design, which are usually expected to be well founded in good analysis.

To summarize, analysis has the idea of taking things apart because you extract properties of something, while design has the idea of combining things together because you model how to realize something.

Modeling is not just important for analysis and design, you could think, but these two concepts were the reason I decided to write about modeling.

If you can, please share your thoughts. Comment! =)

Yet on the Space Invaders Game TSpace

In my last post, I “released” a space invaders game written in TCL/TK. It has its own scheduler and task queue, which it uses to find out which thing on the screen has to be updated. It turns out that these things are done by the TCL implementation too, and it’s likely that they’re more efficient than the one I wrote.

So I modified the code to be a much simpler (without save/load, sounds and speeding up or down features) version of the game using the after command instead of implementing my own scheduler/tasks queue.

Just to be clear, I still could have the save/load, sounds and speeding up/down features if I wanted, with as much ease as in the other versions. I removed them simply because I wanted this stripped version of the game. Using the after command doesn’t imply in not being able to have these features, or in having a harder time in having these features. I could have kept them just as easily.

I actually liked TSpace, and I am likely to keep playing it. And I don’t care about sounds, save, load or messing up with the game speed. That’s why I took these out. I wrote them in the first place because I saw that I could, as a direct result of the different designs that I tried.

The interesting thing about this version is that it doesn’t try to re-invent the wheel, but uses what is already built-in in TCL. And it turns out that it ran much faster (notice that the other version may get slower later in the game, when you get lots of points, but it’s mostly because of the amount of sounds that are played with that many enemies missiles being shot at you — if you take the sounds off [not just turning the sound off in your computer, but actually changing the source code], the slowdown is not so much noticeable).

Here is the code:

To run it, you should use the image files from the TSpace game posted in the previous post.

(Much) Improved Space Invaders Game [in TCL/TK] – TSpace

In my last post, I sort of showed that it was very simple to put a space ship image on a black background with some asteroids floating around. Also having the space ship being controlled by the arrow keys and being able to shoot the asteroids.

Well, I decided to evolve that little game. The result was still a little game, but much better. There are screen shots in the end of this post.

The game went through some phases. The first one was that of the last post, which was pretty much a “can I do this?” thing. The second one was using the coroutines feature of TCL 8.6, which made the game much easier to extend. However, this second version only was a re-write of the first one. Then a third one was an evolution of this second one. I took the extendable version using the coroutines feature and started adding things to it.

This third version was a major breakthrough. It allowed pausing the game easily, also increasing its speed, or decreasing (you could use the plus and minus keys to make the stuff move faster or slower on the screen). Adding stuff was also much easier.

The basic idea was to dynamically generate coroutines, each to control an item on the screen. There was a scheduler function, which would go round robin on these coroutines. Pausing the game just meant telling the scheduler to stop looping through the coroutines queue. Messing up with the game speed was basically changing the milliseconds interval between each iteration in the scheduler.

Also, items on the screen were pretty independent, because they each had its own coroutine. So it was quite a nice thing.

However, it was generating a lot (really tons) of coroutines. And I wondered if I could do differently, just to see with what I’d end up with.

Then I had the idea of having a few controller functions which knew how to deal with different types of items on the screen. For example, the ship controller, the enemy controller, the missile controller. These functions would see the current situation of some item it can control, update it and produce its new current situation. This was the fourth, and current phase.

Each situation is actually called a task, because they replace the coroutines. A task has a procedure, which can perform it, and a bunch of other data (such as x,y position, image type, etc.). The scheduler, which used to run coroutines, now deals with a queue of tasks, and at each iteration, it takes a bunch of tasks, and run their controllers (which would be stored in the task’s procedure field) on the task by getting tha task controller and calling it on the task.

The advantage of this, now, is that the data needed to continue the game, from a given point, is more accessible. Which means that saving and loading game is trivial. And so, I wrote the save game and load game procedures. They pretty much saved this tasks queue, along with some other things, but the tasks queue is the major thing. And the load game function pretty much reads the saved data, and re-draw the canvas accordingly. Each task also has a load function associated with it, which could re-draw an item given its current situation, reflected in the task.

An important feature of TCL played a major role here. And it’s the feature that “everything is a string”. Since that’s true, to save the task queue, all I have to do is print it (there is no code for serialization, or any sort of conversion). And to load it back, I just read it (there is no code for parsing). That’s it. I mentioned some posts ago that  things are a little different from “everything is a string”, however, the language gives you that illusion, and you can program as if that was true. A more, it seems, accurate thing to say is that everything has a string representation. Being able to get the variable value by having its name stored in another variable also helped because it allowed for “the list of variables to save and load”. Currently, if I wanna add some more state to the game, then the code that causes that state to be saved/loaded is close to nothing; I just have to add the variables names to that list of things to save and load.

In an OO style, you could have a task interface, which has a load and iterate methods. And each screen item would have its controller, which would be classes implementing this interface. The task queue would then be a queue of tasks, and the call to iterate and load would perform dynamic dispatches. Obviously, you don’t need classes to do this, so a non-classic OO system also would allow this. And, in TCL, since you can use the value of a variable as the name of a function to call, you can just generate the name dynamically and call that (getting dynamic dispatch in effect). So, it may be that, in TCL, doing the OO actually complicated the program. Anyway, I didn’t do it this way also because idk how to yet. I still haven’t learned how to use OO systems in TCL.

That being said, if you wanna take a look at the game. I uploaded it on 4shared.

Here is how you play the game:

  • The arrows move around.
  • S saves the game.
  • L loads the game.
  • + and – and 0 deal with game speed.
  • P pauses the game.
  • F is the simple shot.
  • D is the triple shot.

To run the game, you need a TCL implementation with the snack extension. An easy way to get it is by getting active tcl, and installing snack after done that. Here is how to:

  1. Go to the downloads page,
  2. Get the 8.6 version of active tcl (it cannot be the other older ones). You should also get the 32bit one, because iirc, snack is not available for 64bit. Even if you run a 64bit windows, you can download the 32bit version and it should all work (which I just found out recently).
  3. Install active tcl.
  4. To install snack, go to the command line and type “teacup install snack” and wait.
  5. A friend of mine had the problem of not getting the Img package out of the box, and he had to install it. So you may need to. Do it by typing “teacup install Img”.

That should be it.

To run the game, extract it and double clicking on the tspace.tcl should do fine on windows. On some linux, you probably will run on the command line with tclsh.

And as a side note, I also found out I cannot spell words very well: I’ve been writing ammOnition and missIle (you will find this in the whole source code).

The images and sounds of the game were not created by me. I got them from sources I could use for learning purposes. Here are the credits (which you can also see in the sources.txt file in the .rar file of the game).

If you find bugs or that something is missing, or strange, post as a comment please. I just found out I forgot to put two required files in the rar archive (which is now fixed). So, if anything, write a comment please.

Updage: I found some bugs after I wrote this post. So if you find a bug, maybe re-downloading the package (which I just updated) may help. But comment anyway =).

Here are some screen shots:

Simple Space Ship Game in TCL/TK

As I mentioned in my last post, I just started learning TCL and TK.

Well, yesterday I decided I would see if I could put a image of a space ship in a black baground, and make that image be controled by the arrow keys. After one hour or two I did that. And I thought “well, this was really easy; maybe I can get it to shot if I press F too”. Then I went on, got the image of a small missle, and made it move from the ship to the top of the screen if I press F. After that, I found asteroids images and put them rolling and moving in the screen and made a space ship game for shooting asteroids.

I started making (learning how to) the game 7 or 8 pm yesterday and finished 9 am today (yes, over night).

And it was really simple. It’s worth pointing out that the code is pretty much a bunch of stuff on top of eachother: not a well tought design. It works because the space ship game is small and simple.

If you want to play the game, you can get Active TCL from here, and then install it. After installing it, do these things:

  1. Get the game source code here.
  2. Save the game source code in your computer, name it prog.tcl (or anything, but I will assume you named it prog.tcl, and put it in some folder, X).
  3. Save these images in X, with the indicated names:
    1. – asts.gif
    2. - missle_1_1.gif
    3. – ship.gif
  4. To run the game, install Active TCL.
  5. Open cmd.exe on your windows machine.
  6. Go to the folder X.
  7. Run from the command line, in folder X, “tclsh prog.tcl”
  8. Have fun.

On linux, install TCL from your package manager, follow step 1 to 3 and run the game by following step 7.

The game is really primitive. There are no sounds, no explosion effets, nothing fancy. But it is very cool =).

Here are some pictures.

Getting into TCL TK

I’ve, these days, just started learning TCL and TK. I’ve always heard it’s a simple way to build GUI, and that both the TCL language and TK were easy to learn.

The people who told me that were right.

A part of it which I think many people would have a problem with is its type system.

The language is weakly typed, and before people starting to jump on me for saying such a thing, let me explain what I mean by that.

In TCL, you’re supposed to program by considering that everything is a string. Internally, that’s not how it works for performance reasons, but every value have a string representation, can be converted back and forth to strings, and will be so whenever needed. When you add 1 and 13, you should see these two as two strings which get parsed by the command which performs the addition operation, and then added internally, and then converted back to you as a string. Again, it’s unlikely to happen that way for performance reasons, but it’s given to you the illusion that this is how things are getting done.

The conceptual model of TCL is (together with some other stuff) that everything is a string.

It’s a little bit like in lisp, in which you have the illusion that your program is a lisp list. Which is not true, although the implementation gives you the illusion of that. Internally, many lisp implementations will interpret your code in strange ways, or compile to some other format (C, JVM Byte Code, CLR’s CLI, some internal byte code format the implementation uses, …).

In TCL, part of your code gets “bytecode compiled”, but part also remains interpreted.

Because, conceptually, you have the illusion that everything is a string, that’s how you should program (there is no other way): all your data are strings. That’s your only data type! Which means the string format is what says what “data type” you’re dealing with: “abc 12 d” is a list, also is a text, but not an integer; “1241″ is an integer, but “12314121441312415145677872″ is not an integer (sizes), and both are lists (of one element). This means that performing list operations on integers are fine because there are no lists or integers. The operations on lists only suppose the string has a list format. Thus, “conversions” (or “looking at the value from another perspective”) happens all the time (of course that, conceptually, there are no conversions; everything is string).

Which is why I say it’s weakly typed.

It’s also dynamically typed, because the various “data types” are not attached to any particular variable. A variable that used to hold what is an integer string may hold a list string.

However, notice that since there is only one data type (the string), all your variables are of type string, and that’s never gonna change. So, in that sense you could say that TCL is statically typed. However, it’s a useless categorization in my opinion.

To talk about typing, looking at the function types would be useful (maybe mandatory), but I won’t do that in here.

One of the things that you can do in TCL is use the value of a variable as a procedure to call. And any value can name a procedure: “procedure”, “function”, “abc”, “strip”, “10″, “12.31″, “do times”, “a+1-2″. You can then store the name of a procedure in a variable, and call it later. It’d just work, because to call a procedure, all you do is start a command with a string whose value names the procedure. If my variable v holds the value of “puts”, and “puts” names a procedure, calling the value of v is fine.

This implies that you can decide, very easily, in runtime, which procedure to call. This leads to a lot of dynamicity. For example, a simple Rock-Paper-Scissors game can be implemented by making three procedures: rock, paper, scissors, which takes a string, and put out the right output message based on the value of that string (e.g. if the rock procedure takes the scissors argument, then it’d print “win”). The game now is basically reading a word, then another, then calling the first on the second.

You can define 10 to name a procedure, and still use it as a number because the command to perform arithmetic doesn’t see a procedure or procedure name: it sees a string. You could then call 10, add 10, get the first element of the list 10, get the substring of 10 from the first up to the last, and so forth.

It’s not necessary to mention how fun it is to play around with languages like that. It looks like total disaster, but it doesn’t seem to lead to disastrous situations in practice; on the contrary. Only time will tell though.

TCL has a bunch of useful built ins. And it’d be interesting if I could show you some programs exemplifying that, but I will leave this to TK, which is what I come to now.

I got into TCL because of TK. I wanted to build GUI apps, and heard that TCL with TK formed an easy to use combination. Sounded good to me. After two days of studying TCL, I thought “this language is actually easy to learn and use, it seems, let me go to the TK side, and learn how to build GUI; if something appears that I have to go back, then I will”. So I started learning a little bit of TK.

I never built a GUI before, and I always thought it was a “hard” thing to do because I’ve looked at some GUI code in the past (from some examples written using C and xstep, and some examples from a QT book I started reading, using QT and C++).

It turns out that TK feels a lot declarative. I’d say more than HTML and CSS, because of the wider amount of options in TK. In HTML/CSS it happens a lot that you build a “widget” out other elements (menus out of lists, for example). This happens in TK too, of course, but you have a much broader range of widgets to start with. Also, laying out your widgets on the window is not like a “struggle with floats or positioning”, you actually can tell “sort of” where you want it to go, and it will go there (it’s actually hard to believe how simple it is).

It’s declarative because you don’t so much say what are the steps to build the GUI (although you can surely see it that way), but you say what you want to be true about your GUI with the provided commands. TK then will just take care from there.

This makes building GUI a lot nice.

So, it has been something like 4 or 5 days since I’ve started learning TCL/TK, and I wrote an app for me. A program which filters the contents of directory by a pattern I specify, and shows me the result in a listing. I used to do that using grep and ls, or find but opening the file later is not that “easy” that way (mostly because I usually don’t have a rule for selecting which file to open after filtering; mostly if I filter music or videos, and there is also the case when I filter, but am unsure on how many files the filter will give me back; opening is really easy if the filter gives only one file name back).

So, a 140 lines of TCL/TK code did the job, and it was much better than I expected; although it has its problems.

The code to build the GUI is less than 40 lines long. It’s a simple GUI, but it’s much smaller than what I expected.

Here is the code:

Here is a photo of the program.

showing how I use dilter

Dilter in the context menu, also openned on the side.

The program is intended to use as something you open by right clicking a directory in windows explorer (yes, I use windows), and using it from there. The program does the files filtering in real time (each keystroke will update the list, which is nice). It can be made to work on linux by changing one line, which is the one which opens the file for you when you double click its item.

Updates on RegEX Parser/Matcher

Some days ago I coded a regex matcher and parser in C++, SML and Scheme. And I posted the code here. The program simply parsed and matched a regex pattern with an input string. It didn’t do any sort of fancy compilation from regex to some automaton, but the algorithms were really simple and really cool.

The first versions lacked some operations: intersection, xor, complement, optional. Now, these operations were added to the C++ and Scheme version. I have nothing against the SML version. Back then I had just finished a SML book, so I was really wanting to code in SML. Not so much right now. Maybe later, I will write the upgraded SML version.

Here are the newer proto.cpp and proto.scm. Check out the older post to find out more about this whole thing.

The syntax is explained at the top of these files.

By the way, proto.h remains the same. You can get it from the old post.

DFA to C Compiler

Just recently I thought I could use C very nicely to implement DFA. Using labels and goto statements do model states and arrows seemed really interesting. And I started writing some code to see how it’d go. Well, I liked it. Here is an example of it being done.

I didn’t run (I didn’t even compiled) this code (just wrote right now), but I am quite confident that it works and it does only accepts strings that are composed of 0 or more “110″. As labels model states, naming labels as you’d do with state is a good practice (to explicitly indicate the current state of computation). It turns out that this is very clear. At least to me (some people can’t like goto, no matter what).

However, there is a lot of code in there which have nothing to do with the DFA specification at all: the function definition, the labels, the switch statement, strings, pointer arithmetic, dereferencing, and so forth. Besides, the action of the switch statement on 0 is what determines if a state is final or not.

Which is all fine, except for the fact that it doesn’t quite map to the way I (and we?) think about this sort of stuff.

I’d much rather write this:

  1 => 1

  1 => 11

  0 => start

Which I think is much better (the ‘@’ before start indicates that start names an accept (final) state).

This is quite different from C, but the ideas map very easily. So I thought I’d make a compiler that take that sort of description and generates a function like that one.

It turns out this compiler is extremely easy to do. I’ve done it. It took me 400 lines of scheme code, and I may have over-complicated it (revisions would be really appreciated). Here is the C code it generates for the above DFA description:

It still doesn’t have a good UI (you have to hard-code the input file name in the source code, for example). Even though, it’s simple to use. It will spit out errors or the program, not both though. The “UI” is pretty much the last 10 lines of the program. So just go ahead and change it to do what you want it to do.

Here is the scheme source file.

This next link is an older version of the source, which I left here because it’s extremely different from the new (above) one.

It was made to run on chicken scheme (of course).

Here is a little explanation on this “DFA specification language”.

    • The general format is <state name>, followed by “:”, then followed by transition rules, which are triples <input> “=>” <output>.
    • <state name> maybe any valid C identifier, but they may start with numbers: a sequence of one or more of any of letter, digit or “_”.
    • <input> is a special keyword (“newline”, “space”, “tab”, “any” or “else”) or a single character. “any” and “else” means the same thing. And the other special keywords are for matching the characters ‘\n’, ‘\t’ or ‘ ‘.
    • If <input> is “any” or “else”, then it means that its output will be used in case  an input character doesn’t match any other input defined in the current state definition. “any” or “else” may repeat (the last one will be the effective one), or may not appear at all (which is the same as having written “else => reject”). “any” or “else” need not to come at the last transition rule (as you’ve probably already concluded).
    • <output> is either a <state name> or a special keyword: “accept” or “reject”. If “accept” is found, it means the DFA will accept the input, regardless of what comes next. If “reject” is found, it means the DFA will reject the input, regardless of what comes next.
    • An exception to the general format is the general format prepended by a “@”, which means that the state being defined is an accept one. Which means that if there is no more input and the machine is currently on this accept state, then it will accept the input; otherwise, it’ll reject the input.
    • A state definition is either a general format or an exceptional general format (the above).
    • A DFA is defined by defining a bunch of states.
    • The start state is the first state.
    • The alphabet is the set of values all char can hold. Notice that there isn’t syntax, though, for specifying all these values in this language.

As mentioned in the last point right above, this language doesn’t let you specify all the characters of the alphabet as inputs (there is no syntax for that). Basically, if you can put it into the file, then it’s going to be used (the algorithm simply takes the non-space-ish character that is supposed to be the input and put it between single quotes in the C output).

If the source program is valid, the output of the compiler will be the definition of a C function, which takes a string and validated it according to the DFA specification.

Here are some additional features of the compiler:

    • The error messages seem quite nice (only time will tell though).
    • It can spot unreachable and undefined (but used) states.

I haven’t tested it so much, but so far, it didn’t generate wrong C programs out of a considered valid DFA definition. I fear though that the “error catcher” is not so good.

It’s yet just a prototype. Also, I also made this purely out of “just wanna code this because it’s fun”. I guess this could be useful if you need validators, but using generic regex libraries would make your program too slow (which is not, as far as I know, the situation most people are in). It looks useful, but that’s not why I made it.

Future ideas are to make a finite state transducer. Also, a program which takes a list of words and make a DFA specification which matches one of the words in the list. A program which can generate combinations of DFA (through unions, intersections, complement, etc.).


Get every new post delivered to your Inbox.