My開発メモ

Haskellで Could not load module ‘System.Directory’と言われた

問題となるソースコードは以下。

deletetodo.hs
import System.IO
import System.Directory
import Data.List

main = do
  contents <- readFile "todo.txt"
  let todoTasks = lines contents
      numberedTasks = zipWith (\n line -> show n ++ " - " ++ line)
                              [0..]
                              todoTasks
  putStrLn "There are your TO-DO items:"
  mapM_ putStrLn numberedTasks
  putStrLn "Which one do you want to delete?"
  numberString <- getLine
  let number = read numberString
      newTodoItems = unlines $ delete (todoTasks !! number) todoTasks
  (tempName, tempHandle) <- openTempFile "." "temp"
  hPutStr tempHandle newTodoItems
  hClose tempHandle
  removeFile "todo.txt"
  renameFile tempName "todo.txt"

出典『すごいHaskell たのしく学ぼう』 p189

このコードを入力して REPL でロードすると、

$ ghci
ghci> :l deletetodo

次のエラーメッセージが出る。

[1 of 2] Compiling Main             ( deletetodo.hs, interpreted )

deletetodo.hs:4:1: error:
    Could not load module ‘System.Directory’
    It is a member of the hidden package ‘directory-1.3.9.0’.
    You can run ‘:set -package directory’ to expose it.
    (Note: this unloads all the modules in the current scope.)
    It is a member of the hidden package ‘directory-1.3.7.1’.
    You can run ‘:set -package directory’ to expose it.
    (Note: this unloads all the modules in the current scope.)
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
  |
4 | import System.Directory
  | ^^^^^^^^^^^^^^^^^^^^^^^
Failed, no modules loaded.

書かれてあるとおり、以下のようにするとコンパイルはできた。

ghci> :set -package directory
package flags have changed, resetting and loading new packages...

ghci> :l deletetodo
[1 of 2] Compiling Main             ( deletetodo.hs, interpreted )
Ok, one module loaded.

bashシェルからの場合は、コンパイルオプションを眺めてみると、

$ ghc --show-options | more

-package オプションがあったので、以下のようにしたらできた。

$ ghc --make -package directory deletetodo.hs

Loaded package environment from /home/se-ichi/.ghc/x86_64-linux-9.4.8/environments/default
[1 of 2] Compiling Main             ( deletetodo.hs, deletetodo.o )
[2 of 2] Linking deletetodo

参考

『すごいHaskell たのしく学ぼう』
著者: Miran Lipovaca
訳: 田中英行・村主崇行
オーム社
平成25年5月30日 第1版第6刷

カテゴリー: Haskell, memo

タグ: directory, GHC, ghci, module, package

カウント: 30