Pixel Pedals of Tomakomai

北海道苫小牧市出身の初老の日常

`stack setup --upgrade-cabal` した

lts-7.19 使ってたら嫌な感じのエラー。

    /tmp/stack25595/cairo-0.13.3.1/Setup.hs:8:29: error:
        • Couldn't match expected type ‘Distribution.Simple.UserHooks.UserHooks’
                      with actual type ‘Cabal-1.24.2.0:Distribution.Simple.UserHooks.UserHooks’
          NB: ‘Cabal-1.24.2.0:Distribution.Simple.UserHooks.UserHooks’
                is defined in ‘Distribution.Simple.UserHooks’
                    in package ‘Cabal-1.24.2.0’
              ‘Distribution.Simple.UserHooks.UserHooks’
                is defined in ‘Distribution.Simple.UserHooks’
                    in package ‘Cabal-1.24.0.0’
        • In the first argument of ‘defaultMainWithHooks’, namely
            ‘gtk2hsUserHooks’
          In the expression: defaultMainWithHooks gtk2hsUserHooks
          In an equation for ‘main’:
              main = defaultMainWithHooks gtk2hsUserHooks

ほぼ読んでない けど、

$ stack setup --upgrade-cabal

で解決。ついちょっと先日にもやった気がする。

build とか install じゃなくて setup なとこに注意。

とかやってたら、ghc-8.0.2 が使えるようになってた。

"hSetBuffering stdin NoBuffering doesn't work on Windows" とのこと

完全に序盤で投げ出されているけど、Haskellroguelikeを実装するエントリ。

https://github.com/jamiltron/Thieflike

Haskell には https://hackage.haskell.org/package/LambdaHack っていうroguelikeがあって そっちのほうが真面目に作られてるのだけど、CLIのバックエンドがvtyなのでwin環境では動かない。一方、Thieflikeはansi-terminalが使われているので動く。はずだったのだけど。

動かしてみると、エンターキーを押さないとキー入力を受け付けてくれない。大昔からチケットが上がっていて、

https://ghc.haskell.org/trac/ghc/ticket/2189

  • Milestone set to 6.8.3
  • Milestone changed from 6.8.3 to 6.10.1
  • Milestone changed from 6.10.1 to 6.10.2
  • Milestone changed from 6.10.2 to 6.12.1
  • Milestone changed from 6.12.1 to 6.12 branch
  • Milestone changed from 6.12 branch to 6.12.3
  • Milestone changed from 6.12.3 to 6.14.1
  • Priority changed from normal to low
  • Milestone changed from 7.0.1 to 7.0.2
  • Milestone changed from 7.0.2 to 7.2.1
  • Milestone changed from 7.2.1 to 7.4.1
  • Milestone changed from 7.4.1 to 7.6.1
  • Milestone changed from 7.6.1 to 7.6.2
  • Milestone changed from 7.6.2 to 7.10.1
  • Milestone changed from 7.10.1 to 7.12.1
  • Milestone changed from 7.12.1 to 8.0.1
  • Milestone changed from 8.0.1 to 7.10.3
  • Milestone changed from 7.10.3 to 8.0.1
  • Milestone 8.0.1 deleted

https://ghc.haskell.org/trac/ghc/ticket/11394

Milestone set to 8.4.1

という終わりの見えない状況で辛い。

msys2の最新版で [lost server] が発生

msys2 をアップデートしたら、 tmuxクリップボード周りを操作すると [lost server] 的なメッセージが出て死ぬようになってしまった。今までも何度かあったこととは言え、辛さに溢れる。

とりあえずこれの通り msys2 のランタイムのバージョン下げたら直った。この辺もいつもの通りの対処法。

https://github.com/Alexpux/MSYS2-packages/issues/791#issuecomment-275253779

MSYS2でHDBC-sqlite3をコンパイル

当然のことながら、msys2のshellではなくmingw64のshellから操作する。

まず、 sqlite3 を入れる。

pacman -S mingw64/mingw-w64-x86_64-sqlite3

後はビルドするだけなんだけど、2点注意がいる。

  • --extra-include-dirs--extra-lib-dirs にmingw64関連のディレクトリを明示する
  • --skip-msys を指定する

--skip-msysここ とか ここ 読めばわかるけど、指定しなければwin環境のGHCがデフォルトで内部に持ってるmsys2のPATHを見てしまうのを、抑制して見ないようにしてくれる。

stack build --skip-msys --extra-include-dirs=/c/msys64/mingw64/include/  --extra-lib-dirs=/c/msys64/mingw64/lib/ HDBC-sqlite3

余談

--extra-* の指定が必要な理由はよくわからない。明示しなくてもmingw64のshellを使ってるのだから勝手に見て欲しいもの。

依存ライブラリのチェックはcabal側でやってて 、ここで使うgccがどれなのかがまず重要である。ここに関しては一応 stack 側で --with-gcc=/c/msys64/mingw64/bin/gcc のような指定は可能で、これを指定するとライブラリのチェックでは落ちなくなる。しかし、結局buildの部分で落ちる。

