Friday, May 25, 2007

Sandy eyes

Yesterday we were invited and ate a raclette. It's a bit hot to eat a raclette but I love it. I ate as if I was hungry.

Back at home around 1:30 am. Wake up at 6.

Ouch. I feel sleepy.

Parsec

I've been learning parsec for a few days and my brain cells are aching.
It's amazing though what can be done with it.

I started to write a parser for a weird programming language we use at work. I would have been too lazy to do it in C, besides I never wrote a parser and have no knowledge about them.

Yet with Haskell and parsec, step by step, I add more things that parse.

It's hell when something doesn't parse the way I thought I tell it to. Being used to debug program by setpping through the code or tracing doesn't help once you switch to Haskell. This is the part that make the brain hurt.

Sunday, May 20, 2007

The theme for this blog sucks.

But I don't have enough time to try do design something.
Me = Sad.

I'll at least try to modify it so its wider.

Sometime.

Hopefully.

Thursday, May 17, 2007

First attempt at using GTK with haskell.

Having managed to connect to a mysql database, the next step I wanted to try is display the result in a graphical window.

I would have loved to find QT bindings for haskell, but it seems that doesn't exist.
So, the choices for a GUI are limited to wxHaskell and Gtk2hs. There are other GUI libraries but they're higher level and I didn't want to begin with something maybe too hard. I choose Gtk2hs.

I downloaded, compiled and installed Gtk2hs without trouble and my first program is an exact replica of the glade tutorial.

After that, I modified the window to add a 4th part to the vbox, made a few modifications and added a treeview :











Now I wanted to be able to add my own data to the list.

I had a look at ListDemo.hs from Gtk2hs' examples, but it creates its own treeview, while I get mine from the XML file glade creates.

I tried reading the docs to find the relevant functions but it's not that well explained. Fortunately, ListTest.hs uses a xml file from glade.

From the example, this is the part that manipulate the treeview :

import Graphics.UI.Gtk
import Graphics.UI.Gtk.Glade
import Graphics.UI.Gtk.ModelView as New

data Phone = Phone { name :: String, number :: Int, marked :: Bool }

main = do

...

view <- xmlGetWidget xml castToTreeView "view"

...

-- create a new list store
store <- storeImpl
New.treeViewSetModel view store
setupView view store

...

setupView view model = do
New.treeViewSetHeadersVisible view True

-- add a couple columns
renderer1 <- New.cellRendererTextNew
col1 <- New.treeViewColumnNew
New.treeViewColumnPackStart col1 renderer1 True
New.cellLayoutSetAttributes col1 renderer1 model $ \row -> [ New.cellText := name row ]
New.treeViewColumnSetTitle col1 "String column"
New.treeViewAppendColumn view col1

...

storeImpl =
New.listStoreNew
[Phone { name = "Foo", number = 12345, marked = False }
,Phone { name = "Bar", number = 67890, marked = True }
,Phone { name = "Baz", number = 39496, marked = False }]



The thing I want, though, is feed it the result of a sql query. The result is a list of rows, each row itself being a list of strings. The first row has the column titles.

The storeImpl funtion needs to change to (dbresult being the result from the database)
storeImpl = New.listStoreNew ( tail dbresult )

Now to display this I have to change the setupView function; it has to take an arbitrary list of columns. If you look at the ListTest example, you see that now many things change between the different column creation : the title, and how to get the column's content from a row.

This is how I did it :

setupView view model cols= do
New.treeViewSetHeadersVisible view True

mapM ( \(num,title) -> newcol view model title $ \row -> [ New.cellText := row !! num ] ) $ zip [0..] cols
where
newcol view model title content = do
renderer <- New.cellRendererTextNew
col <- New.treeViewColumnNew
New.treeViewColumnPackStart col renderer True
New.cellLayoutSetAttributes col renderer model $ content
New.treeViewColumnSetTitle col title
New.treeViewAppendColumn view col

The newcol function creates a new column based on the title and how to get the content from the liststore, to create all the columns I map newcol through the list of numbered columns.

And it works !

Here's the full source of the example, some stuff from the glade tutorial is still left over and there are still some French comments, but it works here :)

import Prelude
import IO hiding (bracket)
import Control.Exception

import Database.HSQL as Hsql
import Database.HSQL.MySQL as Hsql

