Posts by johan.

Why Scala? (2) Compact syntax applied to probabilistic actions

As a little fun project, I developed some probabilistic cellular automata with Scala and very basic AWT graphics. I continue to become more proficient with Scala, and it feels increasingly natural to use. During this exercise I came up with something that I thought was particularly elegant, and that I am pretty sure would have been a lot less readable in Java. I will just reproduce the interesting bits. The basic idea is that I want cells on a 2D grid to take certain redrawing actions according to given probabilities. First, I define this utility function:

object Util {
 
	//sum of floats (probabilities) should be at most 1.0
	def multiAction(acts: Seq[Tuple2[()=> Unit, Float]]) = {
		val r = Math.random
		var soFar: Float = 0
		var acted = false
		for ((act,prob) <- acts) {
			soFar += prob
 
			if (soFar > r && !acted) {
				act()	
				acted = true
			}
		}
	}
 
}

The idea here is that we supply a list of tuples. The first element of each tuple is a function, and the second element is a float value between 0 and 1 representing the probability that each function is evaluated. Only one of the functions supplied is actually evaluated.

This is how I put it to use (excerpt from another class):

Util.multiAction(
  List( 
  (() => {
     cellsWrite(x, y-1) = cellsRead(x, y-1) + 0.01f;
     cellsWrite(x, y+1) = cellsRead(x, y+1) + 0.01f 
    }, 0.2f),
  (() => { 
     cellsWrite(x+1, y) = cellsRead(x+1, y) + 0.01f;
     cellsWrite(x-1, y) = cellsRead(x-1, y) + 0.01f 
    }, 0.1f)
 )
)

Once you take in the braces here it is actually quite simple. We have two anonymous functions taking zero parameters of two statements each. The first one has probability 0.2 and the second probability 0.1, meaning that there’s a 70% probability that nothing will happen. We can also make an arbitrarily long list of such functions on the fly.

To the best of my knowledge, the only way of reproducing this flexibility in Java would be to create anonymous inner classes and put them inside an array. Certainly that would be quite a bit more verbose than this.

The absurdity of flying

The first time I found myself onboard an airplane was when I was 9-10 years old or so. At the time, travelling by myself to visit my aunt who lived on a remote island was a big experience. In particular, I think, the sensation that the environment was managed in the extreme made a big impression on me. The temperatures and winds outside my seat window were a hostile element, but human technological achievement successfully shielded me from these dangers. I could take part in the collective human pride in this affirmation of technological ability.

Much later, when I was a student in London, I was subject to budget constraints and went for the cheapest flight whenever possible. Accordingly I found myself flying with an Irish airline, Ryanair, quite a lot. This enterprise is marked by its grisly yellow and dark blue colour scheme and continuous experimentation in lowered flight standards, comfort and safety, all for the sake of lower prices. For a 1-2 hour flight between England and Sweden it was fully acceptable.

Recently I have been flying between Japan and Sweden quite a bit. The intercontinental flight can last more than ten hours, and takes on quite a different character from short flights. Some of the essential absurdities of any flight journey become increasingly difficult to ignore during this time period.

Firstly, there is the fact that the airplane that more than a hundred passengers ride in is a sealed off, highly fragile, mobile cross-section of society and a habitat for human beings. Airplanes need continuous replacement, draining and replenishment of food, waste, excrement, water, fuel and electricity. The air pressure and temperature inside the cabin are artificially maintained. The similarities with an imagined future biodome on the moon are not a few. What happens if an airplane has to land on a tiny island in the middle of the ocean and doesn’t have enough fuel to fly back, or there is some kind of technical problem? All of these buffered flows which the airplane must always replenish would be interrupted, and our very lives are hooked up to those flows.

