<?xml version="1.0" encoding="iso-8859-1" ?>
<rss version="2.0" 
   xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" 
   xmlns:html="http://www.w3.org/1999/html" 
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
   xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
<channel>
   <title>m3ga blog</title>
   <link>http://www.mega-nerd.com/erikd/Blog</link>
   <description>An ocassional rant</description>
   <language>en</language>
   <copyright>Copyright 2006-2010 Erik de Castro Lopo</copyright>
   <ttl>60</ttl>
   <pubDate>Tue, 24 Feb 2009 11:31 GMT</pubDate>
   <managingEditor>erikd@mega-nerd.com</managingEditor>
   <generator>PyBlosxom http://pyblosxom.sourceforge.net/ 1.4.3 01/10/2008</generator>
<item>
   <title>Calling from C into Ocaml.</title>
   <guid isPermaLink="false">CodeHacking/Ocaml/calling_ocaml</guid>
   <link>http://www.mega-nerd.com/erikd/Blog/CodeHacking/Ocaml/calling_ocaml.html</link>
   <description><![CDATA[

<p>
I've got a project where it would be nice to be able to call Ocaml code from a
C program.
Although interfacing Ocaml and C is covered in the
	<a href="http://caml.inria.fr/pub/docs/manual-ocaml/manual032.html">
	official manual</a>
and the
	<a href="http://caml.inria.fr/pub/docs/oreilly-book/">
	O'Reilly Ocaml book</a>,
neither of these sources have a complete example.
</p>

<p>
As a firm believer in the idea that a 100 lines of working code is worth a
thousand lines of explanatory text in a book or on the web, I thought I'd put
together a small but complete example.
First off, here is the Ocaml code (download
	<a href="http://www.mega-nerd.com/erikd/Blog/files/ocaml-called-from-c.ml">
	ocaml-called-from-c.ml</a>):
</p>

<pre class="code">

  let ocaml_puts name =
      Printf.printf "Program name is '%s'.\n" name ;
      (* Must flush stdout before returning to C. *)
      flush stdout

  let ocaml_string_join join arr =
      (* Create and return a string. *)
      String.concat join (Array.to_list arr)

  (* On program initialisation, register functions to be called from C. *)
  let () =
      Callback.register "ocaml_puts" ocaml_puts ;
      Callback.register "ocaml_string_join" ocaml_string_join

</pre>

<p>
There are two functions that will be called from C, <b><tt>ocaml_puts</tt></b>
and <b><tt>ocaml_string_join</tt></b> and both functions must be registered
as callbacks with the Ocaml runtime using <b><tt>Callback.register</tt></b>.
To find the function signatures of these functions we can use the 
<b><tt>ocamlc</tt></b> program:
</p>

<pre class="code">

  <b>prompt &gt;</b> ocamlc -i ocaml-called-from-c.ml
  val ocaml_puts : string -&gt; unit
  val ocaml_string_join : string -&gt; string array -&gt; string

</pre>

<p>
The first function has a single string parameter and returns nothing, while the
second takes two parameters, a string and an array of strings and returns a
string.
</p>

<p>
The C program which calls these two functions looks like this
(download
	<a href="http://www.mega-nerd.com/erikd/Blog/files/c-main-calls-ocaml.c">
	c-main-calls-ocaml.c</a>):
</p>

<pre class="code">

  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  
  #include &lt;caml/alloc.h&gt;
  #include &lt;caml/mlvalues.h&gt;
  #include &lt;caml/memory.h&gt;
  #include &lt;caml/callback.h&gt;
  
  
  static void
  call_ocaml_void (const char * name)
  {   CAMLparam0 () ;
      CAMLlocal1 (ostr) ;
  
      ostr = caml_copy_string (name);
  
      value * func = caml_named_value ("ocaml_puts") ;
  
      if (func == NULL)
          puts ("caml_named_value failed!") ;
      else
          caml_callback (*func, ostr) ;
  
      CAMLreturn0 ;
  } /* call_ocaml_void */
  
  
  static void
  call_ocaml_string (char * join, char const ** argv)
  {   CAMLparam0 () ;
      CAMLlocal3 (ojoin, oargv, ores) ;
  
      ojoin = caml_copy_string (join);
      oargv = caml_alloc_array (caml_copy_string, argv) ;
  
      value * func = caml_named_value ("ocaml_string_join") ;
  
      if (func == NULL)
          puts ("caml_named_value failed!") ;
      else
          ores = caml_callback2 (*func, ojoin, oargv) ;
  
      printf ("Ocaml returned : '%s'\n", String_val (ores)) ;
  
      CAMLreturn0 ;
  } /* call_ocaml_string */
  
  
  int
  main (int argc, char ** argv)
  {   const char * progname ;
      int k, count ;
      
      progname = argv [0] ;
      if (strstr (progname, "./") == progname)
          progname += 2 ;
  
      if (argc &lt; 2)
      {   puts ("Need at least 1 command line argument.") ;
          exit (1) ;
          } ;
  
      count = argc &gt;= 2 ? atoi (argv [1]) : 1 ;
      count = count &lt; 1 ? 1 : count ;
      printf ("Count : %d\n", count) ;

      /* Must call this before calling any Ocaml code. */
      caml_startup (argv) ;
  
      for (k = 0 ; k &lt; count ; k++)
          call_ocaml_void (progname) ;
  
      for (k = 0 ; k &lt; count ; k++)
          call_ocaml_string (" ", (char const **) (argv + 1)) ;
  
      return 0 ;
  } /* main */


</pre>

<p>
The main function is mostly self explanatory; the only thing to note is that
if we want to call any Ocaml code from C, we must call
<b><tt>caml_startup</tt></b> first.
Looking at the functions that call into Ocaml, note that these functions begin
with a call to <b><tt>CAMLparam0</tt></b> and ends with a call to
<b><tt>CAMLreturn0</tt></b>.
These are both macros, the first of which sets up the Ocaml specific stack
requirements and the second of which cleans up after the first.
The '<b><tt>0</tt></b>' at the end of their names indicates that there are zero
Ocaml managed data objects passed into and returned from the C function
respectively.
</p>

<p>
For values to be passed to Ocaml, we use local Ocaml managed variables set up
with <b><tt>CAMLlocal1</tt></b> if we only have one, or
<b><tt>CAMLlocal3</tt></b> if we have 3.
Data can be copied into these local Ocaml variables using the
<b><tt>caml_copy_*</tt></b> and <b><tt>caml_alloc_*</tt></b> families of
functions.
</p>

<p>
The Ocaml functions we want to call can be looked up by name using
<b><tt>caml_named_value</tt></b> and the function actually called  using
<b><tt>caml_callback</tt></b> if we only have one parameter to pass or
<b><tt>caml_callback2</tt></b> for two parameters.
</p>

<p>
For the call to <b><tt>ocaml_string_join</tt></b> which returns a string, we can
extract the return value from the Ocaml wrapper using <b><tt>String_val</tt></b>.
There are also other functions to retrieve other data types, the only real
caveat being that if the type isn't atomic (eg <tt>int</tt> or <tt>double</tt>)
and you want to return it from the C function it will be necessary allocate
memory for it and copy it because the memory area returned from Ocaml will be
invalid after the call to <b><tt>CAMLreturn0</tt></b>.
</p>

<p>
Finally, building this simple example can be done as follows (using version
3.10.2 of the Ocaml compiler):
</p>

<pre class="code">

  ocamlopt -c ocaml-called-from-c.ml -o ocaml-called-from-c.cmx
  ocamlopt -output-obj -o camlcode.o ocaml-called-from-c.cmx 
  gcc -g -Wall -Wextra  -c c-main-calls-ocaml.c -o c-main-calls-ocaml.o
  gcc camlcode.o c-main-calls-ocaml.o -ldl -lm -L /usr/lib/ocaml/3.10.2 \
         -lasmrun -o c-main-calls-ocaml

</pre>

<p>
The first line compiles to Ocaml file into an Ocaml object (*.cmx) using the
native code compiler, the second takes the Ocaml object and all the other
Ocaml objects needed and generates a object file (camlcode.o) that can be
linked to C code.
The last two lines compile the C code into an object  file and then links
all the C objects and required libraries into an executable.
</p>

<pre class="code">

  <b>prompt &gt;</b> ./c-main-calls-ocaml 4 abc wxyz
  Count : 4
  Program name is 'c-main-calls-ocaml'.
  Program name is 'c-main-calls-ocaml'.
  Program name is 'c-main-calls-ocaml'.
  Program name is 'c-main-calls-ocaml'.
  Ocaml returned : '4 abc wxyz'
  Ocaml returned : '4 abc wxyz'
  Ocaml returned : '4 abc wxyz'
  Ocaml returned : '4 abc wxyz'

</pre>

<p>
At this point its probably a good idea to run the program under
	<a href="http://valgrind.org/">
	valgrind</a>
and vary the first parameter to prove to oneself that un-freed memory when the
program terminates is a constant (due to the Ocaml runtime) and does not vary
in  proportion to the number of times the Ocaml code is called (which would
indicate a memory leak in the interface code).
</p>

]]></description>
   <category domain="http://www.mega-nerd.com/erikd/Blog"></category>
   <pubDate>Tue, 24 Feb 2009 11:31 GMT</pubDate>
