Subtyping is Evil

That’s going to sound like a pretty radical statement to all the programmers reading the blog post but I’m not joking.  Supposedly Philip Wadler, one of the main people behind Haskell and one of the designers of Java generics was overheard saying this.  I can’t vouch for whether this or true or he believes it but it makes me feel slightly less insane to think that he did.

Before getting into this let me say that subtyping should not be confused with separating implementation from interface, which is pretty fundamental to a good programming language and can be achieved in other ways.

Subtyping is a very seemingly nice way to do this and it’s not hard to see how it has become so pervasive.  To make something into a feature of a type system you have to actually formalize it and formalizing subtyping turns out to be horribly, horribly messy.  If the messiness were constrained to the compiler, this wouldn’t matter but it causes problems for users of the language and leads to some unintuitive results.

Say A is a subtype of B, is a list of A a subtype of a list of B?  <sarcasm>As I’m sure everyone who isn’t familiar with this, would intuitively guess, the answer is yes if it’s an immutable list and no if it isn’t.</sarcasm>  It doesn’t seem to me that the type system should have a priori knowledge about what is mutable and what isn’t so this is already a problem.  Some languages such as OCaml and Scala would let you declare that your list class’s type parameter is “covariant” allowing your list of A to be a subtype of a list of B, since you know your list type is immutable.  Is Joe Programmer ever going to understand or use that feature though?  Java 1.5 actually has the best solution to this problem out there, this is the technical reason to declare something which might be a list of A or a list of B as a list of “? extends A”.  This is the part of learning generics I’ve seen the most confusion with though.  Since Java has subtyping doesn’t A implicitly mean “? extends A”?

Another example is the equals method.  How much boiler plate code have you written figuring out the run time type of equals’ argument?  On top of it no matter how your wrote it it’s broken in one way or another.  If your class has subtypes, either your equals method violotes reflexivity or the the Liskov substitution principle.  If it can potentially return true for an argument of a subtype, it is almost certainly not reflexive (it can be if the subtype’s equals ignores the added fields but you don’t generally want that either).  If it can only return true for arguments of exactly the same type it’s not properly substitutable.  Imagine I have a set of cartesian points and point has a subclass colored point.  I can have the point (0, 0) in my set twice because one of them is blue, but there’s a good chance that’s an error if you’re trying to make a set of cartesian points.

Wouldn’t the world be simpler place if equals could be specified to take an argument of the same type as the class declaring it?  It would be straight forward to implement and consist of nothing but comparing fields.  It would always be reflexive unless you were trying to write a broken equals method on purpose.  Of course if it could the inheriting class wouldn’t be a subtype anymore!  If I try to upcast it to its base class something funny has to happen because it doesn’t have an method equals which takes an argument with the type of the base class.

I’ll talk more about what is actually in my langauge soon, I thought this would be a good starting to tear down your assumptions about how things will function first, the enlighten you as to how they do.  I don’t know how many people I will have actually convinced that subtyping is a misfeature, especially without presenting an alternative.  I hope I at least got you thinking about programming language design in a way that you probably haven’t before.

Making Tea

As I mentioned earlier, I’m a big tea drinker.  Programmers a notoriously caffiene junkies, and I don’t drink coffee (I really don’t like the taste) or energy drinks (they scare me), so you can imagine I take tea pretty seriously.  I drink free leaf tea at home and recently got a really nice tea pot.  As I’ve been getting my life settled, at work I’ve been using the hot water button on the water filter machine and tea bags.  This really wasn’t wasn’t cutting it, I got some good tea bags but the hot water button just didn’t get the water hot enough to steep the tea properly.  I finally went out and got an electric kettle for my desk and a decided I might as well get cheap tea pot while I was there to really do things right.

I’m going to echo something Douglas Adams wrote about how most Americans have never had a good cup of tea.  You don’t need to go out and buy a nice tea pot and free leaf tea.  That doesn’t hurt but much of it is just knowing what you’re doing.  Here’s some advice for getting started:

  • Go out and buy some tea bags of an unflavored black tea.  Nothing against green tea or flavored tea.  Green tea is harder to make properly and there’s less margin for error, so learn to make a proper cup of black tea first.  As for flavored tea, there are lots of delicious flavored teas out there but I’m trying not flavoring.  If you can find some premium tea bags that are pyramid shaped and have full leaves rather than dust you should get those.  The best tea bags you’re going to find are going to come out to well less than $1 a bag so there’s no real reason to cheap out.  How much does a cup of coffee cost you?
  • One reason green tea is more difficult than black is that the water needs to be in a specific temperature range, both too hot or too cold will cause it to come out wrong.  The most wide spread mistake people make when making tea is using warm water, not hot water.  For black tea the water can’t be too hot.  You should be using boiling water.  Don’t boil it and then let it sit around either.  Warm up the cup you’re going to steep the tea as well, so that the water doesn’t cool down when it hits the cup.  The easiest way to do this is take a little of the hot water and slosh it around the cup and then pour it out.  Then put the tea bag in your cup and pour the water that is at a full boil over it.
  • Leave the tea bag in for the amount of time the box specified.  Most people just dunk the tea bag in and take it out or leave it in forever.  If you’ve been using warm water to make you’re tea you can leave it in forever because the tea doesn’t really steep at that temperature.  If you use boiling water and leave it in too long it will get bitter.  If you don’t leave it in long enough it will obviously be weak.  If you steep it for the maximum specified time and it’s not strong enough for your taste, use 2 tea bags per cup instead of over-steeping it.