2017-01-29 10:45:52.197846: [debug] Run process: C:\stack_root\setup-exe-cache\x86_64-windows\Cabal-simple_mPHDZzAJ_1.24.2.0_ghc-8.0.1.exe --builddir=.stack-work\dist\ca59d0ab build --ghc-options " -ddump-hi -ddump-to-file"
@(System\Process\Read.hs:340:3)

--  While building package HDBC-sqlite3-2.3.3.1 using:
      C:\stack_root\setup-exe-cache\x86_64-windows\Cabal-simple_mPHDZzAJ_1.24.2.0_ghc-8.0.1.exe --builddir=.stack-work\dist\ca59d0ab build --ghc-options " -ddump-hi -ddump-to-file"
    Process exited with code: ExitFailure 1

...略...

    [7 of 7] Compiling Database.HDBC.Sqlite3 ( Database\HDBC\Sqlite3.hs, .stack-work\dist\ca59d0ab\build\Database\HDBC\Sqlite3.o )

    C:\tools\msys64\tmp\stack12828\HDBC-sqlite3-2.3.3.1\hdbc-sqlite3-helper.c:1:21: error:
         fatal error: sqlite3.h: No such file or directory
    compilation terminated.
    `gcc.exe' failed in phase `C Compiler'. (Exit code: 1)

ここで setup.exe 呼ぶのにも --with-gcc が必要な気がするのだけど、 stack がそれを指定していないように見える。が、面倒なのでこれ以上は追いかけてない。

AllowAmbiguousTypes拡張と型適用

このように f を定義して実行したい。

f :: String -> String
f = show . read

main :: IO ()
main = putStrLn $ f "True"

エラーが出る。

Prelude> :l test.hs
[1 of 1] Compiling Main             ( test.hs, interpreted )

test.hs:5:5: error:
    • Ambiguous type variable ‘a0’ arising from a use of ‘show’
      prevents the constraint ‘(Show a0)’ from being solved.
      Probable fix: use a type annotation to specify what ‘a0’ should be.
      These potential instances exist:
        instance Show Ordering -- Defined in ‘GHC.Show’
        instance Show Integer -- Defined in ‘GHC.Show’
        instance Show a => Show (Maybe a) -- Defined in ‘GHC.Show’
        ...plus 22 others
        ...plus 16 instances involving out-of-scope types
        (use -fprint-potential-instances to see them all)
    • In the first argument of ‘(.)’, namely ‘show’
      In the expression: show . read
      In an equation for ‘f’: f = show . read

test.hs:5:12: error:
    • Ambiguous type variable ‘a0’ arising from a use of ‘read’
      prevents the constraint ‘(Read a0)’ from being solved.
      Probable fix: use a type annotation to specify what ‘a0’ should be.
      These potential instances exist:
        instance Read Ordering -- Defined in ‘GHC.Read’
        instance Read Integer -- Defined in ‘GHC.Read’
        instance Read a => Read (Maybe a) -- Defined in ‘GHC.Read’
        ...plus 22 others
        ...plus 7 instances involving out-of-scope types
        (use -fprint-potential-instances to see them all)
    • In the second argument of ‘(.)’, namely ‘read’
      In the expression: show . read
      In an equation for ‘f’: f = show . read
Failed, modules loaded: none.

showread の対象とする型 a0 がきちんと決まってないのが原因なので、これを f に対して指定できるようにする。

f :: (Show a, Read a) => String -> String
f = (show :: a -> String) . read

再びエラーとなる。

Prelude> :r
[1 of 1] Compiling Main             ( test.hs, interpreted )

test.hs:4:6: error:
    • Could not deduce (Read a0)
      from the context: (Show a, Read a)
        bound by the type signature for:
                   f :: (Show a, Read a) => String -> String
        at test.hs:4:6-41
      The type variable ‘a0’ is ambiguous
    • In the ambiguity check for ‘f’
      To defer the ambiguity check to use sites, enable AllowAmbiguousTypes
      In the type signature:
        f :: (Show a, Read a) => String -> String
Failed, modules loaded: none.

aShowReadインスタンスだと言ってる割には、型の本文に a が登場しないのでこれを決定する方法がない。 AllowAmbiguousTypes を使うとこのチェックを遅らせられるよと教えてくれているので、有効にする。 が、これまたゴツいエラー。

Prelude> :r
[1 of 1] Compiling Main             ( test.hs, interpreted )

test.hs:5:6: error:
    • Could not deduce (Show a2) arising from a use of ‘show’
      from the context: (Show a, Read a)
        bound by the type signature for:
                   f :: (Show a, Read a) => String -> String
        at test.hs:4:1-41
      Possible fix:
        add (Show a2) to the context of
          an expression type signature:
            a2 -> String
    • In the first argument of ‘(.)’, namely ‘(show :: a -> String)’
      In the expression: (show :: a -> String) . read
      In an equation for ‘f’: f = (show :: a -> String) . read

