Friday, 5 September 2008

Parallel Programming 1

I've recently been thinking about how to write a text editor. I've written one or two in my time, so why write another? Well, mostly as a programming exercise and because I can.

In thinking about how to do this in a new and novel way and most importantly: efficient way, I came up against some mental hurdles.

In any programming project there is a translation that has to happen between what's in the head and what comes out on the page. Somehow the model in your head has to be coded into a model inside a computer. This process is hard.

So how to write a text editor from scratch? Inside my head, I have a document, a mouse and keyboard and type away. What's so hard about that? Well the problem is one of representation. Let's break it down.

First the document has to be modelled. Well, its a bunch of text contained in a set of lines. This translates to an Array in code. Good. The size of the array gives you the number of lines and each element of the array contains a line of text. Well, maybe.
The problem is that of wrapping, ideally we want our text to continue on to the next line when we type, if we reach the edge of the screen (or window). Ok, so we can use a big string of text to represent our entire document instead, and wrap to our heart's content. Good. Well maybe not.

The problem with a big string representation of text is that for a given position in the text, you don't know what line on the screen you're on. And you need this information in order to position the insertion point (cursor) correctly. It gets worse, characters on a modern system all have different widths, an i is skinnier than an o, so if we have a line of text in an array, we don't even know where to put the cursor horizontally without taking all those widths into account. Ok, so we write functions to work all these things out for us.

Ok, so we plumb for the big string representation, keep a note of the character position in this string to represent the insertion point and run a bunch of functions when we need to work out screen positions etc. The problem with this, is that it's inefficient. Any time we change any bit of text, we need to recalculate the whole lot to display it on the screen.

So how can the efficiency be improved? The answer is to have a good model in the first place and to only re-calculate the absolute minimum in response to a keystroke for example. This is hard, primarily because we have to take a lot of cases into consideration, and re-calculate differently based on each case.

The answer will be in my next blog.

Wednesday, 20 August 2008

All Change Please

Nothing lasts for ever. It really doesn't. Choices are made, things change. We are awash in a sea of change. This change is fundamental to the fabric of the universe. Nothing is ever at a standstill, from the quantum foam of virtual particles to the ever expanding universe. We are made from change.

Change is inescapable. So why is it so resisted by some of us? Life, in a word. The sheer fact that life persists in this ever changing flux, means that in some sense it thwarts change. This is survival, a fight against change. And being alive, we have the survival instinct. This instinct to survive permeates our very essence. We are born with these instincts, but some need to be learnt and this learning can be imperfect. We learn to fear change because it threatens our survival.

Because of the barrage of change around us, we learn to assess risk to our survival very quickly, even as babies. The assessment of risk is about crystal ball gazing, looking into the future and imagining what might happen. It's an uncertain science by its very nature, some things are more certain than others and can be more easily imagined and played through.

It's uncertainty that builds fear of change. Of course uncertainty isn't really a thing, it's a process, dynamic. This means uncertainty can be reduced. There are several ways to do this. Firstly, is improve your internal modelling of the future, by gathering more information. Ask other people about their experiences, research it. Secondly, find new ways to model it, use your imagination to its full potential, allow yourself to get engrossed in the details of your future - this will increase the certainty that you can beat change and survive. Thirdly, take the risk. This can be done in stages, so that the change (risk to survival) is manageable.

Lastly, have a goal. This works on the assumption that we all constantly strive for something better (see Boredom). If the thing or future is tantalising enough, we'll be prepared to take a greater risk for it. In other words, if we can improve our chances of survival in the longer term in exchange for some uncertainty in the shorter term, then we generally will.

So the goal has to be good, appealing. Goals are of course imaginary and so you need to employ your imagination to its fullest and create an attractive risk. And of course, this doesn't have to be a solitary activity. If you can share it with others, all the better.

Beating Boredom

Boredom is great. I think generally it is seen in a negative light (strange how many words seems to fall in the negative and positive categories: sadness, hate, happiness, love). In fact I would say boredom is one of the really defining traits of being human. Generally it denotes a lack of interest in whatever is going on at a moment in time. Often when lack of interest kicks in, our minds wander on to other things that we would like to do and this seems to intensify the boredom we feel: it is a motivator.

This is an interesting mechanism. Boredom allows us to disconnect from what's going on around us and allow us to dream a bit. Children generally don't deal well with boredom, they can become agitated and downright obnoxious. But as we get older we learn to tolerate it, and this is seen as maturity (a positive word). In learning this tolerance we can become complacent to the point that we stop recognising that we're bored and just accept things as they are.

Maybe we should be more like children. I don't advocate shouting or moaning 'Are we there yet?' or kicking up a fuss whilst waiting for a bus. But allowing ourselves to dream and create options for ourselves, to think about what we want next from life. And, also to enlist others in stopping our boredom. Maybe you'll find out they're bored too.

