Haskellの環境構築 ~importできないとき~
importできないときは
参考書のコードをそのまま入れてみても,実際は動かないことが多い.これはどの言語にも起こる.めげない.
原因
パッケージがそもそもない,あるいはコンパイラが参照できないか.この場合は参照である.
解決策
stackのプロジェクト機能で環境構築をした.この場合はワークスペースディレクトリ内部に*.cabal
という設定ファイルがあるため,ここのライブラリに書き加えれば良い.
改善する前
% stack build project1-0.1.0.0: build (lib + exe) Preprocessing library project1-0.1.0.0... [1 of 1] Compiling Lib ( src/Lib.hs, .stack-work/dist/x86_64-osx/Cabal-1.24.2.0/build/Lib.o ) /Users/hoge/Desktop/Software-engineering/project1/src/Lib.hs:4:1: error: Failed to load interface for ‘Data.Map’ It is a member of the hidden package ‘containers-0.5.7.1’. Perhaps you need to add ‘containers’ to the build-depends in your .cabal file. Use -v to see a list of the files searched for. -- While building package project1-0.1.0.0 using: /Users/hoge/.stack/setup-exe-cache/x86_64-osx/Cabal-simple_mPHDZzAJ_1.24.2.0_ghc-8.0.2 --builddir=.stack-work/dist/x86_64-osx/Cabal-1.24.2.0 build lib:project1 exe:project1-exe --ghc-options " -ddump-hi -ddump-to-file" Process exited with code: ExitFailure 1
Perhaps you need to add ‘containers’ to the build-depends in your .cabal file.
ここから,containsersが足りないようだと分かる. よって,設定ファイル*.cabalに', containers'を追記した.
name: project1 version: 0.1.0.0 -- synopsis: -- description: homepage: https://github.com/githubuser/project1#readme license: BSD3 license-file: LICENSE author: Author name here maintainer: example@example.com copyright: 2017 Author name here category: Web build-type: Simple extra-source-files: README.md cabal-version: >=1.10 library hs-source-dirs: src exposed-modules: Lib build-depends: base >= 4.7 && < 5 , containers default-language: Haskell2010 executable project1-exe hs-source-dirs: app main-is: Main.hs ghc-options: -threaded -rtsopts -with-rtsopts=-N build-depends: base , project1 default-language: Haskell2010 test-suite project1-test type: exitcode-stdio-1.0 hs-source-dirs: test main-is: Spec.hs build-depends: base , project1 ghc-options: -threaded -rtsopts -with-rtsopts=-N default-language: Haskell2010 source-repository head type: git location: https://github.com/githubuser/project1
これで実行することができた.