test.hs:5:29: error:
    • Could not deduce (Read a0) arising from a use of ‘read’
      from the context: (Show a, Read a)
        bound by the type signature for:
                   f :: (Show a, Read a) => String -> String
        at test.hs:4:1-41
      The type variable ‘a0’ is ambiguous
      These potential instances exist:
        instance Read Ordering -- Defined in ‘GHC.Read’
        instance Read Integer -- Defined in ‘GHC.Read’
        instance Read a => Read (Maybe a) -- Defined in ‘GHC.Read’
        ...plus 22 others
        ...plus 7 instances involving out-of-scope types
        (use -fprint-potential-instances to see them all)
    • In the second argument of ‘(.)’, namely ‘read’
      In the expression: (show :: a -> String) . read
      In an equation for ‘f’: f = (show :: a -> String) . read
Failed, modules loaded: none.

これは show :: a -> String が暗黙的に show :: forall a. a -> String と解釈されているのが原因なので、 ScopedTypeVariables 拡張を使う。

{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}

f :: forall a. (Show a, Read a) => String -> String
f = (show :: a -> String) . read

main :: IO ()
main = putStrLn $ f "True"

f の定義についてのエラーはなくなった。後は呼び出し側。

*Main> :r
[1 of 1] Compiling Main             ( test.hs, interpreted )

test.hs:8:19: error:
    • Ambiguous type variable ‘a0’ arising from a use of ‘f’
      prevents the constraint ‘(Read a0)’ from being solved.
      Probable fix: use a type annotation to specify what ‘a0’ should be.
      These potential instances exist:
        instance Read Ordering -- Defined in ‘GHC.Read’
        instance Read Integer -- Defined in ‘GHC.Read’
        instance Read a => Read (Maybe a) -- Defined in ‘GHC.Read’
        ...plus 22 others
        ...plus 7 instances involving out-of-scope types
        (use -fprint-potential-instances to see them all)
    • In the second argument of ‘($)’, namely ‘f "True"’
      In the expression: putStrLn $ f "True"
      In an equation for ‘main’: main = putStrLn $ f "True"
Failed, modules loaded: none.

a を指定すればいいのだから、ここで TypeApplications の登場。

{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}

f :: forall a. (Show a, Read a) => String -> String
f = (show :: a -> String) . read

main :: IO ()
main = putStrLn $ f @Bool "True"
Prelude> :r
[1 of 1] Compiling Main             ( test.hs, interpreted )
Ok, modules loaded: Main.
*Main> :main
True

めでたしめでたし。せっかく TypeApplications を使うなら、 f の定義ももう少し簡潔になる。

f :: forall a. (Show a, Read a) => String -> String
f = show @a . read

余談1. AllowAmbiguousTypes の妥当性

型変数作って TypeApplications 使って適用したいだけなのに突然 AllowAmbiguousTypes 拡張が出てきてなんだよって思ったかもしれないが、 論文中ではこいつの必要性は書いてあって、そもそもは TypeApplications を有効にすると自動的に有効になるようになってた。

このへんのメールのやり取り でない方がいいだろって話になって、 このコミットで消された。

余談2. Proxy

TypeApplications を使えるコンパイラを持っていないあなたでも、型の役割をする式である Proxy 型を使うことができるのだ! 副作用で型の本文に a が入るようになるので、 AllowAmbiguousTypes 拡張も不要になる。

{-# LANGUAGE ScopedTypeVariables #-}

import Data.Proxy

f :: forall proxy a. (Show a, Read a) => proxy a -> String -> String
f _ = (show :: a -> String) . read

main :: IO ()
main = putStrLn $ f (Proxy :: Proxy Bool) "True"

だが、勘がいいあなたなら気がついてしまったかもしれない。 Proxy は可読性をあげてくれるだけで、なくても困らないことを。

{-# LANGUAGE ScopedTypeVariables #-}

f :: forall proxy a. (Show a, Read a) => proxy a -> String -> String
f _ = (show :: a -> String) . read

main :: IO ()
main = putStrLn $ f [False] "True"

余談3. Type defaulting in GHCi

GHCiのデフォルト型の割り当ては強力 なので、なんと f がそのままコンパイルできてしまうのだ!! ただし、勝手に () になってしまう。

*Main> f = show . read
*Main> f "()"
"()"
*Main> f "True"
"*** Exception: Prelude.read: no parse

ConnectionFailure getAddrInfo: does not exist (Name or service not known) が出た

vagrantubuntu/zesty64 を使ったら、 https://github.com/commercialhaskell/stack/issues/2536 的なメッセージが出てかなりの高確率で stack setup に失敗した。

そもそも安定版使ってないほうが悪かったので、 ubuntu/xenial64 使うようにしたら収まった。とは言え、その直前に zesty64 も senial64 もどちらのboxもアップデートされた(20170119.1.0)っぽいので、何が悪かったかは切り分けてない。

2017年センター試験英語

最近ひどく疲れているので http://www.toshin.com/center/ から英語を解いた。リスニングが42点で筆記が169点。おもったよりひどくないと思ってしまったけど、本来満点取らないと駄目なやつだよね、これ。現役の頃を思い出して気分転換にはなったので良しとする。こうやって明確に解法も解答も決まっているのは頭の準備体操にはいいね。