Monthly Archives: January 2011

The Almighty Dollar

@luqui – Buy the change you wish to see in the world.

This Gandhi rip-off tweet is a summary of an idea that I would like to share in more detail. Last week I got sniped and swindled by a street peddler of Children International, a charity organization, largely due to a weakness of boundaries I had at the time. I wasn’t really feeling charitable, and I just wanted him to stop talking at me without being rude. The most immediate way out to my uncreative brain was to sign up and cancel later. I may have felt more charitable if it wasn’t so much money — $30 per month — but I was sure that I was going to cancel, and put it on my immediate to do list.

When I had a moment to clear off some items on the list, as I was looking up the number to call, I pictured the phone call. Having already done research and found that the organization was legitimate and even efficient, there was no excuse there. Then I was going to say “I just can’t afford it right now”… which would be a lie. I am a poor college student, but I am still privileged by college, and I spend about $300 per month on food. Saying I couldn’t afford $30 per month is just outright wrong. Lying to a charitable organization is beyond my (comparatively flexible) morals.

I couldn’t find a way out that was consistent with my self-image. Thus, I haven’t canceled, and I don’t think I’m going to. Faced with the inability to prove that I shouldn’t spend this money, I began to search for reasons why I should. And the above twitter quote is the one I found, in a nutshell.

America is a severely capitalist nation. It has a fair amount of socialism mixed in, but it is still one of the most money-driven countries on this planet. We criticize big corporations in general for being immoral, corrupt, greedy entities that are ruining the world. They have great power, and they wield it in offensive ways. Damn them! Clearly pure capitalism can’t support a humanitarian world.

I used to believe this. But let’s think about it: from where is their power derived? From the economy of their country, of the world. They have tons of money and power because we give it to them. They provide us with valuable services, and we in turn compensate them with money, which is essentially equal to power in a capitalist society. And then we complain when they use that power in a way that offends us. So we are not really unconditionally giving them power: we wish to say “you can have the power to do things we agree with”. Not really power at all. We want to use their services without compensating them.

A dollar is a vote! It is a unit not only of trade, but of trust. But we routinely buy products from companies we do not trust. And no wonder they do evil things… we gave them a symbol of our trust without actually trusting them. We “the proletariat” are the ones who are ultimately producing the value in this nation, and we are collectively being compensated for it. Taken as a whole, we have enough power to match or exceed any corporation (I haven’t done the research, but I think the principle is pretty easy to agree with). We are being very irresponsible with our money — our tokens of trust — giving it to people who we know are evil. We are creating the evil in the world, simply by being fast and loose with our money.

It is widespread knowledge at this point that Monsanto is a profoundly evil corporation. They produce genetically modified plants, and then claim ownership of any plant that crossbreeds with it (using their money to out-lawyer any farmer who disagrees). With the chaotic nature of seeds in the wind, left unchecked they could eventually claim ownership of every plant in the world by seeding a single field on every continent. They buy out politicians to allow them to pollute acres and acres of United States land. The world would be a better place without them — their technology is great, but their behavior as an entity is abhorrent.

A conversation comes up in a grocery store about the evil of Monsanto, and while complaining and loathing the evil in the world, they pick up the cheapest loaf of bread and put it in their basket, thereby handing the evil in the world another token of their trust. If the world refused to buy any Monsanto-derived food, Monsanto would die. Poof, evil extinguished! The choice in what food you buy is asking you a question — do you prefer cheap food, or a moral world? If you buy Monsanto-derived food, you are saying you prefer cheap food. And the world really does listen.

I want to live in a world in which every person has an equal shot at equal lives (if you want more, you get less of a shot). But that hardly describes the Earth. Is my desire for this ideal Earth greater than my desire for three SubWay sandwiches per month? Could I put up with putting in a little more effort to make my food in exchange for helping the world to achieve this goal? Would it be worth it to you? If you say yes but don’t pay for it, you are lying. This isn’t hyperbole. I consider failing to “put your money where your mouth is” an outright lie.

How many proud Americans lie every day? Are you one of them? I still am. I am working to change that in myself.

Do your research! Pay for things made by companies whose behavior is agreeable to you. Don’t just look at the price. Tell the truth about your vision for the rest of the world, the future, even just a little bit. We, the hard-working, moral people of this planet wield most of the power. Let’s use it responsibly.

Buy the change you wish to see in the world.

(Do you value posts like this one? Well… Flattr this :-)

Connectedness

I’m a student of the University of Colorado again. I’ve gone back to finish my bachelor’s in mathematics, which essentially involves fulfilling a bunch of core requirements. I’m going to start the discussion by mixing my experience of one class (religions of south Asia) with a concept from another class (connectedness from topology).

