stackでhaskell-gi

「haskell-gi」のHelloWorldをstackで実行してみた。
# 環境
OS: Gentoo Base System release 2.7
GUI: GNOME
stack: Version 2.5.1
LTS: lts-17.12
# haskell-gi
[https://github.com/haskell-gi/haskell-gi](https://github.com/haskell-gi/haskell-gi)  
上記サイトには説明として「GObject Introspection対応のライブラリのHaskellバインディングを生成します。」と書かれています。

# stackでHelloWorld
公式サイトのexamplesにあるhello-world.hsを実行してみます。
[https://github.com/haskell-gi/haskell-gi/blob/master/examples/introductory/src/hello-world.hs](https://github.com/haskell-gi/haskell-gi/blob/master/examples/introductory/src/hello-world.hs)  

haskell-world.hsのコード

```
{-# LANGUAGE OverloadedStrings, OverloadedLabels #-}

{-# OPTIONS_GHC -fno-warn-unused-do-bind #-}

{- Hello World example of GTK+ documentation. For information please refer to README -}

module Main where

-- import qualified Data.Text as T
import Data.Text ()

import qualified GI.Gio as Gio
import qualified GI.Gtk as Gtk
import Data.GI.Base

printHello :: IO ()
printHello = putStrLn "Hello, World!"

activateApp :: Gtk.Application -> IO ()
activateApp app = do
  w <- new Gtk.ApplicationWindow [ #application := app
                                 , #title := "Haskell Gi - Examples - Hello World"
                                 , #defaultHeight := 200
                                 , #defaultWidth := 200
                                 ]

  bbox <- new Gtk.ButtonBox [ #orientation := Gtk.OrientationHorizontal ]
  #add w bbox
  
  btn <- new Gtk.Button [ #label := "Hello World!"]
  on btn #clicked printHello
  on btn #clicked $ Gtk.widgetDestroy w
  #add bbox btn
  
  #showAll w
  return ()

main :: IO ()
main = do
  app <- new Gtk.Application [ #applicationId := "haskell-gi.examples.hello-world"
                             , #flags := [ Gio.ApplicationFlagsFlagsNone ]
                             ]
  on app #activate $ activateApp app
  Gio.applicationRun app Nothing
  return ()
```
上記コードをstackで実行してみたいと思います。

```
stack new my-project
cd my-project
```
stack.yamlファイルにextra-depsを下記の通りに追記します。

```
extra-deps:
- gtk2hs-buildtools-0.13.8.0
- glib-0.13.8.1
- cairo-0.13.8.1
- pango-0.13.8.1
- gi-gtk-3.0.37
- gi-gdk-3.0.24
- gi-gdkpixbuf-2.0.26
- gi-gmodule-2.0.1
- gi-atk-2.0.23
- gi-cairo-1.0.25
- gi-gio-2.0.28
- gi-gobject-2.0.26
- gi-harfbuzz-0.0.4
- gi-pango-1.0.24
- haskell-gi-0.25.0
- haskell-gi-base-0.25.0
- gi-glib-2.0.25
- reactive-banana-1.2.1.0
```
packege.yamlファイルのdependenciesを下記の通りに設定します。

```
dependencies:
- base >= 4.7 && < 5
- gi-gtk
- haskell-gi-base
- text
- gi-gio
```

src/Main.hsにhello-world.hsコードをコピペします。

```
{-# LANGUAGE OverloadedStrings, OverloadedLabels #-}

{-# OPTIONS_GHC -fno-warn-unused-do-bind #-}

{- Hello World example of GTK+ documentation. For information please refer to README -}

module Main where

-- import qualified Data.Text as T
import Data.Text ()

import qualified GI.Gio as Gio
import qualified GI.Gtk as Gtk
import Data.GI.Base

printHello :: IO ()
printHello = putStrLn "Hello, World!"

activateApp :: Gtk.Application -> IO ()
activateApp app = do
  w <- new Gtk.ApplicationWindow [ #application := app
                                 , #title := "Haskell Gi - Examples - Hello World"
                                 , #defaultHeight := 200
                                 , #defaultWidth := 200
                                 ]

  bbox <- new Gtk.ButtonBox [ #orientation := Gtk.OrientationHorizontal ]
  #add w bbox

  btn <- new Gtk.Button [ #label := "Hello World!"]
  on btn #clicked printHello
  on btn #clicked $ Gtk.widgetDestroy w
  #add bbox btn

  #showAll w
  return ()

main :: IO ()
main = do
  app <- new Gtk.Application [ #applicationId := "haskell-gi.examples.hello-world"
                             , #flags := [ Gio.ApplicationFlagsFlagsNone ]
                             ]
  on app #activate $ activateApp app
  Gio.applicationRun app Nothing
  return ()
```
コンパイルして実行します。

```
stack build
stack exec my-project-exe
```

下記の通りに実行されます。
![20210522.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/87133/69312bf0-4b51-c637-6cb9-344f4edd82cf.png)

[全てのソースコード](https://github.com/ringo-apo/haskell-gi_helloworld)

以上です。