Boredom can also occur at different levels. You could have a completely comfortable life, a well paid job, a nice house to live in, good friends and family. So on a micro level, there's no boredom, you're content and occasionally interested in your life. But you still get twinges, nothing changes, you're just playing the same routines in different combinations. Being content just doesn't cut the mustard. You're bored, at a macro level.

The way out, is to acknowledge the boredom, revel in it. Allow yourself to disconnect, and then start dreaming of something more exciting. This will of course increase the boredom, until the point where you do something and start carrying out those dreams. It may take time, but if you dream enough, the motivation builds and eventually something happens.

Tuesday, 19 August 2008

Conciousness Delayed

I read in New Scientist recently something along the lines that, the impulse to move a finger is created somewhere in the brain a full seven seconds before the finger actually moves. It was something like that anyway. In any case it set me thinking.

It stands to reason that the signals or potentials around the brain take a finite time to be 'processed'. This processing is massively parallel and happens asynchronously. It would also seem reasonable that different pathways take different times to process. For example the auditory pathway may be quicker than the visual pathway, which has to handle greater and more complex information. But, in our concious mind things happen simultaneously. The sound of a plate smashing happens at the same instant you see it hit the ground. The only way this could happen is for the processing in the auditory pathway to be slowed down or delayed and then somehow synchronised with the results of the visual pathway.

This idea could be extended. Maybe our entire concious experience is delayed? By delay I don't mean micro seconds or seconds, but longer. How could this make sense? Surely, this concious experience is needed to in order to react in a timely manner to our environment? Maybe not.

It's obvious that a lot of what goes on in our bodies is ultimately out of the reach of conciousness. We cannot affect the functioning of our liver for example, by thinking about it. But what if everything were out of the reach of our conciousness? What if everything we did was simply driven unconciously. Then, the delayed conciousness wouldn't be so problematic. Instead it would be more like a replay of what just happened outside of conciousness. This severance of the equality of conciousness with reality, would allow the brain to take as long as it likes to create the concious experience, while still processing reality 'out there' in a timely manner.

So what's the point of conciousness? Do we need it? My guess is that it's something to do with social cohesion, language and identity within the group. In other words, consciousness allows us to report our internal world to others.

Programming the Web

I do a lot of web programming at work, with a combination of PHP, HTML, CSS and Javascript. Once you've understood how each language works, using them together is no problem. The thing is, is that really all these components of web programming were designed separately from each other and bolted together. This makes for ineligant code, to both program and read.

There are several things that spring to mind when coding web pages: the need for escaping, the differing syntaxes, the similarity of Javascript and PHP. Wouldn't life be simpler if these components were unified?

The answer is to employ the block notation of C like languages (such as PHP and Javascript). This could be used to immediately simplify HTML and make it less verbose. For example an ordered list might look like:

ol {
li{First item}
li{Second item}
}

Given good identation, this is already more readable. Also this ties in a lot better with the other three components and their syntax. How are HTML attributes to be done? Again the answer is to pinch the notation for functions in PHP and Javascript:

table (width: '25%'; border:'2px') {...}

So the semantics here are that parameters are in round brackets whereas code or children are in braces. The parameter syntax is very similar to that of the existing CSS syntax, so this can also be brought into the fold:

#div (
text-align: 'left';
position: 'absolute';
)


Note the round brackets. This scheme allows us to get rid of the escaping nonsense and to remove the sense of these disparate components being bolted together. How so? Well luckily most HTML tags are different from PHP and Javascript keywords. For example you could do the following:

html {
body {
for ($index = 1; $index<=10; $index++) {
echo 'This is body line '.$index;
}
}
}

This example shows how you can freely mix HTML markup and PHP code together, without having to escape from one to the other. This makes for clarity in reading and reduces the semantic baggage when trying to decipher code. Here's an example which would mix all the components together:

html {
head {
script{
function sayHi() {
alert('Hi there!');
}
}
style {
.box (
background-color:#009966;
)
}
}
body (bgcolor: 'red'; text: 'blue') {
div (onClick: sayHi(); class: 'box') {
'Click me';
}
}
}

The only exceptional thing here is how to handle blocks of text in HTML. These would need to be escaped in some fashion, so as not to be confused with PHP code. I'm not 100% sure how this should be done. A simple way, as shown in the preceding example, would just be to enclose the text in quotes. Another way would be to have an explicit 'text' tag, but then you may as well use the 'echo' statement in PHP.

How would such a file be used in practice? The ideal of course would be for a web server like Apache to read the file and execute it straight off, emitting the result as conventional HTML/CSS/Javascript. A second easier solution, is to write a compiler which would convert the new format syntax into the conventional style. The converted pages would then be used as normal.

Lastly, is the issue of Javascript/PHP. It would be wonderful, if there wasn't a need to learn two separate languages with very similar syntax. Why not go the whole hog and produce a hybrid of the two? Maybe easier said than done.

