2
votes

From the book Beginning Haskell, I learned that I can build a package from cabal setup file (chapter2.cabal). The source is downloadable from http://www.apress.com/downloadable/download/sample/sample_id/1516/

For example, this is an example of the Cabal file from Section 2 example.

name:           chapter2
version:        0.1
cabal-version:  >=1.2
build-type:     Simple
author:         Alejandro Serrano

library
  hs-source-dirs:  src
  build-depends:   base >= 4
  ghc-options:     -Wall
  exposed-modules: 
                   Chapter2.Section2.Example,
                   Chapter2.SimpleFunctions
  other-modules:   
                   Chapter2.DataTypes,
                   Chapter2.DefaultValues

After the cabal build, I can get the dynamic and static libraries compiled.

.
├── Setup.hs
├── chapter2.cabal
├── dist
│   ├── build
│   │   ├── Chapter2
...
│   │   ├── autogen
│   │   │   ├── Paths_chapter2.hs
│   │   │   └── cabal_macros.h
│   │   ├── libHSchapter2-0.1-ghc7.8.3.dylib <-- dynamic lib
│   │   └── libHSchapter2-0.1.a <-- static lib
│   ├── package.conf.inplace
│   └── setup-config
└── src
    └── Chapter2
        ├── DataTypes.hs
        ├── DefaultValues.hs
        ├── Section2
        │   └── Example.hs
        └── SimpleFunctions.hs

Then, how can I use the library functions from other Haskell code (in both ghc and ghci)? For example, src/Chapter2/SimpleFunctions.hs has maxim function, how can I invoke this function compiled in the form of Haskell library?

maxmin list = let h = head list
              in if null (tail list)
                 then (h, h)
                 else ( if h > t_max then h else t_max
                      , if h < t_min then h else t_min )
                      where t = maxmin (tail list)
                            t_max = fst t
                            t_min = snd t 
2

2 Answers

1
votes

To use maxmin from ghci just load the source file:

chapter2$ ghci
> :l src/Chapter2/SimpleFunctions
> maxmin [1,2,3]
(3,1)

I am not sure what you mean when saying 'how to use the maxmin function from ghc'. I suppose what you meant is 'how to use maxmin in my programs' (which can be compiled with ghc). If you look at first line of src/Chapter2/SimpleFunctions.hs you can see that its in a module called Chapter2.SimpleFunctions. So in your own program/code you need to import that module to be able to use maxmin. As an example of this:

chapter2$ cat Test.hs

-- In your favorite editor write down this file.
import Chapter2.SimpleFunctions

main = print $ maxmin [1,2,3]

chapter2$ ghc Test.hs -i.:src/
chapter2$ ./Test
(3,1)

The ghc Test.hs -i.:src/ is teling ghc to look for files in current and src/ directory.

0
votes

With cabal install you configure your system to use the library you just created. The library is installed in ~/.cabal/lib.

enter image description here

For the usage with ghci, you can import the library.

Prelude> import Chapter2.SimpleFunctions
Prelude Chapter2.SimpleFunctions> maxmin [1,2]
(2,1)

For the usage with ghc, you also can import the library so that the compiler do the linking automatically.

import Chapter2.SimpleFunctions

main :: IO ()
main = putStrLn $ show $ maxmin [1,2,3]

Compile and run:

chapter2> ghc ex.hs 
[1 of 1] Compiling Main             ( ex.hs, ex.o )
Linking ex ...
chapter2> ./ex
(3,1)