In the main module we have:
import System.Environment
import System.Random
main = do
args <- getArgs
random <- choose (0,100000) :: Gen Int
newCards = baralhar (mkStdGen random) baralho40
putStrLn $ "-----The End -----"
and we get this error when we compile:
Bisca.hs:13:36: error:
Not in scope: type constructor or class ‘Gen’
|
13 | random <- choose (0,100000) :: Gen Int
| ^^^
I don´t understand what the Gen means and what the solution could be
Hoogle reports that choose is a function from QuickCheck (see here). Gen is a type from Test.QuickCheck.Gen.
You just need to add an import statement for the QuickCheck library (Test.QuickCheck and Test.QuickCheck.Gen) and make sure your build system is aware of the library.
Related
The program (Main.hs, found on SO) looks like this
import Control.Monad.Random
main :: IO ()
main = do
gen <- getStdGen
let values = evalRand diceSums gen
print . take 10 $ values
-- Generate sums from rolling two dice
diceSums :: RandomGen g => Rand g [Int]
diceSums = do
xs <- dieRolls
ys <- dieRolls
return $ zipWith (+) xs ys
-- Single die roll function.
dieRolls :: RandomGen g => Rand g [Int]
dieRolls = getRandomRs (1, 6)
I then run
stack ghci
Prelude> :l Main.hs
[1 of 1] Compiling Main ( Main.hs, interpreted )
Ok, one module loaded.
*Main> main
<interactive>:2:1: error:
* Variable not in scope: main
* Perhaps you meant `min' (imported from Prelude)
*Main> :t dieRolls
<interactive>:1:1: error: Variable not in scope: dieRolls
If I try and compile I get
ghc Main.hs
[1 of 1] Compiling Main ( Main.hs, Main.o )
Main.hs:1:1: error:
The IO action `main' is not defined in module `Main'
What am I doing wrong?
Might it be that stack is insisting on you adding the module declaration at the top of the module,
module Main where
(this seems related, though according to it, without the declaration, main should still be available -- though dieRolls indeed shouldn't.)
Not sure why main doesn't work, but the correct way to run main function from GHCi is :main. This way you can pass arguments to your program.
When I do:
cabal sandbox init
cabal update
cabal install hakaru
cabal repl
λ> :l simple.hs
λ> sample test []
with simple.hs containing:
{-# LANGUAGE MultiParamTypeClasses #-}
import Language.Hakaru.ImportanceSampler
import Control.Monad.State
instance MonadState Int Measure
test :: Measure Int
test = put 1 >> get >>= \i -> return i
my computer runs out of memory.
How can I successfully make the Measure monad an instance of MonadState (i.e. have test above return 1)? The Measure type is already an instance of Monad with bind and return defined. Is there some default way I can define MonadState's put and get in terms of lift, bind, and return to make it work? I tried:
get = lift get
put = lift . put
but I couldn't get the (transformer?) types to work out:
simple.hs:6:9:
Couldn't match type ‘t0 m0’ with ‘Measure’
Expected type: Measure Int
Actual type: t0 m0 Int
In the expression: lift get
In an equation for ‘get’: get = lift get
simple.hs:7:9:
Couldn't match type ‘t1 m1’ with ‘Measure’
Expected type: m1 () -> Measure ()
Actual type: m1 () -> t1 m1 ()
In the first argument of ‘(.)’, namely ‘lift’
In the expression: lift . put
Measure is already defined in a following way:
newtype Measure a = Measure { unMeasure :: [Cond] -> Sampler (a, [Cond]) }
You can see that there is no place to store your Int, so you cannot make it a proper instance of MonadState.
If you want to extend Measure to MonadState, you can use StateT monad transformer:
test :: StateT Int Measure Int
test = put 1 >> get >>= \i -> return i
What happened here? StateT s is a monad transformer, which lets you to combine State s monad with any other monad (in this example Measure)
The exact code which ended up working for me is:
import Language.Hakaru.ImportanceSampler
import Language.Hakaru.Distribution
import Control.Monad.State
import System.IO.Unsafe (unsafePerformIO)
test1 :: StateT Int Measure Int
test1 = do
i <- lift $ unconditioned $ categorical [(0,0.25), (1,0.25), (2,0.5)]
j <- lift $ unconditioned $ categorical [(i,0.25), (1,0.25), (2,0.5)]
put (i + j)
k <- get
return k
run_test1 = unsafePerformIO $ empiricalMeasure 10 (evalStateT test1 0) []
I am trying to generate random number using System.MWC package. I wrote a small test code as below:
module Main where
import Data.Word(Word32)
import Control.Monad.ST as ST
import System.Random.MWC
import Data.Vector.Generic.Base
import qualified Data.Vector.Unboxed as U
test :: Word32 -> Int
test x = runST $ do
gen <- initialize (U.fromList [x] :: U.Vector Word32)
v <- uniformR (1,100) gen
return v
The problem is I am getting instance error when trying to use initialize function. This is the instance error I get:
No instance for (vector-0.9.1:Data.Vector.Generic.Base.Vector
U.Vector Word32)
arising from a use of `initialize'
Possible fix:
add an instance declaration for
(vector-0.9.1:Data.Vector.Generic.Base.Vector U.Vector Word32)
In a stmt of a 'do' block:
gen <- initialize (U.fromList [x] :: U.Vector Word32)
In the second argument of `($)', namely
`do { gen <- initialize (U.fromList [x] :: U.Vector Word32);
v <- uniformR (1, 100) gen;
return v }'
In the expression:
runST
$ do { gen <- initialize (U.fromList [x] :: U.Vector Word32);
v <- uniformR (1, 100) gen;
return v }
Failed, modules loaded: none.
I ran info in ghci to confirm that no instances are defined for Data.Vector.Generic.Base.Vector. I then checked hackage for documentation but no page exists for that package.
So, my question is where is that missing instance defined. I can load it once I know where it is defined. Importing Data.Vector.Generic.Base.Vector doesn't work. Also, mwc-random-0.13.1.1 package has a dependency on vector-0.9.1 as you can see in the error message above.
mwc-random is picking an older installed version of vector rather than the latest one. If you uninstall the older version of vector and reinstall mwc-random to pull the updated dependency of vector-0.10, then the issue resolves itself.
(Note: See the comment chain under the question for more details)
The instances are defined one by one, individually per type.
http://code.haskell.org/vector/Data/Vector/Unboxed/Base.hs
Search for #define primVector. In particular, primVector(Word32 should be the instance you're looking for.
I don't think this fixes your problem though.
I have installed:
fay-0.19.0.1
fay-base-0.19
And I'm trying to compile
-- test.hs
module Main where
import FFI
main = ffi "alert('hello world');"
with
fay test.hs
And getting
fay: unable to resolve qualified names ffi
Note that I am able to compile things with fay-jquery, fay-text, etc. Full source files that compile and run (with fay-base) and do what I expect them to do when executing them. But as soon as I try to use ffi in any of then, I get this error.
fay-jquery, fay-dom, and other packages that use ffi seem to behave well when I import them.
I'm not sure what else to mention? I'm on ghc 7.6.3, running Ubuntu.
Help would be appreciated :)
Import Prelude and add a type signature to anything using FFI.
module Console (main) where
import Prelude
import FFI
main :: Fay ()
main = putStrLn (showInt (fib 10))
fib :: Int -> Int
fib 0 = 0
fib 1 = 1
fib n = fib (n - 1) + fib (n - 2)
showInt :: Int -> String
showInt = ffi "%1+''"
Compiles.
Commenting out import Prelude returns error: src/console.hs:7:8: Not in scope: putStrLn' among others.
Commenting out showInt :: Int -> String returns error: fay: your FFI declaration needs a type signature: showInt = ffi "%1+''"
Fix those issues and give it another whirl.
I am told by our glorious leaders of the Fay Soviet that import Prelude is no longer necessary if you have the right stamps on your labor passport.
I'm learning haskell and decided to try writing some small test programs to get use to Haskell code and using modules. Currently I'm trying to use the first argument to create a password hash using the Cypto.PasswordStore. To test out my program I'm trying to create a hash from the first argument and then print the hash to screen.
import Crypto.PasswordStore
import System.Environment
main = do
args <- getArgs
putStrLn (makePassword (head args) 12)
I'm getting the following error:
testmakePassword.hs:8:19:
Couldn't match expected type `String'
with actual type `IO Data.ByteString.Internal.ByteString'
In the return type of a call of `makePassword'
In the first argument of `putStrLn', namely
`(makePassword (head args) 12)'
In a stmt of a 'do' block: putStrLn (makePassword (head args) 12)
I've been using the following links as references but I am now just trial-erroring to no avail.
http://hackage.haskell.org/packages/archive/bytestring/0.9.0.4/doc/html/Data-ByteString-Internal.html
http://hackage.haskell.org/packages/archive/pwstore-purehaskell/2.1/doc/html/Crypto-PasswordStore.html
You haven't imported ByteString, so it's trying to use the String version of putStrLn.
I've provided toBS for the String->ByteString conversion.
Try
import Crypto.PasswordStore
import System.Environment
import qualified Data.ByteString.Char8 as B
toBS = B.pack
main = do
args <- getArgs
makePassword (toBS (head args)) 12 >>= B.putStrLn
You have to do two things differently. First, makePassword is in IO, so you need to bind the result to a name and then pass the name to the IO function. Secondly, you need to import IO functions from Data.ByteString
import Crypto.PasswordStore
import System.Environment
import qualified Data.ByteString as B
main = do
args <- getArgs
pwd <- makePassword (B.pack $ head args) 12
B.putStrLn pwd
Or, if you won't be using the password result anywhere else, you can use bind to connect the two functions directly:
main = do
args <- getArgs
B.putStrLn =<< makePassword (B.pack $ head args) 12