In addition, hundreds of people are placed very close to each other for an extended period of time with minimal lateral separation (although there is some longitudinal separation in the form of seat rows). A certain neuroticism is provoked. We become hyper-aware of our neighbours and what they do, what they talk about, how they dress and what habits they have. We try our best not to notice. And this lattice, this packing of people, is surveyed from above by the panoptic eyes of the flight stewards and hostesses. Observation not only from above but also from peers becomes essential in maintaining order in a closed-off society where governmental violence cannot reach and the usual norms might easily be violated. Security breaches are to the greatest possible extent preempted by the pre-flight security theatre, and what remains of risk is contained by observation and observability effects.

This pressurised air and pressurised micro-society is spiced up, or muddled, slightly by the increasingly confused roles of the stewards and hostesses. In the jet set era, the air hostess was an object of attraction, an apple of the eyes of businessmen, an icon of liberty who had authority but no doubt also a certain intoxicating effect which helped to pacify. Today she is more clearly authoritarian, but the old role has not quite been erased from people’s minds. Something oedipal threatens to take place. Is this person who serves me food a nurse, a security guard, a mother as well as a possible lover? The neuroticism of the family extended into international airspace. All authority figures merged into one. Male stewards only slightly less confusing.

Fortunately airlines are very happy to serve up small doses of wine and beer to take the edge off the situation. Flying is absurd, but for the moment we have no other way of getting around.

Continuous computing

Disclaimer: I haven’t checked academic sources for any of the statements made in this post – all of it is speculation which may be affirmed or rejected by existing literature.

Existing computing hardware and software are based on a discrete model: the Church-Turing model. The machinery is built on digital logic, and formalisms such as lambda calculus and turing machines are also essentially discrete. But what if we were to attempt to build some kind of continuous, or non-discrete, computer?

Digital logic gives us some unique capabilities that do not seem to exist in the real world, for instance: the ability to read a value without altering it, the ability to copy a value without altering it, the ability to test for equivalence and receive a yes or no as an answer. (The whole idea of “equality” is digital/platonic in nature.)

It will not do to simulate a continuous computer in software, not even with arbitrary precision arithmetic. It seems that some properties that a continuous computer might have would be impossible to simulate on discrete hardware. At least, we would need some kind of non-digital hardware extension that produces the continuous operations.

The discrete, digital model may seem like an abstract ideal, disjoint from reality. Yet continuous, real numbers are at least as much of an ideal. Between any two real numbers, no matter how close they are, there is an infinite amount of intermediate real numbers by definition. It seems implausible that we could find this infinite amount in the real world.

Is the real world continuous or discrete? I don’t know, and last time I asked one of my friends who knows physics, the answer I got was too complicated to be reduced to yes or no, or even to “yes, mostly” or “no, mostly”, if memory serves.

What properties might a continuous computer have? Depending on how it is designed, maybe some or all of the following:

  • If we compute a value twice, there would be a level of precision at which the results appear different
  • In fact, there is no way to establish the absolute equivalence of two values, equality is reduced to a matter of precision and generalisation (as it in practice already is for computer implementations of floating point arithmetic today)
  • The simple act of reading a value might alter it slightly.
  • The more steps a value passes through (i.e. the greater the number of times it is copied), the more it deviates from the original value
  • The ability to truly move a value, as opposed to mere copying and deletion, might become important, to mitigate the above effect (digital computers cannot truly move values)

We must also ask the question: how do we model continuous computing mathematically? Is it enough to allow for numbers with arbitrary range and precision and use standard logic, simulating the destructive effects of computation somehow? (Probably insufficient). Could we generalise lambda calculus/turing machines to abandon their inherent discreteness and end up with a more general formalism?

If we accept the above list of properties, even if we concede that we cannot accurately simulate a C. computer on discrete hardware, maybe we can build a simulator that gives us an idea of what a real device might behave like. But we would have no idea what we’re missing.

Motivation? The main motivation is that it is interesting, i.e. it promises to point us in new and powerful directions, laden with potential discoveries. If something more concrete is needed: intuitively, we should be able to bridge computer software and the physical world much more easily with this kind of system, bringing benefits to UIs, simulation and modelling, etc.

