Monday, July 25, 2016

Haskell: How (not) to Overload Type Classes

After studying haskell as a hobby for some time now, I'm still "thinking OOP"... In particular I still don't find myself comfortable with Type Classes.

Extending Type Classes

Every time I use them I "feel" the need to write a Type Class that extends another one (as if they where c++ classes). But if you want to extend a Type Class you just need a plain function. Like in:
class T1 a where
    m1 :: a -> Int

doubleM1 :: (T1 a) => a -> Int
doubleM1 x = 2 * m1 x 
Of course if you can alter the Type Class definition itself you can add doubleM1' to the Type Class itself like in:
class T1 a where
    m1 :: a -> Int
    doubleM1' :: a -> Int
    doubleM1' x = 2 * m1 x
This second form is more powerful because you can write different implementations for different instances, if so you desire. That is not always a good thing, see:
instance T1 Double where
    doubleM1' x = 3 * m1 x
    m1 = round

My Question

Suppose I have two Type Classes (e.g. they are not not my own Type Classes i.e. I can't modify them)
class T1 a where
    m1 :: a -> Int

class T2 a where
    m2 :: a -> Int
Can I write a Type Class member (or a plain function) that accepts any instance of T1 or T2 and gives back an Int? For instance like in:
class T a where
    m ::  => a -> Int

instance T AnyTypeThatHasAnInstanceForT1 where
    m = m1

instance T AnyTypeThatHasAnInstanceForT2 where
    m = m2
I think my OOP trained brain wants to use overloading in order to avoid writing all my instances twice. What is an idiomatic haskell way of solving such problem?

Monday, October 18, 2010

NetwokManager, resolv.conf & {k,edu,}ubuntu maverick 10.10:
how to tell nm that it must not kill your resolv.conf dns settings

The problem I'm trying to solve is NetworkManager always overwriting /etc/resolv.conf at each reboot, regardless of your /etc/neworking/interfaces file beeing present and setup.

The problem is quite easy to solve, but I could not find a source of info on the net on how to do it.
Even the official docs make no (eveident) mention to this. So...

Basically ubuntu is setup to use NM, but the config also tells NM not to manage the interfaces in /etc/neworking/interfaces. The problem is that, by default, NM clears your /etc/resolv.conf on startup and in /etc/neworking/interfaces there is usually no info to recreate it.

The fix is just to tell NetworkManager to setup the network in boot using info from the /etc/neworking/interfaces, plus adding dns info to that file

In /etc/NetworkManager/nm-system-settings.conf make sure that you have

then in /etc/neworking/interfaces use something like
iface eth0 inet static

Sunday, June 13, 2010

Vaio VPCZ11X9E, no keyboard/touchpad on_boot or after

Asdescribed here we must add i8042.nopnp to the kernel boot parameters in order to use the touchpad at all. Anyhow it sometimes happens that you reboot or suspend/hibernate and end up without mouse and keyboard. Up to today I always had to hard power off the machine, i.e. holding down the power button. Even if this only happens about once every 10 reboots, it is really annoying, so today I attached an usb keyboard to the machine and started investigating.

I discovered that just reloading the psmouse module does the trick of bringing
both the keyboard and the mouse back to life.

In order to make this automatic I added a couple of scripts to
my system.

The first one goes into /etc/pm/sleep.d/99i8042

case "$1" in
modprobe -r psmouse

modprobe psmouse
and the other one, to be saved as /etc/init/fix_touchpad.conf is
# reload the psmouse module

description "Reload the psmouse driver because the buggy i8042 could mess it up after boot"
author "Vincenzo Di Massa <>"

start on starting-dm
modprobe -r psmouse
sleep 1
modprobe psmouse
end script

Saturday, June 05, 2010

Ubuntu lucid lynx 10.04 on the sony vaio core i7 2010 VPCZ11Z9E
switching the intel and nvidia cards

First of all read

To get suspend to ram working when using the intel ** card add
ADD_PARAMETERS="--quirk-test --quirk-s3-bios --quirk-s3-mode"

to /etc/pm/config.d/default (which does not exist by default) and install uswsusp.

To make the nvidia card working I used the the trick described above... I then made it work automatically like this:
1) set grub to automatically boot from the 2.6.32 kernel
2) create a script in /usr/local/bin/force_reboot which just calls /sbin/reboot -f
3) add a grub entry which boots the 2.6.31-20 kernel and add it the parameter init=/usr/local/bin/force_reboot
4) create the config file for nvidia and intel as /etc/X11/xorg.conf.{intel,nvidia}
5) add a boot time service (see the script below) which switches the 2 cards config (must change the xorg.conf file and setup making it able to find mesa/nvidia versions)

# goes into /etc/init/vga_card_switch.conf
# setup the proper video-card

description "Detect wich videocard to use and st it up"
author "Vincenzo Di Massa <>"

