Mamtaj Akter
Mamtaj Akter

Reputation: 21

Could not find module ‘Criterion.Main’

I have copied the following code from criterion tutorial:

import Criterion.Main

-- The function we're benchmarking.
fib m | m < 0     = error "negative!"
      | otherwise = go m
  where
    go 0 = 0
    go 1 = 1
    go n = go (n-1) + go (n-2)

-- Our benchmark harness.
main = defaultMain [
  bgroup "fib" [ bench "1"  $ whnf fib 1
               , bench "5"  $ whnf fib 5
               , bench "9"  $ whnf fib 9
               , bench "11" $ whnf fib 11
                ]
     ]

I am getting the following error:

fiber.hs:1:1: error: Could not find module ‘Criterion.Main’ Use -v to see a list of the files searched for. | 1 | import Criterion.Main | ^^^^^^^^^^^^^^^^^^^^^

The GHC version I am using is 8.4.2 and cabal version is 2.2.0.0.

I tried to install the criterion package by:

cabal update
Downloading the latest package list from hackage.haskell.org
To revert to previous state run: cabal update --index-state='2018-06- 
01T04:23:08Z'

cabal install -j --disable-tests criterion
clang: warning: argument unused during compilation: '-nopie' [-Wunused- 
command-line-argument]
.
.
.
.
cabal: Error: some packages failed to install:
abstract-deque-0.3-IvBVpgU2tvq3eILHsBTjFR failed during the building 
phase.
The exception was:
ExitFailure 1
aeson-1.3.1.1-J9Jy9Bz77dxJho59OWZvUt depends on aeson-1.3.1.1 which 
failed to install.
attoparsec-0.13.2.2-5fvnJr9WRPCJj7fMCLKoI7 depends on attoparsec- 
0.13.2.2
which failed to install.
cassava-0.5.1.0-CNxiRQP2h44BSkY7PLw3nv depends on cassava-0.5.1.0 which 
failed
to install.
criterion-1.4.1.0-1CDqJgx5SYk1Xphp8S6hvK depends on criterion-1.4.1.0 
which failed to install.
microstache-1.0.1.1-DUzquwnO02sC17piNr03EI depends on microstache- 
1.0.1.1 which failed to install.
monad-par-0.3.4.8-5Qx7yEAZEkjJbqZykcUjIa depends on monad-par-0.3.4.8 
which failed to install.
monad-par-extras-0.3.3-755mClpwIBoBMORFcN7gCY failed during the 
building phase. The exception was:
ExitFailure 1
scientific-0.3.6.2-65wDZeAE9ZIBkaesoEq4I0 failed during the building 
phase.
The exception was:
ExitFailure 1
statistics-0.14.0.2-GHJ1OiovyXP1FEjV1emzr8 depends on statistics- 
0.14.0.2 which failed to install.
text-short-0.1.2-JRY9FeZhxkoAZrj3rm5IJZ failed during the building 
phase. The exception was:
ExitFailure 1
uuid-types-1.0.3-tE9Bfk2PgXDUPgbtamBdI failed during the building 
phase. The exception was:
ExitFailure 1

Upvotes: 2

Views: 623

Answers (1)

Michael Snoyman
Michael Snoyman

Reputation: 31315

One approach would be to use the Stack script interpreter to run this. In order to do so, you would first install Stack, then add the script interpreter bits to the top of your file, e.g.:

#!/usr/bin/env stack
-- stack --resolver lts-11.10 script --optimize
import Criterion.Main

-- The function we're benchmarking.
fib m | m < 0     = error "negative!"
    | otherwise = go m
where
    go 0 = 0
    go 1 = 1
    go n = go (n-1) + go (n-2)

-- Our benchmark harness.
main = defaultMain [
bgroup "fib" [ bench "1"  $ whnf fib 1
            , bench "5"  $ whnf fib 5
            , bench "9"  $ whnf fib 9
            , bench "11" $ whnf fib 11
                ]
    ]

Note that I added the --optimize option on the second line to ensure that Stack would compile your code with optimizations on, instead of the default of using runghc.

And finally run the file with stack fiber.hs (or whatever you called the file).

Upvotes: -1

Related Questions