> 기술블로그

이멕스(Emacs)를 사용하는 사람들을 위한 Haskell 설정 방법

haskellemacs

특정 언어나 플랫폼의 개발환경을 아무런 배경없이 설치하려면 알아야할게 많고, 시간도 많이 걸린다. 이를 위해 패키지와 프로젝트를 관리하는 프로그램들을 같이 제공한다. 구글에서 사용하는 flutter 가 이러한 방법을 사용하고 있다.

GHCup

GHCup 은 Haskell 패키지 관리자이다. GHC, HLS, Stack, cabal 패키지의 버전을 관리한다.

PowerShell 에서 다음 명령을 사용하여 설치한다. 설치 경로를 지정하고, 추가 패키지는 설치하는 것이 좋다.

Set-ExecutionPolicy Bypass -Scope Process -Force;[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; try { & ([ScriptBlock]::Create((Invoke-WebRequest https://www.haskell.org/ghcup/sh/bootstrap-haskell.ps1 -UseBasicParsing))) -Interactive -DisableCurl } catch { Write-Error $_ }

tui 명령을 사용하여 현재 상태를 확인할 수 있다.

ghcup tui

Stack

프로젝트의 관리는 Stack 을 사용한다. 여기서는 기본적인 명령들을 살펴본다. 보다 다양하고 자세한 사항은 stack 튜토리얼 을 참고한다.

프로젝트 생성하기

기본 템플릿을 사용하여 Haskell 프로젝트를 생성한다.

stack new [PROJECT] [TEMPLATE]

프로젝트 빌드하기

프로젝트를 빌드한다.

stack build

프로젝트 실행하기

프로젝트를 실행한다. FILENAME 인자를 사용하여 특정 명령을 지정할 수 있다.

stack run [FILENAME]

Hello World

이제 간단한 프로젝트를 만들어보자.

stack new helloworld new-template

그럼 helloworld 디렉토리 하위 프로젝트 관련 파일들이 생성된 것을 볼 수 있다.

이제 프로젝트를 빌드하여 실행파일로 만들어보자.

stack build

만들어진 실행파일을 실행해 보자.

$ stack run helloworld
helloworld-0.1.0.0: unregistering (local file changes:
.stack-work\dist\f1a1ac53\build\autogen\Paths_helloworld.hs
.stack-work\dist\f1a1ac53\build\hello...)
helloworld> build (lib + exe) with ghc-9.4.8
Preprocessing library for helloworld-0.1.0.0..
Building library for helloworld-0.1.0.0..
Preprocessing executable 'helloworld-exe' for helloworld-0.1.0.0..
Building executable 'helloworld-exe' for helloworld-0.1.0.0..
helloworld> copy/register
Installing library in C:\Users\kjkan\dev\Projects\helloworld\.stack-work\install\df8bf79e\lib\x86_64-windows-ghc-9.4.8\helloworld-0.1.0.0-IfCG3xlR0IUdqFrBiuXpS
Installing executable helloworld-exe in C:\Users\kjkan\dev\Projects\helloworld\.stack-work\install\df8bf79e\bin
Registering library for helloworld-0.1.0.0..
someFunc

Emacs 에서 개발환경 설정하기

개발환경은 haskell-mode 를 사용한다. 언어서버와의 연동, 오류분석, 코드분석등을 위해 haskell-mode 실행시 추가 패키지를 로드한다.

(use-package haskell-mode
  :ensure t
  :bind
  (:map interactive-haskell-mode-map
        ("M-/" . helm-lsp-code-actions)
        ("M-?" . xref-find-references)
        ("M-." . xref-find-definitions))
  :hook
  (haskell-mode . company-mode)
  (haskell-mode . display-line-numbers-mode)
  (haskell-mode . flycheck-mode)
  (haskell-mode . lsp-mode)
  (haskell-mode . lsp-completion-mode)
  (haskell-mode . show-paren-mode)
  (haskell-mode . whitespace-cleanup-mode))