failed to compile Haskell project "helloworld" - haskell

I am a beginner of Haskell and I have downloaded ghc-8.8.1 from the official site. I tried to compile my first Haskell program with it and failed. Here is my program helloworld.hs:
main = putStrLn "hello world"
Then I compiled it with cmd (Windows 10):
ghc --make helloworld
And it failed with these words:
[1 of 1] Compiling Main ( helloworld.hs, helloworld.o )
Linking helloworld.exe ...
C:\Users\蔡XX\AppData\Local\Temp\ghc92756_0\ghc_6.rsp: commitBuffer: invalid argument (invalid character)
(蔡XX is my name)
Why is it not working? How can I solve this problem?
BTW, my ghci is working.
Edit: I created a new user in my computer with English name and copied my ghc, and this time it worked. It turns out that this problem was due to the character 蔡 in my path, as the comments said. Thanks guys!

Related

Why is GHC on Replit printing my input multiple times?

I'm using the Replit website to run a Haskell program using GHC. I'm typing code into the editor and running it using the Run button, not just running lines of code using GHCi. I'm running into weird problems with output whenever I use the getLine function, even in extremely simple programs. For example, running the program
main = do ans <- getLine
putStrLn ans
and typing hi at the input prompt prints hi twice instead of once:
cabal v1-run
Preprocessing executable 'Cabal-example' for Cabal-example-0.1.0.0..
Building executable 'Cabal-example' for Cabal-example-0.1.0.0..
[1 of 1] Compiling Main ( Main.hs, dist/build/Cabal-example/Cabal-example-tmp/Main.o )
Linking dist/build/Cabal-example/Cabal-example ...
Running Cabal-example...
hi
hi
hi
The problem gets even weirder when I try to disable input/output buffering in the main function (I needed to do this for a larger program I was working on):
cabal v1-run
Preprocessing executable 'Cabal-example' for Cabal-example-0.1.0.0..
Building executable 'Cabal-example' for Cabal-example-0.1.0.0..
[1 of 1] Compiling Main ( Main.hs, dist/build/Cabal-example/Cabal-example-tmp/Main.o )
Linking dist/build/Cabal-example/Cabal-example ...
Running Cabal-example...
hi
hi^Jhi
I have tested this code on other websites and the code works normally. What am I missing here?
I can duplicate using the Haskell Cabal Template. Looks like a "replit" bug in the "Console" tab for this template. The Console is echoing back user input independent of the Haskell program. Try the program:
main = do
putStrLn "Enter something"
getLine
putStrLn "I'm printing something else"
and the output will include echoed input. (The space before the first "foo" also shouldn't be there.)
Running Cabal-example...
Enter something
foo <-- what I entered
foo <-- echoed by the Console
I'm printing something else
Try running your program in the "Shell" tab using "cabal v1-run", and it should work fine:
~/SympatheticVastHypothesis$ cabal v1-run
Preprocessing executable 'Cabal-example' for Cabal-example-0.1.0.0..
Building executable 'Cabal-example' for Cabal-example-0.1.0.0..
Running Cabal-example...
Enter something
This won't echo <-- what I entered
I'm printing something else
~/SympatheticVastHypothesis$
I think you'll probably have to report it as a bug to the replit folks.

How to debug a stack process being 'killed'?

I'm compiling a haskell file with
$ stack ghc --resolver lts-12.5 --package raw-strings-qq myscript.hs
and execute it with
$ ./myscript
Killed
$
on my Ubuntu machine. I get Killed as an error message and nothing else.
How can I debug this? Does this mean the process takes up to much memory? Are there other possible sources of error?
The response you get from terminal as Killed is because you didn't give some commands:
I use online IDE called Replit, so at the start it doesn't load needed libraries for you, you have to load manually:
ghci -o main main.hs
Sometimes, it automatically loads Main, sometimes it doesn't. So, if you don't see such thing like below, then you have to do it manually as well:
[1 of 1] Compiling Main ( main.hs, interpreted )
Ok, one module loaded.
*Main>
:l main
Maybe, it doesn't directly answer your question, but I got same exception like yours and dealt with it in that way. Well, same exception has the same problem (I guess)?

Why does a Haskell script using putStrLn hang?

On my Windows 7 Home Premium box, why does the following Haskell program hang?
main = do
putStrLn "Hello, World"
The script is compiled (using GHC) like this:
C:\>ghc --make my_script
[1 of 1] Compiling Main ( my_script.hs, my_script.o )
Linking my_script.exe ...
The program is then executed like this:
C:\>my_script.exe
Even after several minutes, there is no output in the Command Prompt window.
GHC version is:
C:\>ghc --version
The Glorious Glasgow Haskell Compilation System, version 8.0.2
(Haskell compiler obtained from https://www.haskell.org/platform/windows.html.)
Update Loading and executing in GHCi yields the following:
C:\>ghci my_script.hs
GHCi, version 8.0.2: http://www.haskell.org/ghc/ :? for help
[1 of 1] Compiling Main ( my_script.hs, interpreted )
Ok, modules loaded: Main.
*Main> main
Hello, World
*Main> :quit
Leaving GHCi.

Getting Source Files to Run in Haskell Programming (WinGHCi)

I can't figure out how to get WinGHCi to load and compile my .hs file.
I have a file, C:\Users\Haskell\Source\hello.hs, that only contains the following line:
main = putStrLn "Hello, world!"
If, at the Prelude> prompt, I run
:cd C:\Users\Haskell\Source\
nothing happens, which I'm assuming means the command was successful. However, when I try to run
:load hello.hs
I get a "[1 of 1] Compiling Main. Ok, modules loaded: Main" message. My prompt then changes from "Prelude" to "*Main" and I type:
ghc -o hello hello.hs
After that, I will get a series of errors talking about how ghc, o, hello, hello, and hs are "Not in scope."
I am in the correct directory. Why won't my program run?
One of my problems is that I'm unable to navigate the directories. I know that :!dir lists the files, and I am in the right directory, but :load hello.hs still doesn't work and I keep getting the scope error.
Any help would be appreciated.
EDIT: A user pointed out that if I have gotten to the *Main prompt, then my program has been loaded and compiled and I do not need to run the ghc command. If that is the case, how would I run it? Haskell.org states that, "You can then run the executable (./hello on Unix systems, hello.exe on Windows)," but an exe has not been created.
I find it easier to first navigate to the directory then invoke ghci. Once in Prelude you can use :l and the file name.
Or, you could load ghci then use :l and use the fully qualified path for the file.
Edit:
After reading your edits, it is clear you are getting your code compiled fine. Once it says it has compiled, there is no reason to try and do so again with ghc (I don't think you can do that from within ghci anyhow).
Now that it is compiled, you can use any of the code and data types defined there in. So to use your main function, just type in main at the *Main> prompt.

Why i can't compile with GHC if code contain module definition?

I'am trying to compile a very small haskell code with ghc:
module Comma where
import System.IO
main = do
contents <- getContents
putStr (comma contents)
comma input =
let allLines = lines input
addcomma [x] = x
addcomma (x:xs) = x ++ "," ++ (addcomma xs)
result = addcomma allLines
in result
The command i'm using to compile is :
ghc --make Comma.hs
And i'm getting this answer:
[1 of 1] Compiling Comma ( Comma.hs, Comma.o )
No file is generated, and there is no warning or errors messages.
If i comment the "module Comma where" line from code it compiles correctly:
[1 of 1] Compiling Main ( Comma.hs, Comma.o )
Linking Comma ...
I don't understand what is happening.
I'm using ghc 7,4,1
(Glasgow Haskell Compiler, Version 7.4.1, stage 2 booted by GHC version 7.4.1)
and ubuntu linux.
I appreciate if anyone could tell why doesn't compile with the module definition
GHC compiles the function Main.main to be the entry point of an executable. When you omit the module declaration, Module Main where is implicitly inserted for you.
But when you explicitly name it something other than Main ghc doesn't find an entry point.
My usual workflow is to use ghci (or ghci + emacs) instead for these snippets which let's you bypass this issue entirely. Alternatively, you could compile with -main-is Comma to explicitly tell ghc to use the Comma module.
No file is generated
Are you sure? I would expect that at least Comma.o and Comma.hi are generated. The former contains the compiled code ready to be linked into an executable, and the latter contains interface information that ghc uses to typecheck modules that import the module Comma.
However, ghc will only link the compiled modules into an executable if there is a main function. By default, that means a function named main in a module named Main. If you don't put an explicit module name, the name Main is assumed, and that's why your test works when you delete the module Comma where line.
To compile and link the Comma.hs file you can either use module Main where instead of module Comma where, or you can use the -main-is flag to tell ghc that Comma.main is to be the main function:
ghc --make -main-is Comma Comma.hs
Or:
ghc --make -main-is Comma.main Comma.hs
If you have a main definition in your file and you want to compile it to an executable you need can only have module Main where.

Resources