m3ga blog http://www.mega-nerd.com/erikd/Blog/index.atom Erik de Castro Lopo http://www.mega-nerd.com/erikd/Blog/index.atom erikd@mega-nerd.com Copyright 2006-2008 Erik de Castro Lopo PyBlosxom http://pyblosxom.sourceforge.net/ 1.4.3 01/10/2008 2008-08-18T09:39:00Z Nemiver : A GUI debugger for GNOME. http://www.mega-nerd.com/erikd/Blog/2008/08/18/nemiver 2008-08-18T09:39:00Z 2008-08-18T09:39:00Z <p> The many years, Linux has lacked a good GUI debugger for C and C++ programs. Yes, everyone knows about <a href="http://www.gnu.org/software/gdb/"> GNU GDB</a>, but that is a command line debugger and really not very useful for stepping through a program. There was also the <a href="http://www.gnu.org/software/ddd/"> Data Display Debugger (DDD)</a> which uses the Motif widget set, usually supplied by the <a href="http://www.lesstif.org/"> Lesstif Project</a>. Unfortunately, Lesstif development has basically been abandoned and <a href="http://www.opengroup.org/openmotif/"> OpenMotif</a> is not really an option because its license fails to meet term 8 of the <a href="http://www.opensource.org/osd.html"> Open Software Definition</a>. </p> <p> This means that for many years, developers on Linux have tended to avoid the <i>"stepping through code with a debugger"</i> approach to debugging. While I think that single stepping is not the most applicable to every debugging problem, there are times when single stepping is useful and possibly also the fastest way to track down a problem. </p> <p> However, I was recently made aware of a new GUI debugger for the GNOME (ie really the Linux) desktop, <a href="http://home.gna.org/nemiver/"> Nemiver</a>. </p> <br /> <center> <a href="http://home.gna.org/nemiver/"> <img src="http://www.mega-nerd.com/erikd/Img/nemiver.png" border="0" alt="nemiver screen shot" /> </a> </center> <br /> <p> The only problem with nemiver is that the version in Ubuntu Hardy is a little old and was giving me a few troubles. However, after building and installing a version 2.2.0-2 package of <a href="http://archive.ubuntu.com/ubuntu/pool/universe/libg/libgtksourceviewmm/"> libgtksourceviewmm-2.0</a> from source I was able to build nemiver from SVN and so far its working way better than DDD ever did. </p> <p> So, here it is, a good looking, stable and capable GUI debugger for Linux. </p> Sydney's Drivers Worst for Bike Rage. http://www.mega-nerd.com/erikd/Blog/2008/08/04/sydney_drivers 2008-08-03T22:17:00Z 2008-08-03T22:17:00Z <p> According to <a href="http://www.smh.com.au/news/national/sydneys-drivers-worst-for-bike-rage/2008/08/01/1217097533825.html"> this article in the Sydney Morning Herald</a> Sydney's drivers are the worst for bike rage; aggression show towards cyclists simply for being on the road. </p> <p> From the article: </p> <blockquote><i> Sydney drivers are more hostile towards cyclists than motorists of any other country, says a world authority on transport. </i></blockquote> <blockquote><i> Christian Wolmar, a Briton who was invited by the State Government to promote bicycle use in NSW, said Sydney's cycling infrastructure is 10 years behind that of London. </i></blockquote> <p> Further on in the article it quotes a spokeswoman for the Roads Minister, Eric Roozendaal, saying: </p> <blockquote><i> "Sydney has around 2000 kilometres of cycle facilities while London only has 550 kilometres," </i></blockquote> <p> Obviously this sounds good until you realize that <a href="http://en.wikipedia.org/wiki/Sydney"> Sydney</a> is 12,144 square kilometers in area while <a href="http://en.wikipedia.org/wiki/Greater_London"> Greater London</a> is only 1579 square kilometers. That means that Sydney has 164 meters of cycle lane per square kilometer while London has 348, almost double. </p> <p> Furthermore, most cycle lanes in Sydney are not just totally inadequate, but <a href="http://www.mega-nerd.com/erikd/Blog/Cycling/not_cycle_lane.html"> outright dangerous</a>. </p> This is Not a Cycle Lane. http://www.mega-nerd.com/erikd/Blog/2008/07/23/not_cycle_lane 2008-07-23T10:46:00Z 2008-07-23T10:46:00Z <center> <img src="http://www.mega-nerd.com/erikd/Img/not_cycle_lane.jpg" border="0" alt="my little pony" /> </center> <br /> <p> Throughout the center of Sydney there are cycle lanes marked like the ones here on Crown St, Woolloomooloo. The cycle lane itself is about a meter wide and the distance between the parked cars and the inside marker of the cycle lane is about 30cm. Also notice that there is traffic in the on coming lane. </p> <p> The problem with these cycle lanes is that they are potentially lethal for cyclists. The problem of course are the parked cars. If a single car contains a person who opens a driver's side door at the wrong time, that door will almost certainly block most of the cycle lane leaving any unlucky cyclist with a split second to either try to brake or swerve out into the car lane to avoid the car door. Obviously swerving into the car lane might put the cyclist in the path of cars or trucks which are probably even more dangerous than the open car door that the cyclist was trying to avoid. </p> <p> The only safe option for cyclists in conditions like these is to ride in the middle of the car lane, far from the parked cars. Here they will be clearly visible to vehicles behind them and also to traffic heading the other way. </p> <p> Cyclists need to lobby their local councils for the removal of these unsafe cycle lanes or replacement with lanes that are safe. </p> Ocaml and Unix.select. http://www.mega-nerd.com/erikd/Blog/2008/07/19/ocaml_select 2008-07-19T11:10:00Z 2008-07-19T11:10:00Z <p> At the June meeting of FP-Syd, Tim Docker gave a presentation about his <a href="http://en.wikipedia.org/wiki/Tuplespace"> Tuple Space</a> Server written in Haskell. This presentation rather intrigued me because I have had a long term interest in numerical analysis and numerical optimisation problems which lend themselves very well to parallel and distributed computing. I decided I should write a Tuple Space Server myself, in Ocaml. </p> <p> Tim's Tuple Space server used threads and <a href="http://en.wikipedia.org/wiki/Software_transactional_memory"> Software Transactional Memory (STM)</a> to handle the connection of multiple masters and workers to the server itself. Although the Ocaml <a href="http://cothreads.sourceforge.net/"> CoThreads library</a> does have an <a href="http://cothreads.sourceforge.net/doc/manual/Stm.html"> STM module</a> I thought there was probably an easier way. </p> <p> In my day job I'm working on some C++ code that handles multiple network sockets and open file descriptors using the POSIX <a href="http://linux.die.net/man/2/select"> <tt><b>select</b></tt></a> system call. On Linux at least, there is a <a href="http://linux.die.net/man/2/select_tut"> select tutorial man page</a> which gives a example of using <tt><b>select</b></tt> written in C. </p> <p> The beauty of <tt><b>select</b></tt> is that it allows a single process to multiplex multiple sockets and/or file descriptors without resorting to threads. However, the C example in the tutorial clearly demonstrates that this system call is a bit of a pain to use directly. Fortunately, for the project at work, I had some really great C++ base classes written by my colleague Peter to build on top of. These base classes hide all the nastiness of dealing with the system call itself by wrapping the <tt><b>select</b></tt> call into a daemon class and providing a simple base class which clients of the <tt><b>select</b></tt> call can inherit from. </p> <p> For Ocaml there is a thin wrapper around the C library function in the <tt><b>Unix</b></tt> module and it has the following signature: </p> <pre class="code"> val select : file_descr list -> file_descr list -> file_descr list -> float -> file_descr list * file_descr list * file_descr list </pre> <p> It takes three lists of file descriptors (one descriptor list for each of read, write and exceptions), a float value for a timeout and returns a tuple of three lists; one each for the file descriptors ready for reading, writing and exception handling. </p> <p> Whereas the C++ solution had a daemon class, the Ocaml version instead has a daemon function. The daemon function operates on a set of tasks, with one file descriptor per task. Each file descriptor was embedded in a struct which I named <tt><b>task_t</b></tt>: </p> <pre class="code"> type task_t = { fd : Unix.file_descr ; mutable wake_time : float option ; mutable select_on : bool ; mutable process_read : task_t -> bool * task_t list ; mutable process_wake : task_t -> bool * task_t list ; finalize : task_t -> unit ; } </pre> <p> The fields of the struct are as follows: </p> <ul> <li> <tt><b>fd</b></tt> : This is the file descriptor or socket that this task is operating on. </li> <li> <tt><b>wake_time</b></tt> : This is an optional, per task timeout value. It is specified as either <tt><b>None</b></tt> for no timeout or <tt><b>Some</b></tt> with a value in seconds (ie <tt><b>Some 10.0</b></tt> specifies a ten second timeout). A typical use might be when writing an network daemon where you want to close/drop any client connections which have been silent for more than a specified amount of time. </li> <li> <tt><b>select_on</b></tt> : If this is true, the daemon will add this task's file descriptor to the list of files waiting for read. This field (set to false) in conjunction with the <tt><b>process_wake</b></tt> can be used to implement deferred actions on a descriptor that ignore readiness for read. </li> <li> <tt><b>process_read</b></tt> : This is the function that is called when the file descriptor has data ready to be read. The function takes the <tt><b>task_t</b></tt> struct as a parameter and returns a tuple containing a <tt><b>bool</b></tt> and a <tt><b>task_t list</b></tt>, which may be empty. There's more on what happens with the return values of this function below. </li> <li> <tt><b>process_wake</b></tt> : This is the function that is called when a <tt><b>wake_time</b></tt> value has been set and there has be no activity on the socket for the specified amount of time. This function's return type is the same as for <tt><b>process_read</b></tt>. </li> <li> <tt><b>finalize</b></tt> : This function will be called when either of the above two functions notifies the daemon that the file descriptor should be closed. </li> </ul> <p> The first thing to note in the above is the careful use of an immutable field for the file descriptor and mutable fields for <tt><b>process_read</b></tt>, <tt><b>process_wake</b></tt> and <tt><b>wake_time</b></tt>. The file descriptor is immutable so that any client code does not change its value behind the back of the daemon. </p> <p> The others fields of the struct are purposely made to be mutable so that they can be changed on the fly. The functions <tt><b>process_read</b></tt> and <tt><b>process_wake</b></tt> both return their results in the same manner, a tuple containing two items: </p> <ul> <li> A <tt><b>bool</b></tt> which indicates whether the daemon should hold onto the task (if <tt><b>true</b></tt>) or if <tt><b>false</b></tt>, the daemon should run the task's <tt><b>finalize</b></tt> function and then drop it to be cleaned up by the garbage collector. </li> <li> A <tt><b>task_t list</b></tt> (possibly empty) of new tasks for the daemon to manage. Typically the daemon's server socket would be managed by the daemon function just like any other socket. When the server socket's <tt><b>process_read</b></tt> function accepts a new client connection it would return the new client task in this list. </li> </ul> <p> The actual daemon run loop keeps the tasks in a hash table where the key is the file descriptor. Once the initial set of tasks is in the hash table, the loop basically does the following: </p> <ol> <li>Find the file descriptors of all the tasks in the hash table which their <tt><b>select_on</b></tt> field set to <tt><b>true</b></tt> (uses <tt><b>Hashtbl.fold</b></tt>). </li> <li>Find the minimum <tt><b>wake_time</b></tt> timeout of all the tasks (this is actually done on the same pass over all items in the hash tables as step 1.). </li> <li>Pass the file descriptors from step 1. to the <tt><b>select</b></tt> with the timeout value found in 2. (The lists for writable and exception file descriptors are empty.) </li> <li>When select returns a list or file descriptors ready to be read, map the file descriptor to a task using the hash table and then run the <tt><b>process_read</b></tt> function of each readable task. </li> <li>For each task whose <tt><b>wake_time</b></tt> is exceeded, run its <tt><b>process_wake</b></tt> function. </li> <li>For steps 4. and 5., if a task's process function returns <tt><b>false</b></tt> as the first element of the tuple it returns, remove the task from the hash table and run the task's finalize function. Also if the second element in the tuple is a non-empty list, then add the tasks to the hash table. </li> </ol> <p> The above code was placed in a module named <tt><b>Daemon</b></tt>. Using this module, I've whipped up a simple demo program, an echo server the source code of which is <a href="http://www.mega-nerd.com/erikd/Blog/files/echo-server.tgz"> available here</a>. The tarball contains four files: </p> <br/> <center> <table class="simple"> <tr> <td><tt>Makefile</tt></td> <td>The project's Makefile.</td> </tr> <tr> <td><tt>daemon.ml</tt></td> <td>The Daemon module.</td> </tr> <tr> <td><tt>echo-server.ml</tt></td> <td>The Echo server.</td> </tr> <tr> <td><tt>tcp.ml</tt></td> <td>A module of TCP/IP helper functions.</td> </tr> </table> </center> <br/> <p> To compile this you will need the Ocaml native compiler which can be installed on Debian or Ubuntu using: </p> <pre class="code"> sudo apt-get install ocaml-nox </pre> <p> The server can be built using <tt><b>make</b></tt> and when run, you can connect to the server using: </p> <pre class="code"> telnet localhost 9301 </pre> <p> All lines sent to the server will be immediately echoed back to you. </p> FP-Syd #5. http://www.mega-nerd.com/erikd/Blog/2008/07/18/fp-syd-05 2008-07-18T08:08:00Z 2008-07-18T08:08:00Z <p> Last night was the 5th meeting of FP-Syd, the Sydney Functional Programming group. The turnout was down a bit (19 people) but I suspect that was due to <a href="http://www.wyd2008.org/"> World Imaginary Friend Day</a> (which for some reason seems to run for a week) clogging up Sydney's streets and public transport system. </p> <p> The first talk was given by the author of this blog and was titled <i>"Ocaml and Unix.select"</i>. I'll be blogging this in more detail some time over the weekend. </p> <p> Next up we had Ben Lippmeier, author of the <a href="http://haskell.cs.yale.edu/haskellwiki/DDC"> Disciple compiler</a> on his entry into the <a href="http://icfpcontest.org/"> ICFP Programming Contest</a>. In the long tradition of eating one's own dog food, Ben used his DDC compiler to write his entry and ran into a nasty intermittent bug in his compiler's run time. Despite the bug he still managed to put together a pretty decent solution to a rather interesting problem. </p> <p> As usual, we had post-meeting drinks at the Redoak hotel. Thanks to Shane, James, the other Googlers and Google itself for use of Google's excellent facilities. </p> Tonight on the Telephone. http://www.mega-nerd.com/erikd/Blog/2008/07/02/telemarketer 2008-07-02T11:48:00Z 2008-07-02T11:48:00Z <p><i> Its a little after 7pm and the phone rings. I answer. </i></p> <p> <b>Me :</b> Hello. </p> <p> <i>Faint music on the other end.</i> </p> <p> <b>Me :</b> Hello! </p> <p> <i>Music continues.</i> </p> <p> <b>Me :</b> Hello !!! </p> <p> <b>Some Marketer :</b> Yes, good morning, blah, blah, blah. Would you be interested in cheap holidays, blah, blah, blah. </p> <p> <b>Me :</b> Tell me more. </p> <p> <b>Marketer :</b> Well, you can go here and you can go there. Blah, blah, blah. </p> <p> <b>Me :</b> That sounds interesting. </p> <p> <b>Marketer :</b> Blah, blah, blah. </p> <p> <b>Me :</b> That sounds good. </p> <p> <b>Marketer :</b> Blah, blah, blah. </p> <p><i> Pause. </i></p> <p> <b>Me :</b> Can I bring my pony? </p> <p> <b>Marketer :</b> Excuse me? </p> <p> <b>Me :</b> Can I bring my pony? </p> <p> <i>Clunk, the marketer hangs up.</i> </p> <p> I managed to waste 1 minute 38 seconds of his time. I need to do better next time. </p> <br /> <center> <img src="http://www.mega-nerd.com/erikd/Img/my_little_pony.png" border="0" alt="my little pony" /> </center> <br /> Goodbye Old Car. http://www.mega-nerd.com/erikd/Blog/2008/06/29/old_car 2008-06-29T04:29:00Z 2008-06-29T04:29:00Z <p> For the last 8 or 9 years I've been driving this very nice 1985 BMW 323i. </p> <br /> <center> <img src="http://www.mega-nerd.com/erikd/Img/bmw-10f.jpg" border="0" alt="bmw-10f" /> </center> <br /> <p> However, the new apartment has off-street parking but not under cover parking. An old car like this would simply not survive being parked out in the open so I've had to sell it rather than see it rust away. It was sold a little over a week ago. </p> <p> Goodbye old car. </p> Goodbye Old House. http://www.mega-nerd.com/erikd/Blog/2008/06/16/old_house 2008-06-16T09:22:00Z 2008-06-16T09:22:00Z <p> For the last eight years, my wife and I have enjoyed the room in this picture (with that view) as our main living area. This apartment was more than big enough for the two of us, but with the arrival of our daughter five and a half years ago, it has been increasingly cramped. </p> <br /> <center> <img src="http://www.mega-nerd.com/erikd/Img/old_house.png" border="0" alt="living room" /> </center> <br /> <p> After a long, long search, we found and bought a new apartment a couple of months ago and we take possession of the new place this week. We'll have about 5 days to move between houses and then we'll have to say goodbye to this wonderful home which we have enjoyed so much. </p> FP-Syd #4. http://www.mega-nerd.com/erikd/Blog/2008/06/06/fp-syd-04 2008-06-06T12:18:00Z 2008-06-06T12:18:00Z <p> Last night was the 4th meeting of FP-Syd, the Sydney Functional Programming group. First up I'd like to thank Shane, James and the other Googlers who organised the use of one of Google's Sydney meeting rooms and laid on drinks and a bunch of tasty snacks. Another big thanks goes to our speakers for the evening and the <i>32 (!!!)</i> people who showed up. </p> <p> Unfortunately one of our planned speakers for the night had to drop out at the last minute. This meant that as people arrived I was asking them if anyone had a short talk they could give at very short notice. The thing that impressed me the most was that we got two offers. </p> <p> First up we had Sean Seefried talk about using Ocaml at Nicta to write Nicta's <a href="http://nicta.com.au/research/projects/goanna/tool/"> Goanna</a> static type checking tool for C and C++ code. It was interesting that they were using an existing commercial C and C++ parser and the converting the C abstract syntax tree (AST) produced to an Ocaml AST where they work on it further. Also interesting was that the chose Ocaml because it was a good language for writing compilers as well as having reasonably good and highly predictable performance. This was an excellent talk considering that Sean had about 10 minutes to prepare. Thanks Sean. </p> <p> The next speaker was Scott Kilpatrick who also had about 10 minutes to prepare as well as being our first international speaker. Scott has just recently completed two B.S. degrees (one in Computer Science, the other in Mathematics) from the University of Texas at Austin and will be starting a Masters there in September. He's is currently interning at Google here in Sydney working on the Google Maps project. </p> <p> Scott gave us a very interesting introduction to SUN's <a href="http://research.sun.com/projects/plrg/Fortress/overview.html"> Fortress</a> language. Scott is working on implementing the type checking system for the Fortress compiler rather than being a dedicated user of the language. He was however able to give us a brief overview of the language, which is designed to replace Fortran in high performance computation tasks. Fortress seems to have a huge array of interesting features and is definitely something to keep an eye on. Thanks Scott. </p> <p> Finally we had Tim Docker speaking about writing a Tuple Server in Haskell using the State Transactional Memory monads in concurrent Haskell. Tim did a great job explaining what Tuple Servers are and what they are used for where he works at Maquarie Bank. He then talked about the monads in Haskell, in particular the STM monad and its use in his tuple space server. It was a great talk and I mid way through it, I realised that a tuple space server is a nice solution to an architectural problem I have. Thanks Tim. </p> <br /> <center> <img src="http://www.haskell.org/sitewiki/images/e/ea/Ddc-alpha1-logo.png" border="0" alt="ddc logo" /> </center> <br /> <p> After the talks we retired to the Redoak hotel for a few beers and a chat. Since a couple of people had missed the previous meeting, Ben Lippmeier, was kind enough to pull out his laptop and give his talk on <a href="http://haskell.cs.yale.edu/haskellwiki/DDC"> <i>"The Disciplined Disciple Compiler"</i></a> again. Interestingly for me, he went into a little more detail on areas that he glossed over in the last meeting. DDC is a really interesting piece of work, being a strictly evaluated dialect of Haskell, with optional lazy evaluation (far more elegant than the way Ocaml does it) and with side effects and destructive updates looked after in the type system. The ideas behind DDC are really compelling. I look forward to seeing it progress. Thanks Ben. </p> Keeping Up With .... http://www.mega-nerd.com/erikd/Blog/2008/05/29/keeping_up_with 2008-05-29T11:54:00Z 2008-05-29T11:54:00Z <p> Here's my pathetic attempt at keeping up with <a href="http://svana.org/sjh/diary/2008/05/28#2008-05-28_01"> Steve Hanley</a>, <a href="http://jon.oxer.com.au/blog/id/55"> Jon Oxer</a> and <a href="http://www.debianslashrules.org/Work/DroolWorthy.html"> Mike Beattie</a>. </p> <center> <img src="http://www.mega-nerd.com/erikd/Img/work_desktop.png" border="0" alt="work desktop" /> </center> <p> From left to right: </p> <ol> <li>My Dell Latitude X1 laptop running Enlightenment E17 on an Ubuntu Hardy base system. The desktop background is a picture of my lovely daughter.</li> <li>A windows machine which is used mainly as a host for Firefox and the Tortoise SVN client. I spent the first six months in this job doing windows development. Fortunately that is all in the past.</li> <li>My Linux desktop system, again running E17, but on an Ubuntu Gutsy base system.</li> <li>One of our very cute <a href="http://www.bcode.com"> bCODE</a> scanners, which has just been PXE booted to a slightly modified Ubuntu Dapper installation, but before having over 100 of bCODE's own Debian packages installed on it.</li> <li>A second bCODE scanner running its Adobe Flash GUI on top of an Ubuntu Dapper install done by cloning a disk image.</li> </ol> <p> The Linux and windows machine share a keyboard and a mouse via the magic of <a href="http://synergy2.sourceforge.net/"> Synergy2</a>. The keyboard is my favourite <a href="http://www.mega-nerd.com/erikd/Blog/Tech/nice_keyboard.html"> Dell Enhanced USB Multimedia Keyboard</a>. </p> <p> The one thing missing from the above photo (two separate pictures taken and stitched together by my colleague Peter) is the whiteboard which is directly behind my seat. Yes, I have my own personal whiteboard and I <b><i>love it</i></b>. </p> Objects vs Modules. http://www.mega-nerd.com/erikd/Blog/2008/05/24/objects_vs_modules 2008-05-23T21:45:00Z 2008-05-23T21:45:00Z <p> Although I've been using Ocaml for a several years now, I've not yet been in a situation where I've needed to write an Ocaml class to define a C++/Java/Python/Smalltalk/OO style object. I've found that most of the problems I encountered could be easily solved using functional code and that Ocaml's objects didn't provide an obviously better solution. Until now (or so I thought). </p> <p> The problem was one of moving around the filesystem keeping track of the old directories so they were easy to return to. The obvious model for this was the <tt><b>pushd</b></tt> and <tt><b>popd</b></tt> built-ins in command shells like GNU Bash. This functionality can be easily wrapped up in an Ocaml object as in the following example and demo code (which needs to be linked to the Unix module): </p> <pre class="code"> class dirstack = object val mutable stack = [] method push dirname = (* Find the current working directory. *) let cwd = Unix.getcwd () in (* Change to the new directory. *) Unix.chdir dirname ; (* If successful, push old cwd onto the stack. *) stack &lt;- cwd :: stack method pop () = match stack with | [] -> failwith "Directory stack is empty." | head :: tail -> Unix.chdir head end let () = print_endline (Unix.getcwd ()) ; let dstack = new dirstack in dstack#push "/tmp" ; print_endline (Unix.getcwd ()) ; dstack#push "/bin" ; print_endline (Unix.getcwd ()) ; dstack#pop () ; print_endline (Unix.getcwd ()) ; dstack#pop () ; print_endline (Unix.getcwd ()) </pre> <p> However, there are some problems with the above code. Firstly, if the <tt><b>push</b></tt> and <tt><b>pop</b></tt> methods need to be used throughout the program, the <tt><b>dstack</b></tt> object needs to be made more widely accessible using one of the following three methods: </p> <ol> <li>Being placed in the global scope.</li> <li>Being made into a Singleton objecct.</li> <li>Being passed around as a parameter to whatever function may need it. </li> </ol> <p> Yuck! Yuck! <i>Double yuck!</i> Suddenly, this object oriented solution didn't look like such a great idea. </p> <p> Then it struck me. This object can be easily transformed into an Ocaml module like this: </p> <pre class="code"> module Dirstack = struct let stack = ref [] let push dirname = (* Find the current working directory. *) let cwd = Unix.getcwd () in (* Change to the new directory. *) Unix.chdir dirname ; (* If successful, push old cwd onto the stack. *) stack := cwd :: !stack let pop () = match !stack with | [] -> failwith "Directory stack is empty." | head :: tail -> stack := tail ; Unix.chdir head end let () = print_endline (Unix.getcwd ()) ; Dirstack.push "/tmp" ; print_endline (Unix.getcwd ()) ; Dirstack.push "/bin" ; print_endline (Unix.getcwd ()) ; Dirstack.pop () ; print_endline (Unix.getcwd ()) ; Dirstack.pop () ; print_endline (Unix.getcwd ()) </pre> <p> This solution using a module is much better than the one using an object. The <tt><b>Dirstack</b></tt> module itself is globally accessible and is already a singleton while the stack used to hold past directories is implemented as a list whose scope is limited to the module itself. (Furthermore, if <tt><b>Dirstack</b></tt> is implemented in its own file instead of using a module defined within a larger file, then the <tt><b>stack</b></tt> variable can be hidden completely by not listing it in the <tt><b>Dirstack</b></tt> interface file.) </p> <p> So while I'm pleased with this solution, it does mean that I'll have to continue my hunt for a problem where an object provides a better solution than any other feature of the Ocaml language. This is particularly ironic because when choosing between two strict statically typed languages, Haskell and Ocaml, I chose Ocaml because I thought I needed objects. However, I stuck with Ocaml because of its pragmatism. </p> Cross Compiling for Legacy Win32 Systems (Part 2). http://www.mega-nerd.com/erikd/Blog/2008/04/20/cross_compiling_2 2008-04-20T10:51:00Z 2008-04-20T10:51:00Z <p> Cross compiling from Linux to Windows requires the installation of a couple of packages. On a Debian or Ubuntu system this can be done using: </p> <pre class="code"> sudo apt-get install build-essential sudo apt-get install mingw32 mingw32-binutils mingw32-runtime wine </pre> <p> I'm running Ubuntu's Hardy Heron pre-release and the following is known to work with these versions: </p> <pre class="code"> mingw32 4.2.1.dfsg-1ubuntu1 mingw32-binutils 2.17.50-20070129.1-1 mingw32-runtime 3.13-1 wine 0.9.59-0ubuntu5 </pre> <p> For an example of a project which can be successfully cross-compiled, I have chosen <tt><b>libogg</b></tt> which is one of the two libraries required to encode and decode Ogg/Vorbis files. I also happen to know that the current <tt><b>libogg</b></tt> sources in the Xiph Foundation's SVN repository cross-compile from Linux to Windows correctly because I <a href="https://trac.xiph.org/changeset/14725"> committed the patch</a> to make it possible. </p> <p> However, we need to look ahead a little. After we have cross compiled <tt><b>libogg</b></tt> we will also want to cross compile the associated <tt><b>libvorbis</b></tt> library which relies on <tt><b>libogg</b></tt>. We therefore need to configure <tt><b>libogg</b></tt> so that when we install it, it can be found by the <tt><b>libvorbis</b></tt> configure script. </p> <p> For me that meant creating a <tt><b>MinGW32</b></tt> directory in my home directory: </p> <pre class="code"> mkdir $HOME/MinGW32 </pre> <p> The next step to to grab the <tt><b>libogg</b></tt> source code from the Xiph SVN server. This can be achieved using the command: </p> <pre class="code"> svn co http://svn.xiph.org/trunk/ogg libogg </pre> <p> Changing into the <tt><b>libogg</b></tt> directory, we are now ready to configure, test and install the library. That can be done using: </p> <pre class="code"> ./autogen.sh ./configure --host=i586-mingw32msvc --target=i586-mingw32msvc \ --build=i586-linux --prefix=$HOME/MinGW32 make make check make install </pre> <p> The first command above, runs the auto tools to generate that configure script. The second command, <tt><b>configure</b></tt> is broken across two lines. It sets up the generated Makefiles to compile Windows binaries from a Linux host, with the install directory we set up before. The third line builds the windows version of <tt><b>libogg</b></tt>, the fourth line runs the test suite, with the windows executables being run under WINE and the final line installs everything in the <tt><b>MinGW32</b></tt> directory created earlier. </p> <p> All of the above commands should pass without errors. If they don't, check your versions of of the mingw cross compiler tools and/or WINE. </p> FP-Syd #3. http://www.mega-nerd.com/erikd/Blog/2008/04/18/fp-syd-03 2008-04-17T14:44:00Z 2008-04-17T14:44:00Z <p> Wow! Just Wow! I've just arrived home after another FP-Syd meeting. </p> <p> First up we had Jeremy Apthorp with a short talk titled <i>"My Favourite Bugs (that don't exist in functional languages)"</i>. Jeremy gave us all a good reminder of why we were all there. It was also a good warm up the second talk. </p> <p> Ben Lippmeier, gave us a talk titled <i>"The Disciplined Disciple Compiler"</i> which is something he's working on as part of his PhD at Australian National University. Ben's <a href="http://haskell.cs.yale.edu/haskellwiki/DDC"> DDC</a> is a compiler for a Haskell dialect named Disciple that uses <a href="http://haskell.cs.yale.edu/haskellwiki/DDC/EffectSystems"> effect typing</a> to deal with issues like side effects and destructive updates. Unlike Haskell, Disciple is strictly evaluated with optional, explicit lazy evaluation. This was a really thought provoking talk. Thanks Ben. </p> <p> I would also like to send out a big thanks to Tom and Scott of <a href="http://www.atlassian.com/"> Atlassian</a>'s Sydney office for providing the venue. </p> <p> After the meeting we headed off the to Readoak hotel for beers and more FP discussion. By 10:30pm we were just about to leave the Redoak when I ran into <a href="http://geekdamana.blogspot.com/"> Damana</a> who used to work at <a href="http://www.bcode.com/"> bCODE</a> and who had organized Sydney Geek Girl get together that same night. </p> Cross Compiling for Legacy Win32 Systems (Part 1). http://www.mega-nerd.com/erikd/Blog/2008/04/16/cross_compiling 2008-04-16T13:12:00Z 2008-04-16T13:12:00Z <p> My main two FOSS projects, <a href="http://www.mega-nerd.com/libsndfile/">libsndfile</a> and <a href="http://www.mega-nerd.com/libsamplerate/">libsamplerate</a> have significant numbers of users that are tied to that particularly odious legacy system, Microsoft Windows. Since I don't normally use Windows myself, maintaining support for that OS has always been a huge pain in the neck. </p> <p> Originally I shipped Microsoft project files for libsndfile, but that became unworkable because the different versions of the Microsoft tools (Visual C++ 5, Visual C++ 6, Visual Studio 2003, Visual Studio 2005 etc) used different and incompatible project file formats. I solved this by shipping a simple Makefile that used Microsoft's <tt><b>nmake</b></tt> and the command line compilers to build libsndfile. However, by about 2004, the Microsoft compiler's complete lack of support for the <a href="http://www.mega-nerd.com/erikd/Blog/Windiots/ms_c99.html"> 1999 ISO C Standard</a> made maintaining support too much trouble, so it was dropped. </p> <p> Instead, I started using <a href="http://cygwin.com/"> Cygwin</a> and <a href="http://www.mingw.org/"> MinGW</a> to compile libsndfile on Windows. Both of these tool-sets use a version of the <a href="http://gcc.gnu.org/"> GNU GCC compiler</a> just like Linux and building libsndfile using these two tool-sets was trivial: </p> <pre class="code"> ./configure make make check </pre> <p> Of course there were howls of protest from Windows users, but since they (with a small number of exceptions) had contributed so little, I didn't fell like I owed them anything. I also started releasing pre-compiled Windows binaries at the same time as the source code tarballs were released. </p> <p> However, while the MinGW compiler was a huge improvement over the Microsoft one it was still a huge pain in the neck. I had to keep a Windows machine and keep it updated and patched against vulnerabilities. Furthermore, installing and updating MinGW was a painful manual process. Oh how I longed for a Debian/Ubuntu style <tt><b>apt-get</b></tt> command to look for and install updates. Finally, copying source code back and forth between Linux and Windows while debugging Windows issues was another pain point because version control systems like GNU Arch and <tt><b>bzr</b></tt> simply didn't work very well on Windows. </p> <p> In about 2004, I tried the MinGW <a href="http://packages.debian.org/source/lenny/mingw32"> Linux to Windows cross compiler</a>, a compiler that runs on Linux but generates binaries for Windows. This compiler worked, but left one rather large problem; how do I run libsndfile's rather large and comprehensive test suite? Compiling libsndfile without running the test suite is a waste of time. I did try to run the tests under <a href="http://www.winehq.org/"> WINE (the Windows emulator)</a>, but at the time tests were failing under WINE that didn't fail on Windows. </p> <p> From that time on, I would try running the cross-compiled test suite under WINE once or twice a year. Then, some time in the last year or so, the number of problems with the test suite dropped to one, which was only a FIXME message. A little hacking on the WINE sources resulted in a patch that was <a href="http://www.winehq.org/pipermail/wine-patches/2008-April/052980.html"> sent to the WINE mailing list</a> and has since been applied to the main WINE source tree. </p> <p> With that bug fixed, I can now cross compile from Linux to Windows and run the full libsndfile test suite under WINE. That means that Windows has just become that little bit less relevant that it was before. </p> <p> A future post will explain how to set up the cross compiler and WINE and walk through compiling and testing of a standard FOSS project. </p> You Stupid Git! http://www.mega-nerd.com/erikd/Blog/2008/04/11/stupid_git 2008-04-11T10:02:00Z 2008-04-11T10:02:00Z <p> As far as I can tell, the absolute, canonical, got-to-first documentation for the <tt><b>git</b></tt> distributed version control system (DVCS) can be found here: </p> <center> <a href="http://www.kernel.org/pub/software/scm/git/docs/user-manual.html"> http://www.kernel.org/pub/software/scm/git/docs/user-manual.html</a> </center> <p> This documentation seems comprehensive and well laid out. It explains commits, <a href="http://www.kernel.org/pub/software/scm/git/docs/user-manual.html#manipulating-branches"> manipulating-branches</a>, <a href="http://www.kernel.org/pub/software/scm/git/docs/user-manual.html#how-to-merge"> merging</a>, collaborative development and the pretty damn interesting <a href="http://www.kernel.org/pub/software/scm/git/docs/user-manual.html#using-git-rebase"> rebase</a> and <a href="http://www.kernel.org/pub/software/scm/git/docs/user-manual.html#using-bisect"> bisect</a> commands. This documentation is called a user manual but it contains sufficient examples to make it a pretty damn fine tutorial. </p> <p> Normally something like <i>"here's a link to the documentation"</i> would not be worthy of a blog post. However, failure to find the canonical user manual could lead a person (ie me) to post messages to mailing lists saying things like: </p> <blockquote><i> "I'm sure git is very clever and all, but its UI and documentation is probably the most user hateful thing I have seen [since] sendmail's cf files." </i></blockquote> <p> or, on finding a one hour long <a href="http://www.linuxworld.com/news/2008/040208-git-screencast.html"> video screen-cast</a> tutorial (apparently aimed at all those Ruby on Rails writing Mac OSX users): </p> <blockquote><i> "This makes me wonder, how fscked up does a DVCS have to be that you need tens of megabytes of video to show how it works when Bzr and many others can do it with less than ten kilobytes of html text?" </i></blockquote> <p> So while I was wrong about the documentation I still have huge reservations about git's user interface and stand by this statement: </p> <blockquote><i> "I am currently trying to learn git and I can see very clearly that git is designed by kernel programmers whose normal approach to a user interface is something like a Unix system call." </i></blockquote> <p> I'm sure <tt><b>git</b></tt> is a powerful tool and the <tt><b>rebase</b></tt> feature is something I've been wishing for in other systems for some time, but <tt><b>git</b></tt>'s UI is already starting to grate. </p> Ocaml : Exception Back Traces in Native Code. http://www.mega-nerd.com/erikd/Blog/2008/04/06/native_backtraces 2008-04-06T02:48:00Z 2008-04-06T02:48:00Z <p> Some time ago I wrote a blog post about <a href="http://www.mega-nerd.com/erikd/Blog/CodeHacking/Ocaml/exception_backtraces.html"> exception back traces</a> which at the time of that post only existed for the Ocaml byte code compiler. </p> <p> However, version 3.10 of the Ocaml compiler which was released about a year ago, included exception back traces for native code as well as byte code. With the imminent release of Ubuntu's Hardy Heron, version 3.10 of the compiler is about to become much more widely available . </p> <p> Enabling exception back traces is as simple as adding the <tt><b>"-g"</b></tt> option to the <tt><b>ocamlopt</b></tt> command line and then setting a single environment variable as follows. </p> <pre class="code"> export OCAMLRUNPARAM="b1" </pre> Goodbye People Telecom. http://www.mega-nerd.com/erikd/Blog/2008/04/02/people_telecom 2008-04-02T09:09:00Z 2008-04-02T09:09:00Z <p> I don't want to be accused of <a href="http://www.urbandictionary.com/define.php?term=blogorrhea"> blogorrhea</a> but I really need to get this off my chest. </p> <p> I've been a customer of <a href="http://www.peopletelecom.com.au/"> People Telecom</a> since they took over Swiftel in 2004. Since this connection is paid for by revenue generated by <a href="http://www.mega-nerd.com/SRC/"> SecretRabbitCode</a> I have been on one of their most expensive and highest bandwidth plans. I have been a good customer; my bills were paid by direct debit from an account that always had sufficient funds, I rarely go anywhere near my download limits and as a moderately advanced Linux user rarely, if ever, call their technical support. </p> <p> However, I am about to end my relationship with People Telecom and this is what led up to it: </p> <ol> <li>My father needed a new ISP so I suggested that he ring People Telecom. He made the call on the morning of Tuesday, March 25th and signed up. He was told that someone would contact him within 4 days to finalize things. </li> <li>On Monday March 30th, over 4 working days after the initial call he rang them again and was told that they had determined him to be a bad credit risk. Now firstly, it was just plain <b><i>rude</i></b> for them to have decided this and not told him and <b><i>stupid</i></b> for not informing him and trying to find an alternative payment solution. Secondly, my father is not a bad credit risk. He's a self funded retiree, paid off his mortgage over 15 years ago and has never had a credit card. As a credit risk, you don't get much better than this. </li> <li>After hearing about this, I rang People Telecom at about 10am to see if there was some way to get my father connected. After negotiating their phone robot and listening to their on-hold music I finally spoke to someone and explained the situation. They said I needed to talk the credit department and switched me though, first to listen to the on-hold music and then to voice mail. I left my details and a message asking them to call me. </li> <li>By 6pm that day I still hadn't received a call from the credit department so I called again, negotiated the phone robot, got transferred to the credit department, listened to the on-hold music and then got put through to voice mail. I left a second message. </li> <li>At around 8pm I thought I might try and contact them via their web site to try and get this sorted out. Unfortunately, their web site was down and giving SQL errors. </li> <li>On Tuesday April 1st at about 9:30 am, I rang again, negotiate the phone robot and got put on hold, but this time there is no music. Thinking their phone system was broken I hung up. </li> <li>I try again at about 10am, negotiate the phone robot and got put on hold, but again, no music so again, I hung up. I then tried again to contact them via their web site and this time it worked. Since the web mail form only allowed a limited number of characters I just asked for them to contact me and left my mobile number. </li> <li>By lunch time I still hadn't been contacted so I tried to ring again. I negotiated the phone robot, got put on hold, ignored the fact that there was nothing to indicate that I actually was on hold and finally got through to a person. </li> <li>I explained the situation briefly and the tech support guy on the other end told me I need to talk to the credit department. I explained that I've tried to talk to the credit department and they didn't return my calls. The tech support guy suggested I talk to the billing department, promises I won't get put on hold, puts me though and I go on hold. Silence. </li> <li>After a short period of time a woman picked up the phone and says she has read the case notes but there's not a lot she can do because its a issue with the credit department. I explain my problems with their credit department and ask to be put through to her manager. She agreed and I got put on hold again. Silence. </li> <li>I finally got to talk to a manager named Eric. I explain the situation, explain that I have now spent over an hour of my very valuable time and suggest that its time for People Telecom to get this sorted out. </li> <li>Eric, the manager suggests I put my fathers connection on my bill. I reply that this is not a good solution because my bill is a company bill, not my personal bill and having my father's bill on my company's bill would complicate accounting. </li> <li>I asked if it was possible to direct debit my father's account and if for some reason his account isn't paid, they bill my personal credit card as a fallback. The manager responds that their system isn't able to do this. </li> <li>Finally, I suggest that they need to come up with a decent solution to this or they lose me as a customer. </li> <li>No solution followed so I told Eric the manager that some time over the next couple of months I will be changing ISPs and then I said goodbye. </li> <li>Late Tuesday afternoon I received an email stating that the issue had been escalated to the Manager of the Credit Department and that I should receive a call tomorrow. By the end of Wednesday that call still hadn't come. </li> </ol> <p> I am dumbfounded. I simply cannot remember ever having this kind of run-around and pig-headed intransigence from any other company ever. </p> <p> I am now searching for a new ISP; one with something like my current 8000k down, 384k up link, about 10 gigs a month with a static IP address and reverse DNS. </p> libsamplerate 0.1.3. http://www.mega-nerd.com/erikd/Blog/2008/03/30/rel_0_1_3 2008-03-30T04:11:00Z 2008-03-30T04:11:00Z <p> About a week ago I released a new version of <a href="http://www.mega-nerd.com/SRC/download.html"> SecretRabbitCode (aka libsamplerate)</a>. </p> <p> The major change was that the new improved SINC based converters I <a href="http://www.mega-nerd.com/erikd/Blog/CodeHacking/SecretRabbitCode/progress.html"> blogged about here</a> are now the default. There were also a couple of minor bug fixes. </p> <p> The fine people at <a href="http://src.infinitewave.ca"> Infinitewave</a> have now updated their test results to include the new converter and it shows Secret Rabbit Code comes very close to the best of the commercial converters in terms of quality. </p> FP-Syd #2. http://www.mega-nerd.com/erikd/Blog/2008/03/27/fp-syd-02 2008-03-27T00:54:00Z 2008-03-27T00:54:00Z <p> Tonight was the second meeting of fp-syd, the Sydney functional programming group. We had a much more focused group this time with 24 attendees. </p> <center> <img src="http://www.mega-nerd.com/erikd/Img/fpsyd-02-a.jpg" border="0" alt="attendees" /> </center> <p> First up I gave a short talk on a data structure I had come up with for handling data in a text editor. This data structure was very much inspired by the book, <i>"Purely Functional Data Structures"</i> by Chris Okasaki. All data in the data structure is treated as immutable which means that it is very easy to store past state and to implement undo / redo. </p> <p> The main talk was split into two parts. First up Manuel Chakravarty gave a talk titled <i>"Monads are Not Scary"</i> and he gave us all an excellent introduction to the topic which I'm pretty sure has convinced all the monad skeptics in the room that monads really aren't anything to be afraid of. </p> <p> The second part of the main talk, was presented by André Pang who built on Manuel's monad introduction and covered Haskell's Parsec monadic parser combinator library. This too was an excellent talk which explained what seems to be a really elegant tool. </p> <center> <img src="http://www.mega-nerd.com/erikd/Img/fpsyd-02-chak.jpg" border="0" alt="Manual" /> <img src="http://www.mega-nerd.com/erikd/Img/fpsyd-02-andre.jpg" border="0" alt="Andre" /> </center> <p> Since the group this week was well over 20 people, we are going to have to look for a bigger venue. Dave Peterson of Orbitec has been a great host for the last two meetings and we would sincerely like to thank him for making his offices available for this bunch of crazy people. </p> Cross Compiling with pkg-config. http://www.mega-nerd.com/erikd/Blog/2008/03/24/pkg-config 2008-03-24T02:24:00Z 2008-03-24T02:24:00Z <p> I'm currently playing with the MinGW cross compiler versions of the GNU C and C++ compilers available via apt-get on Debian and Ubuntu systems. These cross compilers generate windows binaries from a Linux host system which is potentially a much less painful way turning FOSS code into binaries for that particularly odious legacy platform. </p> <p> Most of the software I'm compiling uses the GNU tools; autoconf, automake, libtool and pkg-config for configuring the software before compiling. Autoconf already has good support for cross compiling and automake and libtool just do what autoconf tells them to do. Pkg-config however is the odd one out. </p> <p> Pkg-config's job is to retrieve information about installed libraries so that the compiler can find the required header files for inclusion and libraries for linking. For instance, if you wanted compile a program that uses the <tt>gconf-2.0</tt> library you could find out the required <b><tt>CFLAGS</tt></b> to be passed to the C compiler and required libraries for linking, by doing something like the following in the Makefile. </p> <pre class="code"> GCONF_CFLAGS = $(shell pkg-config --cflags gconf-2.0) GCONF_LIBS = $(shell pkg-config --libs gconf-2.0) </pre> <p> In the above example, when pkg-config is run, it looks in the directory <b><tt>/usr/lib/pkg-config/</tt></b> and reads information from the file <b><tt>gconf-2.0.pc</tt></b> (each installed library should have one or more of these pkg-config files) which then gets printed out. While the information given by pkg-config would be correct for a native build, it is unlikely to be correct for the cross compiling case. </p> <p> This issue came up <a href="http://lists.freedesktop.org/archives/xdg/2003-November/001143.html"> as early as 2003</a> and there is even a <a href="http://pkg-config.freedesktop.org/wiki/CrossCompileProposal"> wiki page</a> which suggests some quite extensive changes to pkg-config. Unfortunately I think these suggestions are somewhat fragile and pkg-config itself (I'm using version 0.22) already has features for a better solution. </p> <p> Like many Unix programs, pkg-config's behaviour can be modified by manipulating certain environment variables. The pkg-config man page explains these variables very well. The first one is <b><tt>PKG_CONFIG_LIBDIR</tt></b> which modifies the default location where pkg-config looks for its per installed library config file. Secondly, the <b><tt>PKG_CONFIG_PATH</tt></b> variable can be set to allow additional pkg-config search paths. </p> <p> Overriding these two variables results in a MinGW cross pkg-config bash script which I have named <b><tt>i586-mingw32msvc-pkg-config</tt></b> and which looks like this: </p> <pre class="code"> #!/bin/bash # This file has no copyright assigned and is placed in the Public Domain. # No warranty is given. # When using the mingw32msvc cross compiler tools, the native Linux # pkg-config executable works fine as long as the default PKG_CONFIG_LIBDIR # is overridden. export PKG_CONFIG_LIBDIR=/usr/i586-mingw32msvc/lib/pkgconfig # Also want to override the standard user defined PKG_CONFIG_PATH with # a mingw32msvc specific one. export PKG_CONFIG_PATH=$PKG_CONFIG_PATH_MINGW32MSVC # Now just execute pkg-config with the given command line args. pkg-config $@ </pre> <p> Now autoconf generated configure scripts that realise that the <b><tt>i586-mingw32msvc-gcc</tt></b> cross compiler is being used will run the above script and get suitable information for the cross compiler rather than the native compiler. </p> <p> The only downside to this solution is that a separate script is required for each cross compiler which uses pkg-config. This however is a minor price to pay and it is unlikely that people will end up with huge numbers of XXXX-pkg-config scripts like was common before the widespread use of pkg-config. </p> <p> Until a better solution becomes available, this is what I will be using. </p> Progress on the Rabbit. http://www.mega-nerd.com/erikd/Blog/2008/03/08/progress 2008-03-08T03:50:00Z 2008-03-08T03:50:00Z <p> For over three years now, I have been working on (on and off, but mostly off) a new algorithm for doing audio <a href="http://en.wikipedia.org/wiki/Sample_rate_conversion"> sample rate conversion</a> in <a href="http://www.mega-nerd.com/SRC/"> Secret Rabbit Code</a>. The idea for the new algorithm has been rattling around in my head for most of that time, but the problem was always the implementation. While I am making progress it has been slow. </p> <p> However, a public comparison between <a href="http://www.mega-nerd.com/erikd/Blog/CodeHacking/SecretRabbitCode/src_compare.html"> a large collection of converters</a> showed that while the conversion quality of Secret Rabbit Code was good, it was nowhere near state of the art. </p> <p> In order to see if I could get Secret Rabbit Code closer to state of the art quickly, I decided to revisit the existing converter during the xmas/new-year break. </p> <p> The existing converter had a set of digital filters whose coefficients were generated by a small program written in <a href="http://www.octave.org/"> GNU Octave</a>. My first task was to convert that program to <a href="http://www.ocaml.org/"> Ocaml</a> which has become my favourite language for technical computing. I then spent quite a bit of time finding and analyzing where the filter design program was loosing precision and finding work arounds. Finally, I spent even more time looking at how the different filter design parameters interact with one another and with the conversion algorithm itself. </p> <p class="readmore"><a href="http://www.mega-nerd.com/erikd/Blog/CodeHacking/SecretRabbitCode/progress.html"><b>Read more &raquo;</b></a></p> Inaugural Meeting of FP-Syd. http://www.mega-nerd.com/erikd/Blog/2008/02/28/fp-syd-01 2008-02-28T11:51:00Z 2008-02-28T11:51:00Z <p> Tonight was the inaugural meeting of fp-syd, the Sydney functional programming group. Here's a somewhat blurry (sorry, my bad) picture of the attendees. </p> <center> <img src="http://www.mega-nerd.com/erikd/Img/fp-syd-01.jpg" border="0" alt="attendees" /> </center> <p> Across the back from left to right we have; Eric, Ben, Ondrej, Dave, Manuel, Rahul, Shane, André and Ben. At the front, left to right we have Sean, Scott, James and Erik. </p> <p> I think the night was an absolutely rip roaring success. We met at about 6:30, we each gave an approximately 10 minute each intro of ourselves and our interests in functional program. The meeting then evolved into a general discussion and then at about 8:30 we moved on to Bazaar Beer Cafe for a beer. </p> <p> Thanks to everyone who attended. It was a huge buzz and I look forward to meeting on a regular basis. </p> Functional Programming and Testing. http://www.mega-nerd.com/erikd/Blog/2008/02/24/testing_ocaml 2008-02-24T12:26:00Z 2008-02-24T12:26:00Z <p> I read quite a lot of programming related blogs, but its rare for me to find one as muddle headed as this one titled <a href="http://beautifulcode.oreillynet.com/2008/02/quality_begs_for_objectorienta_1.php"> "Quality Begs for Object-Orientation"</a> on the O'Reilly network. </p> <p> The author, Michael Feathers, starts the post by mentioning that he is dabbling in Ocaml and then makes the assertion that: </p> <blockquote><i> "I think that most functional programming languages are fundamentally broken with respect to the software lifecycle." </i></blockquote> <p> Now I'm not too sure why he brings up software lifecycle, because all he talks about is testing. However, he does give an example in Java involving testing and wraps up his post by saying that his Java solution is difficult to do in Ocaml, Haskell and Erlang. </p> <p> Feathers gets two things wrong. Firstly he seems to be writing Java code using Ocaml's syntax and then complains that Ocaml is not enough like Java. His conclusion is hardly surprising. Ocaml is simply not designed for writing Java-like object oriented code. </p> <p> The second problem is his claim that testing in functional languages is more difficult than with Java. While this may be true when writing Java code with Ocaml's syntax, it is not true for the more general case of writing idiomatic Ocaml or functional code. </p> <p> So lets look at the testing of <a href="http://en.wikipedia.org/wiki/Object_oriented"> Object Oriented</a> code in comparison to <a href="http://en.wikipedia.org/wiki/Functional_programming"> Functional</a> code. </p> <p> With the object orientated approach, a bunch of data fields are bundled up together in an object and methods defined some of which may mutate the state of the object's data fields. When testing objects with mutable fields, its important to test that the state transitions are correct under mutation. </p> <p> By way of contrast, when doing functional programming, one attempts to write pure functions; functions which have no internal state and where outputs depend only on inputs and constants. </p> <p> The really nice thing about pure functions is that they are so easy to test. The absence of internal state means that there are no state transitions to test. The only testing left is to collect a bunch of inputs that test for all the boundary conditions, pass each through the function under test and validate the output. </p> <p> Since testing pure functions is easier that testing objects with mutable state, I would suggest that assuring quality using automated testing is easier for functional code than for object oriented code. This conclusion directly contradicts the title of Feathers' blog post: <a href="http://beautifulcode.oreillynet.com/2008/02/quality_begs_for_objectorienta_1.php"> "Quality Begs for Object-Orientation"</a>. </p> <p> The lesson to be learned here is that if anyone with a purely Java background wants to learn Ocaml or any other functional language, they have to be prepared for a rather large paradigm shift. Old habits and ways of thinking need to be discarded. For Ocaml, that means ignoring Ocaml's object oriented and imperative programming features for as long as possible and attempting to write nothing but pure stateless functions. </p> <p> <b>Update : 2008-02-26 17:04</b> </p> <p> Conrad Parker posted this to to reddit and the <a href="http://reddit.com/info/69sq5/comments/"> ensuing discussion</a> was quite interesting. </p> Dear Dell http://www.mega-nerd.com/erikd/Blog/2008/02/23/dear_dell 2008-02-22T23:09:00Z 2008-02-22T23:09:00Z <p> I am customer of yours. I own a Dell Latitude X1 laptop, my wife has an Inspiron 6400 and on my recommendation, my father bought a Dell Dimension 5150 desktop system. </p> <p> Dell, I think its important for you to know that none of these machines run windows. All of them run Linux exclusively and have from the day they came into our possession. However for all of these systems we had to pay money to Microsoft for operating systems we have never used. </p> <p> The reason I am writing this now is that the warranty on my Latitude X1 runs out in November of this year. That means that some time between now and November I will be purchasing a new laptop. One candidate for my new machine is the very, very attractive <a href="http://www1.ap.dell.com/content/products/featuresdetails.aspx/xpsnb_m1330"> Dell XPS M1330</a>: </p> <br/> <center> <a href="http://www1.ap.dell.com/content/products/featuresdetails.aspx/xpsnb_m1330"> <img src="http://www.mega-nerd.com/erikd/Img/dell_xps.jpg" border="0" alt="Dell XPS M1330" /> </a> </center> <br/> <p> When I buy my new laptop, I would like to buy it either with Linux pre-installed or without any OS at all. Since I know Linux very, very well indeed, I do not need Dell to support Linux, just the hardware. </p> <p> For me as long time, devoted Linux user and developer (professionally as well as FOSS), having to pay money for a Microsoft operating system I don't use is rather offensive. Microsoft has run a long term campaign to thwart the progress of Linux and my money is being used to fund that campaign. For me, this situation in unconscionable. </p> <p> Apart from the not being able to buy machines without a Microsoft operating system, my experience with Dell has been very good. If I was able to buy a machine from Dell without a Microsoft operating system, buying from Dell would be a no-brainer. </p> <p> So Dell, here is my challenge for you. Offer me a good looking, high end laptop with lots of juicy options like the XPS with either Linux pre-installed or with no operating system and make sure that these options are reflected in the pricing (ie the Linux or no-OS options should be cheaper than the windows option). If you can do that you are almost guaranteed of keeping me as a customer. If not, I will be looking elsewhere for a company that can meet my requirements. If Dell loses this sale, it will likely lose other sales from my immediate family, friends and employers. </p> <p> Dell, the ball is in your court. Thanks for listening. </p> <p> <b>Update : 16:47</b> </p> <p> Someone posted this to <a href="http://reddit.com/r/programming/info/69p2l/comments/"> programming.reddit.com</a> and it seems that if you are in the US at least, you can indeed get a <a href="http://www.dell.com/content/topics/segtopic.aspx/linux_3x?c=us&amp;cs=19&amp;l=en&amp;s=dhs"> Dell XPS with Ubuntu pre-installed</a>. One good thing about the machine offered is that its got an Intel video chipset which I prefer to the Nvidia one. On the downside, its missing some of the options that I can get in the windows version like the solid state disk. </p> <p> I've just looked again at the Dell Austalia website and I simply cannot find a page where I can get a similar machine. So Dell Australia or Asia/Pacific, what about making Ubuntu an option for the Dell XPS available here in Australia? </p> Designing Library APIs. http://www.mega-nerd.com/erikd/Blog/2008/02/05/designing_lib_apis 2008-02-05T10:40:00Z 2008-02-05T10:40:00Z <p> At <a href="http://lca2008.linux.org.au/"> Linux.conf.au 2008</a> I gave a presentation titled <i>"Designing Library APIs: How to Make Users Love Your Library"</i>. The presentation went reasonably well and the marvelous people on the LCA08 audio visual team have already posted the <a href="http://mirror.linux.org.au/pub/linux.conf.au/2008/Fri/mel8-137.ogg"> Ogg Video</a>. </p> <p> During the presentation I mentioned a couple of other resources but due to time limitations I never managed to get to that slide. Those resources are: </p> <ul> <li>Rusty Russell's <a href="http://sourcefrog.net/weblog/software/aesthetics/interface-levels.html"> Interface Simplicity Spectrum</a>. </li> <li>Joshua Bloch's <i>"How to Design a Good API and Why it Matters"</i> which is available as a <a href="http://video.google.com/videoplay?docid=-3733345136856180693"> Google video</a> and a <a href="http://lcsd05.cs.tamu.edu/slides/keynote.pdf"> set of slides</a>. Although this presentation is geared towards Java, don't dismiss it because the principles are general enough to apply to all languages. </li> </ul> <p> Finally, Brad Hards, one of the attendees at my presentation pointed me to this paper, <a href="http://doc.trolltech.com/qq/qq13-apis.html"> Designing Qt-Style C++ APIs</a> which seems to be pretty good. </p> Microsoft and the POSIX Standard. http://www.mega-nerd.com/erikd/Blog/2008/01/11/posix 2008-01-11T12:21:00Z 2008-01-11T12:21:00Z <p> POSIX defines a set of functions for retrieving information, such as file size, creation date, last modification date, ownership etc about files on disk. Two commonly used functions in this set are defined as follows: </p> <pre class="code"> int stat (const char *path, struct stat *buf) ; int fstat (int filedes, struct stat *buf) ; </pre> <p> The first operates on the filename, and the second operates on a pre-existing file descriptor. Both functions interrogate the file system and fill in a bunch of fields of a struct supplied by the caller. </p> <p> Microsoft also provides these functions in its C run time library and even documents them <a href="http://msdn2.microsoft.com/en-us/library/221w8e43(VS.71).aspx">here</a> and <a href="http://msdn2.microsoft.com/en-us/library/14h5k7ff(VS.71).aspx">here</a>. However, <b>stat</b>, the function that is probably more commonly used than <b>fstat</b>, does not always work as expected. In particular, the following scenario: </p> <ul> <li>Create a new file using the <b>open</b> function.</li> <li>Write N bytes to the file descriptor returned by <b>open</b>.</li> <li>Call <b>stat</b> on the file using the file name used during file creation.</li> <li>Call <b>fstat</b> on the file descriptor returned by <b>open</b>.</li> </ul> <p> On any sane system, the file length field of the struct filled in by <b>stat</b> and <b>fstat</b> calls in the above scenario should be the same. On windows however, they are not, <b>fstat</b> returns the correct value N, while <b>stat</b> returns zero. </p> <p> With the help from a kind volunteer on the libsndfile-devel mailing list this broken-ness was confirmed on Win2k, WinXP and Vista. </p> <p> Microsoft; bringing new and improved versions of the <a href="http://en.wikipedia.org/wiki/DeathStation_9000">DeathStation 9000</a> to the desk and laptop bag of every programmer. From that wiki page: </p> <blockquote><i> DeathStation 9000 (often abbreviated DS9K) is a fictional computer architecture often used as part of a discussion about the portability of computer code (often C code). It is imagined to be as obstructive and unhelpful as possible, whilst still conforming to any relevant standards, deliberately acting unexpectedly whenever possible. </i></blockquote> Caught up with Anand and Bruce. http://www.mega-nerd.com/erikd/Blog/2007/12/17/anand_and_bruce 2007-12-17T11:02:00Z 2007-12-17T11:02:00Z <p> I was in London recently and managed to catch up with Anand Kumria and Bruce Badger for a pint and a curry in the Barbican area. I was rather jet-lagged but it was great to catch up with the guys and say hi! </p> <p> The colors in this picture are a bit weird. Blame Bruce's camera <img src="http://www.mega-nerd.com/erikd/Img/smile.png" border="0" style="vertical-align:middle" alt=":-)"/>. </p> <center> <img src="http://www.mega-nerd.com/erikd/Img/anand_bruce.png" alt="Anand, Bruce and me" /> </center> Microsoft and the 1999 ISO C Standard. http://www.mega-nerd.com/erikd/Blog/2007/12/04/ms_c99 2007-12-04T10:54:00Z 2007-12-04T10:54:00Z <p> Any programmer with a better than passing familiarity with the C programming language would know that the current standard for C is the <a href="http://www.open-std.org/jtc1/sc22/wg14/www/standards.html"> ISO/IEC 9899:1999</a> standard published in 1999. If they program on Linux systems or use the <a href="http://gcc.gnu.org/"> GNU C compiler</a> they might also know that while GCC is not fully C99 compliant, it does however <a href="http://gcc.gnu.org/c99status.html"> come pretty damn close</a>. In particular, many of the things listed as broken on the status page are mostly working (Complex, math.h, stdint.h, inline etc) and the things listed as missing are missing because there is probably not a huge demand for them. </p> <p> In the world of Microsoft however, Visual Studio still has no serious attempt at support for C99 even eight years after the standard was released. Microsoft claims conformance with the 1989 C standard but seems to show little to no interest in even attempting to pursue conformance with the later standard. </p> <p> In particular, the Microsoft compiler has the following C99 issues: </p> <ul> <li><b>snprintf</b>. Microsoft does have a function <b>_snprintf</b>, but its behavior does not comply with the requirements of C99.</li> <li>C99 maths functions. The new standard introduced a bunch of new functions including the <a href="https://pod-202.dolphin-server.co.uk/cgi-bin/man/man2html?lrint+3"> <b>lrint</b></a> family of functions.</li> <li>A couple of new header files including <b>&lt;stdint.h&gt;</b>. See <a href="http://msinttypes.googlecode.com/svn/trunk/stdint.h">here</a> for a fix. </li> <li>The <b>inline</b> keyword. Microsoft of course has a non standard <b>__inline</b> keyword instead.</li> <li>Pre-defined macros like <b>__func__</b>. Again Microsoft has a non- standard <b>__FUNCTION__</b> macro.</li> <li>Variable length arrays.</li> <li>Variadic macros.</li> <li>The <b>long long</b> type.</li> </ul> <p> These short comings of the Microsoft compiler can make compiling Free Software written in standards conforming C rather difficult with that compiler. </p> <p> The one that hits me worst with respect to compiling <a href="http://www.mega-nerd.com/libsndfile/">libsndfile</a> and <a href="http://www.mega-nerd.com/SRC/">libsamplerate</a> on windows is the lack of C99 maths functions, in particular <b>lrint</b> and <b>lrintf</b>. My current solution for these functions are inline assembler functions. However, Microsoft has just recently joined the world of 64 bit operating systems and their compiler for that platform supports neither the <b>lrint</b> family of functions nor inline assembler functions. <b>WTF?</b> </p> <p> However, just today, I found out that the GNU GCC, GNU Binutils and MinGW teams have just released <a href="https://sourceforge.net/projects/mingw-w64/"> MinGW for windows 64</a>. That should make things a little easier. </p> Ocaml Snippet : Sqlite3. http://www.mega-nerd.com/erikd/Blog/2007/11/24/snip_sqlite 2007-11-24T03:20:00Z 2007-11-24T03:20:00Z <p> One of the really nice things about using <a href="http://pkg-ocaml-maint.alioth.debian.org/"> Ocaml on Debian</a> and Ubuntu is the large number of really well packaged third party libraries. </p> <p> Most of these libraries are also well documented from doc strings extracted from the source code files using <a href="http://caml.inria.fr/pub/docs/manual-ocaml/manual029.html"> ocamldoc</a>. However, the documentation for most ocaml libraries is purely reference documentation and its not always obvious how to use the library simply from reading the reference docs. What's really needed is example code to be read in conjunction with the reference docs. </p> <p> I'm working on a program where I needed a small, fast easy to administer database. With those requitements, <a href="http://www.sqlite.org/">Sqlite</a> is really hard to beat and best of all, someone has already written <a href="http://ocaml.info/home/ocaml_sources.html"> Ocaml bindings</a>. On Debian or Ubuntu, the Ocaml Sqlite bindings can be installed using: </p> <pre class="code"> sudo apt-get install libsqlite3-ocaml-dev </pre> <p> In order to get a feel for using it and take my first steps into the world of SQL (which I'd had very minimal exposure to before now), I wrote a small program to test out the features provided by the library. </p> <p> The following stand alone program should be taken as an example of how to access a Sqlite database from Ocaml. Since I am not an SQL expert, the actual SQL usage should be taken with a grain of salt. </p> <pre class="code"> exception E of string let create_tables db = (* Create two tables in the database. *) let tables = [ "people", "pkey INTEGER PRIMARY KEY, first TEXT, last TEXT, age INTEGER" ; "cars", "pkey INTEGER PRIMARY KEY, make TEXT, model TEXT" ; ] in let make_table (name, layout) = let stmt = Printf.sprintf "CREATE TABLE %s (%s);" name layout in match Sqlite3.exec db stmt with | Sqlite3.Rc.OK -> Printf.printf "Table '%s' created.\n" name | x -> raise (E (Sqlite3.Rc.to_string x)) in List.iter make_table tables let insert_data db = (* Insert data in both the tables. *) let people_data = [ "John", "Smith", 23; "Helen", "Jones", 29 ; "Adam", "Von Schmitt", 32 ; ] in let car_data = [ "bugatti", "veyron" ; "porsche", "911" ; ] in let insert_people (first, last, age) = (* Use NULL for primary key and Sqlite will generate a unique key. *) let stmt = Printf.sprintf "INSERT INTO people values (NULL, '%s', '%s', %d);" first last age in match Sqlite3.exec db stmt with | Sqlite3.Rc.OK -> () | x -> raise (E (Sqlite3.Rc.to_string x)) in let insert_car (make, model) = let stmt = Printf.sprintf "INSERT INTO cars values (NULL, '%s', '%s');" make model in match Sqlite3.exec db stmt with | Sqlite3.Rc.OK -> () | x -> raise (E (Sqlite3.Rc.to_string x)) in List.iter insert_people people_data ; List.iter insert_car car_data ; print_endline "Data inserted." let list_tables db = (* List the table names of the given database. *) let lister row headers = Printf.printf " %s : '%s'\n" headers.(0) row.(0) in print_endline "Tables :" ; let code = Sqlite3.exec_not_null db ~cb:lister "SELECT name FROM sqlite_master;" in ( match code with | Sqlite3.Rc.OK -> () | x -> raise (E (Sqlite3.Rc.to_string x)) ) ; print_endline "------------------------------------------------" let search_callback db = (* Perform a simple search using a callback. *) let print_headers = ref true in let lister row headers = if !print_headers then ( Array.iter (fun s -> Printf.printf " %-12s" s) headers ; print_newline () ; print_headers := false ) ; Array.iter (Printf.printf " %-12s") row ; print_newline () in print_endline "People under 30 years of age :" ; let code = Sqlite3.exec_not_null db ~cb:lister "SELECT * FROM people WHERE age &lt; 30;" in match code with | Sqlite3.Rc.OK -> () | x -> raise (E (Sqlite3.Rc.to_string x)) let search_iterator db = (* Perform a simple search. *) let str_of_rc rc = match rc with | Sqlite3.Data.NONE -> "none" | Sqlite3.Data.NULL -> "null" | Sqlite3.Data.INT i -> Int64.to_string i | Sqlite3.Data.FLOAT f -> string_of_float f | Sqlite3.Data.TEXT s -> s | Sqlite3.Data.BLOB _ -> "blob" in let dump_output s = Printf.printf " Row Col ColName Type Value\n%!" ; let row = ref 0 in while Sqlite3.step s = Sqlite3.Rc.ROW do for col = 0 to Sqlite3.data_count s - 1 do let type_name = Sqlite3.column_decltype s col in let val_str = str_of_rc (Sqlite3.column s col) in let col_name = Sqlite3.column_name s col in Printf.printf " %2d %4d %-10s %-8s %s\n%!" !row col col_name type_name val_str ; done ; row := succ !row ; done in print_endline "People over 25 years of age :" ; let stmt = Sqlite3.prepare db "SELECT * FROM people WHERE age > 25;" in dump_output stmt ; match Sqlite3.finalize stmt with | Sqlite3.Rc.OK -> () | x -> raise (E (Sqlite3.Rc.to_string x)) let update db = print_endline "Helen Jones has just turned 30, so update table." ; print_endline "Should now only be one person under 30." ; let stmt = "UPDATE people SET age = 30 WHERE " ^ "first = 'Helen' AND last = 'Jones';" in ( match Sqlite3.exec db stmt with | Sqlite3.Rc.OK -> () | x -> raise (E (Sqlite3.Rc.to_string x)) ) ; search_callback db let delete_from db = print_endline "Bugattis are too expensive, so drop that entry." ; let stmt = "DELETE FROM cars WHERE make = 'bugatti';" in match Sqlite3.exec db stmt with | Sqlite3.Rc.OK -> () | x -> raise (E (Sqlite3.Rc.to_string x)) let play_with_database db = print_endline "" ; create_tables db ; print_endline "------------------------------------------------" ; list_tables db ; insert_data db ; print_endline "------------------------------------------------" ; search_callback db ; print_endline "------------------------------------------------" ; search_iterator db ; print_endline "------------------------------------------------" ; update db ; print_endline "------------------------------------------------" ; delete_from db ; print_endline "------------------------------------------------" (* Program main. *) let () = (* The database is called test.db. Delete it if it already exists. *) let db_filename = "test.db" in ( try Unix.unlink db_filename with _ -> () ) ; (* Create a new database. *) let db = Sqlite3.db_open db_filename in play_with_database db ; (* Close database when done. *) if Sqlite3.db_close db then print_endline "All done.\n" else print_endline "Cannot close database.\n" </pre> <p> The above code can be run as a script using: </p> <pre class="code"> ocaml -I +sqlite3 sqlite3.cma unix.cma sqlite_test.ml </pre> <p> or compiled to a native binary using: </p> <pre class="code"> ocamlopt -I +sqlite3 sqlite3.cmxa unix.cmxa sqlite_test.ml -o sqlite_test </pre> <p> When run, the output should look like this: </p> <pre class="code"> Table 'people' created. Table 'cars' created. ------------------------------------------------ Tables : name : 'people' name : 'cars' ------------------------------------------------ Data inserted. ------------------------------------------------ People under 30 years of age : pkey first last age 1 John Smith 23 2 Helen Jones 29 ------------------------------------------------ People over 25 years of age : Row Col ColName Type Value 0 0 pkey INTEGER 2 0 1 first TEXT Helen 0 2 last TEXT Jones 0 3 age INTEGER 29 1 0 pkey INTEGER 3 1 1 first TEXT Adam 1 2 last TEXT Von Schmitt 1 3 age INTEGER 32 ------------------------------------------------ Helen Jones has just turned 30, so update table. Should now only be one person under 30. People under 30 years of age : pkey first last age 1 John Smith 23 ------------------------------------------------ Bugattis are too expensive, so drop that entry. ------------------------------------------------ All done. </pre> GNU gcc and -Wmissing-prototypes. http://www.mega-nerd.com/erikd/Blog/2007/09/13/gcc_missing_prototypes 2007-09-12T21:03:00Z 2007-09-12T21:03:00Z <p> Many people who code in C consider warning messages optional or if they do enable warnings, use gcc's <b>-Wall</b> warning flag and leave it at that. However, there are a number of problems that gcc can warn about but doesn't unless it is specifically told to do so. </p> <p> For example, consider a rather trivial example consisting of a main program file (main.c) like this: </p> <pre class="code"> #include &lt;stdio.h&gt; #include "other.h" int main (void) { printf ("two cubed : %f\n", int_power (2.0, 3)) ; return 0 ; } </pre> a second C file (other.c) like this: <pre class="code"> double int_power (int pow, double value) { double output = value ; for ( ; pow &gt; 1 ; pow --) output *= value ; return output ; } </pre> and the header file for the above C file (other.h) like this: <pre class="code"> double int_power (double value, int pow) ; </pre> <p> Simple. </p> <p> Compiling this code at the command line can be done like this: </p> <pre class="code"> gcc -Wall -Wextra main.c other.c -o program </pre> <p> which gives no warnings. However, when the resulting executable is run, it gives an obviously wrong result: </p> <pre class="code"> two cubed : 0.000000 </pre> <p> What the ..... ? </p> <p> Looking at the code to this rather trivial example, its pretty easy to figure out that the error is caused by the main program and the implementation of the function <b>int_power</b> disagreeing on the order of the two parameters. </p> <p> In a more complicated real world situation, this can lead to seriously difficult to debug problems. The solution of course is to add the <b>-Wmissing-prototype</b> flag to the gcc command line: </p> <pre class="code"> gcc -Wall -Wextra -Wmissing-prototypes main.c other.c -o program </pre> <p> Now the compiler gives us a warning message: </p> <pre class="code"> other.c:3: warning: no previous prototype for 'int_power' </pre> <p> To get rid of this warning, the file other.c should include other.h. When we do that, we get a compile error telling us that there is a conflict between the function implementation in other.c and the function prototype in other.h: </p> <pre class="code"> other.c:6: error: conflicting types for 'int_power' other.h:1: error: previous declaration of 'int_power' was here </pre> <p> The fix of course is to make the implementation of <b>int_power</b> in other.c match the function prototype. Once that is done, the program compiles and even gives the correct result. </p> <p> But we're not quite done yet. The behavior of the original broken code is slightly different when compiled with a C++ compiler. Compiling with g++: </p> <pre class="code"> g++ -Wall -Wextra main.c other.c -o program </pre> <p> results in an error message: </p> <pre class="code"> /tmp/cccTLc2H.o: In function `main': main.c:(.text+0x23): undefined reference to `int_power(double, int)' collect2: ld returned 1 exit status </pre> <p> So how does the C++ compiler know that something is wrong here when the C compiler didn't? </p> <p> The most important thing to notice is that the error is produced by the linker. Secondly, one needs to remember that C++ (unlike C) allows function name overloading; that is, two (or more) functions can have the same name as long as they all have a unique (ordered) set of function argument types. </p> <p> In the case above, the C++ linker (which may be the same as the C linker but behaves differently when linking C++ object files) knows the function called from main.c takes two parameters, a <b>double</b> followed by an <b>int</b>. However, the file other.c has a function of the same name, but with the order of the parameters reversed and hence can't be used. Since there is no other function of that name the linker gives an error. </p> <p> Interestingly, the C++ compiler does not accept the <b>-Wmissing-prototypes</b> warning flag. Personally, I think it should, because obvious warnings from the parser stage of the compiler are an order of magnitude better than obscure error messages from the linker. </p> <p> Finally, some C++ fan-boys might give this as an example of why C++ is a safer language than C. The question I would ask of those people is, <i>"if you are so concerned with programming safety, why are you using C++ instead of <a href="http://www.ocaml.org">Ocaml</a> or <a href="http://www.haskell.org">Haskell</a>?"</i>. I would also suggest that using a good C compiler like <a href="http://gcc.gnu.org/">GNU gcc</a> with every warning message you can find turned on is just as safe as running the same code through a C++ compiler. </p> Reading List. http://www.mega-nerd.com/erikd/Blog/2007/08/25/reading_list 2007-08-25T01:19:00Z 2007-08-25T01:19:00Z <p> I've just received a bundle of books from Amazon. This is going to keep me busy for some time. </p> <center> <img src="http://www.mega-nerd.com/erikd/Img/reading_list.png" border="0" alt="[Stack of scary books]"/> </center> A Simple Introduction to Parsing with Flex and Bison. http://www.mega-nerd.com/erikd/Blog/2007/07/30/flex_bison 2007-07-30T10:35:00Z 2007-07-30T10:35:00Z <p> On Friday night I gave a presentation at <a href="http://www.slug.org.au"> SLUG</a> with title above. Unfortunately the SLUG video recording people weren't there on the night so no video was captured. I am however making the slides and code <a href="http://www.mega-nerd.com/tmp/flex_bison-latest.tgz"> available for download here</a>. The code examples demonstrate a simple email date header parser written in both C and Ocaml. The C code is in five different stages so people can see how the parser was developed. </p> <p> If anyone has any questions about the code, or more generally with the techniques of parsing, I'd be happy to discuss them on the <a href="http://lists.slug.org.au/listinfo/coders"> SLUG coders mailing list</a>. </p> Telstra NextG and Sierra Wireless Aircard 875. http://www.mega-nerd.com/erikd/Blog/2007/05/17/telstra_nextg 2007-05-17T10:27:00Z 2007-05-17T10:27:00Z <p> At my day job we needed to connect our Ubuntu based appliance to Telstra's NextG broadband wireless network using the Telstra supplied Sierra Wireless Aircard 875. These things are PCMCIA cards but internally, they present as a USB serial device. According to <b>lsusb</b>: </p> <pre class="code"> Bus 006 Device 002: ID 1199:6820 Sierra Wireless, Inc. </pre> <p> Kernels after about 2.6.20 will recognize this device and automatically load an appropriate driver and set up three /dev/ttyUSB* devices. For kernels that don't recognize the card do: </p> <pre class="code"> modeprobe usbserial vendor=0x1199 product=0x6820 </pre> <p> Once the /dev/ttyUSB* devices are set up correctly create the files <b>/etc/ppp/peers/telstra_aircard_875</b> and <b>/etc/ppp/peers/telstra_aircard_875_chat</b>. </p> <p> New file : /etc/ppp/peers/telstra_aircard_875. The username and password below need to be there but can be anything, including the exact same ones as I have here. </p> <pre class="code"> lock crtscts modem hide-password noauth /dev/ttyUSB0 460800 defaultroute replacedefaultroute noipdefault usepeerdns noipdefault user anyname password anypassword noauth connect '/usr/sbin/chat -v -f /etc/ppp/peers/telstra_aircard_875_chat' </pre> <p> New file : /etc/ppp/peers/telstra_aircard_875_chat The <b>XXXX</b> below should be replaced with the Telstra supplied PIN number that should be unique for each card. </p> <pre class="code"> ABORT BUSY ABORT 'NO CARRIER' ABORT VOICE ABORT 'NO DIALTONE' ABORT 'NO DIAL TONE' ABORT 'NO ANSWER' ABORT DELAYED ABORT ERROR TIMEOUT 20 "" "AT+CPIN?" READY-AT+CPIN=<b>XXXX</b>-OK "AT&amp;F" OK "ATE1" TIMEOUT 20 OK "ATDT*99***1#" CONNECT \d </pre> <p> Finally, edit the file <b>/etc/ppp/pap-secrets</b>, and add the following entry. Again, this needs to be there, but will be the same for everyone. </p> <pre class="code"> # For telstra_aircard_875 user@telstra.internet * telstra </pre> <p> Getting connected should now be as easy as: </p> <pre class="code"> pppd call telstra_aircard_875 </pre> <p> One of the slightly odd things about this card and Telstra's NextG network is that if the four digit PIN number isn't sent during the CHAT session the CHAT session will connect and pppd will try to negotiate an IP address but get no response, eventually timing out. Anyone who experiences this problem should first make sure that the service has been activated from the Telstra end (possibly by making sure it works under windoze) and then turn on debugging for both chat and pppd. </p> Horses For Courses. http://www.mega-nerd.com/erikd/Blog/2007/04/08/horses_for_courses 2007-04-08T10:16:00Z 2007-04-08T10:16:00Z <img src="http://www.mega-nerd.com/erikd/Img/Delta_Blues_20051225P1.png" border="0" alt="A race horse" /> <!-- Image pinched from: http://en.wikinews.org/wiki/Image:Delta_Blues_20051225P1.jpg --> <br/> <p> In my day job, I work with a hardware engineer named Joe. A couple of months ago, he had to do some C coding to talk to a serial port and came to me for pointers. He was basically on the right track, but was using too many global variables, not checking the return values of system calls etc. These weren't horrible problems, but I explained why practices like this can lead to problems later, showed him better solutions to the same problems and introduced him to the gcc warning flags and <a href="http://www.valgrind.org/"> Valgrind</a>. He was very grateful for my help and was a quick to pick up all the tips I'd given him. </p> <p> More recently Joe came to me with another programming problem; he had to parse some numerical data out of a plain text log file. He already had about 60 lines of C code that opened a file based on a hard coded filename and he was starting to fiddle around with the <b>fgetc</b> function but was a little stuck on how to go further. </p> <p> I had a look at his code and since Joe's a nice Irish chap, his predicament brought to mind a joke I once heard: </p> <blockquote> It is said that there was once an English motorist in Ireland who stopped his car to ask the way to Kilkenny. "Sure and to goodness," replied the Irishman., "If I wanted to go to Kilkenny, I wouldn't be starting from here." </blockquote> <p> The problem is that C is not a very good language for parsing text data. I told Joe that writing his log file parser in C could certainly be done, but that it would be painful, time consuming and error prone in comparison to other programming languages. So I told him that for this particular task Python would be a much better fit and asked him if he'd like me to teach him the basics of Python. Joe's no dummy; he agreed without hesitation. </p> <p> First up I showed him <a href="http://docs.python.org/tut/tut.html"> the Python Tutorial</a>, <a href="http://docs.python.org/modindex.html"> the Python Module index</a> and how to used <a href="http://groups.google.com/advanced_search?q=&amp;"> Google Groups advanced search</a> to find Python specific answers from the comp.lang.python Usenet group. I then showed him the basic hello world program in Python: </p> <pre class="code"> #!/usr/bin/python print "Hello world!" </pre> <p> Over the next hour we built up a good portion of his program. We used used the <a href="http://docs.python.org/lib/module-sys.html"> sys module</a> to get the file name of the log file to parse, the built in Python file handling functions and the <a href="http://docs.python.org/lib/module-re.html"> regular expression module</a>. We even used a <a href="http://www.python.org/doc/2.4/tut/node7.html#SECTION007140000000000000000"> list comprehension</a> to remove outliers from his data set. </p> <p> In the end we had about 30 lines of Python code that was very much closer to Joe's end goal than his original 60 lines of C code. Joe was really, <i>really</i> impressed with how easy Python was in comparison to C. It was at this point that I warned Joe about <a href="http://c2.com/cgi/wiki?BlubParadox"> the Blub Paradox</a>. He was well aware that when he only knew C, C was his first choice for this programming task. However, now that he knows Python as well, he'll be able to pick between C and Python depending on the task. I also told him that many Python programmers see Python as the ultimate programming language and are really Blub programmers even if they don't know it. </p> <p> In my own programming I'm currently using: </p> <ul> <li><b>C</b> : The first language I really became proficient in. Its great for low level hacking, good for libraries and wherever speed of execution is an important aspect. I still love C even if it does seem rather archaic in comparison to the others. </li> <li><b>C++</b> : I'm not real keen on C++ even though I do use it for most of the coding I do at my day job. As a language its incredibly complex and unforgiving. There are a million ways to shoot yourself in the foot and no one lives long enough to experience them all. Its a language that can harbor the most obscure bugs that can be exceedingly difficult to track down. This is a language that as far as I am concerned is long past its use by date. </li> <li><b>Python</b> : Python is a great language for teaching and a great language for small scripts. Some people also use it for bigger projects like <a href="http://bazaar-vcs.org/"> Bzr</a> but for me personally, I find its dynamic type system <a href="http://www.mega-nerd.com/erikd/Blog/CodeHacking/dynamic_typing.html"> too problematic</a> for use on larger projects. </li> <li><b>Ocaml</b> : This is my current favorite for general purpose programming. It can be run as a script just like Python, but can also be compiled to a really fast native binary. It uses strict static type checking to find coding errors at compile time and run time array bounds checking. It has <a href="http://www.mega-nerd.com/erikd/Blog/CodeHacking/Ocaml/variant_types.html"> variant types and pattern matching</a> which are powerful constructs that programmers who have only used mainstream languages like C, C++, Java, Perl, Python etc could only dream about. </li> <li><b>Erlang</b> : Lately I've been learning Erlang, both for a project of my own and for a project at work. I'm learning it because it does parallel, concurrent and distributed programming better than any of the above, probably better than any other language in existence. Its also been pretty easy to pick up because, like Ocaml, its a <a href="http://en.wikipedia.org/wiki/Functional_programming"> functional programming language</a>. Like Python, it uses dynamic type checking, but also has quite a lot of static checking in the compiler. </li> </ul> <p> So, with the above languages at my disposal, I can match a programming language to the task at hand. For numerical and mathematical programming I use Ocaml, for low level programming I use C and from now on, for multi-threaded and concurrent programming I will chose Erlang. </p> <p> More importantly, correctly matching the language to the problem should make the task of developing a solution to the problem far easier than using an inappropriate language. </p> Learning Erlang. http://www.mega-nerd.com/erikd/Blog/2007/04/04/learning_erlang 2007-04-04T13:07:00Z 2007-04-04T13:07:00Z <p> The decision has been made, I'm going to learn the <a href="http://erlang.org/"> Erlang programming language</a>. The main reason for this decision is that Erlang does one thing better than any other programming language I am aware of; parallel, concurrent and distributed processing. </p> <p> The big problem with parallel and concurrent processing in other languages is that the standard method of communication between threads in most languages is shared data protected by mutexes or semaphores which are difficult to get right when there are a lot of threads or a lot of data to be protected. The <a href="http://www.mega-nerd.com/erikd/Blog/CodeHacking/tridge_was_right.html"> standard solution</a> to the problems of dealing with parallelism simply doesn't scale well. </p> <p> Erlang excels at parallel processing because it forgoes the use of semaphores, mutexes and other synchronisation primitives. It replaces these shared data synchronisation methods with message passing; a much simpler mechanism which is much easier to reason about and much harder, maybe even impossible, to get wrong. </p> <p> When learning, a new language, my usual approach is to write lots of small demo programs, with each one demonstrating a different feature. These programs come in really useful later as an easy to reference catalogue of language features. </p> <p> Here's my first complete Erlang program, which takes any number of integer parameters on the command line and prints the factorial of each one: </p> <pre class="code"> #!/usr/bin/env escript -export ([main/1]). % Naive factorial function. fac (0) -> 1 ; fac (N) when N > 0 -> N * fac (N - 1). % Function to print the factorial of each list element. print_fact_list ([]) -> ok ; print_fact_list ([Head | Tail]) -> % Convert the Head from a string to an int. Int = list_to_integer (Head), % Calculate the factorial. Fact = fac (Int), % Print the result. io:format ("fac ~w : ~w~n", [Int, Fact]), % Call the function recursively with the tail of the list. print_fact_list (Tail). % Main function, accepts a list of strings contain argv [1], argv [2] etc. main (List) -> case length (List) of 0 -> io:format ("Usage : factorial.erl &lt;number&gt;\n") ; _ -> print_fact_list (List) end. </pre> <p> To me, this Erlang code looks a little like Ocaml and a little like Prolog which I used briefly at university over a decade ago. A couple of things to note: </p> <ul> <li>Comments begin with the percent character. </li> <li>All variable names have a leading upper case letter. </li> <li>Functions can be defined multiple times and pattern matching is used to decide which function variant is called. </li> <li>Strings are stored as lists of characters and converted to integers using the <b>list_to_integer</b> function. </li> </ul> <p> To run this program requires Erlang, which on Debian and Ubuntu means the packages erlang, erlang-base, erlang-dev and erlang-manpages. It also uses <b>escript</b>, which comes standard with Erlang R11b4 and can be <a href="http://yhafri.club.fr/crux/index.html"> obtained here</a> for earlier versions (Ubuntu Feisty has R11b2). Escript allows Erlang code to be run as a script, just like Python or Ruby. </p> <p> The output of this program when passed the numbers <b>10</b>, <b>20</b> and <b>30</b> results in the following output: </p> <pre class="code"> fac 10 : 3628800 fac 20 : 2432902008176640000 fac 30 : 265252859812191058636308480000000 </pre> <p> Yep, Erlang uses arbitrary precision integers by default. Thats pretty cool. </p> Tridge Was Right. http://www.mega-nerd.com/erikd/Blog/2007/03/28/tridge_was_right 2007-03-28T12:36:00Z 2007-03-28T12:36:00Z <p> At <a href="http://www.linux.org.au/conf/2005/"> Linux.conf.au 2005</a>, Tridge gave a keynote talk about some of the issues the Samba team had run into when designing Samba4. While discussing the problems of writing a complex server which has to serve multiple simultaneous requests he put up a series of three slides. The first said: </p> <center><b> <br/> Threads suck! <br/><br/> </b></center> <p> Having used OS level threads in the past, I was in complete agreement with this. The problems of sharing data across threads and locking/unlocking of that data to make sure the accesses are safe is simply too difficult for mere mortals to get right in anything other than trivial cases. </p> <p> Tridges' second slide said: </p> <center><b> <br/> Processes suck! <br/><br/> </b></center> <p> Splitting multi threaded code into multiple processes fixes the locking problems by removing the ability of the processes to share data (ignoring IPC shared memory of course). Obviously for a server program like Samba, this is not a solution. </p> <p> The third slide in the series said: </p> <center><b> <br/> State machines suck! <br/><br/> </b></center> <p> At the time of Tridge's keynote, I didn't really appreciate what he was saying. </p> <p> The idea is really quite simple; everything is done in a single process so no locking is required. All I/O is multiplexed using the Unix <b>select</b> system call and a state machine keeps track of state of all of the I/O channels. </p> <p> The problem with this is that any blocking I/O operation must be replaced with a non-blocking operation. Failure to do this will mean that a single I/O call that blocks will prevent the servicing of all other I/O operations until the blocked operation decides to complete and return control to the state machine. </p> <p> However, the state machine model does work relatively well for simple examples. Unfortunately, non-blocking I/O leads to a second problem; writing code to do non-blocking I/O is significantly more difficult than for regular blocking I/O. </p> <p> In my day job I've been working on some C++ classes which talk to a web server using HTTP POST operations over a keep-alive connection. This code had a couple of requirements: </p> <ul> <li>Must be non-blocking to fit in with the rest of the code. </li> <li>Must be capable of HTTPS connections using OpenSSL (which is a particularly nasty to get working in non-blocking mode).</li> <li>Must be able to connect via a HTTP proxy in both HTTP and HTTPS modes. </li> <li>Must be able to detect a connection that gets broken and gracefully re-establish it.</li> </ul> <p> I now have code that fits these requirements and a pretty comprehensive test suite. With this experience behind me I have to say that getting this working was a royal pain in the neck. I also agree with Tridge; state machines suck almost as much as threads. </p> <p> Maybe its time for me to learn <a href="http://www.erlang.org/"> Erlang</a>. </p> Lazy Lists. http://www.mega-nerd.com/erikd/Blog/2007/03/22/lazy_lists 2007-03-22T10:43:00Z 2007-03-22T10:43:00Z <p> Lazy evaluation is a default feature of the <a href="http://www.haskell.org/"> Haskell</a> programming language and an optional feature of <a href="http://caml.inria.fr/"> Ocaml</a>. Most programming languages (Ocaml, C, C++, Perl, Python, Java etc) use eager evaluation; where a result specified by a line of code is calculated as soon as the program gets to that line. Lazy evaluation on the other hand, defers the calculation of a result until that result is needed. </p> <p> The real beauty of lazy evaluation is that a result that is never used is never evaluated. Lazy evaluation also allows the specification of lists which are effectively infinite, as long as the programmer doesn't actually try to access every element in the list. Obviously, attempting to do so would take infinite time and and require infinite memory to actually hold the list <img src="http://www.mega-nerd.com/erikd/Img/smile.png" border="0" style="vertical-align:middle" alt=":-)" />. </p> <p> While searching for information on Ocaml's lazy programming features I came across a post at the <a href="http://enfranchisedmind.com/blog/archive/2007/01/01/177"> enchanted mind blog</a>. That post is ok, but the code is just snippets and when put together as it is, doesn't actually work. </p> <p> After a bit of fiddling around, I managed to get it working. However, once I understood it, I didn't think the example was as good as it could be. Firstly, the input to the lazy list is just a standard finite length Ocaml list, but more importantly it doesn't give any idea of how to do a potentially infinite list which is a much more interesting case. </p> <p> That left the field open for a nice blog post demonstrating lazy lists in Ocaml. Read on. </p> <p> Anybody who has done high school or higher mathematics would probably have come across <a href="http://en.wikipedia.org/wiki/Recurrence_relation"> recurrence relations</a> the most well know of which is the <a href="http://en.wikipedia.org/wiki/Fibonacci_number"> Fibonacci sequence</a>. </p> <p> The Fibonacci sequence is often used as example for teaching the concept of <a href="http://en.wikipedia.org/wiki/Recursion"> recursion</a> in computer science <a href="http://blogs.msdn.com/ericlippert/archive/2004/05/19/how-not-to-teach-recursion.aspx"> (even if some people think there are better examples)</a>. The Fibonacci sequence can be expressed recursively in Ocaml like this: </p> <pre class="code"> let rec fibonacci n = match n with | 1 -> 1 | 2 -> 1 | x -> (fibonacci (n - 1)) + (fibonacci (n - 2)) </pre> <p> If one wanted to generate a list containing say the first 20 Fibonac