Tuesday, January 09, 2007

Truthfulness

Cobra copies a lot of syntax and high level semantics from Python, including how to determine truthfulness:

... if stuff
... ... print 'I have some stuff.'

(Sorry for the periods, but Blogger is stripping preceding whitespace. Maybe I'll have to find a new blog site. Suggestions are welcome.)

In Python, as well as the current release of Cobra, "stuff" is considered true if it is non-zero, non-nil, non-empty or the boolean value, true. So even empty strings and collections are considered false. That can be useful since programmers often want to take or avoid an action if a string is blank or a collection is empty. Code becomes more terse:

... if not name
... ... print 'You have no name'
... # vs:

... if name is not nil and not name.length

... ... print 'You have no name'


But that also introduces a lot of expressions in the form "stuff is not nil" either because that is exactly what you need, or for efficiency.

Keep in mind that determining "non-empty" involves inspecting the object to allow a method to partake in the determination ("__nonzero__" in Python; "length" or "count" in Cobra). That's expensive and so between that expense and the numerous "...is not nil" fragments in my code, I decided to reconsider this semantic.

Would it be better to drop the semantics regarding blank strings, empty collections and object participation in truthfulness? I wasn't sure, but felt that an examination of Cobra source code would be telling.

So I augmented the Cobra compiler to count the number of times a reference type was checked for truthfulness vs. the occurrence of "x is not nil". I then applied it to the largest Cobra project to date: the Cobra compiler. The counts came out:
  • 107 - instances of "x is not nil"
  • 509 - instances of simple truthfulness on reference types
So it would appear that high-level truthfulness is quite popular. But as part of doing the count, I printed the expressions that were being considered for their truthfulness.

And I was alarmed at how many of them should have been "is not nil" from an efficiency point of view. Basically, if you know that the type of an object does not customize truthfulness with the "count" method, then "x is not nil" is much more efficient than "x". That's because "x" implies checking the type (String, ICollection) and possibly checking for a "count" method.

I read though all 509 and counted the ones that should have used the simpler calculation of "x is not nil". That changed the numbers to:
  • 412 - x is not nil
  • 204 - truthfulness on reference types
Not only is "is not nil" a more popular computation (2 to 1), but the current approach leads to slower running programs if you're not diligent.

Furthermore, Cobra's compile-time nil tracking means that many variables cannot be nil anyway and therefore the expression:

... if s is not nil and s.length

becomes:

... if s.length

when "s" is a "String" rather than a "String?":

... def doSomething(s as String)
... ... # "nil" cannot be passed in for "s"
... ... # because its type is not "String?"


So I made the change and then updated the Cobra compiler source, test cases, samples and docs. This experience confirmed that the new semantics were usable, writeable, readable, in other words, capable. (Btw this is another advantage to writing a language's compiler (or interpreter as the case may be) in the language itself--you find out fairly quickly which ideas were good and which were bad.) This will show up in the 0.4 release later this month.

Regarding Cobra's ongoing development, there are certainly major additions in the future, for example, operator overloading. But this is the last change I know of that has a major impact on existing code.

Friday, November 17, 2006

New Compiler, New Computer

Yesterday, I released Cobra 0.2. This is the new compiler written in Cobra itself and delivered as a .NET exe (previous versions were Python programs).

Now Cobra will return to a fairly regular release schedule of once per month.

Today, I received a new MacBook Pro 17" 3GB Intel Core 2 Duo. It's bad to the bone. Assuming that Mono runs on it, all future Cobra releases will be tested on Novell Mono on Mac as well as Microsoft .NET on XP Pro. In fact, I plan on running a copy of XP Pro on Parallels on Mac OS X so I can literally use both operating systems at once. I'll have ready access to all that is Mac as well as Visual Studio 2005. Search the Internet and you'll find several existing blog posts of people doing this successfully. In fact, that was the tipping point for me to finally take the plunge into Mac.

By the way, I was a major NeXTstep user back in the day. It feels good to "come home."

Tuesday, November 14, 2006

Comparison to Python

I have added a Comparison to Python essay on CobraLang.com. This will help Python programmers understand the point of Cobra, including in contrast to IronPython.

Saturday, November 04, 2006

New Cobra Compiler

Regarding the new, upcoming Cobra compiler written in Cobra, it will be released this month (November). There will be some minor refinements to the language, improved error checking, faster compilation and the Python dependency will be removed.

I'm about 80%-90% of the way there. I'm really looking forward to getting back to pushing the features and maturity forward after this release.


Saturday, September 16, 2006

Lang.NET 2006 at Microsoft

The presentations at the Lang.NET Symposium have been posted here as the "video" links next to each presenter at http://www.langnetsymposium.com/speakers.asp

The videos capture the projection with the speaker's voice. This is exactly what you would have experienced being there (sans networking, interactive questions, seeing the speaker's face, etc.) At least the two I've watched so far. I haven't checked them all.

Here are some opinionated highlights of the presentations I enjoyed most:
  • Anders Hejlsberg is the author of C# and gives a presentation on "Language Integrated Query (LINQ) and C# 3.0". I really appreciate how LINQ isn't completely baked: the LINQ expressions translate to ordinary dotted expressions allowing any kind of object to participate in data queries. But I could have done without the XML portion as I never drank the .
  • The Spec# demo was totally bad ass. You would definitely want an engineer to be using this when writing any safety critical code (medical, aircraft, etc.) that intersects with your life. You might even want to use it yourself if you have the patience to deal with *manyof your bugs early and upfront (before you get to runtime). Once again, the architecture is open so that other languages might tap into it. In this case, "it" would be the backend theorem prover that analyzes your code.
  • Jim Hugunin covers IronPython which Microsoft sponsors in full.
  • PowerShell was cool (formally called Monad) and long overdue.
  • BLINQ has some potential for getting webapps kick started. Providing the right kinds of customizations will be challenging, though. Only time will tell if it pans out.
  • Miguel de Icaza presented Novell Mono (an open source clone of Microsoft .NET) in a whirlwind of demo apps, dev tools and that funky cool window manager he uses. I gained an appreciation for GTK# from this and some hope that maybe Linux will be a viable desktop someday (even for really picky people like myself).
  • William Cook's presentation approach was fresh and his angle on optimizing database access was just really, really neat. I hope to see it mature and become accessible for use on future enterprise apps.
  • Shriram Krishnamurthi's presentation was pretty wild in terms of what he was able to do with the browser, although I'm not sure what I take from it in terms of how I would change my approach to web apps. But definitely worth watching for langsmith nerds like us.
  • Don Syme covered F#. Like Shriram's presentation, it was neat to see dynamic, animated behavior come out of a program, that if I recall correctly, had no explicit timer nor loop. I guess the trick is for the language and/or runtime support to recognize dynamic sources like "ticks" and "mouse position".
  • If you're interested in futurism and accelerating technology, that would be the "Gary Flake" presentation. He also has some interesting war stories from the Internet bubble.
  • The "Second Life" presentation had the "cool factor" even if it's not going to impact your future development like the Anders H. presentation and others might.

Hopefully someone finds some of the above useful in terms of picking out presentations to watch.

Also, I was searching for "lang.net2006" and found the flickr gallery of that crazy guy who snapped hundreds of photos every day. Check out the intense expression on this dude.

Friday, September 01, 2006

Rewriting Cobra in Cobra

The current compiler for the Cobra programming language is written in Python, but I'm part way through rewriting the compiler in Cobra itself so I can benefit from increased performance, software contracts, static typing, compile-time nil checking and more. This will also remove the Python dependency for users of Cobra.

After the new compiler catches up to the old compiler's capabilities, I will be continuing with new features including optional dynamic typing. I'm a big fan of Objective-C's support for both static and dynamic typing in the same language. The developer, not the language designer, should decide how much static or dynamic binding goes on in his software.

http://CobraLang.com/