It’s really pretty simple.  You may want to add sugar or honey or milk or lemon but try a few sips before adding anything and see how it actually tastes.

Why another programming language?

Side note on the name:  Still taking suggestions, haven’t settled on one yet.  As a tea enthusiast and with Java being a major programming language I was thinking of going with some variety of tea (tea itself is taken).  Sencha perhaps?

So there are lots and lots of programming languages out there, do we really need another one?  I think perhaps the design space of dynamically typed languages is nearly explored for most practical purposes.  The core of dynamically typed languages can be much smaller than statically typed languages, leading to a smaller, simpler design space.  Smalltalk, Python and Ruby are remarkably similar languages in most respects.  I’m not saying there won’t be any more innovation in dynamically typed languages, but very little in the core of them.  There’s much less reason to start from scratch.

Static type systems are currently one of the most heavily researched areas in programming languages and an important part of the core of a language.  So people are unhappy with them and improvements are difficult to graft on to existing languages.  To a large degree I envy the dynamic typing world but I think in types and I find that even the flawed type systems that are out there are a valuable guide designing and understanding programs.

Type systems tend to be either simple but fail in really basic ways (try to write a method in Java that works on any sort of numeric type) or really staggeringly complicated (C++ has a turing complete type system!).  Haskell probably comes the closest to bridging the gap of any language I’m aware of, but isn’t entirely satisfying for several reasons, most of which I’ll discuss as I discuss my design later on.  The big idea in my language design is that Haskell’s “type class” polymorphism is the right basis for a type system.  It gets underused and has several warts in it’s current form in Haskell because its usefulness extends beyond it’s original imagined purpose (which was reconciling operator overloading and type inference).

I think part of the problem, and the reason most type systems end up getting so complex is that they end up happening by patchwork.  Researchers try to make a new theory and add a new feature to fix type systems they find lacking.  They start out with something flawed and try to fix it by layering various other features on top.  There’s no new major theory or anything like that in my language.  I know more about type theory than your average work a day programmer, sure, but not 1/100th as much as any of the PhD’s out there working on Haskell.  I’m not going to beat them to anything theoretically new.  I have a great respect and appreciation for what they do but my lack of ability to innovate in terms of type theory may be an advantage rather than a disadvantage because I think simplification is what’s really needed.  I want to do more with less.  I want my type system to capture the basic cases but still be understandable and unified.

So that’s the basic flavor of what I’m going for.  The details will follow in the future. I’m going to primarily try explain my design as it relates to the OO perspective, rather than the Haskell perspective even though it’s closer to Haskell.  That way more people will understand me.

Bike ride to work: for real this time

It was beautiful out today and I wasn’t very sore at all from my test ride on Sunday so I decided to actually do it today.  Now I am very sore.  It’s supposed to be rainy tomorrow but either way I’m taking the day off from riding.

After Ian’s advice I took the route as I originally described it.  Washington St. was actually safer today than it was on Sunday.  I guess with actual traffic on it less people felt they should take the liberty to try to graze my left ear.  Also the intersection before crossing into Watertown wasn’t bad at all now that I knew it was coming up.  I guess I just assumed it was a bad situation when on my test run I realized I was already at the turn and needed to be 4 lanes over.  Over all it went well, no injuries, no near collisions.  One guy made a left out of the right lane next to me but he didn’t come close to hitting me it just pissed me off out of principle.  It took 30 minutes to get to work and 40 to get home, compared to 50-80 each way via the T.

Now I really just need to get into better shape so I can do it regularly.

Bike to work test ride

I live about 5 miles from work and it takes me more than an hour to get there through public transportation.  The benefits of biking to work should be obvious, I’d save time and get more exercise.  I did biked to work while living out in Arizona.  Of course in Arizona they have wider, straighter, better paved roads so it’s going to be a bit more challenging here.  It seemed like trying to navigate a route for the first time, get to work on time and not die, all at once seemed like a bit much so I took a test ride today along this route.