Edit: After writing the above, I found out that people have investigated the idea of analog computers, which intersects with the idea of the (perhaps poorly named) continuous computing described in this post. The image at the start of this post is a diagram of the Norden bombsight, an optical/mechanical computer used in WW2.

Deletion

A characteristic of a naive approach to the digital world is the tendency to record and store everything. JustBecauseWeCan. Every photo, every e-mail, every song, every web site ever visited, every acquaintance who ever added you as a friend on some social network, every message you ever received. Somebody, probably an author, termed this the “database complex”, I think. A projection of a certain greedy tendency to gather and collect things. This does have certain benefits when coupled with a good search function. Every now and then I find myself having to use some information that only exists in an e-mail that I received 6 months ago or so.

A more advanced approach is selective forgetfulness. Humans cannot go on with their lives if they do not forget memories and experiences that are irrelevant and useless. They become unable to set and act on new targets. I think  that a slightly less naive digital life would contain a measure of deletion. Deletion of files, old e-mails that have probably become useless, “friends” on social networks who are mere acquaintances or even less, and so on. Taking away the old makes space for the new. It can be especially powerful to see the number of files in your home directory reduced from 50 to 5. A lot of confusion and ambivalence is immediately removed.

Part of taking the next step step deeper into the digital age should be deciding, each for themselves, what one’s personal thresholds and principles of deletion are. What should be deleted, when and why? In our brains it has been managed by evolution for us. Now we must manage it by ourselves.

Provocation and adaptation

My last post, on the topic of resisting the circumstances in life, ended with a question. What choices should I make to resist maximally, given that choices make me stronger, i.e. choices have long term side effects on me?

So I would like to, probabilistically, maximise my set of skills in order to best be able to achieve some kind of ambition I have set for myself. Cutting off my hand will probably not help me, but learning arabic might. Being in a car crash is unlikely to be helpful, but being a marathon runner could conceivably be useful. Both involve pain, but one causes irreversible damage, the other causes an increase of strength if done properly. What is the ideal form of schooling for children (If we take the unlikely view that the purpose of schools is teaching things)? That which increases their ability the fastest, which is to say, the most difficult knowledge, the fastest speed of teaching that they can possibly cope with. The maximum trajectory that they can sustain without losing the grip or their interest in the subject.

Should I do the same in life, then? Probably, but it gets tricky, because life experiences that promise to teach me a lot are often unfamiliar, or dangerous, or otherwise involve pain. As we have seen, it is not the case that pain equals learning, but pain can be strongly correlated with learning. To be more precise: if I become crippled in a car crash, or by cutting off my hand, it is because I received stimuli from directions and with intensities that I could not withstand. Provoke me at a slowly building rate, and I will learn to deal with the provocations and perhaps bite back. Provoke me really hard and really fast from the start, and I will die. And then there are provocation vectors to which individuals cannot adapt in a single generation, for instance, drowning. Species might adapt to this kind of threat over several generations. Is not life precisely that which adapts to changing circumstances, potentials and provocations, in particular potential threats or benefits? But intelligent animals, like humans, are a special form of life. We can select what experiences to undergo, and thus what training to receive. This is how we can consciously adapt in advance when we expect a difficult situation. (Young animals play in order to train themselves for adult behaviour, but this kind of training has been conditioned by evolution over many generations. Are there any animals that train selectively to face threats that they have identified during the same generation, like humans do?)

If I identify the maximum “provocation rate” that I am able to withstand concerning a particular skill, another problem I would want to solve is: do skills compete? If I learn Arabic very well, will it downgrade my Russian? If I become a marathon runner, will it disrupt my ballet dancing ability? When a skill involves a particular conditioning of the body and the muscles, it is probably easy to see that some skills conflict. When they involve a conditioning of the mind, it is less obvious. Is the mind flexible enough to support radically opposed skills and viewpoints at the same time? Is this property the same or different for different people?
Questions that lead to more questions.

Resisting circumstances