Last spring I took my (now ex-)':girlfriend on a trip to Hawaii. While we were there, we attended a weekend immersive class on Sanskrit. The class was very “new-agey” — we chanted, meditated, in addition to learning Devanagari (the Sanskrit/Hindi alphabet) and something about Indian religion. The ideas combined with the approach fascinated and inspired me. I have never been much of a religious person; the religious ideas I had heard of always sounded a bit naive and silly. But this new approach gave me a glimpse of another way of looking at the world: the words of the Bhagavad Gita played with the gods, using them half as entities, half as concepts. The philosophical ideas, language, and religion we studied were clearly inseparable, all connected and synthesized into a single world view. Further, this world view seemed to incorporate my objections to the naivety of western world views — emphasizing the duality in all things, focusing not so much on right and wrong but on purpose and spirit, using the malleability and metaphor of truth.

My curiosity whetted, I enrolled in a class about Hinduism at the university. So far it has been a disappointment. What drew me to these ideas in the first place was the connectedness and duality — the yin and yang, so to speak — I perceived in the world view. And we have started by drawing thick lines categorizing the different approaches to divinity. An especially potent event in bringing to my attention my disappointment with the class occurred during our discussion of Bhakti. The professor began to describe the philosophy of Bhakti: that connecting with the divine is about love and devotion, that the details of ritual are not as important as a true spiritual devotion to god. Immediately after this description, the professor wrote on the board BHAKTI RITUALS. Um, teacher, did you not feel that just now? How did you build your immunity to cognitive dissonance?

We have been categorizing, deconstructing, analyzing this beautiful philosophy as if engineers. After the class I suspect I will know many facts, but have no understanding. If I were to talk to a yogi, he will consider me no closer to understanding his spirituality than any other American out of the hat. This is disappointing, since I don’t consider myself to have learned something until I understand it. We have a Hindu temple here in Boulder; I hope to find a way to study there and use the class as a supplement.

But why I am really writing this post is to help me to grip a vague sense I felt as I was processing after the BHAKTI RITUALS class. I am in a topology class this semester, and we are learning set-theoretic point-set topology. The constructivist in me winces every few minutes, lamenting the non-computability of everything we are discussing. I think the same cognitive orientation is fueling my dissatisfaction with the Indian religions class and my taste for constructivism. Classical mathematics seeks to separate the world into true and false, existence and nonexistence, equal and inequal. The inclusion of the law of excluded middle as obvious is evidence of this, as is the surprise felt by the mathematical world over Gödel’s incompleteness theorem. “What? We can’t eventually separate everything into two categories?!”

If you ask a set theorist whether ℕ = ℚ, they will probably say they are not equal (although have equal cardinalities). If you ask a type theorist whether ℕ = ℚ they will say “huh?”. The question cannot be answered, for we must consider what it means to treat 1 : ℕ as a ℚ, and we don’t know how to do that — not without a function that shows how. Indeed, in constructivism we have to be careful when talking about real numbers, since the set of observations matters, i.e. it matters how we look at them. And for any reasonable construction of the reals, their connectedness falls out of the constructivism of the theory: we cannot separate them into two categories in any way. A set theorist can, and has to define himself into a more realistic world where he can’t using the mechanism of topology.

Mathematicians are probably getting upset at me or thinking I’m an idiot. This isn’t a mathematical post, it’s philosophical, thus my fuzzy intuitive discussions. If you have the desire to leave an emphatic corrective comment at this point, maybe take a step back and try to make out the landscape with me. I don’t consider any of this true, I’m just trying to get a feel for the philosophically general idea of connectedness, outside of a particular formal system. I have the impression that we can think of the world — the real one or the mathematical one — this way and it might lead to a more accurate, if less “clear-cut”, way of thinking.

The pure untyped lambda calculus is connected in the Scott topology. This fact has fascinated me since I heard of it, trivial though it might be. We are used to adding traditional totally disconnected types to the lambda calculus and pretending bottoms don’t exist. I have been curious about what it would look like if we embraced this connectedness and extended lambda calculus with connected concepts. They may play more nicely in a connected system. I still have not made any concrete progress on this idea, but it appeals to me as potentially beautiful and powerful. Maybe we are computing in an awkward way without realizing it.

Did you like this post? Accelerate the flow of Karma :-) Flattr this

Designing with Interfaces

In this post I will share a personal discovery about object-oriented design. It is so simple and obvious that it may not be news to experienced OO programmers. However, it should be noted that before I became a Haskell nut, I was a dedicated OO programmer for 7 years. I occasionally empolyed the pattern in my designs without realizing its full generality. So it may yet be news to you vets.

I came across it when exploring the consequences of my recent post, Encapsulation Considered Harmful. I was trying to design a concept language around the idea, and realized that the language design I kept coming back to could be mostly encoded in existing languages.

Here’s the rule: never reference a class directly — always go through an interface. This includes creating new objects — go through a factory interface or take it as a parameter instead.

Obviously this rule cannot be followed 100% because you’ve got to give a concrete instantiation at some point. But push that as far “up” in your program as possible. Also, doing this 100% in contemporary languages would probably involve too much boilerplate. That’s okay, it can be done piecewise, and many of its advantages can still be reaped if it is done partially.