import Graphics.UI.Gtk
import Graphics.UI.Gtk.Glade
import Graphics.UI.Gtk.ModelView as New

data Phone = Phone { name :: String, number :: Int, marked :: Bool }

main = do
initGUI
Just xml <- xmlNew "hellohaskell.glade"
--
-- Réagir à la fermeture de la fenetre
--
window <- xmlGetWidget xml castToWindow "window1"
onDestroy window mainQuit
--
-- Réagir au clic sur bouton fermer
--
clbutton <- xmlGetWidget xml castToButton "button2"
onClicked clbutton $ do
putStrLn "Close Button Clicked"
onClicked clbutton $ do
widgetDestroy window
--
-- Réagir au clic sur bouton ok
--
prompt <- xmlGetWidget xml castToLabel "label1"
txtfield <- xmlGetWidget xml castToEntry "entry1"
okbutton <- xmlGetWidget xml castToButton "button1"
onClicked okbutton $ do
name <- get txtfield entryText
set prompt [ labelText := "Hello " ++ name ]

onEntryActivate txtfield $ do
name <- get txtfield entryText
set prompt [ labelText := "Hello " ++ name ]
--
-- Mettre des lignes dans le listview
--
recupbase <- testdb
view <- xmlGetWidget xml castToTreeView "treeview1"
store <- New.listStoreNew ( tail recupbase )
New.treeViewSetModel view store
setupView view store (head recupbase)

--
-- Lancer le tout
--
mainGUI


setupView view model cols= do
New.treeViewSetHeadersVisible view True

mapM ( \(num,title) -> newcol view model title $ \row -> [ New.cellText := row !! num ] ) $ zip [0..] cols

-- add a couple columns
where
newcol view model title content = do
renderer <- New.cellRendererTextNew
col <- New.treeViewColumnNew
New.treeViewColumnPackStart col renderer True
New.cellLayoutSetAttributes col renderer model $ content
New.treeViewColumnSetTitle col title
New.treeViewAppendColumn view col

-- The part where I handle the SQL query

db_serv = "127.0.0.1"
db_shema = "test"
db_user = "root"
db_pass = ""

sql = "SELECT * FROM adr ORDER BY num"

testdb = withdb db_serv db_shema db_user db_pass (dbquery sql recupRows)

withdb serv shema user pass func =
bracket (Hsql.connect serv shema user pass >>= ( \cnx -> putStrLn "Connected" >> return cnx))
(\cnx -> Hsql.disconnect cnx >> putStrLn "Disconnected")
(\cnx -> func cnx)

dbquery sql func cnx=
bracket (Hsql.query cnx sql >>= ( \stmt -> putStrLn ( "Ran query " ++ sql ) >> return stmt))
(\stmt -> Hsql.closeStatement stmt >> putStrLn "Closed query")
(\stmt -> func stmt)

recupRows :: Statement -> IO [[String]]
recupRows stmt = do
let champs = map (\(a,b,c) -> a) $ getFieldsTypes stmt
lignes <- collectRows (\s -> mapM (getFieldValue s) champs ) stmt
return ( champs : lignes )

Tuesday, May 15, 2007

Stabbed by my "friends"

I am in a climbing club, every year we go out to climb a summit.
This year we were supposed to climb the "dôme des miages", in the Alps.

However, the list of people "admitted" is out and me and my girlfriend are not on it.

It turns out that some people in the club can't stand her. It's been like this for a couple years, and it has reflected in the way they act around me, and while I've suspected it for quite some time, only tonight one had the courage to tell it.

So we're not going to climb this summit after all, but it's not the worse. A bunch of my "colleagues" are hypocrites, and I'm not sure I will stand them anymore until there's a big talk.

I used to think of us as a kind of family. Oh was I mistaken.

Trying to learn Haskell, database.

I've been trying to learn Haskell for a couple months now, I can say I fell in love with the language, while at the same time feeling like I will never understand all of it.

Anyway I wanted to try accessing a MySQL database.

I didn't know what package to use :
  • HDBC, HaSQL rely on ODBC for MySQL, which I don't like.
  • HaskellDB doesn't let you write your own SQL queries, it relies on a higher level description of the database.
  • MySQL-hs is discontinued
  • And there's HSQL.