Friedrich Nietzsche famously said that “what does not kill me, makes me stronger.” While true in some ways, this statement appears to be a generalisation masking a more complex truth. For instance, cutting off one’s hand does not kill one, but hardly makes one stronger, unless one specifically desired greatly improved dexterity of the other hand, even at a very high cost.

It is a fact that we cannot predict all the circumstances that we will find ourselves in throughout our lives. So we cannot predict what skills or strengths we will need either. Any one who has some kind of ambition in life has no way of establishing completely beyond doubt that their ambition will come true. They can only work towards reducing uncertainty.

At this point a number of different attitudes emerge. One could take the view that “Life is nothing but suffering. We must learn to cope with it.” Subsequently one could teach that suffering is a thing in the mind, and that training the mind to absorb suffering without feeling pain or becoming upset is our best hope. Either that, or reduce the ambitions so as to be frustrated less often. The goal of this ambition reduction is zero ambition, zero desires and zero expectations. With this mindset, you can never be let down. Nullified resistance, maximum fluidity.

Another view: life presents us with challenges, some of which we may overcome, some of which it is pointless to even try overcoming. A “pragmatist” view that tries to establish a middle ground. Some suffering is worth resisting, some is too much. People taking this view have some degree of resistance, but also a breaking point at which they would accept that “life is hard” and bend according to the circumstances of fate. Maybe they would also be opportunist and take their chances for easy gains when they can, to get revenge on life.

And finally, let’s look at the other extreme view. Nietzsche also said, perhaps slightly less famously, that “only to the extent that man has resisted, has he lived.” If I take this view, that I should resist adverse circumstances maximally and have my way in life, I must handle the problem mentioned at the beginning of this post — I cannot predict the circumstances that will befall me. No matter how strong I am, it is likely that there will be some set of circumstances that might destroy my aims completely, and me in the process. But let’s say that I take the view that some outcomes are less likely than others. I buy into some form of probability, for instance I think that five dice are less likely to all have the number four facing up than they are to not end up in this configuration. What choices should I make to maximise my ability to resist, given that some choices actually do make me stronger?

On statefulness

Last year I made some attempts at free association around formal languages and state machines. But at that time, not much was said about the idea of a state itself; an idea which I think holds a lot of interesting uncharted territory.

To begin with, what is state really? Intuitively the word distinguishes states of an object. The key here is the plurality. A single state in itself is uninteresting. Only as contrasted with another state does the first state acquire meaning. This leads us to an interpretation: states are a way of grouping all the possible forms-of-existence, for want of a better word, that an object has, which lets us make sense of such forms more easily.

To exemplify: the light switch in my apartment can be on or off. But in physical space, the plastic switch can occupy a very large number of positions between one and zero. However, the spring mechanism forces the switch into the first state or the second state as soon as I release my finger from it, giving rise to two distinct functional states. When I was a kid, I would sometimes play with the rather old light switches in my parents’ house by keeping the switch in the middle between on and off. A humming sound would be emitted, and the lights would flicker on and off. Surely not a very good thing for the fittings, and potentially dangerous, but interesting since this broke down the abstraction – the continuum behind the discrete was exposed.

So given a physical system, then, which remains the same system even as some parts move around, electrical currents flow, etc, we use states to partition all the forms of existence of that system into meaningful ideas. “The door is open/closed”, “The engine is turned on/off”, “The engine is turned on but there’s almost no fuel left”, and so on. States have probably been with us as long as we have been able to think of binary distinctions, which is to say throughout the history of mankind – opposites such as day/night and alive/dead must have been with the human mind from prelinguistic times.

Today, states are an essential way of turning the unmanageable analog realm into a finite, subjugated digital representation.

Meta notes: 1+ year with Monomorphic blogging

After 13 months and 51 posts, my experiments in blogging continue, although they are perhaps better described as polymorphic than monomorphic. Maybe it’s time for some reflections.

On the whole blogging in this format and at this frequency has been a pretty fun and fulfilling process. I get to practice writing free-form, nonscientific texts, and even if many of them might not be read by so many people, the idea that they might be turns it into a useful exercise.

