Why does a Haskell script using putStrLn hang? - haskell

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.

Related

failed to compile Haskell project "helloworld"

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!

Configure stack ghci prompt

Is it possible to configure stack ghci similar to the way ghci is configured via the ~/.ghci file?
Currently stack ghci does not pick up the settings in the ~/.ghci file.
I'm looking into setting the prompt to be a lambda instead of the loaded modules:
:set prompt "\ESC[33m\STXλ> \ESC[m\STX"
Update
[neo#nixos:~/HaskellLearning/IdeTest]$ stack ghci The following GHC
options are incompatible with GHCi and have not been passed to it:
-threaded Configuring GHCi with the following packages: IdeTest Using main module: 1. Package `IdeTest' component exe:IdeTest-exe with
main-is file: /home/neo/HaskellLearning/IdeTest/app/Main.hs GHCi,
version 8.2.2: http://www.haskell.org/ghc/ :? for help Loaded GHCi
configuration from /home/neo/HaskellLearning/IdeTest/.ghci Loaded GHCi
configuration from /home/neo/.ghci [1 of 9] Compiling Ch13
( /home/neo/HaskellLearning/IdeTest/src/Ch13.hs, interpreted ) [2 of
9] Compiling Ch15 (
/home/neo/HaskellLearning/IdeTest/src/Ch15.hs, interpreted ) [3 of 9]
Compiling Ch17 (
/home/neo/HaskellLearning/IdeTest/src/Ch17.hs, interpreted ) ... Ok, 9
modules loaded. Loaded GHCi configuration from
/run/user/1000/ghci31082/ghci-script
*Main Ch13 Ch15 Ch17 Functor HelloError Lib Phone SemigroupAndMonoid>
None of the other 2 configurations mentioned does anything regarding changing the prompt.
Try to put your config under:
~/.ghc/ghci.conf
Works for me on Ubuntu 16.04.

LLVM compilation not working with Haskell on Yosemite

I'm trying to use llvm with haskell and I'm getting the error "illegal hardware instruction"
Details:
I'm compiling a simple hello world program just to test it out:
Code:
module Main where
main :: IO ()
main = putStrLn "Hello World"
GHC version 7.10.1, llvm version 3.4.2 (older version because haskell doesn't work at all with 3.6.1).
Command to compile: ghc -O2 --make firstProgram.hs -fllvm -fforce-recomp
Exact error with command ./firstProgram
[1] 23665 illegal hardware instruction ./firstProgram
Thanks for any help!

how to load .dyn_o in ghci 7.8?

I asked this on haskell-cafe, but there are no responses yet so maybe not enough people are using 7.8. Perhaps more people are reading stack overflow nowadays:
I recently upgraded to 7.8.2 and I have a silly question. How do you
get ghci to load compiled modules? When I try this:
% cat >T.hs
module T where
x :: Int
x = 42
% ghc -c -dynamic-too T.hs
% s
T.dyn_hi T.dyn_o T.hi T.hs T.o
% ghci
GHCi, version 7.8.2: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package filepath-1.3.0.2 ... linking ... done.
Prelude> :l T
[1 of 1] Compiling T ( T.hs, interpreted )
Ok, modules loaded: T.
*T>
It still loads the file interpreted, even though there is a .dyn_o
present. What am I doing wrong?
This is on x86-64 OS X.
There is one other thing which may be related. Since I load everything interpreted now, I've noticed that the input gets very laggy when I have a hundred or so modules loaded. Also the haskeline state gets confused, e.g. I hit escape k to get the previous line, but then it spontaneously goes back into insert mode again. It stays balky and awkward for about 15 seconds before returning to normal slightly. It's almost as if, after loading all the bytecode, it's still doing tons of work in the background, with constant GC freezes. But what work could it be doing? The bytecode is loaded and I haven't asked it to do anything yet! I don't know if this is new to 7.8, or if it's a result of loading bytecode instead of binary.
Turns out this is a known ghc bug:
https://ghc.haskell.org/trac/ghc/ticket/8736
The fix is to compile with -dynamic, not -dynamic-too.

How to run a haskell application in emacs - haskell mode?

Code in helloworld.hs :
main = do
putStrLn "Hello, what's your name?"
name <- getLine
putStrLn ("Hey " ++ name ++ ", you rock!")
Application tested in Terminal:
optimight#optimight:~$ ghc --make helloworld
[1 of 1] Compiling Main ( helloworld.hs, helloworld.o )
Linking helloworld ...
optimight#optimight:~$ ./helloworld
Hello, what's your name?
John
Hey John, you rock!
helloworld.hs loaded in emacs - haskell major mode:
GHCi, version 7.4.1: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> :load "/home/optimight/helloworld.hs"
[1 of 1] Compiling Main ( /home/optimight/helloworld.hs, interpreted )
Ok, modules loaded: Main.
*Main>
Now, How to (What is the procedure? ) test it in emacs - haskell mode environment? (I believe, while I am using emacs - haskell mode , there should be no need to switch over to terminal.)
To do something similar to what you did on the command line you need to load your program in ghci (which you have done) and then call the main method (which you can do by just typing main at the prompt).

Resources