My home computer's OS is kubuntu feisty, and fortunately there's a package for HSQL available.

I gather some examples and try this simple file to begin :
module Main where

import Control.Exception
import System.Environment (getArgs)
import Database.HSQL
import Database.HSQL.mysql
I try it with ghci and it gives me the following error :
Loading package base ... linking ... done.

db2.hs:6:7: parse error on input `Database.HSQL.mysql'
Failed, modules loaded: none.
Damn. I assume something is wrong with kubuntu's haskell package, so I uninstall it all, download a binary of the latest ghc ( 6.6.1 ), install it, then download HSQL ( 1.7 ), install it ( easy with cabal ).

I try again loading my little file with ghci and I get the exact same error message.

I hope someone on IRC will be able to help me.

UPDATE.

Someone on IRC helped me. I had to use 'import Database.HSQL.MySQL'
I feel dumb now.

Kouchner

Yesterday I've heard that Kouchner might have a ministry in the future French government.

I like the guy even though he's a leftist, he has the image of a guy that doesn't play the politician's game. When an idea is good he tells it straight, no matter if it comes from his political friends or not.

This has lead him to support the USA in the Iraq war, for instance, because he thinks we have a duty to interfere against dictatorships.

Today the news are split about it.

Libération focuses on the criticism from the PS, where many see it as a betrayal of socialist values, while Le figaro writes about the problems with Sarkozy's followers, who complain that Sarkozy's openness is leaving them behind.

To sum things up, nobody's happy.

Monday, May 14, 2007

French workers are whiners

A new study shows that the French are the bigger whiners about their jobs.

Somehow I'm not surprised. If I listen the news, everyday brings it's share of people unhappy about something.

After the French come the British and Swedish. The Irish are those who complain the least.

The article ( in French ) (in English)

Sunday, May 13, 2007

It's good to start climbing again.

This morning Nadine and I went climbing at Verdezun. We did only 2 easy ways each, but it had been a long time since we climbed.

Saturday, May 12, 2007

Laptop and kubuntu

I took the plunge. I backuped all the data I had on the laptop thanks to the new hard drive I bought and installed on my main computer.

I am ready to get rid of windows on my laptop too.

There's just a problem. The laptop doesn't have a cdrom drive, and doesn't boot on the network. Bummer.

Ok I buy a USB cdrom drive. Actually, I buy a dvd burner, it's not that expensive and could be useful soon enough.

Anyway I boot the laptop on the latest kubuntu feisty destkop cd, and soon I'm installing it. Everything's going fine, and soon my laptop is installed. The wireless network is recognised, now that's great.

But... There's something odd with the graphics. The screen resolution is not correct. The laptop has a intel i810 chipset, and the resolution is 1280x768. The laptop is currently at 1024x768.
I try to change it, but 1280x768 isn't in the list of choices.

Once again, google to the rescue. I don't remember the details of what I exactly did, the point is I had to run command-line stuff and edit files to get it to work. Which is not cool for a system that wants to conquer the desktop market.

Also, I used to plug the TV-OUT to my.. TV, in order to watch movies. Right now I have no solution to do this. When the laptop was under windows, it worked out of the box, now I have to custom edit a xorg.conf file, and hack until I make it work.

More about this later, as I try it.

Adding a hard drive under Kubuntu

You'd think that with the goal of kubuntu solving bug#1, some simple operations, such as installing a new hard drive on your system, would be available.

You're wrong !

The first thing I did after installing the new hard drive, was to go to system settings, advanced tab, Disk & Filesystems.

There I see it, so far so good.

Now to see what are my options. Can I set up a partition ? format it ? It turns out that the only thing I can do, is set up mount points for partitions. But I don't have a partition yet on that drive. It's not even formatted.

A quick check in my menus shows there's no application I can use to do the task.

At this point, if I had been a windows novice having made the switch to kubuntu, I'd be totally lost and giving a phonecall to the damn friend that told me to install that crap.

Checking google, I see that I can install gparted to do the job. I lauch adept and install it, no problem. I'm just a little bit disappointed that I don't have a KDE software to do it.

I launch gparted, I set up the entire drive as an ext3 partition, so far it's perfect. It now formats my partition... crash.

I relauch it, re-format... crash.

I end up doing a mkfs.ext3 /dev/sdb1.

[ I edited some minor typo ]