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
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).
Related
test1 = hspec $ do
describe "blabla" $ do
it "should be equl" $ verbose $
\input-> ...
In the above code, when a test failed, it prints the failed input. But I'm actually interested in another value that can be calculated from input. Can I ask QuickCheck to print the other value?
Somehow I've never seen it advertised, but you can use hspecs expectations inside of QuickCheck properties. Here is an example:
describe "blabla" $ do
it "should be equl" $ verbose $ \input ->
round input `shouldBe` floor (input :: Double)
Above property is clearly not true, so it should fail. Since we are not only interested input, but also want to know the computed values from it, shouldBe will give us just that:
3) blabla should be equl
Falsifiable (after 2 tests and 4 shrinks):
0.6
expected: 0
but got: 1
Naturally, due to verbose, only input will be printed for passing tests, while computed value (eg. round input) will only be printed for a failed test case, which is what you were looking for it seems anyways.
I've noticed some really strange behaviour:
I'm trying to get the haskell z3 bindings to work on windows. But if I insert code from the library into the Main module putStrLn stops working. How can that be?
The Setup
I used the precompiled x64 binary of the z3 solver (z3.4.8.4) for windows (and put it in C:\mylibs\z3-4.8.4_x64).
Then I created a stack project haskellZ3Test
The relevant parts from package.yaml:
dependencies:
- base >= 4.7 && < 5
- z3 <= 408.0
The relevant parts from stack.yaml:
resolver: lts-13.28
packages:
- .
- location:
git: https://github.com/IagoAbal/haskell-z3.git
commit: b10e09b8a809fb5bbbb1ef86aeb62109ece99cae
extra-dep: true
extra-include-dirs:
- "C:\\mylibs\\z3-4.8.4_x64\\include"
extra-lib-dirs:
- "C:\\mylibs\\z3-4.8.4_x64\\bin"
The Problem
Here is Main version 1:
module Main where
import Z3.Monad
main :: IO ()
main = putStrLn "hello world"
If I stack build and then stack exec haskellZ3Test-exe I get hello world on the command line as expected.
However - here is Main version 2:
module Main where
import Z3.Monad
ast :: Z3 AST
ast = do
a <- mkFreshBoolVar "A"
b <- mkFreshBoolVar "B"
mkImplies a b
main :: IO ()
main = putStrLn "hello world"
If I stack build and then stack exec haskellZ3Test-exe now I get nothing ...
Honestly I'm pretty puzzled - compiling and linking seems to work fine.
Any help is highly appreciated. Many thanks in advance.
I have problem using doctest with QuickCheck on NixOS
-- code.hs
--
-- $setup
-- >>> import Test.QuickCheck
--
-- |
-- This test will pass
-- >>> 1 + 1
-- 2
--
-- This test use QuickCheck
-- prob> \xs -> reverse xs = reverse . id $ xs
--
-- The last test will fail to ensure doctest was running
-- >>> 1 == 0
-- True
--
main :: IO ()
main = pure ()
The default haskellPackages.doctest doesn't include QuickCheck
$ nix-shell -p haskellPackages.doctest --run 'doctest code.hs'
code.hs:4: failure in expression `import Test.QuickCheck'
expected:
but got:
<no location info>: error:
Could not find module ‘Test.QuickCheck’
It is not a module in the current program, or in any known package.
Examples: 3 Tried: 1 Errors: 0 Failures: 1
so I try to make an overrride version as mydoctest
# ~/.config/nixpkgs/overlays/doctest.nix
self: super:
with super.haskellPackages;
let
myghc = ghcWithPackages (p: with p;
[
QuickCheck
]);
in
{
mydoctest = doctest.override {
ghc = myghc;
};
}
It still can't find QuickCheck
$ nix-shell -p mydoctest --run 'doctest code.hs'
code.hs:4: failure in expression `import Test.QuickCheck'
expected:
but got:
<no location info>: error:
Could not find module ‘Test.QuickCheck’
It is not a module in the current program, or in any known package.
Examples: 3 Tried: 1 Errors: 0 Failures: 1
but it work if I call it via ghc
$ nix-shell -p mydoctest --run 'ghc -e ":!doctest code.hs"'
code.hs:15: failure in expression `1 == 0'
expected: True
but got: False
Examples: 3 Tried: 3 Errors: 0 Failures: 1
myghc is there but didn't use by mydoctest
$ nix-shell -p mydoctest --run 'doctest --version'
doctest version 0.16.0
using version 8.4.3 of the GHC API
using /nix/store/g1dj8c6h6g8rbb8cv9bgmp1h2f3bxk1l-ghc-8.4.3-with-packages/bin/ghc
$ nix-shell -p mydoctest --run 'which ghc'
/nix/store/qj3vnm903p77nxiraxqxm9b8gbz5rpia-ghc-8.4.3-with-packages/bin/ghc
How can I make doctest to use my override ghc ?
Haskell derivations are not suitable as nix-shell environments by themselves.
The Nixpkgs manual recommends creating a derivation for the purpose of a nix-shell only, using a ghcWithPackages invocation as a dependency.
If the file you're trying to run is part of a haskell package, you can use cabal2nix to do most of the work for you.
I recommend the latter, because it does not pollute your user-wide state (whether profile or user-specific overlay), it is easier to share with others and provides structure that is sufficient to scale from toy example to a valuable package.
I had the same problem trying to get my nix/haskell dev environment set up and found a solution in this reddit post. To get doctest to use a specific version of ghc you can set the environment variable NIX_GHC:
$ doctest --version
doctest version 0.16.3
using version 8.10.3 of the GHC API
using /nix/store/9bm9bs9c4zpl17zdk1d0azid44x3h99b-ghc-8.10.3/bin/ghc-8.10.3
$ export NIX_GHC=/nix/store/zvx9dmfqaa8wibygkbibg4kl98pfj9qv-ghc-8.10.3-with-packages/bin/ghc
$ doctest --version
doctest version 0.16.3
using version 8.10.3 of the GHC API
using /nix/store/zvx9dmfqaa8wibygkbibg4kl98pfj9qv-ghc-8.10.3-with-packages/bin/ghc
I am trying to update my structured haskell mode using the cabal install command. Unfortunately, it's failing
obelix:~ rk$ cabal install structured-haskell-mode
Warning: The install command is a part of the legacy v1 style of cabal usage.
Please switch to using either the new project style and the new-install
command or the legacy v1-install alias as new-style projects will become the
default in the next version of cabal-install. Please file a bug if you cannot
replicate a working v1- use case with the new-style commands.
For more information, see: https://wiki.haskell.org/Cabal/NewBuild
clang: warning: argument unused during compilation: '-nopie' [-Wunused-command-line-argument]
Resolving dependencies...
Starting structured-haskell-mode-1.1.0
Building structured-haskell-mode-1.1.0
Failed to install structured-haskell-mode-1.1.0
Build log ( /Users/rk/.cabal/logs/ghc-8.4.3/structured-haskell-mode-1.1.0-BtZnu5PUgZA64b8d6lKBIV.log ):
cabal: Entering directory '/var/folders/m7/cw4w1yc11pv_t25pmlyqc4qm0000gn/T/cabal-tmp-1494/structured-haskell-mode-1.1.0'
Configuring structured-haskell-mode-1.1.0...
clang: warning: argument unused during compilation: '-nopie' [-Wunused-command-line-argument]
Preprocessing executable 'structured-haskell-mode' for structured-haskell-mode-1.1.0..
Building executable 'structured-haskell-mode' for structured-haskell-mode-1.1.0..
[1 of 1] Compiling Main ( src/Main.hs, dist/build/structured-haskell-mode/structured-haskell-mode-tmp/Main.o )
src/Main.hs:165:18: error:
• The constructor ‘Deriving’ should have 3 arguments, but has been given 2
• In the pattern: Deriving _ ds#(_ : _)
In the pattern: Just (Deriving _ ds#(_ : _))
In a case alternative:
Just (Deriving _ ds#(_ : _))
-> [spanHSE
(show "InstHeads")
"InstHeads"
(SrcSpan
(srcSpanFilename start)
(srcSpanStartLine start)
(srcSpanStartColumn start)
(srcSpanEndLine end)
(srcSpanEndColumn end)) |
Just (IRule _ _ _ (IHCon (SrcSpanInfo start _) _)) <- [listToMaybe
ds],
Just (IRule _ _ _ (IHCon (SrcSpanInfo end _) _)) <- [listToMaybe
(reverse ds)]]
|
165 | Just (Deriving _ ds#(_:_)) ->
| ^^^^^^^^^^^^^^^^^^^
cabal: Leaving directory '/var/folders/m7/cw4w1yc11pv_t25pmlyqc4qm0000gn/T/cabal-tmp-1494/structured-haskell-mode-1.1.0'
cabal: Error: some packages failed to install:
structured-haskell-mode-1.1.0-BtZnu5PUgZA64b8d6lKBIV failed during the
building phase. The exception was:
ExitFailure 1
It seems that it's calling the Deriving constructor with the wrong number of arguments. Any suggestions for a quick fix?
I'm checking out the computing music development library Euterpea and I'm trying to compile the following basic program:
import Euterpea
t251 :: Music Pitch
t251 = let dMinor = d 4 wn :=: f 4 wn :=: a 4 wn
gMajor = g 4 wn :=: b 4 wn :=: d 5 wn
cMajor = c 4 bn :=: e 4 bn :=: g 4 bn
in dMinor :+: gMajor :+: cMajor
main = play t251
the program works fine inside ghci, but when I try to compile it using ghc test.hs I get the following error message:
Linking test ... /usr/bin/ld:
/home/fayong/.cabal/lib/PortMidi-0.1.3/ghc-7.6.3/libHSPortMidi-0.1.3.a(ptlinux.o):
undefined reference to symbol 'pthread_create##GLIBC_2.2.5'
//lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO
missing from command line collect2: error: ld returned 1 exit status
It seems to be a linking error but I don't know how to fix it. I tried to pass -lpthread as an option but it didn't work either.
I installed Euterpea via cabal, on linux mint 17
I got the same error and fixed it by running ghc with the '-threaded' option. I think it has something to do with a recent update to the play, but I will have to look into it more.