De volta para o Blogspot

Rank 2 Types

I’ve always seen the forall a types in some GHC messages or in other person’s code, but I couldn’t get the point of it until I needed to use it myself. I was writing a Gtk2hs application with some windows, and I noticed that for each created window, I was doing the same steps. So I created a function that does what is needed for each function:


basic :: WidgetClass widget => String -> IO (Widget, (GObject -> Widget) -> String -> IO widget)

basic gladeFile =
  do
    (windowGlade :: GladeXML) <- getGlade gladeFile
    let
      windowGet :: Get
      windowGet = xmlGetWidget windowGlade
    (window :: Widget) <- windowGet castToWidget "window"
    (close  :: Button) <- windowGet castToButton "close"
    onClicked close $ widgetDestroy window
    modifyIORef windows (window :)
    return (window, windowGet)

Notice that this code uses GHC extension PatternSignatures, which I like a lot.

The problem was that the returned function, windowGet was not generalized enough, so I couldn’t use it with more than one type, even it being very general:

windowGet :: WidgetClass widget => (GObject -> widget) -> String -> IO widget

If I used it with, say, windowGet castToButton "ok" and WindowGet castToSpinButton "value", it would give, in the seconde line, the type error: Couldn’t match expected type `Button’ against inferred type `SpinButton’.

After asking in #haskell, and reading a little bit of the GHC User’s Guide, I got the point. This was only possible with Rank 2 Types. windowGet must be:

windowGet :: forall widget. WidgetClass widget => (GObject -> widget) -> String -> IO widget

So I changed the type signature for basic, and added Rank2Types to the LANGUAGE pragma, and it worked fine.

basic :: String -> IO (Widget, forall widget. WidgetClass widget => (GObject -> Widget) -> String -> IO widget)

Glade message dialog image …

In a message dialog, when I click in the … of the Image field, I get a tree of Objects. I don’t understand hat this means, and I don’t know where to get documentation from it. This is Glade 3.4.5-3 in Debian lenny.

Glade-3 comboBox Active Item

I don’t know if this is a known issue, but it seems strange to me that when I change the Active item value in the proprierties of a Combo Box in Glade 3, from -1 to any value, like 0 or 1, I have this message as soon as the glade file is loaded:

(test:5012): Gtk-CRITICAL **: gtk_tree_row_reference_new: assertion `GTK_IS_TREE_MODEL (model)' failed
(test:5012): Gtk-CRITICAL **: gtk_cell_view_set_displayed_row: assertion `GTK_IS_TREE_MODEL (cell_view->priv->model)' failed

And besides it, the Active Item keeps empty, and is not the first or second on the item list.

I’m using Glade-3 3.4.5 with libglade 2.6.2 in debian lenny.

Da discrição

Não te abras com teu amigo
Que ele um outro amigo tem.
E o amigo de teu amigo
Possui amigos também…

MÁRIO QUINTANA. Da discrição. p. 40. In: Espelho Mágico. p. 35-42. In: Quintana de bolso: Rua dos cataventos & outros poemas. Porto Alegre: L&PM, 2006.

Da realidade

O sumo bem só no ideal perdura…
Ah! Quanta vez a vida nos revela
Que “a saudade da amada criatura”
É bem melhor do que a presença dela…

MÁRIO QUINTANA. Da realidade. p. 39. In: Espelho Mágico. p. 35-42. In: Quintana de bolso: Rua dos cataventos & outros poemas. Porto Alegre: L&PM, 2006.

Da felicidade

Quantas vezes a gente, em busca da ventura,
Procede tal e qual o avozinho infeliz:
Em vão, por toda parte, os óculos procura,
Tendo-os na ponta do nariz!

MÁRIO QUINTANA. Da felicidade. p. 39. In: Espelho Mágico. p. 35-42. In: Quintana de bolso: Rua dos cataventos & outros poemas. Porto Alegre: L&PM, 2006.

Da eterna procura

Só o desejo inquieto, que não passa,
Faz o encanto da coisa desejada…
E terminamos desdenhando a caça
Pela doida aventura da caçada.

MÁRIO QUINTANA. Da eterna procura. p. 37. In: Espelho Mágico. p. 35-42. In: Quintana de bolso: Rua dos cataventos & outros poemas. Porto Alegre: L&PM, 2006.

Eu sei que é clichê

No retrato que me faço
— traço a traço —
às vezes me pinto nuvem,
às vezes me pinto árvore…

às vezes me pinto coisas
de que nem há mais lembrança…
ou coisas que não existem
mas que um dia existirão…

e, desta lida, em que busco
— pouco a pouco —
minha eterna semelhança,

no final, que restará?
Um desenho de criança…
Corrigido por um louco!

MÁRIO QUINTANA. O auto-retrato. p. 49. In: Apontamentos de história sobrenatural. p. 43–75. In: Quintana de bolso: Rua dos cataventos & outros poemas. Porto Alegre: L&PM, 2006.

Glade 3 with Message Dialog

When a Message Dialog is created in Glade 3.4.5-3 (Debian sid and lenny), saved and re-opened, it crashes. More details: Bug in debian .