How to improve your developer experience in Haskell
Code editor setup
This is a example of how you can setup your code editor using visual studio code
https://gyazo.com/aa29ee08c3f9c43972690993b248a05d
There's two split terminal with ghcid on one side and repl on other. (See below for more information on ghcid)
For IDE, I use intero
There's also, haskell-ide-engine
Caveat with haskell-ide-engine is that the initial build takes quite long time and it consumes decent amount of memory. Also, from my experience, HIE does not work well against project with decent amount of modules.
GHCi - Customizing your REPL setup
.ghci allows you to customize your REPL.
Have it located on one of the below:
Root of the project (e.g. project/.ghci)
Home directory (e.g. ~/.ghci)
Write statements as if you were inside the GHCi REPL
Basic .ghci file
code: .ghci
:set prompt "λ> "
:seti -XOverloadedStrings
:seti -XScopedTypeVariables
:set -Wall
:set -fno-warn-type-defaults
:set -DGHCI
:set +s
:set +t
:set -package pretty-simple
import Text.Pretty.Simple (pPrint)
:set -interactive-print pPrint
pretty-simple is a pretty printer for Haskell data type.
You'd need pretty-simple package which enables the ghci to print things in a readable way. If you are using stack, run stack build pretty-simple.
If you are using custom prelude, (e.g. RIO, Protolude) then import them as well.
Apply stylish-haskell on all the haskell file on given directory
stylish-haskell is Haskell code prettifier.
The command below allows you to apply stylish-haskell on all the haskell files on given directory.
code:stylish.hs
find . -type f -name "*hs" -not -path '.git' -not -path '*.stack-work*' -print0 | xargs -0 stylish-haskell -i
Use ghcid to show errors and warnings
ghcid is an small tool which enables you list of the errors and warnings in your project in the console.
How it works is that, it opens ghci and runs :reload whenever your source code changes, formatting the output to fit a fixed height console. If you're spamming :r on the repl to check the errors/warning, this should help you greatly.
Here's Matt Parson's blogpost explaining ghcid.
Use makefile to ease your CLI experience
When using stack, entering command becomes tedious. For example, when you want to run ghcid on test suites, stack command will be:
ghcid --command "stack ghci <project_name>:test:<project_name>-test --ghci-options=-fobject-code"
Instead, you can use makefile, which stores list of bash scripts you can use.
Example
gist
code:makefile
PROJECT_NAME = <enter your project name here>
help: ## Print documentation
@grep -E '^a-zA-Z_-+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
stylish-haskell: ## Apply stylish-haskell on all *.hs files
@find . -type f -name "*.hs" -not -path '.git' -not -path '*.stack-work*' -print0 | xargs -0 stylish-haskell -i
ghci: ## Run repl
@stack ghci $(PROJECT_NAME):lib --haddock-deps --ghci-options=-fobject-code
ghcid: ## Run ghcid
@ghcid --command "stack ghci $(PROJECT_NAME):lib --ghci-options=-fobject-code"
run-test: ## Build & run test
@stack build --fast && \
stack test --fast
test-ghci: ## Run repl on test suites
@stack ghci $(PROJECT_NAME):lib $(PROJECT_NAME):test:$(PROJECT_NAME)-test
test-ghcid: ## Run ghcid on test suites
@ghcid --command "stack ghci $(PROJECT_NAME):lib $(PROJECT_NAME):test:$(PROJECT_NAME)-test --ghci-options=-fobject-code"
.PHONY: stylish-haskell ghci ghcid run-test test-ghcid test-ghci help
If you want to run ghcid on test suites, then you can run make test-ghcid.
If you want to apply stylish-haskell on all haskell files, run make stylish-haskell
#development