Recently Flattr buttons were added to this blog, which allows users who use the service to donate money and show appreciation for my texts (some such people indeed exist – thanks a lot, all two of you!). Initially I had a single button for the entire blog, but now I am trying out a format where I have one button per post.

I’ve noticed, on this blog and elsewhere, that I can’t quite decide if I should write with British or American English. I feel culturally uncertain as a writer of this language. But recently I’ve come to think that I should embrace my European background, so more of the British variety in the future is a likely prospect.

Topics have been varied. The tag and category systems have been used in an attempt to bring some order to the table, but they’ve become too chaotic to be useful. A restructuring is perhaps in order during the next 13 months.

One of the most popular topics I’ve written about has been the Scala language. People tend to google Scala a lot, and it’s actually really uplifting to see the interest in it (since I hold it to be a way forward). If you are a blogger who wants to get a billion page views, write about Scala. I don’t want to consciously pander to the readers too much, so in itself it is not a reason for me to write about the topic. I will write about Scala when I want to say something about it. (A difficult principle to really practice.)

I’ve tried out some different WordPress themes occasionally, but so far I haven’t found anything I like better than this “Infinimum” theme. It feels very clean, functional and modern.

That will be enough of the reflections for now.

The identity crisis of the internet

The architecture of the Internet is fundamentally decentralized, a fact that continues to impress to this day. The breadth and depth of the sea of applications and uses we have made of it, and its resilience, impress perhaps all the more, because many of our experiences from everyday life tell us that some of the strongest things in society are singular and centralized — huge companies and governments, for instance. I’m actually not an expert on internet architecture, but my understanding is that the only thing that is fixed in it is the DNS system, which relies on some top level hardcoded IP addresses and coordination.

But even though the Internet is built on a decentralized architecture, it also supports applications/services that are highly centralized in their architecture and in their intended use. Google and Facebook are two very famous such applications. On the other extreme are applications that might be called P2P, including notorious file sharing systems such as Bittorrent, and also simple email (which was designed for decentralized use but is becoming heavily centralized with services like Gmail).

In recent days there’s been much discussion about Facebook’s role, particularly since it has been taking more and more liberties with the vast amounts of data about it users that it holds, scaling back the notions of privacy and integrity as they see fit. Many people are calling for decentralized alternatives to Facebook to rear their heads, and I suppose people have been calling for decentralized search engines as well for some time.

Much seems to be at stake here. What’s the future direction of the internet? A few giants holding all the data, monopolising certain functions, or a distributed network of peers, creating functionality together? The debate is ideologically charged and could be mapped into a big government/small government discussion, although I think it would be fruitless to do so. What is certain is that radically different applications can be created using the centralized/decentralized models and that it is rarely a case of merely “porting” an app from one architecture to another, the way you port an application from C to Java. On an abstract level, the two models could serve as substrates for the same functionalities (such as social network services), but the concrete implementations would have very different characteristics.

Do we create centralized applications because our legal systems, property rights systems, and so on, have not evolved at the same pace as our infrastructure, so that our tendencies, habits and ideals from a brick-and-mortar world are preserved in the world of fiber and switches, appearing ever more outdated?

In Sweden this debate has been especially pronounced recently with companies like Flattr being firmly on the side of decentralized models. Flattr is trying to be a universal donation system for content on the internet, and the vision behind it is a large number of decentralized creators of “content” (which are themselves consumers).

I’m not sure which model will win in the long run. I prefer to think that both models have a role to play and that they can coexist nicely. But lately it seems as if the centralized model has had a bit too much momentum. Let’s dig deeper into the decentralizing potential of the internet!

A problem solving method

Here’s a general method for synthesising solutions to complex problems, intended for use by people.

1. Enumerate the constraints

2. Find an initial solution that feels right but doesn’t quite work, based on previous knowledge of the domain

3. Use the information contained in the constraints to adjust the solution, so that it works

4. Either you have succeeded, or new constraints came out of the process => go to step 1 and repeat