The Good:

  • It took an hour and a half, there and back, including a detour along the bike path along the Charles River (just for the enjoyment of it), at a leisurely pace, stopping several times to check my map.  The potential for time savings is pretty clear.
  • I work in the western part of Cambridge so I cut through Watertown, which is more suburban and less busy than Cambridge or Boston.
  • I work on the northern side of a park which I approach from the south and the park has a paved bike path around it for the end of my route.

The Bad:

  • Washington Street is fairly major but very narrow and I don’t know how comfortable I’d feel riding down it at rush hour.  I might head a bit out of my way to Chestnut Hill Ave and take that to Brighton Center.  I took Chestnut Hill Ave on my way back and it’s a fair amount wider.
  • The streets I’m riding through in Watertown are horribly poorly marked.  Luckily my generally poor instincts were right and I managed not to get lost this time, and after a few times through it won’t be an issue.  Still, visible street signs are a nice thing to have.

The Ugly:

  • The 5 way intersection before I cross over the river into Watertown.  I really might move my route to go over the next crossing to the west to avoid this.  I’m certainly not going to try to get into the left lane here on a bike, so I might just have to take the last right before it, and find a suitable place take a left on Western Ave.

Thoughts from people who have experience biking around Boston would be very much appreciated.

Board gaming

Board Game Collection

Board Game Collection

So as you can see I have a pretty decent game collection.  Not quite as big as my former coworker who got me into board games (he owned over 300!) but there are some good ones in there and you probably haven’t played most of them before.  Unfortunately these games have been collecting dust for a whlie now.  If you are in the Boston area and you’re interested in doing a game night at some point let me know.  Here’s a run down of what I’ve got there:

  1. Tigris and Euphrates – My personal favorite.  Very deep, subtle strategy game.  A little hard to learn and unintuitive at first so it’s only worth playing if you are going to do so at least a few times though.
  2. Settlers of Catan – Known as the “gateway boardgame.”  It’s simple enough to pick up and have fun in a sitting but is much more interesting than most standard boardgames like Risk or Monopoly.  Generally used to get people intrigued and later hooked on the hard stuff like Tigris.
  3. Puerto Rico – A deep strategy game with complex rules, so like Tigris not the first one you should dive into.  It’s one of the most well liked games by the board gaming community though and was ranked #1 on boardgamegeek.com for many years after being release (it’s now #2).
  4. Go (the wooden board under Puerto Rico) – Considered the eastern version of chess.  It has incredibly simple rules but equally incredible strategic depth.
  5. Poker – Well you all know what poker is.  Poker night would be good too if anyone is interested.
  6. Tichu – A chinese card game with a standard deck plus 4 special cards.  It’s got elements from many different card games you’re probably familiar with but mixes them together in an interesting way.
  7. Citadels – A strategy card game on probably about the complexity level of Settlers.  Involves a lot of predicting what opponents will do and avoiding being predicted.
  8. Chess – No explanation needed I think.
  9. Maharaja – Somewhere between Settlers and Puerto Rico in complexity.  Very pretty looking game.
  10. Robo Rally – Unlike the previous games listed, this is not really about strateg, it’s about chaos.  Everyone plans their move at once, when everyone is locked into their moves they execute them and interfere with eachother and cause eachother to drive off cliffs and such.
  11. Blokus – The simplest game I own, you try to put all of your tetrisy sort of blocks on the board in a legal configuration and try to minimize how many your opponents can get down.  Can be learned in about 30 seconds.

Creating a new programming language

I’ve had an idea for a programming language I want to create for several months now.  I think it’s probably a good one because otherwise I would have forgotten it or lost interest in it or realized it was bad by now.  If I ever get it done it won’t be for a long time, writing a compiler is a big task and I have a day job and don’t want to spend the rest of my day coding too.  Still, I ‘d really like to get it done someday.  In any case I’ll be blogging about my ideas for it, that way I’ll have notes too look back on and maybe some feedback, and maybe someone will like my ideas and motivate me to actually do it.

Anyway the task at hand is described by the following quote:

“The most important thing in the programming language is the name. A language will not succeed without a good name. I have recently invented a very good name and now I am looking for a suitable language.
— Donald Knuth

Unfortunately I have suitable langauge and no name yet.  Any ideas out there?

C++

C++ is more or less my native language in programming.  My high school actually still taught Pascal, of all languages, in the intro programming course there so technically that was my first language.  Even so, we did such simple programs it could have just as easily been in C or Java or Basic and I don’t think I would have come out much differently.

