Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Haskell Quickbite #179

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 37 additions & 48 deletions haskell.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
## Haskell at CARC

Haskell is a strongly typed functional language. In this QuickByte you will learn how to setup Haskell at CARC and create a simple matrix multiplication project using stack.
Haskell is a strongly typed functional language. In this QuickByte you will learn how to use the GHCUP module to install ghc versions and run a simple stack program in haskell.

On Wheeler:
### Stack setup on Hopper

First we will install and load the stack environment:
1) `module load ghcup`

1) `module load miniconda3`
2) `ghcup install ghc 9.10.1`

2) `conda create -n haskell stack -c conda-forge`

3) `source activate haskell`

Or, alternatively, load the spack module:

`module load haskell-stack-stable-gcc-4.8.5-exczvin`

*Please note* that you should choose one method for using Haskell-Stack, Anaconda or the installed module, as you can run into
compatability issues switching between methods.
`ghcup` is the Glasglow haskell compiler upgrader. You can download as many ghc versions as you want, and they will be stored in your home directory under the `~/.ghcup/ghc/` directory.

Now create a new stack project:

Expand All @@ -27,56 +18,54 @@ Now create a new stack project:

Edit the main source file with your preferred text editor:

6) `emacs app/Main.hs`
6) `vim app/Main.hs`

The following code creates and multiplies two 4x4 matrices:

module Main where

--------------------------
``` haskell
module Main where
import Lib
import Data.Matrix

import Lib
import Data.Matrix
main :: IO ()

main :: IO ()
m1 = matrix 4 4 $ \(i,j) -> 2*i - j
m2 = matrix 4 4 $ \(i,j) -> 2*i - j

m1 = matrix 4 4 $ \(i,j) -> 2*i - j
m2 = matrix 4 4 $ \(i,j) -> 2*i - j
test = multStd m1 m2

test = multStd m1 m2
main = do
putStrLn (show test)

main = do
putStrLn (show test)
```
--------------------------
Add the dependency on the matrix package to the project:

7) `vim package.yaml`

Add the dependency on the matrix package to the project:
8) Add `- matrix` under dependencies in the executables section so it reads:

7) `emacs package.yaml`

8) Add "- matrix" under dependencies in the executables section so it reads:
dependencies:
- matmul
- matrix

```
dependencies:
- matmul
- matrix
```

Build your project:

9) `stack build`

Run your compiled program:

```
(haskell) mfricke@wheeler-sn:~/matmul$
.stack-work/install/x86_64-linux/lts-13.17/8.6.4/bin/matmul-exe
┌ ┐
│ -18 -16 -14 -12 │
│ 14 8 2 -4 │
│ 46 32 18 4 │
│ 78 56 34 12 │
└ ┘
```
now that your program has been compiled, you can search for the location the executable file was created;

[rdscher@hopper matmul]$ find . -type f -name matmul-exe
./.stack-work/dist/x86_64-linux/ghc-9.6.5/build/matmul-exe/matmul-exe

And now we can run our program;

[rdscher@hopper matmul]$ ./.stack-work/dist/x86_64-linux/ghc-9.6.5/build/matmul-exe/matmul-exe
┌ ┐
│ -18 -16 -14 -12 │
│ 14 8 2 -4 │
│ 46 32 18 4 │
│ 78 56 34 12 │
└ ┘

*This quickbyte was validated on 6/10/2024*