Saturday, 12 October 2013

Haskell Lens Isomorphisms

A small snippet of code I wrote to try and get my head around Van Laarhoven lenses. It shows the isomorphisms between three common representations of lenses.

{-# LANGUAGE RankNTypes #-}

-- Traditional lens as a pair of getter and setter
type Lens a b = (b -> a, b -> a -> b)

-- Lens implemented with a store comonad
data Store a b = Store { get :: a, set :: a -> b }

instance Functor (Store a) where
    fmap f (Store a ab) = Store a (f . ab)

type SLens a b = b -> Store a b

-- Van Laarhoven lens
type VLLens a b = forall g . Functor g => (a -> g a) -> (b -> g b)

-- Lens / SLens isomorphism

lensToSlens :: Lens a b -> SLens a b
lensToSlens (getter, setter) = \b -> Store (getter b) (setter b)

sLensToLens :: SLens a b -> Lens a b
sLensToLens slens = (get . slens, set . slens)

-- SLens / VLLens isomorphism

sLensToVLLens :: SLens a b -> VLLens a b
sLensToVLLens slens = \f b -> let Store a ab = slens b in fmap ab $ f a

vLLensToSLens :: VLLens a b -> SLens a b
vLLensToSLens vllens = \b -> ((flip vllens) b) (\a -> Store a id)

Thursday, 3 October 2013

Ubuntu on the Gigabyte P34G Ultrablade

In my previous post on the Gigabyte P34G I looked at my reasons for buying it and first impressions. In this post, I'm going to look at installing and setting up Ubuntu GNU/Linux on this machine.

Installation and Setup

Creating USB installation media

On another Ubuntu machine:
  • Download the Ubuntu Live CD ISO.  I started with Ubuntu 13.04, but you are probably better off to go straight to 13.10 for reasons described below.
  • Copy to USB stick using usb-creator-gtk (or dd if you're feeling more adventurous, but make sure you don't wipe your hard disk by mistake!).

BIOS settings

To get to the BIOS settings you first need to boot into Windows 8 and then do a proper shutdown (i.e. not a fastboot shutdown).  I used the pre-installed Gigabyte utility to do this as the standard Windows shutdown just went into fastboot mode.

Insert the USB stick then boot into the BIOS by holding down <F2> during startup.
BIOS settings:
  • Disable 3D Graphic Acceleration.  The X server won't start unless you do this.  I think it's getting confused about which graphics adapter to use.
  • Intel Rapid Start Technology.  Not sure if this is necessary.
  • Disable Secure Boot.  Again, not sure that it's necessary.
  • Set boot sequence to boot from the USB stick.
Boot the USB stick and select 'Install'.

Note: I originally tried installing Linux Mint 15, but could not get the installation media to boot, even with the BIOS changes described above, which was why I went with Ubuntu instead.


I deleted the Windows partition on the 256GB SSD and replaced it with:
There were two partitions for Windows restore, a small 20MB partition at the start and a larger 30GB partition at the end of the SSD.  I kept both of these for now, in case I need to restore Windows 8 for some reason.  I will probably eventually end up removing the 30GB and extending the size of /home. I also kept the EFI boot partition which is required for UEFI boot as the disk uses GPT.

If you want to dual boot Windows 8 and Ubuntu you should be able to shrink the size of the Windows partition to fit the Linux partitions in.

I partitioned the 1TB HDD as
  • 16GB for swap.  Unlikely to ever actually use this, but it's there just in case.
  • Remainder Ext4 partition to mounted as /data.


After booting for the first time I noticed that the wifi card (Intel Centrino 7260) was not detected.  After reading I determined that I needed to upgrade the Linux kernel to 3.11 (from 3.08), and possibly install some firmware binaries, to get this card to work.  I couldn't find kernel 3.11 packages for Ubuntu 13.04, but noticed that it is the standard kernel in 13.10, which is due for release in just over 2 weeks.  I decided the easiest option was probably to just upgrade to the latest development version of Ubuntu 13.10 which I did by connecting an ethernet cable to my router and running sudo upgrade-manager -d.  If you're installing from scratch, I'd recommend going directly to Ubuntu 13.10 rather than installing 13.04 first.  It may have even been officially released by the time you read this.  Installing the firmware binary was not necessary.  It must have been included either by Ubuntu or an update to the Linux kernel since the above-linked post was written.

The other thing I've noticed about wifi is that it occasionally just stops working.  It's happened maybe two or three times in the week since I've had the laptop.  It's probably a sign that the drivers aren't yet mature.  It can be easily fixed by running sudo rmmod iwlwifi; modprobe iwlwifi to remove and re-insert the wifi driver module.  Hopefully this problem will go away after some future kernel upgrade.

SSD options in fstab

Once you've got the system up there are a couple of flags you need to add to the entries for the SSD partitions in /etc/fstab:
  • Add the discard flag to enable TRIM.  The will prevent performance decreasing over time as the drive fills up.
  • Add noatime flag to avoid causing a write to the SSD every time you do a read (which will wear the SSD out much faster than necessary).
I'm suprised the Ubuntu installer doesn't set these options by default for SSD drives.


The first time I tried playing a YouTube video in Ubuntu there was no sound.  I've since noticed that the sound is now working so not sure what was going on there.


The touchpad is very sensitive to accidental palm touches, making typing a frustrating experience. You can easily disable it when necessary using  Fn-F10 key combination. You can also use synclient to configure palm detection, see e.g.  I had to use very low values (PalmMinWidth=3 PalmMinZ=1) to get it to be usable, but YMMV.  You can also use synclient to configure other settings, such as two-finger scrolling and three-finger tap for middle mouse button.

One weird thing I noticed was that the other evening, after I had been playing with BIOS settings trying to work out what the minimum changes were that were needed to get Ubuntu to boot, I booted up and found that the touchpad was no longer working.  Neither xinput nor synclient could find the touchpad at all.  I plugged in a USB mouse which worked, but nothing I tried could get the touchpad to re-appear.  Feeling frustrated, I turned off the computer and went to bed.  Next day when I started it up the touchpad was back.  No idea what happened there and hoping it doesn't happen again.


Bluetooth is provided by the Intel Centrino 7260 wifi card.  Ubuntu reports that it is working, but in a quick test I could not get it to pair up with my phone.  The phone couldn't see the laptop and neither could the laptop see the phone.  I didn't investigate this too much because I don't really have a need for it.  It may be another sign of driver immaturity for this card.


Suspend seems to work.  I have it set to suspend when I close the lid and it has so far always come back ok.  I am hoping a future Linux kernel will support Intel Rapid Start and, as mentioned previously, have left space on the SSD for it.

Still to do

Things I still need to do:
  • Install Bumblebee and the NVidia drivers to allow the GTX 760M to work with Optimus.  I'm currently not using it at all and relying on the Intel integrated graphics.  I did briefly try using the latest NVidia driver, which is supposed to have Optimus support without requiring Bumblebee, however, even after re-enabling "graphics acceleration" in the BIOS I could not get the X server to start.  I haven't had time to investigate this further.
  • I find that the hard disk occasionally spins up when I'm not using it.  This may be related to the kernel "swappiness" parameter (I have a swap partition there, but it shouldn't be using it with 16GB of RAM), or it may be due to some other activity.  Not sure yet.
  • Install XMonad instead of the Unity window manager.
  • Check out whether I can get Intel Rapid Start to work.

