<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
 
 <title>Bluish Coder</title>
 <link href="http://bluishcoder.co.nz/atom.xml" rel="self"/>
 <link href="http://bluishcoder.co.nz/"/>
 <updated>2012-04-11T11:40:28+12:00</updated>
 <id>http://bluishcoder.co.nz/</id>
 <author>
   <name>Chris Double</name>
   <email>chris.double@double.co.nz</email>
 </author>

 
 <entry>
   <title>Travelling to Pitcairn Island</title>
   <link href="http://bluishcoder.co.nz/2012/04/11/travelling-to-pitcairn-island.html"/>
   <updated>2012-04-11T12:00:00+12:00</updated>
   <id>http://bluishcoder.co.nz/2012/04/11/travelling-to-pitcairn-island</id>
   <content type="html">&lt;a href='http://www.flickr.com/photos/wheatstrong/211069189/' title='Pitcairn Island Approach by wheatstrong, on Flickr' style='float:right'&gt;&lt;img width='240' height='180' alt='Pitcairn Island Approach' src='http://farm1.staticflickr.com/98/211069189_6616901143_m.jpg' /&gt;&lt;/a&gt;
&lt;p&gt;From Sunday 15th April through to 29th April I&amp;#8217;ll be mostly offline as I take some leave to visit &lt;a href='http://en.wikipedia.org/wiki/Pitcairn_Islands'&gt;Pitcairn Island&lt;/a&gt;, one of the remotest inhabited islands with a population of about 50 people.&lt;/p&gt;

&lt;p&gt;My first stop is flying from New Zealand to &lt;a href='http://en.wikipedia.org/wiki/Tahiti'&gt;Tahiti&lt;/a&gt; where I spend a couple of days, then I fly to &lt;a href='http://en.wikipedia.org/wiki/Mangareva'&gt;Mangareva&lt;/a&gt; on the 17th to board the &lt;a href='http://www.xplore-expeditions.com/1-15991-The-yacht.php'&gt;Xplore sailing yacht&lt;/a&gt; for the approximately two day trip to Pitcairn. I spend a couple of days on the island itself, then return on the Xplore back to Mangareva, followed by flying back to Tahiti for a few more days.&lt;/p&gt;

&lt;p&gt;The trip was easy to organise through &lt;a href='http://pitcairntravel.pn/'&gt;Pitcairn Travel&lt;/a&gt;. Longer trips are available than the one I&amp;#8217;m taking but none are scheduled at this time of year. Assuming this trip goes well I hope to go for longer, and maybe to the other Islands in the Pitcairn group, in the future.&lt;/p&gt;

&lt;p&gt;Why Pitcairn? Pitcairn is the island that was settled by the &lt;a href='http://en.wikipedia.org/wiki/Mutiny_on_the_Bounty'&gt;Bounty mutineers&lt;/a&gt;. My grandmother was born on the island and through her I&amp;#8217;m a descendant of three mutineers (&lt;a href='http://en.wikipedia.org/wiki/Fletcher_Christian'&gt;Fletcher Christian&lt;/a&gt;, &lt;a href='http://en.wikipedia.org/wiki/Descendants_of_the_Bounty_Mutineers'&gt;John Mills&lt;/a&gt;, &lt;a href='http://en.wikipedia.org/wiki/Ned_Young'&gt;Ned Young&lt;/a&gt; and their Tahitian wives are my sixth great grandparents). I&amp;#8217;m looking forward to visiting the &lt;a href='http://www.flickr.com/photos/suraark/5860727435/'&gt;Bounty monument&lt;/a&gt; in Tahiti and the &lt;a href='http://www.panoramio.com/photo/25634071'&gt;Bounty Plaque&lt;/a&gt; on Pitcairn.&lt;/p&gt;

&lt;p&gt;Electricity is available on Pitcairn for about 10 hours per day which limits laptop/gadget usage time. Luckily I plan to spend as much time as possible exploring the island, weather permitting.&lt;/p&gt;

&lt;p&gt;I&amp;#8217;ll have internet access while in Tahiti but I suspect access to be a bit hit or miss on Pitcairn. In the past internet access was available by sharing satellite internet that was provided by a United States Geologic Survey station on the island. A &lt;a href='http://bit.ly/IyCi2g'&gt;description of the setup is available here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Later the Pitcairn Island Government arranged &lt;a href='http://cbc.am/pitcairn.htm'&gt;their own satellite internet capability&lt;/a&gt;. Recently &lt;a href='http://www.rnzi.com/pages/news.php?op=read&amp;amp;id=66624'&gt;speeds have been improved&lt;/a&gt; to 512 kilobits per second - shared amongst the approximately 50 people on the island. Costs for residents of the island are around $40 per 400MB of usage from what I hear. I would imagine that if someone wanted to regularly access the internet there for work they&amp;#8217;d require a dedicated satellite internet connection just for that (Something like &lt;a href='http://www.pactelint.com/products/satellite-internet-products.php?RICS-Ku-Band-1'&gt;Pactel&amp;#8217;s VSAT internet&lt;/a&gt; maybe). I&amp;#8217;ll be sure to do a later post on what using the modern web is like in this part of the world.&lt;/p&gt;

&lt;p&gt;Anyone in the area of Tahiti, Mangareva or Pitcairn, let me know, I&amp;#8217;d be keen to meet.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Building and Running Boot To Gecko on the Nexus S</title>
   <link href="http://bluishcoder.co.nz/2012/03/20/building-and-running-b2g-on-nexus-s.html"/>
   <updated>2012-03-20T12:00:00+13:00</updated>
   <id>http://bluishcoder.co.nz/2012/03/20/building-and-running-b2g-on-nexus-s</id>
   <content type="html">&lt;p&gt;Update 2012-03-29 - The Nexus S port has moved to an ICS base system and the existing Gingerbread base no longer works correctly. I&amp;#8217;ve adjusted the instructions below to build the ICS based system. It just involves using &amp;#8216;config-nexuss-ics&amp;#8217; instead of &amp;#8216;config-nexus&amp;#8217;.&lt;/p&gt;