When I did my first major programs I did them in C++ and I think I learned such a big complex language pretty well for someone of such little experience.  I really wanted to learn to do things right and read up on OOP and templates and operator overloading and all that stuff.  Even some of the weird stuff, partial template specialization, template meta-programming, etc.  My first 2 coops were in C++ as well.

While C++ hasn’t been my favorite langauge for a long time, and I’ve become quite a programming language snob in the mean time, I preferred it to Java until recently.  For much of that time Java didn’t have generics, which was a big thing for me.  Part of it I think was that the Java community has a problem that they thing that the more code and the more layers of abstraction the better.  This is as big or bigger of a mistake than the typical programming mistake of the shorter the better.  Part of it was that C++ gives you more tools.

My 3rd coop was all Java and before starting my current job which is C++, I probably hadn’t touch C++ in a year and a half.  Coming back to it now after my break from it, I have a new respect for many of the design decision Java made and a new appreciation for the quagmire that C++ is.  It’s not that I don’t remember how to code in it.  It’s not my team either, some of them know it better than me.  Perhaps that even accentuates it for me.  Even when everyone knows exactly what they’re doing C++ gets in your way.  It feels strange to write because sophmore in highschool me would be throwing a fit about this assertion but here we are.

On Free Will

(Warning: long rambling philosophical post I’ve been working on, on and off since starting this blog follows.  Read on at your own peril.)

Free will is not one of my favorite philosophical topics, but I feel compelled to get my thoughts about it posted here.  It’s come up in conversation a number of times in the last few months and I haven’t managed to bring anyone over to my side or vice versa.  Maybe with my argument static and rereadable it will be more convincing or easier to pick out the precise flaw in it.

Firstly the reason I don’t find the argument that interesting is that regardless of whether or not you believe in free will, you act as though you do.  I tend to go for philosophy where at the conclusion I can say OK, I should try to live my life in a certain way.

So there are basically two versions of free will out there, the kind which is compatible with determinism and the kind which isn’t.  The compatible version says free will is the freedom to do what you want.  This is my stance and so my tasks in defending it are to show the incompatible version isn’t logically consistent, making my version the free-est free will possible, and secondly to show that this version does at least in some sense constitutes free will even though determinism seems to be at odds with free will.

The argument knocking down the incompatible version is pretty standard.  Almost every time I’ve given it someone tells me the discussion takes place in the movie Waking Life.  Stop it.  Nothing against the movie but I know.  It’s not like they invented the argument either, it’s pretty much philosophy 101. </rant>

Anyway the incompatible version claims for free will to truly exist it must be possible to have done something different.  Here’s the problem: if it’s the same you with the same knowledge and memories and desires you had at the time you made the decision and there are 2 possible outcomes, it isn’t you making the decision at all!  The current you with new knowledge, transported back in time could certainly have done something different but it needs to be the exact same past you making the decision or it’s not free will in their book.  If that decision was only based on your mental state in relation to the world, to someone holding this view you are just a biological robot.  Now whether or not you are a biological robot is a larger discussion and one for another day and I’m going to try to make an argument for Compatibilism without addressing that thorny issue.  I claim that whether or not our decisions are predetermined, consciousness at some level differentiates us from the other matter in the universe.

I’m now going to argue that the Compatibilist version of free will is in fact free will from a  fairly Wittgensteinian point of view here.  Imagine you’re mind continued on as is and your body did as well, except your body stopped listening to your mind.  Your body went about your daily routine and went to work and interacted with people but as a biological robot, entirely unconsciously and of its own accord.  Meanwhile your mind continues to see and hear everything going on but can’t have any effect over your body.  I think the only phrase anyone would use to describe this set of circumstances would be that you lost your free will.  And because this is THE way to describe that phenomenon, can we say free will is anything other than the ability of our thoughts to cause actions?

As a final note, when I last discussed this with Brandon he asked something along the lines of “how do I know my thoughts are causing the actions and don’t simply line up by coincidence?”  Strictly speaking you don’t but this is nothing more or less than Hume’s problem of induction.  If you are going to give up the belief that your thoughts cause your actions you might as well give up physics too.  Belief in both sets of causal principles have exactly the same philosophical basis.

Health status update 1

Since around Thanksgiving I’ve been on the maximum dose of the medication for my condition that doesn’t have serious side effects.  I’ve been getting by to varying degrees, so we’ve been taking a wait and see approach.  This week I got much worse and started missing work as a result.  Now I am starting on the medication with the potential for serious side effects (corticosteroids).  The good news is that today I feel fantastic.  Practically like a regular person.  Not 100% better but at the same time due to my point of reference I am in a spectacular mood right now.  So hopefully these drugs will be a short term thing and I can heal up a bit and get off them.

And now off to savor the rest of beautiful day.