Monday, 30 September 2013

Gigabyte P34G Ultrablade laptop

Key specifications

  • Gigabyte P34G Ultrablade laptop
  • 14 inch 1920x1080 AHVA (IPS) matte display
  • Intel Haswell Core i7-4700HQ CPU with integrated Intel HD 4600 graphics
  • NVidia GeForce GTX 760M discrete graphics with Optimus support
  • 16GB DDR3 1600MHz RAM
  • 256GB mSATA SSD
  • Space for a 2.5 inch x 9.5mm SATA HDD (I purchased and installed a Seagate 1TB 5400rpm SSHD)
  • 21mm thickness
  • 1.76kg weight (including HDD)

Reasons for choosing it

This laptop is replacing a 5 year old Lenovo Thinkpad T61 which has a brilliant keyboard and touchpad (including middle mouse button) and average 15.4 inch 1680x1050 TFT screen with poor viewing angles and quite dull display.  With its slow (by today's standards) Core 2 Duo CPU, slow 256GB HDD, only 4GB RAM (with no possibility to upgrade) and pretty much dead battery (lucky to get 5 minutes out of it) it was time to upgrade.

I was originally tempted by the 15 inch Macbook Pro Retina, but ruled it out due to the price.  Next I considered the new generation of Haswell Thinkpads, which have been announced, but have not yet been released.  I have only ever owned Thinkpad laptops before (previously an IBM T40) and have always been very happy with them, in particular build quality, keyboard and touchpad, so was a bit hesitant to go for something else.  The models I was considering are the T440s and T440p.

I decided to go for a 14 inch model rather than 15.6 inch because I wanted something a bit thinner and lighter than my current Thinkpad.  Also it seems to have become almost impossible to get a 15 inch laptop without a numeric keypad, even the new Thinkpads have them.  I want to have my right hand on the right-hand side of the keyboard when typing, not over on the left with my wrist twisted into an RSI-inducing position.

Requirements for my ideal laptop

  • 14 inch (at least) 1920x1080 (at least) matte IPS display (with good viewing angles and reasonable colour gamut, suitable for photo editing)
  • Haswell Core i7 CPU, preferably quad core
  • Preferably at least 16GB RAM
  • At least 256GB SSD and preferably option for an additional HDD or larger SSD
  • No numeric keypad
  • Reasonably thin (prefer < 25mm) and light (prefer < 2kg)

Reasons I chose the Gigabyte over the Thinkpad models

  • Decent quad core CPU (T440s has a ULV dual core CPU, T440p will probably have the 4700MQ which is not much different to the 4700HQ on the P34G)
  • 16GB RAM standard (T440s maxes out at 12GB, T440p supports up to 16GB, but at extra cost)
  • Similar size and weight to T440s (T440p is a fair bit thicker and heavier)
  • Option for 256 GB SSD and 1TB HDD (in the Thinkpads you have to choose one or the other and 256GB seems a bit small if you're doing lots of photo editing and want space for virtual machines and/or Windows/Linux dual boot)
  • It's available now, Thinkpads not expected to be available until at least November.
  • At $1700 it's probably about $1000 less than I'd expect to pay for one of the new Thinkpads with similar specs.

First thoughts

  • Case feels very solid, despite being so thin and light.  Seems to have very good build quality and no noticeable flex.
  • Display is bright and crisp with good viewing angles.  There is some backlight leakage along the bottom and in the top right corner, but only noticeable when looking at a very dark screen in a very dark room.  There is one bright green stuck pixel in the centre-left of the display, but again is only noticeable when looking at very dark images (although I never noticed any stuck/dead pixels on either of my Thinkpads).
  • Keyboard is not nearly as good as either of my previous Thinkpad keyboards, but then I don't think anything is these days.  I'm sure it will be ok, although I've noticed that occasionally pressing letters near the centre (e.g. N, M) will result in a double press being registered.  Page Up, Page Down, Home and End keys are only accessible by using the arrow keys with the Fn button pressed.  I think that is pretty typical these days (my colleague's new Dell XPS is the same), but it seems strange given how essential these keys are for navigation.  I think Page Up and Page Down can be emulated via 3-finger swiping on the touchpad, so maybe I'll get used to it.
  • Touchpad seems unresponsive at first, especially for two-finger scrolling.  However, after some adjustments it seems to be ok.  I miss the middle mouse button on the Thinkpad, but it can be emulated with a three-finger tap.  Something else to get used to I guess.  The touchpad is huge compared to what I'm used to.  I keep accidentally touching it while typing, which leads to unexpected and annoying behaviour.  Will need to do some tweaking to the palm detection settings to avoid this.
  • Windows 8 starts up very quickly and feels snappy, but how do I actually use it?  They seem to have made the most useful functionality deliberately hard to find.  Will need to get Linux on here ASAP.
  • Despite having two massive fans, with two massive air vents at the back (one each for the CPU and GPU), the machine is almost silent when idling.  I suspect the fan noise will pick up when I put the CPU and GPU to some serious use.
  • The hard drive was very easy to install: remove about 15 screws and take off the back cover.  Plug HDD into the SATA cable and slot into place.  Replace the cover and screws.  I plan to use the HDD as secondary storage for my photos and a Windows VM or two so most of the time it's not spinning and the machine is quiet.  You definitely know when it spins up though – it's quite noisy.

Still to come...

I plan to write a follow-up post on my experience installing Ubuntu GNU/Linux on this machine.

Update 2013-10-3: Blog post on Ubuntu installation is now available

Sunday, 29 September 2013

Running E-tax 2013 on Ubuntu 13.10

E-tax is the Australian Tax Office's antiquated Windows software for completing and filing personal tax returns. It's possible to run it under Linux using Wine. Here are the steps for Ubuntu 13.10. It should also work on older versions of Ubuntu and possibly other Debian derivatives.
  • export WINEARCH=win32
  • sudo apt-get install winetricks
  • winetricks msxml4
  • wget
  • msiexec /i etax2013_1.msi

Update 14/10/2013

When filling out my spouse's details I noticed that the radio buttons for spouse's gender were disabled.  At the time I just assumed they no longer considered the question relevant and didn't think more of it.  However, when I went to lodge my tax return I got the cryptic error
V2357 - Spouse's ATI amount for income test purposes is incorrect.
I couldn't work out what the problem was so eventually ended up booting up an old laptop with Windows Vista, installing e-Tax on it and copying my tax file across.  That didn't resolve the error, but at least I could view the error description in the help docs, which basically just said call the ATO personal tax help line if you get this error.

Of course I was doing this on a Saturday afternoon and the help line is only open 8am to 6pm Monday to Friday so I had to wait until Monday morning.  I called the help line, got transferred to e-Tax technical support then back to the help line before I got someone who was able to help.  After 45 minutes on the phone, we eventually determined that the problem was with the aforementioned spouse's gender radio button.  It turns out that on Vista the buttons do work and once I entered the correct gender the error went away.

Note to the ATO's e-Tax developers: would it have been so hard to supply a meaningful error message so I didn't have to waste so much of my time and your help desk's time on this trivial issue?