LoginSignup
1
0

Haskellの環境構築

Last updated at Posted at 2024-06-06

概要

本投稿は以下のことを目的として書かれています。

  • MacでHaskellの実行環境を整える
  • Stackプロジェクトの作成を前提としない実行環境を構築する
  • VSCode上でフォーマッタ(ormolu)を適用する
  • Haskellファイル単体で実行できるようにする

Haskellの環境構築の記事としては以下のものが詳しいので、
ちゃんとした環境を構築したい場合は以下の記事を参照してください。

インストール

GHCupインストール

curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org | sh

GHCのインストール

ghcup install ghc 9.8.2

haskell-language-serverのインストール

ghcup install hls 2.7.0

フォーマッタ本体のインストール

brew install ormolu
# こっちは使わないがエラーメッセージ対応のため入れておく
brew install hlint

VSCodeの拡張機能をインストール

VSCodeのsettings.jsonに以下を追加

  "haskell.serverExecutablePath": "~/.ghcup/bin/haskell-language-server-wrapper-2.7.0.0",
  "haskell.manageHLS": "GHCup",
  "haskell.trace.server": "messages",
  "haskell.formattingProvider": "ormolu",
  "haskell.hlint.executablePath": "/opt/homebrew/bin/hlint",
  "ormolu.path": "/opt/homebrew/bin/ormolu",
  "ormolu.args": ["--no-cabal"],
  "[haskell]": {
    "editor.formatOnSave": true,
    "editor.defaultFormatter": "sjurmillidahl.ormolu-vscode",
  },
  "haskell.toolchain": {
    "ghc": "9.8.2",
    "cabal": "3.6.2.0",
    "stack": "2.15.5"
  },
  "haskell.ghcupExecutablePath": "~/.ghcup/bin/ghcup",
  "code-runner.executorMap": {
    "haskell": "runghc $fullFileName"
  },
  "code-runner.executorMapByFileExtension": {
    ".hs": "runghc $fullFileName"
  },
  "code-runner.clearPreviousOutput": true,
  "code-runner.runInTerminal": true,

その他

GHCupでインストールしたパッケージのバージョン情報一覧を表示

ghcup list

GHCupでインストールしたモジュールのバージョン変更

ghcup set ghc <version>

実行コマンド

  • ファイル実行(runghc)
    • Ctrl + Alt + n

hlintの抑制

xs !! 0を使うなどのSuggestionやWarningを抑制したい場合は以下のようにする

  1. .hlint.yamlを作成
  2. - ignore: {name: "Use head"} のようにファイルに追記する
    • nameの部分は公式GitHubのHint Nameの文字列を指定すること
  3. 保存後にファイルを開き直すとSuggestion, Warningなどが消える

未解決

ghcが$PATHに無い

VSCodeでHaskellのソースファイルを開くと以下のエラーが表示される。
ghcのパスは通っているがエラーが消えない。
今のところ実動作に影響は無いので放置

Failed to find executable "stack" in $PATH for this Stack project.

参考

1
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0