unable to load modules - haskell

I've just began using haskell.. I tried to run a sample script:
import Data.List
module main where
mylength = foldr (const (+1)) 0
main = print (mylength "haskell")
I get a simple error "modules loaded, none." I have zero idea why this is happening.
I'm using GHCi under win7 32bit (if that matters). Am I missing something here?
the example doesn't even include the "module main where" but even without that it fails
to run.

The module line always goes before imports. And the module name should start with a capital letter.
module Main where
import Data.List
mylength = foldr (const (+1)) 0
main = print (mylength "haskell")
If this doesn't work for you, please tell us
the full error message you get; and
which version of which compiler you're using

You don't need module line there.
When you do put it in, though, it must go before any import statements.
module main where -- Optional, in the case of main.
import Data.List
myLength :: [a] -> Int
myLength = foldr (const (+1)) 0
main = print (mylength "haskell")

Related

Haskell: parse error on input `import'

I am trying to recreate the output of this Haskell code:
forM_ = flip mapM_
import Control.Monad.Cont
main = do
forM_ [1..3] $ \i -> do
print i
forM_ [7..9] $ \j -> do
print j
withBreak $ \break ->
forM_ [1..] $ \_ -> do
p "loop"
break ()
where
withBreak = (`runContT` return) . callCC
p = liftIO . putStrLn
The expected output is as follows:
$ runhaskell for.hs
1
2
3
7
8
9
loop
But I am getting the following error:
Test4.hs:2:1: parse error on input `import'
Any ideas about what is going wrong?
Test4.hs:2:1: parse error on input `import'
The error points to the second line of your file. Quoting the first two lines:
forM_ = flip mapM_
import Control.Monad.Cont
The problem is that an import declaration must be at the beginning of a module, before any definitions (the only things that can come before an import are language pragmas, such as those used to enable GHC extensions, and the module declaration). In your case, the first line of the file is a definition, and so the misplaced import declaration in the second line leads to a parse error. Since that first line wasn't actually part of the code snippet in the post you linked to, you can simply delete it.

org-babel for haskell not works of eval haskell block

I am use org-mode blogging, I use org-babel to evaluate the code as following :
#+BEGIN_SRC haskell
import Data.Function (fix)
f :: Int -> Int
f = (+ 1)
main :: IO ()
main = do
putStrLn $ show $ f 1
#+END_SRC
#+RESULTS:
: <interactive>:9:25: Not in scope: ‘f’
I found the org-babel for haskell use infer-haskell mode to start session and eval the code. I also say the session was created, and if I don't define the function but directly putStrLn "hello" , it works.
hope anyone can fix the bug :)
In this article, Yoshinari Nomura describes a way to evaluate Haskell blocks using runhaskell via a Ruby script. I do not understand japanese, so I cannot translate the details, but the method has allowed me to run haskell blocks without having to write specifically for the interpreter.
#+BEGIN_SRC haskell
import Data.Function (fix)
f :: Int -> Int
let f = (+ 1)
main :: IO ()
main = do
putStrLn $ show $ f 1
#+END_SRC
#+RESULTS:
: 2
Org's babel mode is running the Haskell code with ghci. In ghci you are required to use let to declare functions.

Fay: unable to resolve qualified names ffi?

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.

How to dynamically call a function which defined in multiple modules in the same signature

I've defined a lot of functions (say, 100+), each of which do a specific work but with the same signature. That is something like:
module R001 (run) where run = <do-...>
module R002 (run) where run = <do-...>
What I wanna do is to provide the actual 'run' as user input, such that:
main = do
runWith $ read $ getLine
where
runWith :: Int -> IO ()
runWith n = R<n-padded-with-0>.run
Currently, I import all modules qualified, and put all the run's into a list of [Maybe (IO())], so this works:
runWith n = case Rs !! (read $ getLine) of
Just run -> run
Nothing -> undefined
But as the n grows, I have to continously maintain a big list.
Is there any way I can define the big list using TemplateHaskell, or just load the corresponding module as needed at runtime without having to seperate each module into different shared libraries.
Based on epsilonhalbe's answer, I did some research:
import R1 (run1)
import R2 (run2)
test = $(functionExtractor "^run")
main :: IO ()
main = do
putStrLn $ show $ length $ test
run1 -- remove on second attempt
run2 -- remove on second attempt
This block of code prints 2 following the results of run1 and run2. If I remove the last two lines, it just prints 0. It seems that functions imported but not referenced won't be extracted ...
I once had a similar problem haskell load module in list maybe this helps.
You can create a list of functions with regexp and choose a function by userinput from that list.
I don't know if you have to import all "runs" qualified by hand or if you can
import R*.hs (run)
i would rather write one file with run1 = …, run2 = … and generate a list of all runs and a function chooser function which takes a function from a list of functions with the same type signature.
{-# LANGUAGE TemplateHaskell #-}
import Language.Haskell.Extract
import myRunFunctions
main = do
let listOfRuns = $(functionExtractor "^run")
putStrLn "please choose a run"
putStrLn $ show listOfRuns
let run = runWith $ read $ getLine
run
where
runWith n = listOfRuns !! n
Attention: I have not run this code this is just a stream of thought put in haskell syntax
i hope this is helpful
After edit:
In my example i wrote all run* in one file and there i generated the list of all run functions, which worked instantly - have a look at my Nucleotide Project especially the files Rules.hs and Nucleotide.hs.
Runs.hs
module Runs where
import Language.Haskell.Extract
listOfRuns = map snd $(functionExtractor "^run")
run1 = …
run2 = …
Main.hs
import Runs
main = do
putStrLn "please choose a run"
putStrLn $ show listOfRuns
let run = runWith $ read $ getLine
run
where
runWith n = listOfRuns !! n
happy to be helpful
Is it absolutely critical that the different run functions live in different modules? If you can put them all in one module, you could make run be a function of an Int (or Integer if you prefer).
module AllMyCircuits where
run 0 = {- do blah blah blah -}
run 1 = {- do blah blah blah -}
run 2 = {- do yikes -}
module Main where
import AllMyCircuits
main = readLn >>= run

Function variable not in scope Haskell

Hi i have the following code
import Data.Maybe
import Test.QuickCheck
import System.Random
rndExpr :: Gen Expr -> IO Expr
rndExpr gen = do
rnd <- newStdGen
return (generate 5 rnd gen)
But i get "not in scope "generate", why is this so?
Regards
Darren
Edit i am importing Test.QuickCheck but it still complaints about the "generate" is not in scope.
Edit 2
How would you write this function so that it would work with quickcheck version 2? I simple tried to put "unGen" where generate was with no succsess, i also installed quickcheck v 2 (cabal install QuickCheck-2.1.0.3)
I need a function with following properties stdGen->Gen Expr->Expr'
and unGen seem to give me that functionality, but as I said, my compiler cant find that function. Are there any other functions that I could use for this problem?
It seems like you are using generators from Test.QuickCheck, and generate is a function from version 1 of quickCheck. In version 2 of quickCheck things are a bit different so there is no such function. However, you atleast need to import Test.QuickCheck, and similar functionality can be gotten from unGen like this:
rundExpr gen = fmap (flip (unGen gen) 5) newStdGen
Please note that unGen is in Test.QuickCheck.Gen so you have to import that too.
generate isn't a function in System.Random. Perhaps you are looking for next?
EDIT:
Let me be clear: I don't know why you are using QuickCheck/Arbitrary for a task that Random/MonadRandom would seem to be more fitting. I'll assume you considered your options and move on.
Must you select your generator? Can't you use sample' :: Gen a -> IO a?
getVal :: IO a
getVal = sample' arbitrary
This should work for QC2.
OTOH, if you really want to use your own StdGen (or want to avoid IO) then try:
import System.Random
import Test.QuickCheck
import Test.QuickCheck.Gen
func :: StdGen -> Int
func g = unGen arbitrary g 0
This will use the StdGen named g and a count (0 here,) to generate your value. Because unGen doesn't step the generator, and the counter stepping doesn't give good randomness properties (it seems, you can try and see for yourself) you might end up wanting to wrap this with something that generates StdGens (yuck).
If you don't know what version package you are using then run:
$ ghc-pkg list | grep QuickCheck
(QuickCheck-2.1.1.1)
QuickCheck-1.2.0.1
In my setup (seen above) I have both 1 and 2, but 2 is hidden (the () means hidden) so when I use GHCi and import Test.QuickCheck it's version 1 that I get.

Resources