Command "ghc" returning Variable not in scope - haskell

I am starting in Haskell and encounter this weird error message when trying to compile a .ghci file in Haskell. I have this very simple code for example:
main = do
putStrLn "Greetings! What is your name?"
inpStr <- getLine
putStrLn $ "Welcome to Haskell, " ++ inpStr ++ "!"
I saved the code in a file called basicio.hs and tried to run
ghc basicio.hs
Instead of the String I get the following message
<interactive>:2:1: error:
Variable not in scope: runghc :: t0 -> b0 -> c
<interactive>:2:8: error: Variable not in scope: basicio
I am not sure what's wrong, the command ":load" works fine and find my file.

This error messages looks like it was generated by typing "runghc basicio" at the GHCi prompt:
GHCi, version 8.6.5: http://www.haskell.org/ghc/ :? for help
Loaded GHCi configuration from ...
> runghc basicio
<interactive>:3:1: error: Variable not in scope: runghc :: t0 -> t
<interactive>:3:8: error: Variable not in scope: basicio
>
However, the runghc command, and the compiler command ghc are both meant to be run directly from the command line.

Related

Attempting to run Haskell File on GHC error

When attempting to load Haskell file test.hs into terminal. I get this response:
<interactive>:5:1: error: Variable not in scope: test :: b0 -> c
<interactive>:5:6: error: Variable not in scope: hs :: a -> b0
This is the code from test.hs:
double x = x + x
quadruple x = double (double x)
How do I get the file to load in terminal?
openai says look for documentation or may need to import module?
That's what happens if you type test.hs at the GHCi prompt. You meant to type :l test.hs instead.

IO error when executing runhaskell

I'm trying to execute a main method within one of my classes in haskell. When I run the command runhaskell mod12PA.hs I get an error and I can't figure out why.
Here's the code:
-- The main program: read points from stdin, write an SVG file to stdout.
main :: IO ()
main = do
putStrLn "Enter points: "
points <- getLine
putStrLn ("Points: " ++ points)
The error:
<interactive>:41:1: error:
Variable not in scope: runhaskell :: t0 -> b0 -> c
<interactive>:41:12: error: Variable not in scope: mod12PA
<interactive>:41:20: error: Variable not in scope: hs :: a -> b0
Was able to figure out that I needed to be compiling the file outside of ghci and then running the executable. I was trying to execute the file from within ghci which was not working.
Ran ghc --make mod12PA.hs
Then mod12PA.exe
While you do need to run the command outside of GHCi, runhaskell mod12PA.hs should work. (runhaskell serves a different purpose than ghc --make: it runs the code without compiling it.)

Haskell: GHCi Load as much as possible, rather than fail or error

In Haskell using GHCi is there a way to load a file such as the one below, that lets you test the methods that have bindings.
Use case: trying to test part of my module, while having skeleton code for the rest of it. (as to not have an XY problem)
module X (methodA, methodB, methodC) where
methodA :: String->String
methodA name = "Hello " ++ name
methodB :: Int -> String
methodC :: String -> String
This obviously outputs the correct error : The type signature for ‘methodB’ lacks an accompanying binding.
For example I would like something similar to below to work.
GHCi, version 8.0.1: http://www.haskell.org/ghc/ :? for help
Prelude> :l example.hs
[1 of 1] Compiling X ( example.hs, interpreted )
example.hs:6:1: error:
The type signature for ‘methodB’ lacks an accompanying binding
example.hs:8:1: error:
The type signature for ‘methodC’ lacks an accompanying binding
Failed, modules loaded: none.
Prelude> methodA "jamesmstone"
<interactive>:2:1: error:
Variable not in scope: methodA :: [Char] -> t
You can use -fdefer-type-errors. This will give you warnings about type errors but won't prevent you from running the other well-typed parts of your program.
Example:
If Program.hs contains:
foo :: Int
foo = 'a'
main = putStrLn "Hello, world"
Then with -fdefer-type-errors you can still load and run main:
$ ghci -fdefer-type-errors Program.hs
GHCi, version 7.10.2: http://www.haskell.org/ghc/ :? for help
[1 of 1] Compiling Main ( Program.hs, interpreted )
Program.hs:5:7: Warning:
Couldn't match expected type ‘Int’ with actual type ‘Char’
In the expression: 'a'
In an equation for ‘foo’: foo = 'a'
Ok, modules loaded: Main.
*Main> main
Hello, world
*Main>
Just give them a binding that doesn't work:
methodB = undefined
methodC = undefined

Why does underconstrained type sometimes give runtime error?

Consider the following:
do putStr ""
return $ map read ["2"]
Run in GHCI, this gives
[*** Exception: Prelude.read: no parse
I expected an "ambigious type variable" compilation error, but instead this comes up at runtime. Why?
For comparison, the following three examples do give "ambiguous type variable" errors:
map read ["2"]
do putStr ""
return $ read "2"
do [1]
return $ map read ["2"]
By the way, here's the original code where I encountered this.
readInts :: String -> IO [Integer]
readInts f = do s <- readFile f
return $ map read $ splitOneOf " \n" s
This has been resolved. My original code attempted to read from "" at the end of the list. I received the same exception for a different reason when I investigated in GHCI.
This happens because of GHCi's extended default rules. If you put the same code in a file without a signature, you should see the error you expected.
(In this case it's probably picking () as the type, which is why you get a parse error.)

Haskell GHC Dynamic Compliation Only works on first compile

Following the GHC tutorial posted here and alterations to this code following the advice in a previous stack overflow question I asked, I have created a program which is able to compile and run a module in Test.hs with a function print to print a string to the screen:
import GHC
import GHC.Paths
import DynFlags
import Unsafe.Coerce
main :: IO ()
main =
defaultErrorHandler defaultLogAction $ do
func <- runGhc (Just libdir) $ do
dflags <- getSessionDynFlags
setSessionDynFlags dflags
target <- guessTarget "Test.hs" Nothing
addTarget target
r <- load LoadAllTargets
case r of
Failed -> error "Compilation failed"
Succeeded -> do
m <- findModule (mkModuleName "Test") Nothing
setContext [IIModule m]
value <- compileExpr ("Test.print")
do let value' = (unsafeCoerce value) :: String -> IO ()
return value'
func "Hello"
return ()
The problem with this code, as noted in the comments, is that it only seems to work the first time you run it (When Test.hs has not yet been complied). If you attempt to run the code a second time, the following error appears:
mkTopLevEnv: not interpreted main:Test
I believe this has something to do with the fact that the code has already been compiled. If I delete the .hi and .o files and run the program again, the program runs correctly with the correct output. What am I missing? I am currently using ghc version 7.4.1
(Note: I have tried looking through the GHC API but could not find any references to mkTopLevEnv)
Simon Marlow suggests here that replacing
guessTarget "Test.hs" Nothing
with
guessTarget "*Test.hs" Nothing
should avoid the error you're getting, on the grounds that it tells GHC not to load the .o file.
See the whole thread on a page via nabble
Of course, you could delete the .hi and .o files each time, but that's an ugly workaround.

Resources