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).
Related
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!
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.
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.
I have a trivial Template Haskell program that prints the name of the current module (Main, here):
{-# LANGUAGE TemplateHaskell #-}
module Main
( main
) where
import Language.Haskell.TH
import Language.Haskell.TH.Syntax
modName ∷ String
modName = $(fmap loc_module qLocation »= λmod → return (LitE (StringL mod) ))
main ∷ IO ()
main = putStrLn modName
When I compile this, I get the following Loading messages from ghc:
tsuraan#localhost ~/test/modname $ ghc --make Main
[1 of 1] Compiling Main ( Main.hs, Main.o )
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package array-0.4.0.0 ... linking ... done.
Loading package deepseq-1.3.0.0 ... linking ... done.
Loading package containers-0.4.2.1 ... linking ... done.
Loading package pretty-1.1.1.0 ... linking ... done.
Loading package template-haskell ... linking ... done.
Linking Main ...
Why does ghc load all these packages when Template Haskell is enabled? Whenever I build a program that uses Template Haskell, especially one that is built against a lot of packages, my compile warnings are overwhelmed with these superfluous "Loading" messages. It would be nice if I could stop the messages from being printed, or stop the (unnecessary?) module loading from happening at all.
Template Haskell runs at compile time, via a bytecode interpreter (GHCi). Any package dependencies that you have -- at compile time -- will be loaded dynamically into GHC -- at compile time, so that you can execute your splices.
One of your dependencies is the Template Haskell library itself, which in turn depends on most of the core things.
You can prevent the "Loading package" lines from being printed by passing the -v0 flag to GHC. (This also suppresses the "Compiling" and "Linking" lines, but warnings and errors are still shown.)
Ok, I've been using the -i compile option to specify the folder to some haskell source when I compile using GHC.
ghc -threaded -i/d/haskell/src --make xxx.hs
I understand it uses those files as 'libraries' while compiling but can i do same in GHCi?
I usually import haskell prepackaged lib e.g. import Data.List or :m +Data.List.
I tried import /d/haskell/src -- does not work!
EDIT
From Haskell doc: Chapter 2 Using GHCi
Note that in GHCi, and ––make mode, the -i option is used to specify the search path for source files, whereas in standard batch-compilation mode the -i option is used to specify the search path for interface files.
The '-i' flag is fine, the problem is with loading the module.
Within ghci, :m will only switch to either pre-compiled modules, or modules which were specified on the command-line. You need to use :add MyModule to tell ghci to compile a Haskell source file.
If you have
./src/Module/SubModule.hs
you can load it with the following:
localuser$ ghci -isrc
GHCi, version 7.0.3: 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 ffi-1.0 ... linking ... done.
Prelude> :add Module.SubModule
[1 of 1] Compiling Module.SubModule ( src/Module/SubModule.hs, interpreted )
Ok, modules loaded: Module.SubModule.
*Module.SubModule>
I think you can say :set -i /d/haskell/src; many, but not all, GHC options can be set that way. Alternatively, you should be able to use it as a parameter directly: ghci -i /d/haskell/src.