IO error when executing runhaskell - haskell

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.)

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.

Command "ghc" returning Variable not in scope

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.

Running my program with command line arguments

I am attempting to run my program using the command line. I am trying to return my command line arguments:
import System.Environment
import Data.List
main :: IO()
main = do
args <- getArgs
progName <- getProgName
putStrLn "The arguments are:"
mapM putStrLn args
putStrLn "The program name is:"
putStrLn progName
I am executing the code by calling the main function with my arguments:
main argument arguments "more arguements"
However, I am getting a complier error:
<interactive>:33:6: Not in scope: ‘argument’
<interactive>:33:15: Not in scope: ‘arguments’
Is there an issue with how I am calling my function with my arguments?
You have to use :main if you want to simulate command line arguments. main alone only executes your IO () action, but doesn't actually build the arguments. For all what GHCi knows, main doesn't necessarily need to be IO (), it could be Int -> Int -> IO ().
However, if you use :main, GHC will use main in the same way it would get invoked during an runhaskell call, e.g. with interpreting the following parameters as command line arguments.
Alternatively, you can use withArgs from System.Environment:
ghci> withArgs ["argument", "arguments", "more arguments"] main

Dynamically loading compiled Haskell module - GHC 7.6

I'm trying to dynamically compile and load Haskell modules using GHC API. I understand the API fluctuates quite a bit from on one version to another so I'm specifically talking about GHC 7.6.*.
I have tried running the same code on MacOS and Linux. In both cases the Plugin module compiles fine but gives the following error on load: Cannot add module Plugin to context: not interpreted
The problem is similar to the one in this where the module would only load if it was compiled in the same run of the host program.
-- Host.hs: compile with ghc-7.6.*
-- $ ghc -package ghc -package ghc-paths Host.hs
-- Needs Plugin.hs in the same directory.
module Main where
import GHC
import GHC.Paths ( libdir )
import DynFlags
import Unsafe.Coerce
main :: IO ()
main =
defaultErrorHandler defaultFatalMessager defaultFlushOut $ do
result <- runGhc (Just libdir) $ do
dflags <- getSessionDynFlags
setSessionDynFlags dflags
target <- guessTarget "Plugin.hs" Nothing
setTargets [target]
r <- load LoadAllTargets
case r of
Failed -> error "Compilation failed"
Succeeded -> do
setContext [IIModule (mkModuleName "Plugin")]
result <- compileExpr ("Plugin.getInt")
let result' = unsafeCoerce result :: Int
return result'
print result
And the plugin:
-- Plugin.hs
module Plugin where
getInt :: Int
getInt = 33
The problem is that you're using IIModule. This indicates that you want to bring the module and everything in it, including non-exported stuff into the context. It's essentially the same as :load with an asterisk in GHCi. And as you've noticed, this only works with interpreted code since it let's you "look inside" the module.
But that's not what you need here. What you want is to load it as if you used :module or an import declaration, which works with compiled modules. For that, you use IIDecl which takes an import declaration which you can make with simpleImportDecl:
setContext [IIDecl $ simpleImportDecl (mkModuleName "Plugin")]

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