Stack
Profiling with Stack
Section titled “Profiling with Stack”Configure profiling for a project via stack. First build the project with the --profile flag:
stack build --profileGHC flags are not required in the cabal file for this to work (like -prof). stack will automatically turn on profiling for both the library and executables in the project. The next time an executable runs in the project, the usual +RTS flags can be used:
stack exec -- my-bin +RTS -pStructure
Section titled “Structure”File structure
Section titled “File structure”A simple project has the following files included in it:
➜ helloworld lsLICENSE Setup.hs helloworld.cabal src stack.yamlIn the folder src there is a file named Main.hs. This is the “starting point” of the helloworld project. By default Main.hs contains a simple “Hello, World!” program.
Main.hs
module Main where
main :: IO ()main = do putStrLn "hello world"Running the program
Section titled “Running the program”Make sure you are in the directory helloworld and run:
stack build # Compile the programstack exec helloworld # Run the program# prints "hello world"Build and Run a Stack Project
Section titled “Build and Run a Stack Project”In this example our project name is “helloworld” which was created with stack new helloworld simple
First we have to build the project with stack build and then we can run it with
stack exec helloworld-exeViewing dependencies
Section titled “Viewing dependencies”To find out what packages your project directly depends on, you can simply use this command:
stack list-dependenciesThis way you can find out what version of your dependencies where actually pulled down by stack.
Haskell projects frequently find themselves pulling in a lot of libraries indirectly, and sometimes these external dependencies cause problems that you need to track down. If you find yourself with a rogue external dependency that you’d like to identify, you can grep through the entire dependency graph and identify which of your dependencies is ultimately pulling in the undesired package:
stack dot --external | grep template-haskellstack dot prints out a dependency graph in text form that can be searched. It can also be viewed:
stack dot --external | dot -Tpng -o my-project.pngYou can also set the depth of the dependency graph if you want:
stack dot --external --depth 3 | dot -Tpng -o my-project.pngInstalling Stack
Section titled “Installing Stack”Mac OSX
Using Homebrew:
brew install haskell-stackCreating a simple project
Section titled “Creating a simple project”To create a project called helloworld run:
stack new helloworld simpleThis will create a directory called helloworld with the files necessary for a Stack project.
Stack install
Section titled “Stack install”By running the command
stack installStack will copy a executable file to the folder
/Users/<yourusername>/.local/bin/Stackage Packages and changing the LTS (resolver) version
Section titled “Stackage Packages and changing the LTS (resolver) version”Stackage is a repository for Haskell packages. We can add these packages to a stack project.
Adding lens to a project.
Section titled “Adding lens to a project.”In a stack project, there is a file called stack.yaml. In stack.yaml there is a segment that looks like:
resolver: lts-6.8Stackage keeps a list of packages for every revision of lts. In our case we want the list of packages for lts-6.8 To find these packages visit:
https://www.stackage.org/lts-6.8 # if a different version is used, change 6.8 to the correct resolver number.Looking through the packages, there is a Lens-4.13.
We can now add the language package by modifying the section of helloworld.cabal:
build-depends: base >= 4.7 && < 5to:
build-depends: base >= 4.7 && 5, lens == 4.13Obviously, if we want to change a newer LTS (after it’s released), we just change the resolver number, eg.:
resolver: lts-6.9With the next stack build Stack will use the LTS 6.9 version and hence download some new dependencies.