We have recently done this refactor on our game, and I have to say, it is gorgeous! I really like the clean, uniform way it separates concerns. More importantly, it is an abstraction instead of an encapsulation mechanism. That means that when code doesn’t depend on a particular detail, you can always substitute something else for that detail. Keep that property in mind, it will guide you in applying this pattern correctly.

Here is a simplified example from our game. We used to have a class FileSystem to access the filesystem. ProgressResult<T> was a class that monitored the progress of loading a file from disk and yielded a T when it was finished. Unit is the trivial type: struct Unit {}.

class FileSystem
{
    ProgressResult<List<string>> List();
    ProgressResult<byte[]> Load(string filename);
    ProgressResult<Unit> Save(string filename, byte[] bytes);
}

And in our World class, a class for the main game interface, we used it like so:

class World
{
    FileSystem filesystem;
    public World(...) {
        filesystem = new FileSystem();
        ...
    }
    // using methods on filesystem here
}

After applying this pattern, it becomes:

interface IProgressResult<T>
{
    ...
}

interface IFileSystem
{
    IProgressResult<List<string>> List();
    IProgressResult<byte[]> Load(string filename);
    IProgressResult<Unit> Save(string filename);
}

class FileSystem : IFileSystem { /* as before */ }

class World
{
    IFileSystem filesystem;
    public World(IFileSystem filesystem) {
        this.filesystem = filesystem;
    }
}

See how we avoided creating a filesystem in World, and instead took it as a parameter? World is now more flexible than it used to be: we can (and did!) instantiate it with a disk filesystem and an Amazon S3 filesystem. They have different sorts of ProgressResults too, thus that is also an interface.

Now, if you go to apply this pattern in your project, you might find that it breaks down for more complex designs. It’ll do that if you aren’t precise about your phrasing. For example, in another part of our project we had the following combinator library for our UI:

class UI 
{
    public static UI Over(UI over, UI under) { ... }
    public static UI VerticalGroup(params UI[] uis) { ... }
    public static UI Window(string title, UI contents) { ... }
    public static UI Button(string text, Action onClick) { ... }
    // some non-static members for inspection
}

To abstract over these implementations, we need to promote these static functions into methods on a factory-like interface. It may seem that this is the way forward:

interface IUI { /* the non-static members */ }
interface IUIModule 
{
    IUI Over(IUI over, IUI under);
    IUI VerticalGroup(params IUI[] uis);
    IUI Window(string title, IUI contents);
    IUI Button(string text, Action onClick);
}

But this is not the way. Imagine if you were to construct some IUIs with a SilverlightUIModule and then try to combine them (with, say, VerticalGroup) with a UnityUIModule. What is the unity module supposed to do with silverlight UIs? This design flaw will show its head much earlier than this predicament, however: you will find in UnityUIModule that you need to downcast the IUIs you get into a specific type. Downcasting is an indication that you are doing it wrong. The correct way is to make IUIModule parametric in the UI type:

interface IUI { /* the non-static members */ }
interface IUIModule<TUI> where TUI : IUI 
{
    TUI Over(TUI over, TUI under);
    TUI VerticalGroup(params TUI[] uis);
    TUI Window(string title, TUI contents);
    TUI Button(string text, Action onClick);
}

Now UnityUIModule implements IUIModule<UnityUI> and SilverlightUIModule implements IUIModule<SilverlightUI>, so it is a compile-time type error to pass a silverlight UI to a unity combinator. In addition, you do not need to downcast: the UnityUIModule now knows statically that it will only be passed SilverlightUIs.

One thing remains… how are you supposed to use that damn module? Surely you don’t make every class that uses a IUIModule parametric in the TUI parameter like this:

class World<TUI> where TUI : IUI
{
    IUIModule<TUI> uiModule;
    public World(IUIModule<TUI> uiModule) {...}
}

Type parameters would accumulate in classes, and every time you used that class you would have to write all those fucking type parameters. That can’t be the way to go!

Well… actually… remember the rule? You never reference a class concretely, you only go through interfaces. And the interfaces don’t accumulate type parameters from usages in the implementation like classes do, they only accumulate type parameters from the cleaner and scarcer usage in interfaces. So the worry of unsightly verbose code is unfounded. It’s fine, let the type parameters accumulate, they are a way of stating your assumptions. They tell you exactly how a class may be reused. So to defeat this notational burden in this example, we must continue the pattern: create an IWorld!

As you do this, all your assumptions will bubble up to the top of your program. Each more complex thing is parametric in all the simpler things it is made of. When you go to write code that can actually be executed, you will find an ocean of flexibility: you basically have created a rich combinator library for your domain, allowing it to target multiple underlying frameworks easily, getting multiple different (reasonable, even desirable!) behaviors just by passing a different parameter to some object you are building at the top level. Mmmm, parametricity.

(For dynamically typed languages, duck typing will mostly take care of this pattern for you. But the design motivation remains, make your code parameteric: take parameters saying where it should get the objects it is creating and the functions it is calling rather than referencing them directly!)

Did you enjoy this article? Let me know and Flattr this. I appreciate it.