&lt;p&gt;Last year Mozilla &lt;a href='http://hacks.mozilla.org/2011/07/announcing-boot-to-gecko-b2g-booting-to-the-web/'&gt;announced the Boot to Gecko&lt;/a&gt; project - a mobile OS based on web technologies. Recently it was &lt;a href='http://tech2.in.com/news/general/mwc-2012-mozilla-qualcomm-and-telefonica-join-b2g-project/285212'&gt;demoed at MWC 2012&lt;/a&gt;. Work is being done to improve video playback on B2G using hardware codecs in &lt;a href='https://bugzilla.mozilla.org/show_bug.cgi?id=714408'&gt;bug 714408&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I&amp;#8217;ve built and run B2G on the emulator before but I wanted to try it out on real hardware to test the video support and play around with the OS. I upgraded my main phone to a &lt;a href='http://www.samsung.com/nz/galaxynote'&gt;Galaxy Note&lt;/a&gt; recently leaving a my Nexus S spare for trying different ROMS on it. Support for the Nexus S has started becoming available for B2G (previously the main consumer phone for testing was the Galaxy S II) so I gave it a try. The Nexus S I have is the GSM (non-4G) version. The steps to get the source code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ git clone git://github.com/andreasgal/B2G
$ cd B2G
$ make sync&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This takes a long time (on New Zealand networks anyway&amp;#8230;). Multiple gigabytes of git submodules are cloned.&lt;/p&gt;

&lt;p&gt;You&amp;#8217;ll want to make sure you have a build environment set up, as per this &lt;a href='https://developer.mozilla.org/en/Mozilla/Boot_to_Gecko/Setting_Up_Boot_to_Gecko_Build_Environment'&gt;MDN article&lt;/a&gt; so that &amp;#8216;adb&amp;#8217; and other android tools work. Once done, configure for a Nexus S build:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ make config-nexuss-ics&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will download binaries for the phone and get you to confirm a bunch of licenses. To build:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ make gonk
...
$ make
...&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &amp;#8216;gonk&amp;#8217; make invocation builds the underlying android layer. The following &amp;#8216;make&amp;#8217; builds gecko and related parts of B2G. Once those are completed you can flash the phone with the result. &lt;em&gt;Note, you do the following at your own risk! You&amp;#8217;re flashing your phone, overwriting everything, with experimental, possibly buggy software&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;To flash a Nexus S you need to have unlocked the bootloader. If you haven&amp;#8217;t done this yet, boot into the bootloader (hold the up volume key down while pressing the power button) and run:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ make unlock-bootloader&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This runs &amp;#8220;fastboot oem unlock&amp;#8221; which is the command to unlock the Nexus S bootloader. You&amp;#8217;ll need to agree to it on the phone. I also installed the CyanogenMod recovery firmware but this is optional. Instructions to do that are &lt;a href='http://wiki.cyanogenmod.com/wiki/Nexus_S:_Full_Update_Guide'&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To flash, boot the phone into recovery mode (Hold down the up volume key while pressing power, from the menu that appears choose &amp;#8216;Recovery&amp;#8217;), plugin in the USB cable to your PC. Run:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ make flash-only&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&amp;#8216;flash-only&amp;#8217; will flash your B2G build onto the phone. You&amp;#8217;ll lose everything on the phone, sorry.&lt;/p&gt;

&lt;p&gt;If your Nexus S was running a 2.3 based Android this should just work. If it was running ICS (as mine was) then you might get an error about unsupported baseband and/or bootloader versions. If you get this, I fixed it by editing &amp;#8216;glue/gonk/device/samsung/crespo/board-info.txt&amp;#8217; to add the versions that my bootloader and baseband had. My file looked like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;require board=herring
require version-bootloader=I9020XXJK1|I9020XXKA3|I9020XXKL1
require version-baseband=I9020XXJK8|I9020XXKB1|...|I9020UCKE1|I9020XXKI1&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note the addition of &amp;#8216;I9020XXKL1&amp;#8217; and &amp;#8216;I9020XXKI1&amp;#8217; to the bootloader and baseband lines respectively. After editing I redid &amp;#8220;make gonk&amp;#8221;, &amp;#8220;make&amp;#8221; and &amp;#8220;make flash-only&amp;#8221;.&lt;/p&gt;

&lt;p&gt;After &amp;#8216;flash-only&amp;#8217; your phone will reboot. If it boots into an error box saying &amp;#8220;no homescreen found&amp;#8221; then do the following while the USB cable is connected and that error is showing:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ make install-gaia
$ adb reboot&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will install the user files and reboot the phone. B2G should now be running on the device. Enjoy! I&amp;#8217;ve found calls, text messages, Wifi and Web Browsing works.&lt;/p&gt;

&lt;p&gt;&lt;img alt='Nexus S running B2G' src='/images/nexus_s_b2g_320.jpg' /&gt;&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Pattern Matching Against Linear Objects in ATS</title>
   <link href="http://bluishcoder.co.nz/2011/12/16/pattern-matching-against-linear-objects-in-ats.html"/>
   <updated>2011-12-16T18:00:00+13:00</updated>
   <id>http://bluishcoder.co.nz/2011/12/16/pattern-matching-against-linear-objects-in-ats</id>
   <content type="html">&lt;p&gt;In a project I&amp;#8217;m working on I&amp;#8217;m using linear lists. This is the &lt;code&gt;list_vt&lt;/code&gt; type in the &lt;a href='http://www.ats-lang.org'&gt;ATS&lt;/a&gt; prelude. &lt;code&gt;list_vt&lt;/code&gt; is similar to the list types in Lisp and functional programming languages except it is linear. The memory for the list is not managed by the garbage collector and the type system enforces the rule that only one reference to the linear object can exist. This sometimes requires a bit of extra effort when using pattern matching against the &lt;code&gt;list_vt&lt;/code&gt; instances.&lt;/p&gt;

&lt;h2 id='pattern_matching'&gt;Pattern Matching&lt;/h2&gt;

&lt;p&gt;When pattern matching against linear objects you can do a destructive match or a non-destructive match. The former will destroy and free the memory allocated for the object automatically. The latter will not. Destructive matches are done by having the pattern match clause prefixed with a &lt;code&gt;~&lt;/code&gt;. For example, the following will print an integer list and destroy the list while it does it:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;fun print_list (l: List_vt (int)): void =
  case+ l of
  | ~list_vt_nil () =&amp;gt; printf(&amp;quot;nil\n&amp;quot;, @())
  | ~list_vt_cons (x, xs) =&amp;gt; (printf(&amp;quot;cons %d\n&amp;quot;, @(x)); print_list(xs))

fun test1 (): void = {
  val a = list_vt_cons {int} (1, list_vt_nil)
  val () = print_list (a)
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Things get complicated when doing non-destructive matches. The following won&amp;#8217;t typecheck:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;fun print_list2 (l: !List_vt (int)): void =
  case+ l of
  | list_vt_nil () =&amp;gt; printf(&amp;quot;nil\n&amp;quot;, @())
  | list_vt_cons (x, xs) =&amp;gt; (printf(&amp;quot;cons %d\n&amp;quot;, @(x)); print_list(xs))

fun test2 (): void = {
  val a = list_vt_cons {int} (1, list_vt_nil)
  val () = print_list2 (a)
  val () = list_vt_free (a)
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The problem with this example is that when the match is made we are effectively taking the linear object out of the variable &lt;code&gt;l&lt;/code&gt;. This leaves &lt;code&gt;l&lt;/code&gt; with a different type, but we&amp;#8217;ve stated in the function signature for &lt;code&gt;print_list2&lt;/code&gt; that the type is not modified or consumed. We need a way of putting the linear object back into &lt;code&gt;l&lt;/code&gt; once we&amp;#8217;re done using the match. This primitive to do this is &lt;code&gt;fold@&lt;/code&gt; which I briefly introduced in my &lt;a href='/2011/02/27/linear-datatypes-in-ats.html'&gt;linear datatypes post&lt;/a&gt;. &lt;code&gt;fold@&lt;/code&gt; will change the type of &lt;code&gt;l&lt;/code&gt; back to the original and prevent access to the pattern match variables. Usage looks like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;fun print_list2 (l: !List_vt (int)): void =
  case+ l of
  | list_vt_nil () =&amp;gt; (fold@ l; printf(&amp;quot;nil\n&amp;quot;, @()))
  | list_vt_cons (x, !xs) =&amp;gt; (printf(&amp;quot;cons %d\n&amp;quot;, @(x)); print_list2(!xs); fold@ l)

fun test2 (): void = {
  val a = list_vt_cons {int} (1, list_vt_nil)
  val () = print_list2 (a)
  val () = list_vt_free (a)
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You&amp;#8217;ll notice with this version that the match for &lt;code&gt;list_vt_cons&lt;/code&gt; has changed the &lt;code&gt;xs&lt;/code&gt; parameter to be &lt;code&gt;!xs&lt;/code&gt;. The second argument in the cons constructor is a linear object. If the object itself is matched against &lt;code&gt;xs&lt;/code&gt; then it is another example of aliasing the linear object. It is taken out of the &lt;code&gt;l&lt;/code&gt; and needs to be put back. The way ATS handles this is to require pattern matching with a &lt;code&gt;!&lt;/code&gt; prefixed. This makes &lt;code&gt;xs&lt;/code&gt; be a pointer to the object rather than the object itself. So in this example &lt;code&gt;xs&lt;/code&gt; has the type &lt;code&gt;ptr addr&lt;/code&gt; where &lt;code&gt;addr&lt;/code&gt; is the address of the actual &lt;code&gt;List_vt&lt;/code&gt; object. This is why the &lt;code&gt;xs&lt;/code&gt; is prefixed by &lt;code&gt;!&lt;/code&gt; in the recursive call to &lt;code&gt;print_list2&lt;/code&gt;. The &lt;code&gt;!&lt;/code&gt; means dereference the pointer, so the &lt;code&gt;List_vt&lt;/code&gt; it is pointing to is passed as the argument to the recursive call.&lt;/p&gt;

&lt;p&gt;In this way the linear object is never taken out, we only access it via its pointer. The &lt;code&gt;fold@&lt;/code&gt; call in this clause will change &lt;code&gt;xs&lt;/code&gt; back to the &lt;code&gt;List_vt&lt;/code&gt; object. The &lt;code&gt;fold@&lt;/code&gt; call is done after the usage of &lt;code&gt;!xs&lt;/code&gt;. If it was done before then we wouldn&amp;#8217;t have access to the view for &lt;code&gt;xs&lt;/code&gt; to be able to derefence it. &lt;code&gt;print_list2&lt;/code&gt; is still tail recursive as the &lt;code&gt;fold@&lt;/code&gt; call is only used during typechecking and is erased afterwards.&lt;/p&gt;

&lt;h2 id='filtering_a_linear_list'&gt;Filtering a linear list&lt;/h2&gt;

&lt;p&gt;In my project I needed to filter a linear list. Unfortunately ATS doesn&amp;#8217;t have a filter implementation in the standard prelude for linear lists (it does for persistent lists). My first attempt at writing a &lt;code&gt;list_vt_filter&lt;/code&gt; looked like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;fun list_vt_filter (l: !List_vt (int), f: int -&amp;lt;&amp;gt; bool): List_vt (int) =
  case+ l of
  | list_vt_nil () =&amp;gt; (fold@ l; list_vt_nil)
  | list_vt_cons (x, !xs) when f (x) =&amp;gt; let
                                          val r = list_vt_cons (x, list_vt_filter (!xs, f))
                                        in
                                          fold@ l; r
                                        end
  | list_vt_cons (x, !xs) =&amp;gt; let
                                val r = list_vt_filter (!xs, f)
                              in
                                fold@ l; r
                              end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This should look familiar since it&amp;#8217;s very similar to the &lt;code&gt;print_list2&lt;/code&gt; code shown previously in the way it uses non-destructive matching and &lt;code&gt;fold@&lt;/code&gt;. The function &lt;code&gt;list_vt_filter&lt;/code&gt; takes a &lt;code&gt;list_vt&lt;/code&gt; as an argument and a function to apply to each element in the list. That function returns &lt;code&gt;true&lt;/code&gt; if the element should be included in the result list. Usage looks like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;val a  = list_vt_cons (1, list_vt_cons (2, list_vt_cons (3, list_vt_cons (4, list_vt_nil ()))))
val b  = list_vt_filter (a, lam (x) =&amp;gt; x mod 2 = 0)
val () = list_vt_foreach_fun&amp;lt;int&amp;gt; (a, lam(x) =&amp;lt;&amp;gt; $effmask_all (printf(&amp;quot;Value: %d\n&amp;quot;, @(x))))
val () = list_vt_free (b)
val () = list_vt_free (a)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;One issue with this implementation is it is not tail recursive. It has stack growth proportional to the size of the result list.&lt;/p&gt;

&lt;h2 id='tail_recursive_filtering'&gt;Tail Recursive Filtering&lt;/h2&gt;

&lt;p&gt;In Lisp code I&amp;#8217;d often build the result list tail recursively by passing an accumulator, with each new element in the result being prepended to the accumulator. This builds a list in the reverse order so before returning it the list would be reversed. The ATS code for this is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;fun list_vt_filter (l: !List_vt (int), f: int -&amp;lt;&amp;gt; bool): List_vt (int) = let
  fun loop (l: !List_vt (int), accum: List_vt (int)):&amp;lt;cloptr1&amp;gt; List_vt (int)  =
    case+ l of
    | list_vt_nil () =&amp;gt; (fold@ l; accum)
    | list_vt_cons (x, !xs) when f (x) =&amp;gt; let
                                            val r = loop (!xs, list_vt_cons (x, accum))
                                          in
                                            (fold@ l; r)
                                          end
    | list_vt_cons (x, !xs) =&amp;gt; let
                                 val r = loop (!xs, accum)
                                in
                                  (fold@ l; r)
                                end
in
  list_vt_reverse (loop (l, list_vt_nil))
end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;cloptr1&lt;/code&gt; function annotation marks the inner function as being a closure where the memory for the closure&amp;#8217;s environment is managed by the compiler using malloc and free instead of the garbage collector (which is what &lt;code&gt;cloref1&lt;/code&gt; would signify). See my post on &lt;a href='/2010/06/20/closures-in-ats.html'&gt;closures in ATS&lt;/a&gt; for more about the different closure and function types used by ATS.&lt;/p&gt;

&lt;p&gt;Unfortunately the requirement to use &lt;code&gt;fold@&lt;/code&gt; after we&amp;#8217;ve finished with using the pattern matched variables makes the code slightly more verbose as we need to do the tail recursion, obtaining the result, then do the &lt;code&gt;fold@&lt;/code&gt; and return the result. Remember that the &lt;code&gt;fold@&lt;/code&gt; is erased at type checking type which is how this code remains tail recursive even though the code structure makes it look like it isn&amp;#8217;t.&lt;/p&gt;

&lt;p&gt;One downside to this approach is we iterate over the list twice. Once to build the result, and once over the result to reverse it.&lt;/p&gt;

&lt;h2 id='single_pass_tail_recursive_filtering'&gt;Single Pass Tail Recursive Filtering&lt;/h2&gt;

&lt;p&gt;The creation of the result list can be done in a single pass if we could create a &lt;code&gt;cons&lt;/code&gt; with no second argument, and fill in that argument later when we have a result to store there that passes filtering. ATS allows construction of datatypes with a &amp;#8216;hole&amp;#8217; that can be filled in later. The &amp;#8216;hole&amp;#8217; is an unintialized type and we get a pointer to it. An example of doing this is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var x = list_vt_cons {int} {0} (1, ?)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This creates a &lt;code&gt;list_vt_cons&lt;/code&gt; with the data set to &lt;code&gt;1&lt;/code&gt; but no second parameter. Instead of that parameter being of type &lt;code&gt;List_vt (int)&lt;/code&gt; it is of type &lt;code&gt;List_vt (int)?&lt;/code&gt;, the &lt;code&gt;?&lt;/code&gt; signifying it is uninitialized. For this example we have to pass the universal type parameters explicitly (the &lt;code&gt;{int} {0}&lt;/code&gt;) as the ATS type inference algorithm can&amp;#8217;t compute them.&lt;/p&gt;

&lt;p&gt;To get a pointer to the &amp;#8216;hole&amp;#8217; we have to pattern match:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;val+ list_vt_cons (_, !xs) = x
val () = !xs := list_vt_nil
val () = fold@ x&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In this example the &lt;code&gt;xs&lt;/code&gt; is a pointer, pointing to the &lt;code&gt;List_vt (int)?&lt;/code&gt;. It assigns a &lt;code&gt;list_vt_nil&lt;/code&gt; to this, making the tail of the &lt;code&gt;cons&lt;/code&gt; a &lt;code&gt;list_vt_nil&lt;/code&gt;. Just like in our previous pattern matching examples using &lt;code&gt;case&lt;/code&gt;, the code has to do a &lt;code&gt;fold@&lt;/code&gt; to change the type of &lt;code&gt;x&lt;/code&gt; back to that containing a linear object once we&amp;#8217;ve finished using &lt;code&gt;xs&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now that we can get pointers to the tail of the list we can implement a single pass tail recursive filter function:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;fun list_vt_filter (l: !List_vt (int), f: int -&amp;lt;&amp;gt; bool): List_vt (int) = let
  fun loop (l: !List_vt (int), res: &amp;amp;List_vt (int)? &amp;gt;&amp;gt; List_vt (int)):&amp;lt;cloptr1&amp;gt; void =
    case+ l of
    | list_vt_nil () =&amp;gt; (fold@ l; (res := list_vt_nil))
    | list_vt_cons (x, !xs) when f (x) =&amp;gt; let
                                            val () = res := list_vt_cons {int} {0} (x, ?)
                                            val+ list_vt_cons (_, !p_xs) = res
                                           in
                                             loop (!xs, !p_xs); fold@ l; fold@ res
                                           end
    | list_vt_cons  (x, !xs) =&amp;gt; (loop (!xs, res); fold@ l)

  var res: List_vt (int)?
  val () = loop (l, res)
in
  res
end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;loop&lt;/code&gt; function here no longer returns a result. Instead the result is passed via a reference (the &lt;code&gt;&amp;amp;&lt;/code&gt; signifies &amp;#8216;by reference&amp;#8217;). When there is something that needs to be stored in the list, a &lt;code&gt;cons&lt;/code&gt; is created with a hole in the tail position. This &lt;code&gt;cons&lt;/code&gt; is stored in the result we are passing by reference and we tail recursively call with the hole as the new result. ATS converts this to nice C code that is a simple loop rather than recursive function calls.&lt;/p&gt;

&lt;h2 id='miscellaneous'&gt;Miscellaneous&lt;/h2&gt;

&lt;p&gt;The code examples in this post use &lt;code&gt;List_vt (a)&lt;/code&gt;. This is actually a typedef for &lt;code&gt;list_vt (a,n)&lt;/code&gt; where &lt;code&gt;a&lt;/code&gt; is the type and &lt;code&gt;n&lt;/code&gt; is the length of the list. The typedef allows shorter examples without needing to specify the sorts for the list length. Using the full type though has the advantage of being able to specifiy a bit more type safety. For example, the original filter function would be declared as:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;fun list_vt_filter {n:nat} (l: !list_vt (int,n), f: int -&amp;lt;&amp;gt; bool): [r:nat | r &amp;lt;= n] list_vt (int, r)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This defines the type of the result as having a length equal to or less than that of the original list. This helps prevent errors in the implementation of the filter - it can&amp;#8217;t accidentally leave extra items in the list. I cover this type of thing in my post on &lt;a href='/2010/09/01/dependent-types-in-ats.html'&gt;dependent types&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Another addition to safety that adding the extra sorts can provide is the ability to check that the function terminates. This can be done by adding a termination metric to the function definition:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;fun list_vt_filter {n:nat} .&amp;lt;n&amp;gt;. (l: !list_vt (int,n), f: int -&amp;lt;&amp;gt; bool): [r:nat | r &amp;lt;= n] list_vt (int, r)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The compiler checks that &lt;code&gt;n&lt;/code&gt; is decreasing on each recursive call. If this fails to happen the recursive calls may not terminate and it becomes a compile error. This is discussed in the &lt;a href='http://www.ats-lang.org/DOCUMENT/INTPROGINATS/HTML/x2390.html'&gt;Termination-Checking for Recursive Functions&lt;/a&gt; section of the &lt;a href='http://www.ats-lang.org/DOCUMENT/INTPROGINATS/HTML/book1.html'&gt;Introduction to Programming in ATS book&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;A description of how &lt;code&gt;fold@&lt;/code&gt; works is in the &lt;a href='http://www.ats-lang.org/DOCUMENTATION/MISC/manual_main.pdf'&gt;ATS/Anairats User&amp;#8217;s Guide PDF&lt;/a&gt;. It&amp;#8217;s in the &amp;#8216;Dataviewtypes&amp;#8217; section of the &amp;#8216;Programming with Linear Types&amp;#8217; chapter and is referred to as folding and unfolding a linear type.&lt;/p&gt;

&lt;p&gt;It&amp;#8217;s the usage of linear types and dealing with their restrictions that makes my examples a bit more complex. If you use ATS mainly with non-linear types and link with the garbage collector then it becomes very much like using any other functional programming language, but with additional features in the type system. My interest has been around avoiding using a garbage collector and having the compiler give errors when memory is not allocated or free&amp;#8217;d correctly. Don&amp;#8217;t be put off from using ATS by these complex examples if you&amp;#8217;re fine with using garbage collection and non-linear datatypes. You might never need to deal with the cases that bring in the extra complexity.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Using Records and C structs in ATS</title>
   <link href="http://bluishcoder.co.nz/2011/11/01/using-records-and-c-structs-in-ats.html"/>
   <updated>2011-11-01T18:00:00+13:00</updated>
   <id>http://bluishcoder.co.nz/2011/11/01/using-records-and-c-structs-in-ats</id>
   <content type="html">&lt;p&gt;&lt;a href='http://www.ats-lang.org'&gt;ATS&lt;/a&gt; has record types which are like tuples but each item is referenced via a name. They closely approximate C structs and in the generated C code are represented in this way. The following uses a record to hold &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; values representing a &lt;code&gt;point&lt;/code&gt; on a 2D plane:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;fun print_point (p: @{x= int, y= int}): void =
  printf(&amp;quot;%d@%d\n&amp;quot;, @(p.x, p.y))

implement main () = {
  val p1 = @{x=10, y=20}
  val () = print_point (p1)
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;A literal record object in this example is created using the &lt;code&gt;@{ ... }&lt;/code&gt; syntax. Dereferencing record fields is done using &amp;#8217;&lt;code&gt;.&lt;/code&gt;&amp;#8217;. The generated C code shows that the representation for the record is a C struct:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;typedef struct {
  ats_int_type atslab_x ;
  ats_int_type atslab_y ;
} anairiats_rec_0 ;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;@&lt;/code&gt; syntax used for the record literal and type marks the record as a &amp;#8216;flat&amp;#8217; record. Flat records have value semantics and variables of this type have a size in bytes equivalent to the size of the underlying C structure. This is shown by the generated C code for the &lt;code&gt;main&lt;/code&gt; function above:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;ATSlocal (anairiats_rec_0, tmp4) ;

__ats_lab_mainats:
tmp4.atslab_x = 10 ;
tmp4.atslab_y = 20 ;

/* tmp3 = */ print_point_0 (tmp4) ;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here the variable &lt;code&gt;tmp4&lt;/code&gt; is the &lt;code&gt;p1&lt;/code&gt; in our ATS code. It is an instance of the C struct representing the record and is created on the stack. It is initialized and passed to the &lt;code&gt;print_point&lt;/code&gt; function by value:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;ats_void_type
print_point_0 (anairiats_rec_0 arg0) {
  ATSlocal (ats_int_type, tmp1) ;
  ATSlocal (ats_int_type, tmp2) ;

  ...
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Records can also be defined using a &lt;code&gt;&amp;#39;{...}&lt;/code&gt; syntax (note the &lt;code&gt;&amp;#39;&lt;/code&gt; instead of &lt;code&gt;@&lt;/code&gt;) for boxed records which are heap allocated and the memory managed by the garbage collector. Boxed records have pointer semantics and have a size in bytes equivalent to the size of a pointer:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;implement main () = {
  val x = sizeof&amp;lt;@{x=int}&amp;gt;
  val y = sizeof&amp;lt;&amp;#39;{x=int}&amp;gt;
  val a = int_of_size x
  val b = int_of_size y
  val () = printf (&amp;quot;%d %d\n&amp;quot;, @(a, b))
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This outputs &amp;#8220;4 8&amp;#8221; showing the flat type as having the size of an &lt;code&gt;int&lt;/code&gt; and the boxed type having the size of a pointer. Most usage of records in ATS I&amp;#8217;ve done is using flat records as I tend to avoid the use of the garbage collector.&lt;/p&gt;

&lt;p&gt;To pass a reference to a flat record so it can be modified by a function you need to mark the function argument as &amp;#8216;by reference&amp;#8217; using &lt;code&gt;&amp;amp;&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;typedef point = @{x= int, y= int}

fun print_point (p: point): void =
  printf(&amp;quot;%d@%d\n&amp;quot;, @(p.x, p.y))

fun add1 (p: &amp;amp;point): void = {
  val () = p.x := p.x + 1
  val () = p.y := p.y + 1
}

implement main () = {
  var p1 = @{x=10, y=20}
  val () = add1 (p1)
  val () = print_point (p1)
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In this example I use &lt;code&gt;typedef&lt;/code&gt; to create a type alias for the record so I can refer to the type as &lt;code&gt;point&lt;/code&gt;. The &lt;code&gt;add1&lt;/code&gt; function takes a &lt;code&gt;point&lt;/code&gt; by reference (as indicated by the &lt;code&gt;&amp;amp;&lt;/code&gt; prefix). This works like C++ reference arguments. The function effectively takes a pointer to an instance of the struct and can modify the instance passed to the function. For this to work the &lt;code&gt;point&lt;/code&gt; passed to the function must be an &lt;code&gt;lvalue&lt;/code&gt;. That is, it must be mutable. This is done in ATS by making it a &lt;code&gt;var&lt;/code&gt; vs a &lt;code&gt;val&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Note that it&amp;#8217;s a type error to create an uninitialized &lt;code&gt;point&lt;/code&gt; object and pass it to &lt;code&gt;add1&lt;/code&gt;. For example, the following gives a type check error:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;implement main () = {
  var p1: point?
  val () = add1 (p1)
  val () = print_point (p1)
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Types that are unintialized have a &lt;code&gt;?&lt;/code&gt; suffix added to it. The type of &lt;code&gt;p1&lt;/code&gt;, due to it being unintialized, is &lt;code&gt;point?&lt;/code&gt;. Since &lt;code&gt;add1&lt;/code&gt; takes a &lt;code&gt;point&lt;/code&gt; this fails type checking. Initializing it allows it to pass:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;implement main () = {
  var p1: point
  val () = p1.x := 5
  val () = p1.y := 10
  val () = add1 (p1)
  val () = print_point (p1)
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As well as passing by reference to functions you can pass a pointer and deal with the pointer management directly. This requires using the proof system and I hope to go through dealing with pointers and their associated proofs in a later post:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;fun add1 {l:agz} (pf: !point @ l | p: ptr l): void = {
  val () = p-&amp;gt;x := p-&amp;gt;x + 1
  val () = p-&amp;gt;y := p-&amp;gt;y + 1
}

implement main () = {
  var p1 = @{x=10, y=20}
  val () = add1 (view@ p1 | &amp;amp;p1)
  val () = print_point (p1)
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;When interfacing with C API&amp;#8217;s you often have to deal with C structures. In ATS you can declare a type as being defined as a struct in C with a matching ATS record definition so that ATS can be used to access the struct. The following example shows a struct declared in C, a function that uses it in C, and how this is wrapped in ATS:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;%{^
typedef struct Point {
  int x;
  int y;
} Point;

void print_point (Point* p) {
  printf(&amp;quot;%d@%d\n&amp;quot;, p-&amp;gt;x, p-&amp;gt;y);
}
%}

typedef point = $extype_struct &amp;quot;Point&amp;quot; of {x= int, y= int}
extern fun print_point (p: &amp;amp;point): void = &amp;quot;mac#print_point&amp;quot;

implement main () = {
  var p1: point
  val () = p1.x := 10;
  val () = p1.y := 20;
  val () = print_point (p1)
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;$extype_struct&lt;/code&gt; keyword creates a type that is represented by a C struct with the given name. By using the &lt;code&gt;of {x= int, y= int}&lt;/code&gt; suffix we define the record layout as seen via ATS. This will stop ATS from creating its own structure to map the type and instead uses the C structure. The generated C code for the &lt;code&gt;main&lt;/code&gt; function looks like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;ATSlocal (Point, tmp1) ;

__ats_lab_mainats:
/* Point tmp1 ; */
ats_select_mac(tmp1, x) = 10 ;
ats_select_mac(tmp1, y) = 20 ;
print_point ((&amp;amp;tmp1)) ;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note it uses the C struct type directly. I use this approach in my &lt;a href='https://github.com/doublec/ats-libzmq'&gt;0MQ&lt;/a&gt; wrapper to wrap the &lt;code&gt;zmq_msg_t&lt;/code&gt; and &lt;code&gt;zmq_pollitem_t&lt;/code&gt; structures.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Performing Compatible Updates of Mozilla Central Git Forks</title>
   <link href="http://bluishcoder.co.nz/2011/10/28/performing-compatible-updates-of-mozilla-central-git-forks.html"/>
   <updated>2011-10-28T16:00:00+13:00</updated>
   <id>http://bluishcoder.co.nz/2011/10/28/performing-compatible-updates-of-mozilla-central-git-forks</id>
   <content type="html">&lt;p&gt;Earlier this year I posted about &lt;a href='/2011/02/10/git-conversion-of-mozilla-central.html'&gt;how I created my git mirror&lt;/a&gt; of the mozilla-central mercurial repository. I&amp;#8217;ve been keeping a &lt;a href='https://github.com/doublec/mozilla-central'&gt;fork on github&lt;/a&gt; updated regularly since then that a number of people have started using.&lt;/p&gt;

&lt;p&gt;Users of my mirror have wanted to be able to keep up to date with the mercurial mozilla-central repository themselves, or add updates from other branches of the Mozilla source. It takes a large amount of time to start a conversion from scratch but it&amp;#8217;s possible to start from the existing git mirror, perform the incremental updates from mercurial yourself, and still stay compatible with the incremental updates I push to my github mirror.&lt;/p&gt;

&lt;p&gt;&lt;a href='http://hg-git.github.com/'&gt;hg-git&lt;/a&gt; uses a &lt;code&gt;git-mapfile&lt;/code&gt; in the &lt;code&gt;.hg&lt;/code&gt; directory of the mercurial clone to keep track of the mapping between mercurial and git commits. By using the same &lt;code&gt;git-mapfile&lt;/code&gt; that I use for my mirror you can start your own incremental update from the latest data in the &lt;code&gt;git-mapfile&lt;/code&gt; instead of from the beginning. I keep an up-to-date &lt;code&gt;git-mapfile&lt;/code&gt; in &lt;a href='http://www.bluishcoder.co.nz/git-mapfile.bz2'&gt;git-mapfile.bz2&lt;/a&gt;. It&amp;#8217;s compressed with &lt;code&gt;bzip2&lt;/code&gt; as the uncompressed version is quite large.&lt;/p&gt;

&lt;p&gt;The steps to start your own update process are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Clone the &lt;a href='https://hg.mozilla.org/mozilla-central'&gt;mercurial mozilla-central repository&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;Clone my &lt;a href='https://github.com/doublec/mozilla-central'&gt;git mirror&lt;/a&gt; as a bare repository in &lt;code&gt;.hg/git&lt;/code&gt;&lt;/li&gt;

&lt;li&gt;Place the &lt;code&gt;git-mapfile&lt;/code&gt; in &lt;code&gt;.hg&lt;/code&gt;&lt;/li&gt;

&lt;li&gt;Do an &lt;code&gt;hg bookmark -f -r default master&lt;/code&gt; to mark the commit to convert to&lt;/li&gt;

&lt;li&gt;Perform &lt;code&gt;hg gexport&lt;/code&gt; to update &lt;code&gt;.hg/git&lt;/code&gt; with recent mercurial commits&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The &lt;code&gt;.hg/git&lt;/code&gt; directory should now be up-to-date with respect to the mercurial repository. And the additional git commits will have the same SHA id as any commits I push into my mirror when I perform my own update.&lt;/p&gt;

&lt;p&gt;The steps to do an incremental update are the normal:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Pull from the mercurial mozilla-central repository&lt;/li&gt;

&lt;li&gt;Run &lt;code&gt;hg bookmark -f -r default master&lt;/code&gt;&lt;/li&gt;

&lt;li&gt;Run &lt;code&gt;hg gexport&lt;/code&gt;&lt;/li&gt;

&lt;li&gt;Push or pull to/from &lt;code&gt;.hg/git&lt;/code&gt; as needed&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;As a working example, the following shell commands on a Linux system should set up your own repository ready for incremental updating:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ hg clone https://hg.mozilla.org/mozilla-central
$ cd mozilla-central/.hg
$ git clone --bare git://github.com/doublec/mozilla-central.git git
$ wget http://www.bluishcoder.co.nz/git-mapfile.bz2
$ bunzip2 git-mapfile.bz2
$ cd ..
$ hg bookmark -f -r default master
$ hg gexport
$ cd .hg/git
$ git push --all ~/some/other/repo.git&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Incremental updates just repeat the last few commands above:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ hg pull -u
$ hg bookmark -f -r default master
$ hg gexport
$ cd .hg/git
$ git push --all ~/some/other/repo.git&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that I use &lt;code&gt;hg gexport&lt;/code&gt; and push from the &lt;code&gt;.hg/git&lt;/code&gt; repository using the &lt;code&gt;git&lt;/code&gt; command instead of doing an &lt;code&gt;hg push&lt;/code&gt; and relying on &lt;code&gt;hg-git&lt;/code&gt; to do the conversion and push. In my &lt;a href='http://www.bluishcoder.co.nz/2011/02/10/git-conversion-of-mozilla-central.html'&gt;original article&lt;/a&gt; I did the latter. The &lt;code&gt;hg-git push&lt;/code&gt; method uses slower python based routines to do the push which can take a long time and large amounts of memory on big repositories like mozilla-central. By splitting this up into &lt;code&gt;gexport&lt;/code&gt; and using the native &lt;code&gt;git&lt;/code&gt; command I save a lot of time and memory.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Building Rust</title>
   <link href="http://bluishcoder.co.nz/2011/10/24/building-rust.html"/>
   <updated>2011-10-24T18:00:00+13:00</updated>
   <id>http://bluishcoder.co.nz/2011/10/24/building-rust</id>
   <content type="html">&lt;p&gt;Update 2011-11-08 - The steps to build Rust have changed since I wrote this post, there&amp;#8217;s now no need to build LLVM - it&amp;#8217;s included as a submodule in the Rust git repository and will automatically be cloned and built.&lt;/p&gt;

&lt;p&gt;A while ago I wrote a &lt;a href='/2011/03/31/a-quick-look-at-the-rust-programming-language.html'&gt;quick look at Rust&lt;/a&gt; post, describing how to build it and run some simple examples. Since that post the bootstrap compiler has gone away and the &lt;code&gt;rustc&lt;/code&gt; compiler, written in Rust, is the compiler to use.&lt;/p&gt;

&lt;p&gt;The instructions for &lt;a href='https://github.com/graydon/rust/wiki/Getting-started'&gt;building Rust in the wiki&lt;/a&gt; are good but I&amp;#8217;ll briefly go through how I installed things on 64-bit Linux. The first step is to build the required version of &lt;code&gt;LLVM&lt;/code&gt; from the LLVM source repository. I use a git mirror and install to a local directory so as not to clash with other programs that use older &lt;code&gt;LLVM&lt;/code&gt; versions.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ git clone git://github.com/earl/llvm-mirror.git
$ cd llvm-mirror
$ git reset --hard 9af578
$ CXX=&amp;#39;g++ -m32&amp;#39; CC=&amp;#39;gcc -m32&amp;#39; CFLAGS=-m32 CXXFLAGS=-m32 LDFLAGS=-m32 ./configure \
    --{build,host,target}=i686-unknown-linux-gnu \
    --enable-targets=x86,x86_64,cbe --enable-optimized --prefix=/home/chris/rust
$ make &amp;amp;&amp;amp; make install&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note the use of &lt;code&gt;prefix&lt;/code&gt; to ensure this custom &lt;code&gt;LLVM&lt;/code&gt; build is a local install. I also do a &lt;code&gt;git reset&lt;/code&gt; to go to the commit for SVN revision 142082 which is the current version that works with Rust according to the &lt;a href='https://github.com/graydon/rust/wiki/Getting-started'&gt;wiki&lt;/a&gt;. After installation add the install &lt;code&gt;bin&lt;/code&gt; to the &lt;code&gt;PATH&lt;/code&gt; and &lt;code&gt;lib&lt;/code&gt; to &lt;code&gt;LD_LIBRARY_PATH&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ export PATH=~/rust/bin:$PATH
$ export LD_LIBRARY_PATH=~/rust/lib:$LD_LIBRARY_PATH&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Next step, clone and build Rust:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ git clone git://github.com/graydon/rust
$ mkdir build
$ cd build
$ ../rust/configure
$ make&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The build process downloads an existing build for your platform to bootstrap from. It uses this to build a &lt;code&gt;stage1&lt;/code&gt; version of the compiler. This &lt;code&gt;stage1&lt;/code&gt; is used to build a &lt;code&gt;stage2&lt;/code&gt;, and that is then used to build a &lt;code&gt;stage3&lt;/code&gt;. All the built compilers should work exactly the same if things are working correctly. The compiler can be run within the &lt;code&gt;build&lt;/code&gt; directory, or outside it if you put the &lt;code&gt;stage3/bin&lt;/code&gt; directory in the path:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ export PATH=~/path/to/build/stage3/bin:$PATH
$ rustc
error: No input filename given.&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Test with a simple &amp;#8216;hello world&amp;#8217; program:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ cat hello.rs
use std;
import std::io::print;

fn main () {
  print (&amp;quot;hello\n&amp;quot;);
}
$ rustc hello.rs
$ ./hello
hello&lt;/code&gt;&lt;/pre&gt;</content>
 </entry>
 
 <entry>
   <title>Overloading Functions in ATS</title>
   <link href="http://bluishcoder.co.nz/2011/10/19/overloading-functions-in-ats.html"/>
   <updated>2011-10-19T15:00:00+13:00</updated>
   <id>http://bluishcoder.co.nz/2011/10/19/overloading-functions-in-ats</id>
   <content type="html">&lt;p&gt;&lt;a href='http://www.ats-lang.org'&gt;ATS&lt;/a&gt; allows overloading of functions where the function that is called is selected based on number of arguments and the type of the arguments. The following example shows overloading based on type to provide a generic print function:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;symintr myprint
fun myprint_integer (a: int)    = printf(&amp;quot;%d\n&amp;quot;, @(a))
fun myprint_double  (a: double) = printf(&amp;quot;%f\n&amp;quot;, @(a))
fun myprint_string  (a: string) = printf(&amp;quot;%s\n&amp;quot;, @(a))

overload myprint with myprint_integer
overload myprint with myprint_double
overload myprint with myprint_string

implement main() = {
  val () = myprint (&amp;quot;hello&amp;quot;)
  val () = myprint (10)
  val () = myprint (20.0)
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The keyword &lt;code&gt;symintr&lt;/code&gt; introduces a symbol that can be overloaded. The keyword &lt;code&gt;overload&lt;/code&gt; will overload that symbol with an existing function. In this example we overload with three functions that take different types. The overload resolution is performed at compile time. The actual C code generated for the main function includes:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;/* tmp4 = */ myprint_string_2 (ATSstrcst(&amp;quot;hello&amp;quot;)) ;
/* tmp5 = */ myprint_integer_0 (10) ;
/* tmp3 = */ myprint_double_1 (20.0) ;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The ATS standard prelude includes a &lt;code&gt;print&lt;/code&gt; function that is overloaded in this manner for most of the standard types. One downside to the way overloading works is the overload resolution sometimes fails in template functions. The following code gives a compile error for example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;fun {a:t@ype} printme (x: a) = print(x)

implement main() = {
  val () = printme (10)
  val () = printme (20.0)
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The error given is that the symbol &lt;code&gt;print&lt;/code&gt; cannot be resolved. The ATS compiler attempts to resolve the overload by looking up the &lt;code&gt;t@ype&lt;/code&gt; sort. There is no overload for this so the resolution fails. This can be worked around using a template function to call the overloaded function and partially specialize the implementation of the new template function. The following code demonstrates this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;extern fun {a:t@ype} gprint (x: a):void
implement gprint&amp;lt;int&amp;gt; (x) = print_int(x)
implement gprint&amp;lt;double&amp;gt; (x) = print_double(x)

fun {a:t@ype} printme (x: a):void = gprint&amp;lt;a&amp;gt;(x)

implement main() = {
  val () = printme (10)
  val () = printme (20.0)
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;print&lt;/code&gt; symbol can be overloaded with the new &lt;code&gt;gprint&lt;/code&gt; function to allow &lt;code&gt;print&lt;/code&gt; to be called over &lt;code&gt;t@ype&lt;/code&gt; sorts:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;extern fun {a:t@ype} gprint (x: a):void
implement gprint&amp;lt;int&amp;gt; (x) = print_int(x)
implement gprint&amp;lt;double&amp;gt; (x) = print_double(x)

overload print with gprint

fun {a:t@ype} printme (x: a) = print(x)

implement main() = {
  val () = printme (10)
  val () = printme (20.0)
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This example is contrived in that you could just specialize &lt;code&gt;printme&lt;/code&gt; but in real world code this issue comes up occasionally. The most common example for me has been using &lt;code&gt;=&lt;/code&gt; in a template function, comparing arguments that are template type parameters. &lt;code&gt;=&lt;/code&gt; is an overloaded function and the overload lookup fails in the same manner as above. A workaround is to create an &lt;code&gt;equals&lt;/code&gt; template function specialized over the types you plan to compare as above.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Temporal Media Fragment Support in Firefox</title>
   <link href="http://bluishcoder.co.nz/2011/08/08/temporal-media-fragment-support-in-firefox.html"/>
   <updated>2011-08-08T12:00:00+12:00</updated>
   <id>http://bluishcoder.co.nz/2011/08/08/temporal-media-fragment-support-in-firefox</id>
   <content type="html">&lt;p&gt;The W3C has a &lt;a href='http://www.w3.org/2008/WebVideo/Fragments/'&gt;Media Fragments Working Group&lt;/a&gt; whose &lt;a href='http://www.w3.org/2008/01/media-fragments-wg.html'&gt;mission&lt;/a&gt; is to specify temporal and media fragments in the Web using URI&amp;#8217;s. The &lt;a href='http://www.w3.org/TR/media-frags'&gt;draft specification&lt;/a&gt; goes through in detail how these fragments work. I recently became a member of the working group and I&amp;#8217;ve been working on adding support for the &lt;a href='http://www.w3.org/TR/media-frags/#naming-time'&gt;temporal dimension portion&lt;/a&gt; of the specification to Firefox.&lt;/p&gt;

&lt;p&gt;In the most basic form you can specify a start time and an end time in the fragment part of a URI in an HTML video or audio element. For example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&amp;lt;video src=&amp;#39;/mediafrag/AudioAPI.webm#t=50,100&amp;#39;&amp;gt;&amp;lt;/video&amp;gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &amp;#8216;#t&amp;#8217; portion of the URI identifies the fragment as being a temporal media fragment. In this example &amp;#8216;50,100&amp;#8217; means start the video at a current time of 50 seconds, and stop playing at 100 seconds. There are various other formats for the temporal media fragment &lt;a href='http://www.w3.org/TR/media-frags/#naming-time'&gt;defined in the specification&lt;/a&gt;. Examples can be seen in the &lt;a href='http://www.w3.org/2008/WebVideo/Fragments/TC/ua-test-cases.html'&gt;UA Test Cases&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Development of this feature is being done in &lt;a href='https://bugzilla.mozilla.org/show_bug.cgi?id=648595'&gt;bug 648595&lt;/a&gt;. I&amp;#8217;ve done test builds of the first iteration of the patch and they are available at my &lt;a href='http://www.bluishcoder.co.nz/mediafrag/'&gt;Firefox media fragment test builds page&lt;/a&gt;. The page has builds, an example, and a list of limitations which are currently:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Temporal syntax only. This means no spatial or track dimensions.&lt;/li&gt;

&lt;li&gt;NPT time support only. No SMPTE time codes or Wall-clock time codes.&lt;/li&gt;

&lt;li&gt;When changing the media fragment portion of a URL the media is not immediately updated. You need to refresh the page to see the change. This is most noticeable when directly navigating to the video and adding or changing a fragment.&lt;/li&gt;

&lt;li&gt;The user interface for identifying the fragment in the standard controls is ugly and needs polish.&lt;/li&gt;

&lt;li&gt;The HTML standard includes an &amp;#8216;initialTime&amp;#8217; attribute for obtaining the start time. There is no way to obtain an end time so I&amp;#8217;ve exposed a &amp;#8216;mozFragmentEnd&amp;#8217; attribute on the video DOM object.&lt;/li&gt;
&lt;/ul&gt;</content>
 </entry>
 
 <entry>
   <title>Safe destruction in the presence of sharing in ATS</title>
   <link href="http://bluishcoder.co.nz/2011/08/08/safe-destruction-in-the-presence-of-sharing-in-ats.html"/>
   <updated>2011-08-08T11:00:00+12:00</updated>
   <id>http://bluishcoder.co.nz/2011/08/08/safe-destruction-in-the-presence-of-sharing-in-ats</id>
   <content type="html">&lt;p&gt;While using the &lt;a href='http://monkey.org/~provos/libevent/'&gt;libevent API&lt;/a&gt; from &lt;a href='http://www.ats-lang.org/'&gt;ATS&lt;/a&gt; I came across a scenario where it was important to call a function to release objects in a particular order. I wanted to have ATS enforce at compile time that the destruction occurs safely in the right order. The following example uses the built in libevent HTTP API for creating simple web servers. It uses the &lt;a href='/2011/04/25/sharing-linear-resources-in-ats.html'&gt;ATS libevent wrapper&lt;/a&gt; I wrote about previously.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;fn cb {l1,l2:agz} (request: !evhttp_request l1, arg: !event_base l2): void = let
  val () = printf(&amp;quot;here\n&amp;quot;, @())
in
 ()
end

fn server () = let
  val _ = signal (SIGPIPE, SIG_IGN)
  val base = event_base_new ()
  val () = assert_errmsg (~base, &amp;quot;event_base_new failed&amp;quot;)

  val http = evhttp_new (base)
  val () = assert_errmsg (~http, &amp;quot;evhttp_new failed&amp;quot;)

  val r = evhttp_set_cb_with_base (http, &amp;quot;/&amp;quot;, cb, base)
  val () = assert_errmsg (r = 0, &amp;quot;evhttp_set_cb_with_base failed&amp;quot;)

  val r = evhttp_bind_socket (http, &amp;quot;0.0.0.0&amp;quot;, uint16_of_int(8080))
  val () = assert_errmsg (r = 0, &amp;quot;evhttp_bind_socket failed&amp;quot;)

  val r = event_base_dispatch (base)
  val () = assert_errmsg (r &amp;gt;= 0, &amp;quot;event_base_dispatch failed&amp;quot;)

  val () = evhttp_free (http)
  val () = event_base_free (base)
in
  ()
end

implement main () = server ()&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The call to &lt;code&gt;http_new&lt;/code&gt; requires an &lt;code&gt;event_base&lt;/code&gt; object. Internally, inside the C API, the returned &lt;code&gt;evhttp&lt;/code&gt; object holds a pointer to this &lt;code&gt;event_base&lt;/code&gt;. This results in the pointer being shared in two places and requires careful destruction to prevent using a destroyed object.&lt;/p&gt;

&lt;p&gt;Later I call &lt;code&gt;evhttp_free&lt;/code&gt; to destroy and release resources associated with the &lt;code&gt;evhttp&lt;/code&gt; object, followed by &lt;code&gt;event_base_free&lt;/code&gt; to do the same with the &lt;code&gt;event_base&lt;/code&gt; object. I do it in this order to prevent a dangling reference to the &lt;code&gt;event_base&lt;/code&gt; inside the &lt;code&gt;evhttp&lt;/code&gt; object. The &lt;code&gt;evhttp&lt;/code&gt; object is associated with that particular &lt;code&gt;event_base&lt;/code&gt; and it&amp;#8217;s important not to pass an incorrect base to functions that use the http object. Ideally code like the following shouldn&amp;#8217;t compile:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;val base = event_base_new ()
val base2 = event_base_new ()
val http = evhttp_new (base)

// Wrong event_base passed, fail compilation
val r = evhttp_set_cb_with_base (http, &amp;quot;/&amp;quot;, cb, base2)

// Destruction out of order, fail compilation
val () = event_base_free (base)
val () = evhttp_free (http)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To model this in ATS I changed the ATS definition of &lt;code&gt;evhttp&lt;/code&gt; to have an &lt;code&gt;event_base&lt;/code&gt; associated with it at the type level and modified &lt;code&gt;evhttp_new&lt;/code&gt; to return this relationship (&lt;code&gt;agz&lt;/code&gt; is an address that cannot be NULL, &lt;code&gt;agez&lt;/code&gt; is an address that can be NULL):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;absviewtype evhttp (l:addr, l2:addr)
fun http_new {l1:agz} (base: !event_base l1): [l2:agez] evhttp (l2,l1) = &amp;quot;mac#evhttp_new&amp;quot;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;With this change the type for &lt;code&gt;evhttp&lt;/code&gt; depends on the value of the pointer to the &lt;code&gt;event_base&lt;/code&gt; given as an argument. Now we can use this in the definition of &lt;code&gt;evhttp_set_cb_with_base&lt;/code&gt; to ensure that the correct &lt;code&gt;event_base&lt;/code&gt; object is used:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;fun evhttp_set_cb_with_base {l1,l2:agz}
       (http: !evhttp (l1, l2),
        path: string,
        callback: {l3:agz} (!evhttp_request l3, !event_base l2) -&amp;gt; void,
        arg: !event_base l2): int = &amp;quot;mac#evhttp_set_cb&amp;quot;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This definition states that the &lt;code&gt;event_base&lt;/code&gt; given as the &lt;code&gt;arg&lt;/code&gt; paramater, and as the second argument to the &lt;code&gt;callback&lt;/code&gt;, must be the same as that associated with the &lt;code&gt;http&lt;/code&gt; object. This is done by using the same &lt;code&gt;l2&lt;/code&gt; dependent type argument with these types. This gives us the desired error checking in the first &amp;#8216;fail compilation&amp;#8217; test above.&lt;/p&gt;

&lt;p&gt;The next step is to prevent out of order destruction. This is done by passing the &lt;code&gt;event_base&lt;/code&gt; object as a proof argument to the &lt;code&gt;evhttp_free&lt;/code&gt; function:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;fun evhttp_free {l1,l2:agz} (base: !event_base l1 | http: evhttp (l2, l1)): void = &amp;quot;mac#evhttp_free&amp;quot;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;By adding this as proof argument I&amp;#8217;m stating that the caller of &lt;code&gt;evhttp_free&lt;/code&gt; must have a usable &lt;code&gt;event_base&lt;/code&gt; object that was used to create this &lt;code&gt;evhttp&lt;/code&gt; object to prove we can safely destroy it. The following is now a compile time error:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;val () = event_base_free (base)
val () = evhttp_free (base | http)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is due to &lt;code&gt;event_base_free&lt;/code&gt; consuming the &lt;code&gt;base&lt;/code&gt; linear object. The type of &lt;code&gt;base&lt;/code&gt; following this call is no longer defined. The &lt;code&gt;evhttp_free&lt;/code&gt; call is now an error due to using an undefined &lt;code&gt;base&lt;/code&gt;. The following will work:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;val () = evhttp_free (base | http)
val () = event_base_free (base)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This method of ensuring correct order of destruction and resource usage requires no changes to the libevent C code. Everything occurs during type checking. The generated C code looks exactly like normal libevent C usage with no runtime overhead for tracking the association between &lt;code&gt;evhttp&lt;/code&gt; and &lt;code&gt;event_base&lt;/code&gt; objects.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>WDCNZ HTML Media Presentation</title>
   <link href="http://bluishcoder.co.nz/2011/08/08/wdcnz-html-media-presentation.html"/>
   <updated>2011-08-08T09:30:00+12:00</updated>
   <id>http://bluishcoder.co.nz/2011/08/08/wdcnz-html-media-presentation</id>
   <content type="html">&lt;p&gt;Last month I went down to Wellington to give a joint talk at the &lt;a href='http://wdcnz.com/'&gt;WDCNZ&lt;/a&gt; conference. The topic was &amp;#8220;HTML Media: Where we are and where we need to go&amp;#8221;. The talk was shared between &lt;a href='http://blogs.msdn.com/b/nigel'&gt;Nigel Parker&lt;/a&gt;, Mobile and Developer Evangelist for Microsoft, and myself. The conference was excellent with some great speakers and talks.&lt;/p&gt;

&lt;p&gt;Nigel and I covered using HTML video and audio elements and how they can be used today across multiple browsers and mobile devices. We also covered upcoming API&amp;#8217;s and directions in the web media area. Demo&amp;#8217;s were shown on Microsoft Internet Explorer 9, Windows Phone 7 (running the Mango update which has a browser that supports HTML media) and Firefox. Nigel has slides and a summary &lt;a href='http://blogs.msdn.com/b/nigel/archive/2011/07/18/wdcnz-html-media-where-we-are-amp-where-we-need-to-go.aspx'&gt;in his blog post about the talk&lt;/a&gt;. I think it took a bit for attendees to get over the shock over a Mozilla and Microsoft representative sharing the stage and working together! Nigel&amp;#8217;s post explains how this came about.&lt;/p&gt;

&lt;p&gt;I spoke to a few Kiwi developers afterwards about using HTML video and there was a fair bit of interest in using it. The main obstacles seemed to be people unsure what codec to use and wanting support for adaptive streaming. The first is an education issue, getting people aware of what codecs to support for maximum coverage across browsers and how to encode to those formats. With regards to adaptive streaming there has been discussion between interested parties in various mailing lists and groups - it&amp;#8217;s definitely something that is wanted.&lt;/p&gt;

&lt;p&gt;At the time of the talk Nigel and I weren&amp;#8217;t able to find any existing New Zealand based sites that use HTML video. Hopefully this will change in the future and Microsoft New Zealand are leading by example by &lt;a href='http://www.microsoft.com/nz/rwc2011/'&gt;using HTML video&lt;/a&gt; on their own site.&lt;/p&gt;</content>
 </entry>
 
 
</feed>