start on filesystem
#console output
PCIID=$(dmesg | awk '/Boot video device/ {print $4}')
lspci | grep $PCIID | grep -q nVidia && FOUND=nvidia
if [ "$FOUND" = "nvidia" ]; then
ln -sf /etc/X11/xorg.conf.nvidia /etc/X11/xorg.conf
update-alternatives --set gl_conf /usr/lib/nvidia-current/
ln -sf /etc/X11/ /etc/X11/xorg.conf
update-alternatives --set gl_conf /usr/lib/mesa/
exec ldconfig
end script

** to get intel and nvidia working properly you must install grub, as described above and remove the nomodeset option from the boot parameters

Of course all of the above is hackish.

Monday, March 08, 2010


Today I discovered that exceptions are great: have you ever realized that the semantic of an exception includes the information about what is the expected branch to take?
This means that exceptions (plus a smart compiler) could make your code run faster, in average, than using an ancient language where you must use if-statements.

Maybe this is obvious to most of you, but I'm happy I just "discovered" this.

Saturday, February 27, 2010

Speculations about Mobile OSes.
Here we get a preview on the look of the new Symbian^3 (and Symbian^4 is already ongoing).

The iPhone has 15% market share, Symbian has about 40% but it is decreasing.
It is true that Nokia took 4 long years to understand why it was loosing marker shares, but today it looks like the european phone-maker is moving toward making the gap in UI-design from competitor's phone slimmer.

Now I get curious: suppose Symbian phones reach GUI feature/beautifulness parity with competitors, will they also be cheaper because of Symbian's low footprint on resources usage?
Will Symbian phones be lighter and have longer lasting batteries? Will Qt make them more easy/profitable to develop?

Suppose the answer to the above question is "yes", will Apple and Google be forced to develop even better GUIs exploiting new interaction design concepts and ideas in order to keep growing on the market? What beautiful interfaces will Nokia's competitor develop if Symbian^3 (and ^4) manages to keep up to par with todays iPhone and Android?

This mobile evolution is getting me very curious: since I was born I've never seen so many operating systems fight on the same market... It is wonderfully interesting and it feels so good to know that all of this is possible because of the growth of the Free Software movement :-)

Saturday, October 31, 2009

How to embed ODP (or office ppt) files as flash content on your webpages (including controls).

I spent some time working on this. It would have been nice to avoid "inventing" this stuff on my own, so I'm going to share it here for others to read.
My purpose was to convert my presentations to flash and then to embed them int my webpages along with nice controls. Something similar to what allows, but:you can do it on your websitethe slides are converted to flash using a nice and small script (which uses openoffice)First of all you need to convert your odp (or ppt or anything openoffice can read) into flash: to achieve this just open the file in openoffice and export as flash (really! openoffice can do that out of the box).
Now you can put your files on the web and serve them. If you are happy you are done :-)
If you would like to embed the flash content into the webpage you need to write something like the following into your webpage:

<div class="slides">
data="<?php echo $filename; ?>"
id="<?php echo $filename; ?>"
name="<?php echo $filename; ?>"
value="<?php echo $filename; ?>"
Of course <?php echo $filename; ?> must be replaced with your filename if you are not using php :-)
Now your slides are embedded but you can't control how they are played. Let's add some controls. First of all you need some javascript functions:

function RewindSlides(slides)
var text = document.getElementById("Page_"+slides);
text.value = 1;

function SlidePrev(slides)
SlideMove(slides, -1);

function SlideNext(slides)
SlideMove(slides, 1);

function SlideMove(slides, movement)
var flashSlide=document.getElementById(slides);
var text = document.getElementById("Page_"+slides);
text.value = parseInt(text.value)+movement;

function SlideGoto(slides)
var flashSlide=document.getElementById(slides);
var totalFrames = flashSlide.TotalFrames();

var text = document.getElementById("Page_"+slides);
var n = text.value;

if (n<1)
else if (n>totalFrames/2)
n = parseInt(totalFrames/2);
text.value = n;

if (n*2 >= totalFrames - 1 )
flashSlide.GotoFrame(totalFrames - 5);
else if (n<2)>

And then you can just out the following controls into your HTML:

<input type="button" value="Rewind" name="Rewind"
onClick="RewindSlides('<?php echo $filename; ?>');">
<input type="button" value="Prev" name="Prev"
onClick="SlidePrev('<?php echo $filename; ?>');">
<input type="button" value="Next" name="Next"
onClick="SlideNext('<?php echo $filename; ?>');">
<input type="button" value="Goto" name="Goto"
onClick="SlideGoto('<?php echo $filename; ?>');">
<input type="text"
value="1" name="Page_<?php echo $filename; ?>"
id="Page_<?php echo $filename; ?>">

The end result can be shown on my page: