Skip to content

Latest commit

 

History

History

examples

Haskell examples

Haskell project Directory examples\ contains Haskell code examples coming from various websites - mostly from the Haskell project.

In the following we present the two examples Factorial and QuickSort.

We can build/run code examples in directory examples in several ways :

Build tool Build file Parent file Environment(s)
cabal.exe Factorial.cabal n.a. Any
cmd.exe build.bat 1 n.a. Windows only
make.exe Makefile Makefile.inc Any
mvn.cmd pom.xml pom.xml Any
sh.exe build.sh Cygwin/MSYS2/Unix only
stack.exe stack.yaml n.a. Any

Factorial Example

The directory structure of project Factorial looks as follows:

> cd
H:\examples\Factorial
 
> tree /a /f . | findstr /v "^[A-Z]"
|   .gitignore
|   .hlint.yaml
|   build.bat
|   build.sh
|   Factorial.cabal
|   Makefile
|   stack.yaml
|   Setup.hs
|
\---app
        Main.hs

Cabal

Command cabal run all builds and executes the Haskell application (build file Factorial.cabal):

> where cabal
C:\opt\ghc-8.10.7\bin\cabal.exe
 
> cabal run all
Resolving dependencies...
Build profile: -w ghc-8.10.7 -O1
In order, the following will be built (use -v for more details):
 - Factorial-0.1.0.0 (exe:Factorial) (first run)
Configuring executable 'Factorial' for Factorial-0.1.0.0..
Preprocessing executable 'Factorial' for Factorial-0.1.0.0..
Building executable 'Factorial' for Factorial-0.1.0.0..
[1 of 1] Compiling Main             ( app\Main.hs, H:\examples\Factorial\dist-newstyle\build\x86_64-windows\ghc-8.10.7\Factorial-0.1.0.0\x\Factorial\build\Factorial\Factorial-tmp\Main.o )
Linking H:\examples\Factorial\dist-newstyle\build\x86_64-windows\ghc-8.10.7\Factorial-0.1.0.0\x\Factorial\build\Factorial\Factorial.exe ...
factorialRec(5) =120
factorialRec2(5)=120
factorialFold(5)=120
factorialProd(5)=120

Stack

Command stack run builds and executes the Haskell application (build file stack.yaml):

> where stack
C:\opt\stack-2.13.1\stack.exe
 
> stack --silent run
factorialRec(5) =120
factorialRec2(5)=120
factorialFold(5)=120
factorialProd(5)=120

🔎 We can build a profile-version of the project and execute the profile-instrumented Haskell application as follows:

> stack build --profile
 
> stack exec target\dist\29cc6475\build\Factorial\Factorial.exe -- +RTS -p
factorialRec(5) =120
factorialRec2(5)=120
factorialFold(5)=120
factorialProd(5)=120

Profiling results are stored in file Factorial.prof:

> more Factorial.prof
        Tue Mar 03 22:21 2020 Time and Allocation Profiling Report  (Final)
 
           Factorial.exe +RTS -p -RTS
 
        total time  =        0.00 secs   (0 ticks @ 1000 us, 1 processor)
        total alloc =      56,488 bytes  (excludes profiling overheads)
 
COST CENTRE MODULE           SRC                         %time %alloc
 
main        Main             app\Main.hs:(33,1)-(39,13)    0.0   36.0
CAF         GHC.IO.Handle.FD <entire-module>               0.0   61.5
 
                                                                                      individual      inherited
COST CENTRE      MODULE                   SRC                        no.     entries  %time %alloc   %time %alloc
 
MAIN             MAIN                     <built-in>                 125          0    0.0    0.9     0.0  100.0
 CAF             GHC.IO.Handle.Text       <entire-module>            176          0    0.0    0.1     0.0    0.1
[..]

Make

Command make -s run builds and executes the Haskell application (build file Makefile)

> where make
C:\opt\make-3.81\bin\make.exe
 
> make -s run
factorialRec(5) =120
factorialRec2(5)=120
factorialFold(5)=120
factorialProd(5)=120

Maven

Command mvn -q compile exec:exec builds and executes the Haskell application (build file pom.xml)

> where mvn.cmd
C:\opt\apache-maven\bin\mvn.cmd
 
> mvn -q clean compile exec:exec
[1 of 1] Compiling Main             ( app\Main.hs, target\gen\Main.o )
Linking target/Main.exe ...
factorialRec(5) =120
factorialRec2(5)=120
factorialFold(5)=120
factorialProd(5)=120

Batch

Command build clean run builds and executes the Haskell application (option -verbose prints progress messages) :

> where build
H:\examples\Factorial\build.bat
 
> build clean run
factorialRec(5) =120
factorialRec2(5)=120
factorialFold(5)=120
factorialProd(5)=120

🔎 Commands build and mvn accept one or more subcommands while cabal and stack accept only one. For instance the following command lines produce the same result:

> build clean run
> mvn clean compile exec:exec
> cabal clean && cabal run all
> stack clean && stack run

With option -debug command build also displays several useful informations, e.g.

  • Execution environment : display actual value of properties, options, variables.
  • Conditional processing : compile only if target is older than sources.
  • Execution transparency : the executed console command with options and arguments can be copied/run separately.
> build -debug run
[build] Properties : _PACKAGE_NAME=Factorial
[build] Options    : _TIMER=0 _VERBOSE=0
[build] Subcommands: _CLEAN=1 _COMPILE=1 _DOC=0 _LINT=0 _RUN=1 _TEST=0
[build] Variables  : "CABAL_DIR=%APPDATA%\cabal"
[build] Variables  : "GHC_HOME=C:\opt\ghc-8.10.7"
[build] 00000000000000 Target : "H:\examples\Factorial\target\Factorial.exe"
[build] 20210208190257 Sources: "H:\examples\Factorial\app\*.hs"
[build] _ACTION_REQUIRED=1
[build] ghc.exe -Wall -Werror -o "H:\examples\Factorial\target\Main.exe" -hidir "H:\examples\Factorial\target\gen" -odir "H:\examples\Factorial\target\gen"  "H:\examples\Factorial\app\Main.hs"
[1 of 1] Compiling Main             ( H:\examples\Factorial\app\Main.hs, H:\examples\Factorial\target\gen\Main.o )
Linking H:\examples\Factorial\target\Main.exe ...
[build] H:\examples\Factorial\target\Main.exe
factorialRec(5) =120
factorialRec2(5)=120
factorialFold(5)=120
factorialProd(5)=120
[build] _EXITCODE=0

QuickSort Example

The directory structure of project QuickSort looks as follows:

> cd
H:\examples\QuickSort
 
> tree /a /f . | findstr /v "^[A-Z]"
|   .gitignore
|   build.bat
|   build.sh
|   QuickSort.cabal
|   Setup.hs
|   stack.yaml
|
\---app
        Main.hs

Cabal

Command cabal run all builds and executes the Haskell application (build file QuickSort.cabal):

> where cabal
C:\opt\ghc-8.10.7\bin\cabal.exe
 
> cabal clean && cabal run all
Resolving dependencies...
Build profile: -w ghc-8.10.7 -O1
In order, the following will be built (use -v for more details):
 - QuickSort-0.1.0.0 (exe:QuickSort) (first run)
Configuring executable 'QuickSort' for QuickSort-0.1.0.0..
Preprocessing executable 'QuickSort' for QuickSort-0.1.0.0..
Building executable 'QuickSort' for QuickSort-0.1.0.0..
[1 of 1] Compiling Main             ( app\Main.hs, H:\examples\QuickSort\dist-newstyle\build\x86_64-windows\ghc-8.10.7\QuickSort-0.1.0.0\x\QuickSort\build\QuickSort\QuickSort-tmp\Main.o )
Linking H:\examples\QuickSort\dist-newstyle\build\x86_64-windows\ghc-8.10.7\QuickSort-0.1.0.0\x\QuickSort\build\QuickSort\QuickSort.exe ...
input list       : [8,4,0,3,1,23,11,18]
sorted(filter)   : [0,1,3,4,8,11,18,23]
sorted(list comp): [0,1,3,4,8,11,18,23]