</item>
<item>
   <title>Ocaml : Null Cursor in lablgtk.</title>
   <guid isPermaLink="false">CodeHacking/Ocaml/gtk_null_cursor</guid>
   <link>http://www.mega-nerd.com/erikd/Blog/CodeHacking/Ocaml/gtk_null_cursor.html</link>
   <description><![CDATA[

<p>
I need to be able to hide the cursor in an Ocaml/lablgtk/Cairo program I'm
writing (a touchscreen calibration utility to go with the
	<a href="http://www.mega-nerd.com/Zytouch/">
	Zytouch driver</a>).
Usually this is done by creating a small 1x1 pixel cursor that is transparent,
but I couldn't find any existing Ocaml/lablgtk code to do it.
With a little help from
	<a href="http://www.math.nagoya-u.ac.jp/~garrigue/">
	Jacques Garrigue</a>
on the
	<a href="http://yquem.inria.fr/cgi-bin/mailman/listinfo/lablgtk">
	lablgtk mailing list</a>
I came up with the following function to create a cursor:
</p>

<pre class="code">

  let create_null_cursor win =
      let w, h = 1, 1 in
      let mask = Gdk.Bitmap.create ~window:win ~width:w ~height:h () in
      let pixmap = Gdk.Pixmap.create ~window:win ~width:w ~height:h ~depth:1 () in
      let color = Gdk.Color.alloc (Gdk.Color.get_system_colormap ()) (`RGB (0, 0, 0)) in
      Gdk.Cursor.create_from_pixmap pixmap mask color color w h

</pre>

<p>
which I use as follows:
</p>

<pre class="code">

  let win = GWindow.window ~resizable:true () in
  ignore (win#connect#destroy GMain.quit) ;

  (* More code here. *)

  win#show () ;

  (* Must set the cursor after win#show () or we get a Gpointer.NULL exception. *)
  let cursor = create_null_cursor win#misc#window in
  Gdk.Window.set_cursor win#misc#window cursor ;

  GMain.main ()

</pre>

<p>
Problem solved.
</p>


]]></description>
   <category domain="http://www.mega-nerd.com/erikd/Blog"></category>
   <pubDate>Tue, 02 Dec 2008 10:36 GMT</pubDate>
</item>
<item>
   <title>Just Drawing Stuff on the Screen.</title>
   <guid isPermaLink="false">CodeHacking/Ocaml/stuff_on_screen</guid>
   <link>http://www.mega-nerd.com/erikd/Blog/CodeHacking/Ocaml/stuff_on_screen.html</link>
   <description><![CDATA[

<p>
	<a href="http://camltastic.blogspot.com/">
	Richard Jones</a>
laments that
	<a href="http://camltastic.blogspot.com/2008/08/just-draw-something-on-f-ing-screen.html">
	drawing stuff on the screen is harder</a>
than it should be.
I haven't seen his code, but it looks like he might be trying to do it with Ocaml
and GTK which probably is more difficult than it should be.
GTK isn't really meant for that sort of stuff.
</p>

<p>
Fortunately, there is a really well designed and thoroughly thought out library
for doing graphics called
	<a href="http://www.cairographics.org/">
	Cairo</a>,
which even has a really great set of
	<a href="http://www.cairographics.org/cairo-ocaml/">
	Ocaml bindings</a>.
On Debian/Ubuntu, the Cairo bindings can be installed using:
</p>

<pre class="code">

   sudo apt-get install libcairo-ocaml-dev

</pre>

<p>
I messed about with Ocaml and Cairo about a year ago and came up with this
little demo.
</p>

<pre class="code">

  (*
  **    http://www.e-dsp.com/what-are-fourier-coefficients-and-how-to-calculate-them/
  **
  **    http://en.wikipedia.org/wiki/Fourier_series#Definition
  *)

  type fourier_series_t =
  {   a0 : float ;
      an : float array ;
      bn : float array ;
      }


  let initial_size = 200

  let two_pi = 8.0 *. atan 1.0

  let sum_float_array ary =
      Array.fold_left (fun x y -&gt; x +. y) 0.0 ary


  let calc_series max_n ary =
      (*
      **    This uses a rough numerical approximation to integration.
      **    As long as the array is long enough (say 1000 or more elements), the
      **    results should be reasonable.
      *)
      let len = float_of_int (Array.length ary) in
      let calc_Xn trig_func n =
          let n = n + 1 in
          let ary = Array.mapi (
		  		fun i x -&gt; x *.
                trig_func ((float_of_int (n * i)) *. two_pi /. (len -. 1.0))
				) ary
          in
          2.0 *. (sum_float_array ary) /. len
      in
      let a0 = (sum_float_array ary) /. len in
      let an = Array.init max_n (calc_Xn cos) in
      let bn = Array.init max_n (calc_Xn sin) in
      { a0 = a0 ; an = an ; bn = bn }


  let waveform_of_series outlen series =
      (*
      **  Given a fourier series, calculate a single cycle waveform of the
      **  specified length.
      *)
      let calc_point i =
          let x = two_pi *. (float_of_int i) /. (float_of_int (outlen - 1)) in
          let asum = sum_float_array (Array.mapi (
                    fun i an -&gt; an *. (cos (float_of_int (i + 1) *. x))) series.an
                    )
          in
          let bsum = sum_float_array (Array.mapi (
                    fun i bn -&gt; bn *. (sin (float_of_int (i + 1) *. x))) series.bn
                    )
          in
          series.a0 +. asum +. bsum
      in
      Array.init outlen calc_point


  let fold_over_clipped_sine gain len =
      let point i =
          let x = gain *. sin (two_pi *. (float_of_int i) /. (float_of_int len)) in
          if x &gt; 1.0 then x -. 2.0
          else if x &lt; -1.0 then x +. 2.0
          else x
      in
      Array.init len point


  let redraw w series _ =
      let cr = Cairo_lablgtk.create w#misc#window in
      let { Gtk.width = width ; Gtk.height = height } = w#misc#allocation in
      Cairo.save cr ;
      (   Cairo.identity_matrix cr ;
          let border = 20.0 in
          Cairo.move_to cr border border ;
          Cairo.line_to cr border (float_of_int height -. border) ;
          Cairo.stroke cr ;

          let wave_width = width - 100 - (int_of_float border) in
          let middle = float_of_int height /. 2.0 in
          let wave_height = 0.7 *. (middle -. border) in

          Cairo.move_to cr border middle ;
          Cairo.line_to cr (border +. float_of_int wave_width) middle ;
          Cairo.stroke cr ;

          Cairo.move_to cr (border +. float_of_int wave_width) border ;
          Cairo.line_to cr (border +. float_of_int wave_width)
                                      (float_of_int height -. border) ;
          Cairo.stroke cr ;

          Cairo.set_source_rgb cr 1.0 0.0 0.0 ;
          let wave_data = waveform_of_series wave_width series in
          Cairo.move_to cr border (float_of_int height /. 2.0) ;
          Array.iteri (fun i x -&gt;
                        Cairo.line_to cr (border +. float i)
						      (middle -. wave_height *. x))
                        wave_data ;
          Cairo.stroke cr ;
          ) ;
      Cairo.restore cr ;
      true


  let () =
      if Array.length Sys.argv != 2 then
      (   Printf.printf "Usage : %s &lt;series length&gt;\n\n" Sys.argv.(0) ;
          exit 0 ;
          ) ;

      let series_len = int_of_string (Sys.argv.(1)) in

      let w = GWindow.window ~title:"Fourier Series Demo" ~width:600 ~height:400 () in
      ignore (w#connect#destroy GMain.quit) ;

      let b = GPack.vbox ~spacing:6 ~border_width:12  ~packing:w#add () in
      let f = GBin.frame ~shadow_type:`IN ~packing:(b#pack ~expand:true ~fill:true) () in
      let area = GMisc.drawing_area ~width:initial_size ~height:initial_size
                    ~packing:f#add ()
      in
      let array_len = 1000 in
      let wave = fold_over_clipped_sine 1.2 array_len in
      let series = calc_series series_len wave in

      ignore (area#event#connect#expose (redraw area series)) ;

      w#show () ;
      GMain.main ()

</pre>

<p>
The above code can be compiled using:
</p>

<pre class="code">

    ocamlopt -I +cairo -I +lablgtk2 cairo.cmxa lablgtk.cmxa cairo_lablgtk.cmxa \
	    gtkInit.cmx fsdemo.ml -o fsdemo

</pre>

<p>
and the output looks like this:
</p>

<br/>
<center>
<img src="http://www.mega-nerd.com/erikd/Img/fourier-series-demo.png "
		border="0" alt="Fourier Series Demo screen shot" />
</center>
<br/>

<p>
So while I agree that the 140 of lines of code here is about 30 times as much as
Richard's code from his ZX80 days, I also think the results are at least 30
times as good.
</p>


]]></description>
   <category domain="http://www.mega-nerd.com/erikd/Blog"></category>
   <pubDate>Wed, 20 Aug 2008 12:29 GMT</pubDate>
</item>
<item>
   <title>Ocaml and Unix.select.</title>
   <guid isPermaLink="false">CodeHacking/Ocaml/ocaml_select</guid>
   <link>http://www.mega-nerd.com/erikd/Blog/CodeHacking/Ocaml/ocaml_select.html</link>
   <description><![CDATA[

<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>

]]></description>
   <category domain="http://www.mega-nerd.com/erikd/Blog"></category>
   <pubDate>Sat, 19 Jul 2008 11:10 GMT</pubDate>
</item>
<item>
   <title>Objects vs Modules.</title>
   <guid isPermaLink="false">CodeHacking/Ocaml/objects_vs_modules</guid>
   <link>http://www.mega-nerd.com/erikd/Blog/CodeHacking/Ocaml/objects_vs_modules.html</link>
   <description><![CDATA[

<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>


]]></description>
   <category domain="http://www.mega-nerd.com/erikd/Blog"></category>
   <pubDate>Fri, 23 May 2008 21:45 GMT</pubDate>
</item>
<item>
   <title>Ocaml : Exception Back Traces in Native Code.</title>
   <guid isPermaLink="false">CodeHacking/Ocaml/native_backtraces</guid>
   <link>http://www.mega-nerd.com/erikd/Blog/CodeHacking/Ocaml/native_backtraces.html</link>
   <description><![CDATA[

<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>


]]></description>
   <category domain="http://www.mega-nerd.com/erikd/Blog"></category>
   <pubDate>Sun, 06 Apr 2008 02:48 GMT</pubDate>
</item>
<item>
   <title>Functional Programming and Testing.</title>
   <guid isPermaLink="false">CodeHacking/Ocaml/testing_ocaml</guid>
   <link>http://www.mega-nerd.com/erikd/Blog/CodeHacking/Ocaml/testing_ocaml.html</link>
   <description><![CDATA[

<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>


]]></description>
   <category domain="http://www.mega-nerd.com/erikd/Blog"></category>
   <pubDate>Sun, 24 Feb 2008 12:26 GMT</pubDate>
</item>
<item>
   <title>Ocaml Snippet : Sqlite3.</title>
   <guid isPermaLink="false">CodeHacking/Ocaml/snip_sqlite</guid>
   <link>http://www.mega-nerd.com/erikd/Blog/CodeHacking/Ocaml/snip_sqlite.html</link>
   <description><![CDATA[

<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>

]]></description>
   <category domain="http://www.mega-nerd.com/erikd/Blog"></category>
   <pubDate>Sat, 24 Nov 2007 03:20 GMT</pubDate>
</item>
<item>
   <title>Lazy Lists.</title>
   <guid isPermaLink="false">CodeHacking/Ocaml/lazy_lists</guid>
   <link>http://www.mega-nerd.com/erikd/Blog/CodeHacking/Ocaml/lazy_lists.html</link>
   <description><![CDATA[

<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 Fibonacci numbers
using the above recursive function, the 19th number in the sequence would be 
calculated twice, the 18th number three times so on.
Its simply not efficient.
</p>

<p>
A better solution is to use a lazy list, which calculates new values of the
sequence as they are needed, based on entries already in the list.
Here's an example that creates a lazy list of the fibonacci numbers:
</p>

<pre class="code">

  type lazy_fib_t =
      Node of int * lazy_fib_t Lazy.t

  let create_fib_list () =
      let rec fib_n minus_2 minus_1 =
          let n = minus_1 + minus_2 in
          Printf.printf "fib_n %d %d -> %d\n" minus_2 minus_1 n ;
          Node (n, lazy (fib_n minus_1 n))
      in
      lazy (Node (1, lazy (Node (1, lazy (fib_n 1 1)))))

  let print_fib_list depth lst =
      let rec sub_print current remaining =
          if current > depth then ()
          else
          match Lazy.force remaining with
          |    Node (head, tail) ->
                  Printf.printf "%3d : %d\n" current head ;
                  sub_print (current + 1) tail
      in
      sub_print 0 lst

  let _ =
      let fib_list = create_fib_list () in
      print_fib_list 4 fib_list ;
      print_endline "------------" ;
      print_fib_list 6 fib_list ;

</pre>

<p>
This is a complete working Ocaml program.
To run it, just save the text to a file, say <i>"lazy_fib.ml"</i> and then
do:
</p>

<pre class="code">

  ocaml lazy_fib.ml

</pre>

<p>
We'll look at the output in detail later.
First lets break it down; looking at the program, from top to bottom we have:
</p>

<pre class="code">

  type lazy_fib_t =
      Node of int * lazy_fib_t Lazy.t

</pre>

<p>
The above two lines define a recursive type called <b>lazy_fib_t</b>, which has
a single variant called <b>Node</b> which contains a tuple of an integer and
the head of a lazy list.
</p>

<pre class="code">

  let create_fib_list () =
      let rec fib_n minus_2 minus_1 =
          let n = minus_1 + minus_2 in
          Printf.printf "fib_n %d %d -> %d\n" minus_2 minus_1 n ;
          Node (n, lazy (fib_n minus_1 n))
      in
      lazy (Node (1, lazy (Node (1, lazy (fib_n 1 1)))))

</pre>

<p>
The function above, <b>create_fib_list</b>, creates a lazy list.
It also contains an internal function, <b>fib_n</b>, which we'll look at later.
The last line of the function is where all the magic is; it creates three nodes
of a lazy list, the first two containing the first two integers of the 
Fibonacci sequence and a third node which is a 
	<a href="http://en.wikipedia.org/wiki/Closure_%28computer_science%29">
	closure</a>,
containing a call to the internal function <b>fib_n</b> with the correct 
parameters to generate the next number in the sequence.
</p>

<p>
The internal function <b>fib_n</b> takes two parameters, the values of the
sequence for <b>n - 1</b> and <b>n - 2</b>.
From these two values, it generates the value for <b>n</b>, prints a message
and then constructs a new Node containing the value for <b>n</b> and a lazy
evaluation for the next value.
</p>

<p>
The next function is the function which prints the first n elements of a lazy
list.
It looks like this:
</p>

<pre class="code">

  let print_fib_list depth lst =
      let rec sub_print current remaining =
          if current > depth then ()
          else
          match Lazy.force remaining with
          |    Node (head, tail) ->
                  Printf.printf "%3d : %d\n" current head ;
                  sub_print (current + 1) tail
      in
      sub_print 0 lst

</pre>

<p>
The <b>print_fib_list</b> function contains an internal function
<b>sub_print</b> which is called with a current depth of zero and the head of
the lazy list to be printed.
The internal function recursively moves down the list until <b>current</b> is 
greater than <b>depth</b>, which cause the recursion to complete and unwind.
</p>

<p>
At each node of the lazy list where <b>current</b> is less than or equal to
<b>depth</b>, the function forces the evaluation of the node.
The forcing will only evaluate a node if it hasn't already been evaluated.
Once the node has been force evaluated, the value is printed and the function
is called recursively.
</p>

<p>
Finally, the main function of the program is this:
</p>

<pre class="code">

  let _ =
      let fib_list = create_fib_list () in
      print_fib_list 4 fib_list ;
      print_endline "------------" ;
      print_fib_list 6 fib_list ;

</pre>

<p>
All it does is call the function <b>create_fib_list</b>, and then print the 
first four Fibonacci numbers of the list, prints a dashed line and then prints
the first six Fibonacci numbers of the list.
Its important to note that the print function is called with the same list on
both occasions.
</p>

<p>
When the program is run, the output should look like this:
</p>

<pre class="code">

    0 : 1
    1 : 1
  fib_n 1 1 -> 2
    2 : 2
  fib_n 1 2 -> 3
    3 : 3
  fib_n 2 3 -> 5
    4 : 5
  ------------
    0 : 1
    1 : 1
    2 : 2
    3 : 3
    4 : 5
  fib_n 3 5 -> 8
    5 : 8
  fib_n 5 8 -> 13
    6 : 13

</pre>

<p>
As can be seen above, the first time the print function is called, the
<b>fib_n</b> closure is called for all values of <b>n</b> greater than one.
Each time <b>fib_n</b> is called a new node is generated in the list.
When the print function is called the second time, it <b>fib_n</b> is only
called for values that weren't evaluated on the first call to the print 
function just as was expected.
</p>

<p>
One of the few problems with the above implementation is that it uses integers
which in Ocaml on 32 bit CPU platforms is only a 31 bit integer.
It would however be relatively easy to use Ocaml's <b>Big_int</b> module which
provides arbitrary length integers.
</p>


]]></description>
   <category domain="http://www.mega-nerd.com/erikd/Blog"></category>
   <pubDate>Thu, 22 Mar 2007 10:43 GMT</pubDate>
</item>
<item>
   <title>Xtreme Numerical Accuracy.</title>
   <guid isPermaLink="false">CodeHacking/Ocaml/xtreme_numerical_accuracy</guid>
   <link>http://www.mega-nerd.com/erikd/Blog/CodeHacking/Ocaml/xtreme_numerical_accuracy.html</link>
   <description><![CDATA[

<p>
I'm working on a digital filter design program in Ocaml which was suffering
from some numerical issues with Ocaml's native 64 bit floats.

The problem was that the algorithm operates on both large floating point
numbers and small floating point numbers.
These numbers eventually end up in a matrix, and I then use
	<a href="http://en.wikipedia.org/wiki/Gaussian_elimination">
	Gaussian elimination</a>
to solve a set of simultaneous equations.
</p>

<p>
Anyone who has done any numerical computation will know that adding large
floating point numbers to small floating numbers is a recipe for numerical
inaccuracy.
For me, the numerical issues were screwing things up badly.
</p>

<p>
When faced with a problem like this there are two possible solutions:
</p>

<ul>
<li>Do all the computations symbolically, and only substitute numbers at the
	very last stage and then being careful to process the numerical parts
	in a way to minimize rounding and truncation error.
	</li>
<li>Replace the floating point operations with operations on a number type
	which can provide higher (and maybe even arbitrary) precision.
	</li>
</ul>

<p>
The first option, doing all the computations symbolically was not practical
due to the complexity of the computation.
That left only the second option.
</p>

<p>
Looking around for what was available for Ocaml, I found the
	<a href="http://contfrac.sourceforge.net/">
	contfrac project</a>
on Sourceforge.
As all the math geeks (hi Mark) have probably guessed by now, contfrac
expresses numbers in terms of a really cool mathematical concept called
	<a href="http://en.wikipedia.org/wiki/Continued_fraction">
	continued fractions</a>.
</p>

<p>
The idea is that any number can be represented by a (potentially infinite) list
of integers <b>[ a0 ; a1, a2, a3, ...]</b>.
Given the list of integers, the number itself can be calculated using:
</p>

<center>
<img src="http://www.mega-nerd.com/erikd/Img/contfrac.png"
	border="0" alt="equation"/>
</center>

<p>
All
	<a href="http://en.wikipedia.org/wiki/Rational_numbers">
	rational numbers</a>
have a finite length continued fraction expansion.
For example, the rational number 75/99 is expressed as <b>[ 0 ; 1, 3, 8 ]</b>.
</p>

<p>
Not surprisingly, all the
	<a href="http://en.wikipedia.org/wiki/Irrational_numbers">
	irrational numbers</a>
have infinite length continued fraction expansions.

The surprising thing (for me at least) is that many of the irrational numbers
have CF expansions that are surprisingly regular.
The square root of two is expressed as <b>[ 1 ; 2, 2, 2, ...]</b> with an
infinitely repeating list of 2s.
The natural logarithm <b>e</b> is expressed as <b>[ 2 ; 1, 2, 1, 1, 4, 1, 1,
6, ...]</b> which again has a regular pattern, as does the golden ratio,
<b>[ 1 ; 1, 1, 1, ...]</b>.
While all the previous CF expansions have a degree of regularity, the expansion
of <b>pi</b>, is <b>[ 3 ; 7, 15, 1, 292, 1, 1, 1, 2, 1, 3, 1, 14, 2, 1, 1, 2,
2, 2, 2, 1, 84, 2, 1, 1, 15, 3, 13,...]</b>, which looks completely random.
</p>

<p>
With numbers expressed as continued fractions, the Ocaml contfrac module then
implements addition, subtraction, multiplication and division.
Once the four arithmetic operations are defined, contfrac then implements a
number of trigonometric and transcendental functions in terms of the same
continued fractions.
</p>

<p>
Unfortunately, the module doesn't implement everything I need so I'm going to
have to hack on some extra functionality.
The actual Ocaml implementation uses Ocaml's lazy lists which is an aspect of
Ocaml I hadn't played with yet.
Time for some fiddling with lazy lists.
</p>

]]></description>
   <category domain="http://www.mega-nerd.com/erikd/Blog"></category>
   <pubDate>Wed, 21 Mar 2007 09:49 GMT</pubDate>
</item>
<item>
   <title>Ocaml : Exception Backtraces.</title>
   <guid isPermaLink="false">CodeHacking/Ocaml/exception_backtraces</guid>
   <link>http://www.mega-nerd.com/erikd/Blog/CodeHacking/Ocaml/exception_backtraces.html</link>
   <description><![CDATA[

<p>
There's a
	<a href="http://www.cs.ubc.ca/~murphyk/Software/Ocaml/why_ocaml.html">
	paper dated December 2002 by Kevin Murphy</a>
where he explains why he was looking at Ocaml.
That article was recently linked on
	<a href="http://programming.reddit.com/">
	programming.reddit.com</a>
and there was a comment complaining that
	<a href="http://programming.reddit.com/info/mx0t/comments/cmz38">
	Ocaml couldn't print out backtraces on exceptions</a>.
Someone posted later that this was not right, but I've heard this complaint
often enough that I thought I should blog about how to do it.
</p>

<p>
First off, Ocaml has two compilers, one which produces bytecode and one which
produces native binaries.
The native code compiler is not currently able to produce exception backtraces
and this is where the Reddit commenter got the idea.
However, there is
	<a href="http://caml.inria.fr/mantis/view.php?id=3885 ">
	a patch in the Ocaml bug tracker</a>
which adds backtrace capabilities.
I'm hoping that this goes into the compiler proper in the next release or two.
</p>

<p>
For a project that is currently compiling with ocamlopt (the native code
compiler), changing the to bytecode compiler is as simple as editing the
Makefile and replacing all invocations of <b>"ocamlopt"</b> with
<b>"ocamlc -g"</b> where the <b>"-g"</b> turns on exception backtraces.
You can then rebuild the application.
The final step is to turn on backtraces in the bytecode run time environment
which is done by setting an environment variable:
</p>

<pre class="code">

  export OCAMLRUNPARAM="b1"

</pre>

<p>
Once compiled to bytecode and with the environment variable set, the
application can be run and should produce the required backtrace.
The following is an example of a backtrace from something I'm working on
at the moment (I hacked the code to make sure I could get one).
</p>

<pre class="code">

  Fatal error: exception Invalid_argument("index out of bounds")
  Raised by primitive operation at unknown location
  Called from file "meyers_diff.ml", line 93, characters 1-31
  Called from file "meyers_diff.ml", line 200, characters 10-52
  Called from file "meyers_diff.ml", line 221, characters 16-60
  Called from file "meyers_diff.ml", line 264, characters 11-148
  Called from file "meyers_diff.ml", line 305, characters 17-50
  Called from file "array.ml", line 130, characters 31-51
  Called from file "meyers_diff.ml", line 323, characters 1-316

</pre>

<p>
Obviously it would be nicer if function names were included here, but this is
more than sufficient for debugging purposes.
</p>

]]></description>
   <category domain="http://www.mega-nerd.com/erikd/Blog"></category>
   <pubDate>Sun, 22 Oct 2006 00:39 GMT</pubDate>
</item>
<item>
   <title>Ocaml : Code for Variant Types and Pattern Matching.</title>
   <guid isPermaLink="false">CodeHacking/Ocaml/variant_types_code</guid>
   <link>http://www.mega-nerd.com/erikd/Blog/CodeHacking/Ocaml/variant_types_code.html</link>
   <description><![CDATA[

<p>
Since my blog post on Ocaml's
	<a href="http://www.mega-nerd.com/erikd/Blog/CodeHacking/Ocaml/variant_types.html">
	Variant Types and Pattern Matching</a>
I've had two requests for the code, so here it is:
</p>

<pre class="code">

type expr_t =
    |  Var of string
    |  Plus of expr_t * expr_t
    |  Times of expr_t * expr_t
    |  Divide of expr_t * expr_t


let rec simplify expr =
    match expr with
        |   Times (a, Plus (b, c)) ->
                Plus (simplify (Times (a, b)), simplify (Times (a, c)))
        |   Divide (Divide (a, b), Divide (c, d)) ->
                Divide (simplify (Times (a, d)), simplify (Times (b, c)))
        |   anything -> anything (* Comment : default case *)


let rec str_of_expr expr =
    match expr with
        |   Var v -> v
        |   Plus (a, b) ->
                "(" ^ (str_of_expr a) ^ "+" ^ (str_of_expr b) ^ ")"
        |   Times (a, b) ->
                (str_of_expr a) ^ "*" ^ (str_of_expr b)
        |   Divide (a, b) ->
                (str_of_expr a) ^ " / " ^ (str_of_expr b)


let _ =
    let expr = Times (Var "x", Plus (Var "y", Var "z")) in
    Printf.printf "  orig : %s\n" (str_of_expr expr) ;
    let expr = simplify expr in
    Printf.printf "  new  : %s\n" (str_of_expr expr)

</pre>

<p>
The above code is a single self contained program; to run that program, save it
to to a file named say <i>"cas.ml"</i> then run it (assuming you have ocaml 
installed) using the command "<b>ocaml cas.ml</b>" which should result in the
following output:
</p>

<pre class="code">

  orig : x*(y+z)
  new  : (x*y+x*z)

</pre>

<p>
Obviously, this is just just a demo, but it should be pretty clear that this
code could easily be extended to something more useful.
</p>

]]></description>
   <category domain="http://www.mega-nerd.com/erikd/Blog"></category>
   <pubDate>Mon, 11 Sep 2006 08:41 GMT</pubDate>
</item>
<item>
   <title>Ocaml : Variant Types and Pattern Matching.</title>
   <guid isPermaLink="false">CodeHacking/Ocaml/variant_types</guid>
   <link>http://www.mega-nerd.com/erikd/Blog/CodeHacking/Ocaml/variant_types.html</link>
   <description><![CDATA[

<p>
At last month's SLUG meeting,
	<a href="http://certifiedwaif.livejournal.com/">
	Mark Greenaway</a>
asked if anybody knew of any good Computer Algebra Systems (CAS) available
under Linux.
I spoke up and told him that I looked around for the same thing some time ago,
couldn't find anything that I liked so I ended up writing something that fit my
particular needs from scratch.
</p>

<p>
Later that night I was talking to
	<a href="http://www.advogato.org/person/robertc/">
	Robert Collins</a>
and
	<a href="http://research.operationaldynamics.com/blogs/andrew/">
	Andrew Cowie</a>
about stretch languages; languages that differ radically from the languages a
programmer already knows so that learning the new language teaches them new
programming concepts and paradigms.
</p>

<p>
For me, my last stretch language was Ocaml which I started using around mid
2004.
I discovered Ocaml when I went looking for a language to implement my Computer
Algebra System (CAS) in.
I did do a trial implementation in C, but that was simply too much of a pain in
the neck.
I also knew that C++ was not a sufficiently big step away from C to be useful
for this project.
My other main language at the time was Python, but I knew Python's
	<a href="http://www.mega-nerd.com/erikd/Blog/CodeHacking/dynamic_typing.html">
	dynamic typing</a>
would make my life difficult.
</p>

<p>
It was at about this time that I asked my friend
	<a href="http://www.algorithm.com.au/mt/">
	André Pang</a>
to suggest a language.
André had recently given a talk at SLUG titled
	<a href="http://www.algorithm.com.au/mt/talks/beyond_c_c_perl_and_python.html">
	"Beyond C, C++, Python and Perl"</a>
and seemed to know about a whole bunch of different languages.
I told him that I was looking for something that was strongly typed, statically
typed, had garbage collection for memory management and had Object Oriented
(OO) features.
</p>

<p>
One of André's suggestions was Java which I was already familiar with.
However, I disliked the fact that Java does not allow one to write code outside
of a class and Java was also a little too verbose for my tastes.
He also tried to push Haskell, but Haskell doesn't have traditional OO features.
In retrospect this wouldn't have been a problem, but at the time I rejected
Haskell for this reason.
However, his final suggestion was Ocaml which seemed to fit all of my
requirements.
While investigating Ocaml I found a
	<a href="http://www.ocaml-tutorial.org/data_types_and_matching#Pattern_matching__on_datatypes_">
	small example</a>
on the
	<a href="http://www.ocaml-tutorial.org/">
	Ocaml Tutorial</a>
that implemented a bare bones CAS.
</p>

<p>
The two things that makes Ocaml <b><i>really</i></b> great for CAS are variant
types and pattern matching on these variants.
I'll look at these separately.
</p>

<h3>Variant Types in Ocaml.</h3>

<p>
Ocaml's variant types are a little like a type safe, bullet proof version of
unions in C and C++.
In Ocaml one defines a variant type like this:
</p>

<pre class="code">
type expr_t =
    |  Var of string
    |  Plus of expr_t * expr_t
    |  Times of expr_t * expr_t
    |  Divide of expr_t * expr_t
</pre>

<p>
So, here we have a type named <b>expr_t</b> (a mathematical expression) that 
can hold one of four things:
</p>

<ul>
<li> A variable name which is just a string.</li>
<li> A plus node with a left sub expression and a right sub expression.</li>
<li> A times node with a left sub expression and a right sub expression.</li>
<li> A divide node with a numerator sub expression and a denominator sub
	expression.</li>
</ul>

<p>
All of the sub expressions are of the same type, <b>expr_t</b>, which makes
this a recursive type.
Using this recursive variant type, an expression like <b>"x * (y + z)"</b> can
be build like this:
</p>

<pre class="code">

let expr = Times (Var "x", Plus (Var "y", Var "z")) ;;

</pre>

<p>
which results in a tree structure with each operator and our variables
<b>x</b>, <b>y</b> and <b>z</b> being held in a node of type <b>expr_t</b>
and represented by a circle in this diagram:
</p>

<center>
<img src="http://www.mega-nerd.com/erikd/Img/variant_types_expr_tree.png" alt="expression tree"/>
</center>

<p>
with the variable <b>expr</b> being the <b>Times</b> node at the top of the
diagram.
</p>

<p>
The really nice thing about variants is that each instance knows which variant
it is.
That means that its not possible (by mistake or on purpose) to access a node of
one variant as another variant.
The Ocaml compiler simply won't let that happen.
</p>

<p>
Compare this to a C version using unions where the programmer has be make sure
he/she accesses each instance correctly, or the acres of code required to do
the same thing with objects in C++ or Java.
</p>

<h3>Pattern Matching on Variants.</h3>

<p>
So once we can construct a mathematical expression we would also want to print
it out.
Thats where Ocaml's pattern matching comes in.
Here's a function to convert any expression tree into a string representation
that can be printed.
</p>

<pre class="code">
let rec str_of_expr expr =
    match expr with
        |   Var v -> v
        |   Plus (a, b) ->
                "(" ^ (str_of_expr a) ^ "+" ^ (str_of_expr b) ^ ")"
        |   Times (a, b) ->
                (str_of_expr a) ^ "*" ^ (str_of_expr b)
        |   Divide (a, b) ->
                (str_of_expr a) ^ " / " ^ (str_of_expr b)
</pre>

<p>
The function is called <b>str_of_expr</b> and the "<b>rec</b>" just before
the function name means that the function is recursive.
The function takes a single parameter of type <b>expr_t</b> and returns a
string.
</p>

<p>
The "<b>match expr with</b>" on the next line is a bit like a switch statement
in C, C++, Java and other languages.
On the lines following the <b>match</b> there are four options, one for each
of the variants of the <b>expr_t</b> type.
So for instance, if the <b>expr</b> is a <b>Var</b> variant the function just
returns the string that is held by <b>Var</b> and if its a <b>Plus</b> node
with two sub expressions, <b>a</b> and <b>b</b>, then the function is called
on each of the sub expressions and a string is built using Ocaml's string
concatenation operator "<b>^</b>".
</p>

<p>
The above usage of pattern matching is pretty simple and can done almost as
easily in other languages.
So lets look at something a little more complicated.
</p>

<h3>More Advanced Pattern Matching.</h3>

<p>
One of the many things one might want to do in a CAS is applying mathematical
transforms on an expression.
For instance, we might want to be able to expand out our expressions above
<b>"x * (y + z)"</b> to give <b>"(x * y + x * z)"</b>.
Fortunately, using Ocaml's advanced pattern matching this is really easy.
Here's an example:
</p>

<pre class="code">
let rec simplify expr =
    match expr with
        |   Times (a, Plus (b, c)) ->
                Plus (simplify (Times (a, b)), simplify (Times (a, c)))
        |   Divide (Divide (a, b), Divide (c, d)) ->
                Divide (simplify (Times (a, d)), simplify (Times (b, c)))
        |   anything -> anything (* Comment : default case *)

</pre>

<p>
The function <b>simplify</b> has two transformations and a default case
which does nothing.
Again, the function is recursive, but the first two match cases match on much
more complex expression trees.
In fact, the first match case will exactly match our expression and generate
the expression we're after, <b>"(x * y + x * z)"</b>.
</p>

<p>
Obviously, to make a real Computer Algebra System requires quite a bit more 
than what I have here.
However, as you can see, Ocaml's variant types and pattern matching are a
perfect fit for the problems a programmer writing a CAS would face.
In fact, few other languages, with the possible exception of Haskell, would
have fit this problem as well.
</p>


]]></description>
   <category domain="http://www.mega-nerd.com/erikd/Blog"></category>
   <pubDate>Sun, 03 Sep 2006 02:53 GMT</pubDate>
</item>
<item>
   <title>Ocaml : Fold.</title>
   <guid isPermaLink="false">CodeHacking/Ocaml/fold</guid>
   <link>http://www.mega-nerd.com/erikd/Blog/CodeHacking/Ocaml/fold.html</link>
   <description><![CDATA[

<p>
In a previous post, I blogged about Ocaml's
    <a href="http://www.mega-nerd.com/erikd/Blog/CodeHacking/Ocaml/iter_and_map.html">
    <b>iter</b> and <b>map</b></a>
functions and how they can be applied to arrays and lists.
In some circumstances, these functions can be used as a replacement for a
<b>for</b> loop.
However, there are some other situations where <b>iter</b> and <b>map</b> are
can only provide a non-optimal solution.
For example, here's a small program which uses Ocaml's imperative features to
calculate the sum of the elements of an integer array:
</p>

<pre class="code">
let _ =
    let a = [| 1 ; 2 ; 5 ; 7 ; 11 |] in
    let sum = ref 0 in
    for i = 0 to Array.length a - 1 do
        sum := !sum + a.(i)
    done ;
    Printf.printf "Sum : %d\n" !sum
</pre>

<p>
The value <b>sum</b> is a reference to an integer which is initialized to 
zero and the referenced sum is then updated inside the <b>for</b> loop.
Operating on references is a little different to operating on values; it
requires the use of the de-reference operator <b>"!"</b> to access the
referenced value and requires the use of the de-reference assignment  
operator <b>":="</b> to update the referenced value.
</p>

<p>
Like the previous <b>iter</b> and <b>map</b> examples, there are a number
of places this can go wrong, even though its only a very small chunk of
code.
Here's how to use <b>iter</b> to acheive the same result:
</p>

<pre class="code">
let _ =
    let a = [| 1 ; 2 ; 5 ; 7 ; 11 |] in
    let sum = ref 0 in
    Array.iter (fun x -> sum := !sum + x) a ;
    Printf.printf "Sum : %d\n" !sum
</pre>

<p>
bit thats only a small win.
</p>

<p>
Fortunately, there is a significantly better
	<a href="http://en.wikipedia.org/wiki/Higher_order_functions">
	Higher Order Function</a>
solution to this problem, a concept called fold and implemented as
functions <b>fold_left</b> and <b>fold_right</b>.
The following example program uses both and reduces the for loop in
the first example to a single line, including the initialization of the
accumulator used to calculate the sum:
</p>

<pre class="code">
let _ =
    let a = [| 1 ; 2 ; 5 ; 7 ; 11 |] in
    let fold_left_sum = Array.fold_left (fun x y -> x + y) 0 a in
    Printf.printf "Fold_left sum  : %d\n" fold_left_sum ;
    let fold_right_sum = Array.fold_right (fun x y -> x + y) a 0 in
    Printf.printf "Fold_right sum : %d\n" fold_right_sum
</pre>

<p>
So lets have a look at a single fold_left:
</p>

<pre class="code">
Array.fold_left (fun x y -> x + y) 0 a
</pre>

<p>
Obviously, the first parameter passed to <b>fold_left</b> is an anonymous
function which takes two parameters <b>x</b> and <b>y</b> and returns their
sum and the last parameter is simply the array the fold is being applied.
The second parameter, <b>0</b> in this case, is where all the magic happens.
The value <b>0</b> is the value that will be passed to the anonymous
function as the <b>x</b> parameter, the first time it is called.
For subsequent calls, the value of the <b>x</b> parameter will be the return
value of the previous call of the anonymous function.
</p>

<p>
Obviously the easiest way to visualize this is with an example that prints
out the values. Here it is:
</p>

<pre class="code">
let _ =
    let a = [| 1 ; 2 ; 5 ; 7 ; 11 |] in
    let fold_left_sum = Array.fold_left
    (   fun x y ->
            Printf.printf "%4d %4d\n" x y ;
            x + y
            )
        0 a
    in
    Printf.printf "\nFold_left sum  : %d\n" fold_left_sum
</pre>

<p>
For those of you too lazy to try this yourselves
	<img src="http://www.mega-nerd.com/erikd/Img/smile.png"
		border="0" style="vertical-align:middle" alt=":-)"></img>,
here is the output:
</p>

<pre class="code">
   0    1
   1    2
   3    5
   8    7
  15   11

Fold_left sum  : 26
</pre>

<p>
There, just as I explained it.
So what about <b>fold_right</b>?
Well there are two differences and they are a little subtle so here's
the program:
</p>

<pre class="code">
let _ =
    let a = [| 1 ; 2 ; 5 ; 7 ; 11 |] in
    let fold_right_sum = Array.fold_right
    (   fun x y ->
            Printf.printf "%4d %4d\n" x y ;
            x + y
            )
        a 0
    in
    Printf.printf "\nFold_right sum : %d\n" fold_right_sum
</pre>

<p>
and here's the output:
</p>

<pre class="code">
  11    0
   7   11
   5   18
   2   23
   1   25

Fold_right sum : 26
</pre>

<p>
The two differences are:
</p>

<ul>
	<li> With <b>fold_left</b> the function is applied from first entry to
		last entry while with <b>fold_right</b> the function is applied in
		reverse order.</li>
	<li> With <b>fold_left</b> the initializer/previous result is passed as
		the first parameter to the applied function while with
		<b>fold_right</b> the initializer/previous result is passed as
		the second parameter.</li>
</ul>

<p>
Like <b>iter</b> and <b>map</b>, Ocaml's fold functions can reduce the number
of points of possible error in a program.
More importantly, for the code reader who understands and is comfortable
with these techniques, reading and understanding code using these functions
is quicker than reading and understanding the equvalient for loop.
</p>

<p>
Ocaml rocks!
</p>

]]></description>
   <category domain="http://www.mega-nerd.com/erikd/Blog"></category>
   <pubDate>Tue, 11 Jul 2006 10:43 GMT</pubDate>
</item>
<item>
   <title>Ocaml : Iter and Map.</title>
   <guid isPermaLink="false">CodeHacking/Ocaml/iter_and_map</guid>
   <link>http://www.mega-nerd.com/erikd/Blog/CodeHacking/Ocaml/iter_and_map.html</link>
   <description><![CDATA[

<p>
Ocaml, like most other languages has arrays for storing multiple values of
the same type.
Ocaml also has built in lists; lists that behave like the singly linked list
that people write in lower level languages like C but without the pain.
</p>

<p>
To operate on arrays in Ocaml it is certainly possible to use a <b>for</b>
loop to work on each element in the array in turn just like one would do
in other languages.
Here's a simple example program:
</p>

<pre class="code">
let _ =
    let int_array = [| 1 ; 2 ; 3 ; 7 ; 11 |] in
    for i = 0 to Array.length int_array - 1 do
        Printf.printf "%5d\n" int_array.(i)
    done
</pre>

<p>
Note : This program can be run (assuming you have ocaml installed) by simply
saving the above code in a text file with a ".ml" extension and then running:
</p>

<pre class="code">
    ocaml &lt;filename&gt;
</pre>

<p>
The astute reader will notice that the <b>for</b> loop approach used above has
a couple of things that the programmer must get right.
Even in this really simple example, the programmer has to know that array
indices start at zero, has to know or find out the length of the array, has
to subtract one from that length to avoid accessing elements beyond the last
element and then has to explicitly access each element of the array.
</p>

<p>
Fortunately, in functional languages like Ocaml, it is possible to avoid all
of these potential causes of error by using more functional idioms like
	<a href="http://en.wikipedia.org/wiki/Higher_order_functions">
	Higher Order Functions</a>
(HOF).
</p>

<p>
The idea behind using HOF on lists and arrays is that the programmer defines a
function that operates on a single element and then applies that function to
each entry one at a time.
Here's a simple example program which defines a list of integers and then prints
it.
</p>

<pre class="code">
let printline_int x =
    Printf.printf "%5d\n" x

let _ =
    let int_array = [| 1 ; 2 ; 3 ; 7 ; 11 |] in
    Array.iter printline_int int_array
</pre>

<p>
<b>Array.iter</b> is a higher order function named <b>iter</b> in the
<b>Array</b> module.
It takes two paramaters, a function and an array and applies the function
to each element of the array.
In this particular case, the function <b>printline_int</b> will be applied
to each element in <b>int_array</b> from the first to the last.
</p>

<p>
However, the <b>printline_int</b> function very simple and is only used once in
this very small program.
It therefore makes just as much sense to make it a
	<a href="http://en.wikipedia.org/wiki/Closure_%28computer_science%29">
	closure</a>
like this:
</p>

<pre class="code">
let _ =
    let int_array = [| 1 ; 2 ; 3 ; 7 ; 11 |] in
    Array.iter (fun x -> Printf.printf "%5d\n" x) int_array
</pre>

<p>
The bit enclosed within the parentheses here defines an anonymous function
which takes a single parameter <b>x</b> and then defines the body of the
function on the right hand side of the arrow.
</p>

<p>
However, even for this most recent example there's a more compact way to write
it.
Because the anonymous function's parameter is the last token in the function
body it can also be written like this:
</p>

<pre class="code">
let _ =
    let int_array = [| 1 ; 2 ; 3 ; 7 ; 11 |] in
    Array.iter (Printf.printf "%5d\n") int_array
</pre>

<p>
In this example, the bit within the parentheses is a partially applied
function; a function which needs one or more extra parameters to complete
the call.
In this case the bit within the parentheses behaves just like the
<b>printline_int</b> function in the earlier example.
</p>

<p>
All of the <b>Array.iter</b> examples above apply a function to the elements
of the array and that function has no return value.
In Ocaml, a function without a return value is said to return <b>unit</b>
(much like <b>void</b> in C, C++, and Java etc).
</p>

<p>
So here's another example where we take the original array, add one to each
element to create a new array and then print out the new array:
</p>

<pre class="code">
let _ =
    let int_array = [| 1 ; 2 ; 3 ; 7 ; 11 |] in
    let plus_one = Array.map (fun x -> x + 1) int_array in
    Array.iter (Printf.printf "%5d\n") plus_one
</pre>

<p>
<b>Array.map</b> is much like <b>Array.iter</b>.
The big difference is that the function that is passed to <b>Array.map</b>
and applied to each element in the array must return a value.
In this case, the anonymous function simply takes an int parameter and
returns an int.
However, it could just as easily take an int and return some other type.
Here's an example where the anonymous function takes an int and returns
a float:
</p>

<pre class="code">
let _ =
    let int_array = [| 1 ; 2 ; 3 ; 7 ; 11 |] in
    let exp_val = Array.map (fun x -> exp (float_of_int x)) int_array in
    Array.iter (Printf.printf "%10.2f\n") exp_val
</pre>


<p>
There are equivalent functions to the ones above which work on lists, called
unsurprisingly <b>List.iter</b> and <b>List.map</b>.
There are also other interesting functions like <b>iteri</b> (arrays only),
<b>filter</b> (lists only), <b>fold_left</b> and <b>fold_right</b> (both 
arrays and lists) and a couple of others.
They are all powerful tools which allow algorithms to be implemented in Ocaml
more succinctly and with less chance of error than with imperative programming
languages.
</p>

]]></description>
   <category domain="http://www.mega-nerd.com/erikd/Blog"></category>
   <pubDate>Sat, 08 Jul 2006 06:39 GMT</pubDate>
</item>
<item>
   <title>Functional Triangles.</title>
   <guid isPermaLink="false">CodeHacking/Ocaml/functional_triangles</guid>
   <link>http://www.mega-nerd.com/erikd/Blog/CodeHacking/Ocaml/functional_triangles.html</link>
   <description><![CDATA[

<p>
Our friend
	<a href="http://certifiedwaif.livejournal.com/">
	Mark Greenaway</a>
has been playing around with Ocaml.
He's written a program that draws n-sided polygons (for odd n) and then draws
lines between the midpoint of each line segment and the opposite vertex to 
show that all of those lines will coincide at a point which is the middle of
the polygon.
</p>

<p>
Mark's
	<a href="http://styx.knobbits.org/triangles.ml">
	solution</a>
to this problem is pretty typical of a programmer who comes to Ocaml from
imperative languages.
Typically it uses a bunch of for loops when better, more functional techniques
exist
	<img src="http://www.mega-nerd.com/erikd/Img/smile.png"
		border="0" style="vertical-align:middle" alt=":-)"></img>.
</p>

<p>
Here's my solution to the problem:
</p>


<pre class="code">
(* To compile this: ocamlc graphics.cma triangles2.ml -o triangles2 *)
open Graphics

let pi = 3.1415926535897932384626433832795
let window_width = 640
let window_height = 480

let gen_points count =
  let cx = window_width / 2 in
  let cy = window_height / 2 in
  let ary = Array.create count 0 in
  let radius = 0.9 *. float_of_int cy in
  let delta_angle = 2.0 *. pi /. (float_of_int count) in
  Array.mapi (fun i x ->
    ( cx + truncate (radius *. sin (float_of_int i *. delta_angle)),
      cy + truncate (radius *. cos (float_of_int i *. delta_angle))
    )) ary

let gen_outer_lines points =
  set_color black ;
  let (x, y) =  points.(Array.length points - 1) in
  moveto x y ;
  Array.iter (fun (x, y) -> lineto x y) points

let gen_center_lines points =
  set_color red ;
  let do_line sx sy ex ey =
    moveto sx sy ;
    lineto ex ey
  in
  let len = Array.length points in
  let mid = len / 2 in
  Array.iteri (fun i (sx, sy) ->
    let (first, second) = ((i + mid) mod len, (i + mid + 1) mod len) in
    let (ex1,  ey1) = points.(first) in
    let (ex2,  ey2) = points.(second) in
    do_line sx sy ((ex1 + ex2) / 2) ((ey1 + ey2) / 2)
    ) points

let _ =
  let points = gen_points 5 in
  open_graph " 640x480" ;
  gen_outer_lines points ;
  gen_center_lines points ;
  set_color blue ;
  Array.iter (fun (x, y) -> fill_circle x y 10) points ;
  ignore (read_line ())

</pre>

<p>
There's a few algorithmic tweaks here and there but the big difference is
the complete lack of for loops.
</p>


]]></description>
   <category domain="http://www.mega-nerd.com/erikd/Blog"></category>
   <pubDate>Thu, 06 Jul 2006 14:09 GMT</pubDate>
</item>
<item>
   <title>GTK+ Callbacks in OCaml.</title>
   <guid isPermaLink="false">CodeHacking/Ocaml/gtk_callbacks</guid>
   <link>http://www.mega-nerd.com/erikd/Blog/CodeHacking/Ocaml/gtk_callbacks.html</link>
   <description><![CDATA[

<p>
GUI programming is one of those areas thats always a bit of a pain regardless
of language used.
Ocaml however has some language features (not present in C, C++ Java, Python
etc) which make GUI programming in Ocaml somewhat more elegant.
</p>

<p>
Regardless of the language used, GUI programming means writing a bunch of
small functions (or class methods in OO systems) that get called when the GUI
elements are manipulated by the user of the application.
These small event handling functions are often called callbacks and are
usually associated with the GUI widget (button / menu / whatever) when
the widget is created.
For example if C/GTK+, to catch the mouse button click even in some widget
<b>w</b> the programmer has to define a button press handler with a function
signature like:
</p>

<pre class="code">
  static gboolean
  button_press_callback (GtkWidget *w, GdkEventButton *ev, gpointer data) ;
</pre>

<p>
When the program's user clicks the mouse button in the widget, this function
will be called with a pointer to the widget as the first parameter and a
pointer to the button event data (cursor x/y position, time etc) as the
second parameter.
The third parameter is a generic pointer.
At the time the callback is registered, the programmer can set this pointer
to be a pointer to anything he/she wishes and then cast it to the right
type in the callback.
One common usage is to set this as a pointer to a struct containing the current
state of the application, so that this state can be modified within the callback
function.
</p>

<p>
One common problem I have always run up against when doing the above is when I
have more than one instance of a particular widget type and want to handle all
of them using a single callback.
If the last parameter is a pointer to the state, then how do I differentiate
between the different widgets that generate the event.
Yes, it can be done using the widget pointer, but thats just a pain.
Usually, the best solution is to make the state data a global variable (yuck)
and then make the last parameter a value which identifies which widget generated
the event.
</p>

<p>
Ocaml has a really neat solution to the above problem; a programming trick
called
	<a href="http://en.wikipedia.org/wiki/Closure_%28computer_science%29">closures</a>.
In this particular context, closures allow something that behaves like a
partial application of a function.
</p>

<p>
Consider the function :
</p>

<pre class="code">
  let add_2 a b =
      a + b
</pre>

<p>
For those who don't read Ocaml, this is a function that takes two integer
parameters and returns their sum.
Using the above function we can define a new function like this:
</p>

<pre class="code">
  let add_to_x = add_2 x
</pre>

<p>
As you can see, this uses our function from above, but calls it with only one
parameter.
So what the hell is <b>add_to_x</b>?
Well its a closure (a type of function) that takes a single integer parameter
and adds it to the value the variable <b>x</b> contained when the closure was
created.
</p>

<p>
People with a background in OO languages can look at a closure like
<b>add_to_x</b> as an object with a single method, but without all the overhead
of defining a class and instantiating an object of that class.
</p>

<p>
So, when doing GUI programming in Ocaml we can define a callback with any number
of parameters and with the last parameter as button event data:
</p>


<pre class="code">
  let button_press_callback a b c d event =
      (* Code goes here, and then return true. *)
      true
</pre>

<p>
and when we register the callback with create a closure of the callback function
with all but the last parameter.
This is a much neater way of doing things than any widget callback handling I've
ever seen in C, C++, C#, Java or Python.
</p>


]]></description>
   <category domain="http://www.mega-nerd.com/erikd/Blog"></category>
   <pubDate>Tue, 25 Apr 2006 05:15 GMT</pubDate>
</item>
<item>
   <title>Ocaml and Gtk.</title>
   <guid isPermaLink="false">CodeHacking/Ocaml/ocaml_and_gtk</guid>
   <link>http://www.mega-nerd.com/erikd/Blog/CodeHacking/Ocaml/ocaml_and_gtk.html</link>
   <description><![CDATA[

<p>
I'm currently working on an application that, when its (nearly
	<img src="http://www.mega-nerd.com/erikd/Img/smile.png"
		border="0" style="vertical-align:middle" alt=":-)"></img>
) ready, will be released under the GNU GPL.
</p>

<p>
This app will require a lot of list and text manipulation so I really wanted
to write this in
	<a href="http://caml.inria.fr/">Ocaml</a>
where list handling is so nice and text handling is so hard to screw up.
I also need a GUI for this app and since I had already done a couple of GUIs
in C/GTK+ that may have been an option. Unfortunately, manipulating lists
and strings in C is just way too painful to contemplate.
I therefore decided to go with Ocaml.
</p>

<p>
The Ocaml bindings to GTK+ is called
	<a href="http://wwwfun.kurims.kyoto-u.ac.jp/soft/lsl/lablgtk.html">lablgtk2</a>
and there is also an
	<a href="http://pllab.kaist.ac.kr/~shoh/ocaml/lablgtk2/lablgtk2-tutorial/">Ocaml version</a>
of the 
	<a href="http://www.gtk.org/tutorial/">GTK+ 2 tutorial</a>
which includes Ocaml versions of all the example programs for the original C/GTK+
tutorial.
</p>

<p>
I always find doing GUIs rather painful and just because I'm writing a GUI in
Ocaml doesn't make it any less painful.
The GUI basics are always pretty easy; the pain arises when you want to do 
anything a little unusual and the transition is not at all smooth. One minute
you're rocking along thinking, damn, this is easy, and then you hit a brick wall
trying to get one tiny little detail working.
These problems are even worse in Ocaml because the documentation is nowhere 
near as good as it could be.
In fact, getting the GUI I wanted was proving so difficult that I decided the best
way forward might be to write a custom GTK+ widget in C and then wrap that in
Ocaml.
</p>

<p>
The first step along this path is to write the widget and a test application
in C.
This was surprising easy after my battles with Ocaml and lablgtk.
The GTK+ developer documentation is pretty good and sticking any GTK+ function
name into Google will usually turn up a number of mailing list posts or whatever
with example code.
</p>

<p>
I now had a damn good start on a widget in C that I needed to wrap in Ocaml.
Or so I thought.
It turned out that wrapping the widget and getting data back and forth across
the Ocaml/C boundary was going to make this a very difficult exercise.
</p>

<p>
However, I learnt a lot while hacking together the widget in C, so I went back
to Ocaml and lablgtk2 and came across a really good method for learning how
to do things.
</p>

<ul>
	<li> First figure out how to do it in C (reading docs / Google / whatever).</li>
 	<li> Grep for same concepts in the lablgtk2 include files (much like C 
		header files) to find out what they are called.</li>
	<li> Google for "concept lablgtk2" which usually turns up a code snippet
		that works.</li>
</ul>

<p>
With the above information discovery system the Ocaml version of the GUI is
getting pretty close to being as full featured as the C version.
I'm also finding that lablgtk2 is really nice to work with.
Its not just a plain wrapper around the GTK+ functionality.
In many places it deviates significantly from the way the GTK+/C interface
does things, but where it does, that is a good thing.
</p>

<p>
Once the GUI is done, I need to hook it up to the back end code which is already
at least partially working and then I'll be ready for the first pre-alpha release.
After hacking in Ocaml for over a year now, I really look forward to releasing
my first Ocaml app to the public.
</p>

]]></description>
   <category domain="http://www.mega-nerd.com/erikd/Blog"></category>
   <pubDate>Fri, 21 Apr 2006 12:44 GMT</pubDate>
</item>
</channel>
</rss>
