Closed2

Haskell学習メモ

saharasahara

環境構築

環境:macOS Big Sur 11.6

参考にしたサイト:https://qiita.com/yoshii0110/items/3bb9a06c0dee20f4fd62

stackをインストール

brew install stack

stackを使って、グローバルにghcや基本パッケージをインストール

% stack setup       
Writing implicit global project config file to: /Users/<username>/.stack/global-project/stack.yaml
Note: You can change the snapshot via the resolver field there.
Using latest snapshot resolver: lts-18.13
Preparing to install GHC to an isolated location.
This will not interfere with any system-level installation.
Downloaded ghc-8.10.7.                                      
ld: unknown option: --version                                                      
ld: unknown option: --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib: file: /Users/ogasawara/.stack/programs/x86_64-osx/ghc-8.10.7/lib/ghc-8.10.7/rts/libHSrts.a(Disassembler.o) has no symbols

(中略)
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib: file: /Users/ogasawara/.stack/programs/x86_64-osx/ghc-8.10.7/lib/ghc-8.10.7/rts/libHSrts_thr.a(RetainerProfile.thr_o) has no symbols
(中略)

Installed GHC.    
stack will use a sandboxed GHC it installed
For more information on paths, see 'stack path' and 'stack exec env'
To use this GHC and packages outside of a project, consider using:
stack ghc, stack ghci, stack runghc, or stack exec

動作確認も兼ねて、GHCのバージョンを表示してみる

% stack ghc -- --version
The Glorious Glasgow Haskell Compilation System, version 8.10.7

とりあえず、VSCode上で始めたので、VSCodeのエクステンションをインストール

Name: Haskell
Id: haskell.haskell
Description: Haskell language support powered by the Haskell Language Server
Version: 1.7.1
Publisher: Haskell
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=haskell.haskell

エクステンションのページを見ると、HaskellのLanguarge Serverに対応しているようだが、

https://pages.michinobu.jp/t/vscode-haskell-language-server.html

などを見る限り、StackをインストールしただけではHaskell Language Server(以下、HLS)はインストールされないらしい。ので、

https://haskell-language-server.readthedocs.io/en/latest/installation.html

を見てHLSのインストール(ソースからビルド)から始める。…と思ったところ、brewでインストールできることが判明(^ω^)

brew install haskell-language-server
…
haskell-language-server is built for GHC versions 8.6.5, 8.8.4, 8.10.7.
You need to provide your own GHC or install one with
  brew install ghc
…

/usr/local/binには、

haskell-language-server-8.6    
haskell-language-server-8.6.5  
haskell-language-server-8.8    
haskell-language-server-8.8.4  
haskell-language-server-8.10   
haskell-language-server-8.10.7 
haskell-language-server-wrapper

が生成されていた。

エクステンションのページを今一度見てみたら、「このエクステンションはHLSをダウンロードするよ。」とあって、実際 ~/Library/Application\ Support/Code/User/globalStorage/haskell.haskell/内にあった。

brew経由のものは、brew uninstall haskell-language-serverでアンインストール。

saharasahara

Hello, worldしてみる

適当なディレクトリで

stack new hello-world

すると、以下のようなディレクトリ構造でプロジェクトが生成される

% tree .
.
├── ChangeLog.md
├── LICENSE
├── README.md
├── Setup.hs
├── app
│   └── Main.hs
├── hello-world.cabal
├── package.yaml
├── src
│   └── Lib.hs
├── stack.yaml
├── stack.yaml.lock
└── test
    └── Spec.hs

3 directories, 11 files

src/Lib.hsを開き、次のような状態に編集する

module Lib
    ( someFunc
    ) where

someFunc :: IO ()
- someFunc = putStrLn "someFunc"
+ someFunc = putStrLn "Hello, world."

ビルド

% stack build
[1 of 2] Compiling Main             ( /Users/username/.stack/setup-exe-src/setup-mPHDZzAJ.hs, /Users/username/.stack/setup-exe-src/setup-mPHDZzAJ.o )
[2 of 2] Compiling StackSetupShim   ( /Users/username/.stack/setup-exe-src/setup-shim-mPHDZzAJ.hs, /Users/username/.stack/setup-exe-src/setup-shim-mPHDZzAJ.o )
Linking /Users/username/.stack/setup-exe-cache/x86_64-osx/tmp-Cabal-simple_mPHDZzAJ_3.2.1.0_ghc-8.10.7 ...
Building all executables for `hello-world' once. After a successful build of all of them, only specified executables will be rebuilt.
hello-world> configure (lib + exe)
Configuring hello-world-0.1.0.0...
hello-world> build (lib + exe)
Preprocessing library for hello-world-0.1.0.0..
Building library for hello-world-0.1.0.0..
[1 of 2] Compiling Lib
[2 of 2] Compiling Paths_hello_world
Preprocessing executable 'hello-world-exe' for hello-world-0.1.0.0..
Building executable 'hello-world-exe' for hello-world-0.1.0.0..
[1 of 2] Compiling Main
[2 of 2] Compiling Paths_hello_world
Linking .stack-work/dist/x86_64-osx/Cabal-3.2.1.0/build/hello-world-exe/hello-world-exe ...
hello-world> copy/register
Installing library in /Users/username/Documents/haskell/hello-world/.stack-work/install/x86_64-osx/a55b666436d3d75ce45fc72ba25f0d5d654f54061ac25329614ec712d13cd608/8.10.7/lib/x86_64-osx-ghc-8.10.7/hello-world-0.1.0.0-62fcPSqcN8p22j06EIqoAY
Installing executable hello-world-exe in /Users/username/Documents/haskell/hello-world/.stack-work/install/x86_64-osx/a55b666436d3d75ce45fc72ba25f0d5d654f54061ac25329614ec712d13cd608/8.10.7/bin
Registering library for hello-world-0.1.0.0..

実行

 % stack exec hello-world-exe
Hello, world.
このスクラップは4ヶ月前にクローズされました