Stack

Command stack run builds and executes the Haskell application (build file stack.yaml):

> where stack
C:\opt\stack-2.13.1\stack.exe
 
> stack clean && stack --silent run
input list       : [8,4,0,3,1,23,11,18]
sorted(filter)   : [0,1,3,4,8,11,18,23]
sorted(list comp): [0,1,3,4,8,11,18,23]

Make

Command make -s run builds and executes the Haskell application (build file Makefile)

> make -s run
input list       : [8,4,0,3,1,23,11,18]
sorted(filter)   : [0,1,3,4,8,11,18,23]
sorted(list comp): [0,1,3,4,8,11,18,23]
sorted(ST)       : [0,1,3,4,8,11,18,23]

Maven

Command mvn -q compile exec:exec builds and executes the Haskell application (build file pom.xml)

> mvn -q clean compile exec:exec
[1 of 1] Compiling Main             ( app\Main.hs, target\gen\Main.o )
Linking target/Main.exe ...
input list       : [8,4,0,3,1,23,11,18]
sorted(filter)   : [0,1,3,4,8,11,18,23]
sorted(list comp): [0,1,3,4,8,11,18,23]

Batch

Command build clean run builds and executes the Haskell application:

> where build
H:\examples\QuickSort\build.bat
 
> build clean run
input list       : [8,4,0,3,1,23,11,18]
sorted(filter)   : [0,1,3,4,8,11,18,23]
sorted(list comp): [0,1,3,4,8,11,18,23]

With option -debug command build also displays several useful informations, e.g.

  • Execution environment : display actual value of properties, options, variables.
  • Conditional processing : compile only if target is older than sources.
  • Execution transparency : the executed console command with options and arguments can be copied/run separately.
> build -debug run
[build] Properties : _PACKAGE_NAME=Factorial
[build] Options    : _TIMER=0 _VERBOSE=0
[build] Subcommands: _CLEAN=0 _COMPILE=1 _DOC=0 _LINT=0 _RUN=1 _TEST=0
[build] Variables  : "CABAL_DIR=%APPDATA%\cabal"
[build] Variables  : "GHC_HOME=C:\opt\ghc-8.10.7"
[build] 00000000000000 Target : "H:\examples\Factorial\target\Factorial.exe"
[build] 20210208190257 Sources: "H:\examples\Factorial\app\*.hs"
[build] _ACTION_REQUIRED=1
[build] "C:\opt\ghc-8.10.7\bin\ghc.exe" -Wall -Wmissing-import-lists -Wincomplete-uni-patterns -Werror -hidir "H:\examples\Factorial\target\gen" -odir "H:\examples\Factorial\target\gen" -o "H:\examples\Factorial\target\Factorial.exe"  "H:\examples\Factorial\app\Main.hs"

Loaded package environment from %APPDATA%\ghc\x86_64-mingw32-8.10.7\environments\default
[1 of 1] Compiling Main             ( H:\examples\Factorial\app\Main.hs, H:\examples\Factorial\target\gen\Main.o )
Linking H:\examples\Factorial\target\Factorial.exe ...
[build] "H:\examples\Factorial\target\Factorial.exe"
factorialRec(5) =120
factorialRec2(5)=120
factorialFold(5)=120
factorialProd(5)=120
[build] _EXITCODE=0

Footnotes

[1] build.bat

In project Factorial the batch file build.bat reads several properties directly from Factorial.cabal if the Cabal project file is present; for instance: name, synopsis, version and ghc_options.

mics/November 2024