Monday, 18 August 2008

Choices Choices

Choice is funny thing. Not that it always makes me laugh, but more like it's hard to pin down. But, I think there are three parts to the art of choosing.

The first part, is that there is a choice to be made. We need to create the choice in the first place. What? Ok, imagine being in a restaurant with a menu in front of you. What choice do you have? Well you could have a starter of soup and a main of fish. Aha you say! The menu dictates your choices. Well, not really. You could get up and kiss the waitress and walk out of the restaurant. You could ignore the waitress and sit in silence. You could sit at someone else's table. You could have chosen not to go to the restaurant at all. You create the things to choose.

The second part is making the choice. In fact this is very simple. In that particular instant, you choose to do one thing over another. Have another beer instead of go home. Sleep another five minutes instead of getting up. Tell your girlfriend you love her instead of being angry at her. The act of making a choice is very very simple and we all do it constantly, without even thinking most of the time.

The third part is the hardest and that's carrying out the choice. The consequences of some choices are very simple: eating soup. Others can be complex in the extreme: bringing up a child. Surely that's what gives a choice its substance? Well, not really. At the point at which we've chosen to do something, the consequences of the choice are still inside our heads. None of it has actually happened yet. And even after we've started getting embroiled in the consequences, we still have the choice to stop and do something else.

So what stops us from choosing the things we want to do in our lives? A great many things. First is physicality. I can't go swimming right now if there is no pool near me. I can't lift 200 kg, as I don't have the strength. Second is social conditioning. I don't kiss waitresses and I don't walk in the street naked. But unlike physicality, there is absolutely nothing to stop me doing this (other than the physicality of other people). Third is direct past experience or learning. I know that it hurts to break my arm, so I try and avoid it. Other people have reported that it's nasty to be run over by a car, so I choose not to do it.

How then is someone to make a good choice and avoid stress? The following things will help:

1. Let your choices be directed by a goal or aspirations.
2. If you don't like the choices available, make more suitable ones up.
3. Don't be held back by the potential consequences of a choice: it hasn't happened yet.
4. Put yourself into the place or circumstance which allows you to make the choices you want.
5. Sometimes it's better to choose - even if you think it's random - than to choose not to choose.

Friday, 15 August 2008

Happy Blogday

Well this is my first blog ever. Thanks to Mark Roworth for the inspiration to write this, you can read about his adventures on his blogspot.

Being new to blogging I really don't have any idea about what I should put down. But if I keep it up, it will probably evolve into something interesting - hopefully.

I'm currently sitting in an air conditioned room up on the seventh floor of a tower block building that constitutes City College. Out of the window I can see a residential tower block built on top of a car park, an office complex which includes the DVLA office and the side of Brighton Station.

City College has been my work home for over four years now and I feel like part of the furniture, old, well used and in need of attention - ha ha! Don't get me wrong though, I like it here. The people are friendly and the stress level is minimal - which for those of you that know me, is high on my agenda. And, god damn-it, the work is occasionally interesting. Who could ask for more? Me probably.

One of the things I like to do to make my work life more interesting is to think up new stuff. Of course being a programmer, I have to do this pretty much most of the time. Every bit of software I have to write is a unique being, with its own personality and problems. These beings are hungry things and devour concepts and ideas and time with great voracity. Like cheese, most of these ideas are fairly mundane and common place. But, some of them are more like gourmet food and require a lot of preparation, thought and a fair sprinkling of skill.

This analogy with food is apt, since food is also a great love in my life. I especially like strong tasting, interesting food. The flip side is that food is a very functional thing for me: I get hungry, I eat. This means that I'm happy to eat the same food day in day out, as long as it tastes good.

And that brings me on to stress. Essentially, the less of it the better. Of course, stress can mean many different things. But my definition includes a discomfort of some sort. So it may be physical, like not having had enough sleep. Or it may be mental, like trying to decide whether I want a new job. I do actually have a strong philosophy on stress, which helps to keep me sane, and also explains a large part of my character.

My working assumption is that stress is produced from within, but is often triggered from without. What do I mean? That I'm the person experiencing the stress, and that this stress is often triggered by other people or circumstances. I would go so far to say that all stress is triggered by other people. We are not islands, but connected.

This point of view allows me to have some semblance of control over stress. I can choose to move my left foot if I want to, and equally I can control being stressed, it's just learning how that's the hard bit.

To control it, you need to understand the source of stress. Fundamentally it's to do with choice. Now, I think there's no such thing as no choice, in any situation there's always a choice to be made however small or large or complicated or difficult. Stress occurs when there's more than one choice to be made in a given moment. It's this dichotomy between choices that is stress or causes stress. So to remove stress, the dichotomy needs to be broken, i.e. a choice needs to be made. I'll talk about making choices in a later blog.