Haskell Stack Tutorial

Table of Contents

1 Haskell Stack on Ubuntu

  • Stack will be installed all file under $HOME/.stack

2 Haskell Stack Tool

  • Stack is a better tool to build Haskell project.
    • You can specify your resolver and package names inside myproject.cabal file.
    • stack build will automatically download the packages to stack project. It is like Java Maven build.
  • Home brew it on your MacOS

    brew update
    brew install haskell-stack
    # you might need to relink the stack symbol links
    rm /usr/local/bin/stack
    brew link --overwrite haskell-stack
    
  • Find out the location of stack and check your stack version

    which stack
    # /usr/local/bin/stack
    stack --version
    # /usr/local/Cellar/haskell-stack/1.9.3:
    
  • Create new project with Stack called myproject

    stack new myproject simple
    
  • Download GHC for your project, it will take a while to download and it will not interfere your local GHC.

    stack setup
    
  • Build Your Hello World

    stack build
    
  • Run your project

    stack exec myproject
    
  • Add time package to your build. myproject.cabal

    # myproject.cabal
    executable myproj
    hs-source-dirs:      src
    main-is:             Main.hs
    default-language:    Haskell2010
    build-depends:       base >= 4.7 && < 5,
                         time
    
  • Now we can use functions(e.g. getCurrentTime) from time package

    import Data.Time
    main::()
    main = do
         time <- getCurrentTime
         putStrLn (show time)
    
    • stack build and stack exec myproject
  • Try to figure out how to use your own module on Stack.

    • It seems to me you have to put your modules under src inside .cabal file.
    • You can use symbol link. e.g.
    cd src && ln -s $b/haskelllib/AronModule.hs AronModule.hs
    
    # myproject.cabal
    hs-source-dirs:  src
    
  • Use different resolver Long Term Supported 12.13 ghc-8.4.3 in file: stack.yaml

    -- ghc 8.4.3
    stack new myproject --resolver=12.13 simple
    -- check your stack.yaml                                  
    
  • GHC 8.6.5 resolver: lts-13.28 GHC 8.6.5 Resolver 13.28

3 Add dependency to .cabal file does not mean the package will be download.

  • some packages needs to be added to stack.yaml with extra-deps~
  • I still did not understand which package are supported to in stack.yaml or myproject.cabal.

4 Hoogle search function from non standard package

hoogle +package.name function_name

5 Update Hoogle database

# warning: take lots of horse power. freeze your computer
sudo hoogle database all

6 Change install directory, install binary in /tmp/bin

stack install  --local-bin-path  /tmp/bin

7 Get executable path

stack path --local-install-root

Author: cat

Created: 2019-08-02 Fri 14:55

Validate