Categories
Programming

Ownership types

I was reading the OOPSLA ’02 proceedings and found the paper on Ownership Types for Safe Programming. This is a static type system for multi-threaded programming. The claim is that a well-typed program in this system is guaranteed to be free of data race-conditions and deadlocks. Wow! Okay, I’ll read more and see what the catch is. Dependent types seemed cool, until I read about the undecidability!

Categories
Programming

Static typing vs. dynamic typing

The small corner of the blog world which I live in (need a name for that concept) has recently started debating dynamic typing vs static typing (and I’ve already refuted some claims). I’m of the view that static typing is a big benefit especially in large systems where no one person understands everything. As ever, there are pros and cons to both approaches, so here’s another braindump of ideas and opinions ..

Categories
Programming

STL transform() meets constructors

Using the STL transform() function you can apply “function like things” to a collection to make a new collection. That means anything which can be called by sticking brackets after it, so functions are fine and function objects are fine.

But not all “function like things” work. What happens if you have a collection of ints, and you want to transform them into a collection of Foo’s, where the constructor for class “Foo” takes a single integer argument.

Well, you can’t just pass “Foo” to transform() because that’s a type, not a value. You can’t pass Foo::Foo either, It doesn’t work.

You can create a static method, say Create(int i), which just builds a Foo object and returns it. That works. But it’s incredibly inelegant.

Categories
Programming

Catching up with Alan Kay

Alan Kay is someone who’s opinion I value. He has had many great ideas. I’m still playing catchup with ideas he had decades ago.

As a computer programmer I tend to view computers as, well, compute-rs. I think of them doing computer stuff. With the internet, I think of them as endpoints for moving data around – music, information, instant messaging. I live in a world of protocols, tools and document formats.

I recently dived into the blogging world, and that was a huge new internet-time flow of information. But at the end of the day, networks just let people communicate. It’s still just a network of people. All the clever network magic doesn’t create anything new. The blog world isn’t very exciting if people are just recycling links to the latest cool web page. It’s only exciting when people are writing original content.

So my epiphany is that, in the Alan Kay world, you don’t look at computers as compute-rs. You look at them as pencil and paper on steroids. You don’t use them to write yet another naff utility. You don’t do computers – you explore the world and you use the computer as your scratch pad, your laboratory, your experiments, your classroom. Imagine if Newton had Mathematica! Imagine if Leonardo had a laptop. Maybe they’d waste their time surfing the latest cool website, or maybe they would have used to do even more real stuff.

I do a lot of music recording and editing on my computer. So, I do use it as a tool. But I don’t think of it as an instrument. I’ve heard less geeky people describe their iMac as an instrument, but I can’t stop myself hearing the bytes flying around.

I think I’ve been programming too long. I couldn’t see the wood for the trees.

So now I understand why Alan Kay wrote Squeak. It’s not about teaching kids how to become programmers. It’s about letting them explore the world using something hugely more powerful than pencil and paper. You’re not giving them a fish – you’re teaching them how to fish.

Now, maybe twenty-five years from now I’ll have a similar epiphany about what the hell Alan’s latest venture, Croquet is all about. The tagline is obviously a good idea – “if we were to create a new operating system and user interface knowing what we know today, how far could we go”. But how far is Alan Kay going to go?

Categories
Programming

Strong typing vs Strong testing refute

Bruce Eckel writes about strong typing vs strong testing – a topic close to my heart. But he makes the common mistake of reducing “runtime typing vs. static typing” to a language feature comparison. Java is a very poor example of static typing!

But I think I disagree with his larger point – that you’re better off with test systems rather than type system. I think you should have both! At work, I’ve watched large programs being built up and I think you reap great benefits from static typing. For a start, types act as documentation. The type of a function argument indicates how it’ll be used. Abstract interfaces act as barriers to complexity within a large system. Type annotations such as ‘const’ in C++ allow you to express some of the semantics of the system and the compiler will check all of your code for violations.

Can tests ever replace this? I think not. Firstly, in my experience, checking enough of the cases is hard. And the cases which don’t get tested are probably the rarely executed ones where bugs lurk. What happens if someone trips over the network cable as your program is sending data? Does that error handler work? How about if the harddisk is full and your program can’t write a temporary file?

Sure, static type systems can’t check very many properties of your program – that’s why I still love unit tests – but they can check quite a lot including a lot of common mistakes. Given that it takes a compiler a few milliseconds to type-check a function I think static typing is a big win. The compiler won’t suffer from deadline stress and forget to check your new code.

I wonder if many of the great claims made about programs written in python arise from people who don’t write very large systems, and who don’t have to be very strict about dealing with every single failure condition. It’s one thing for bittorrent to fall over when something goes bad. It’s quite another if software in a life-support system bails.

I see things like ‘const’ in C++ and I wonder if there are other annotations which I could add to my source code, so that they act both as documentation of my intent and so that the compiler will check them. This is what lead me to my current fascination with computer languages and their facilities. I talked to Anthony about this a while ago, since he did his PhD on something-to-do-with-type-systems and he murmed in a “it’s tricky” way. Around that time, I looked at dependent types. With this more powerful type system you can express stuff like “foo() is a function which takes two arguments: a vector of integers called ‘data’ and an integer called ‘length’ but furthermore the vector will always have that length”. But, apparently that makes the type system undecidable in general. In plain english, your compiler can say “your program is well-typed” or “there’s a type error” or it could go into a loop trying to decide. That’s not great and I find it a bit worrying that something which can be expressed so simply in english messes up your compiler so bad.

But, hey, even I have doubts. Not because I feel shackled by the type system in ocaml or haskell but because systems like Squeak have much better tools for exploring and tweaking systems. Hmm ….