Getting Source Files to Run in Haskell Programming (WinGHCi) - haskell

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.

Related

Why can't the ghci find the hs file that I am trying to compile?

I'm learning Haskell for the first time and I can't see to understand why the ghci can't find the file I'm trying to compile. Especially since, I saved the file. This is my file,
import System.IO
trueAndFalse = True && False
Now this is what I ran in the compiler,
<no location info>: error: can't find file: tut-1.hs
Failed, no modules loaded.
The "Failed, no modules loaded." makes me think you're talking about ghci. If so, you can find out where ghci is looking for files with :show paths. Here's what it looks like when I try:
> :show paths
current working directory:
/home/<my username>
module import search paths:
.
The module import search paths tells you what directories it's looking in. A lone . in that list refers to the current working directory. So, for me, if I wanted a file to be easily accessible from that ghci instance, I would have to save it in /home/<my username>.
Of course there are ways of changing all of these pieces -- which paths are in the import search path, which directory is the current working directory, and so forth -- but this should get you going for simple usage.
And, by the way, a note on terminology: the GHC tool suite comes with both a compiler and an interpreter. The compiler's executable is ghc, and the interpreter's executable is ghci. Knowing about that distinction may help you avoid confusion in future conversations!

Cant load Prelude in ghci interpreter in VS code

I have installed Haskell platform following the instructions on chocolatey and haskell.org.
I am using Windows 10.
My hello.hs program complies in command prompt, but when I try to do the same in VS code,
it won't load Prelude, which I assume is necessary for running Haskell programs.
I think it might be a configuration problem, but I cant find any useful documentation on it.
How could I fix this and turn on Prelude?
Are the problems that VS code shows relevant to this problem?
Nothing looks wrong in your screenshots. The prompt text is ghci> rather than Prelude> because GHCi no longer defaults to showing the loaded modules on the prompt (see the GHC 9.0.1 changelog. Prelude is being loaded regardless. The warning shown in the IDE is a style suggestion that is inconsequential for the purpose of getting your code to run. As chi suggests, :l hello.hs at the GHCi prompt should be enough to have it loaded.
If I type main into the console, then it works
ghci> main
"haskell"
name
"hi, name"
ghci>
This might work
$ ghci
ghci> import Prelude
Prelude> printStrLn "Hello World!"
Hello World!
Prelude>
You may need to just run import Prelude.

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

How to set up org-babel for Haskell with Stack

I'm running:
GNU Emacs 24.4.1
Stack Version 1.3.3
org-mode
haskell-mode
I've looked through:
Emacs Org-Mode & Literate Haskell
https://gist.github.com/reetinder/4022989
https://wiki.haskell.org/Emacs/Literate_programming
https://wiki.haskell.org/Literate_programming
https://github.com/haskell/haskell-mode/issues/1429
While the gist above looks promising, I haven't found anything that looked to be an authoritative way to get org-haskell running (eg, nothing on melpa), and certainly nothing aimed specifically at whatever intricacy running a stack environment rather than using my global ghc would entail.
When I try to:
#+BEGIN_SRC haskell
let x = "test"
putStrLn x
#+END_SRC
I get
executing Haskell code-block
...which hangs forever. When I C-g, I see:
GHCi, version 7.6.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.
Some flags have not been recognized: prompt2, ghci |
ghci λ> let x = "TESTING!"
putStrLn x
"org-babel-haskell-eoe"
Prelude|
<interactive>:4:1: parse error on input `putStrLn'
ghci λ> "org-babel-haskell-eoe"
When I tab to the haskell buffer, I see it has genuinely evaluated what I sent, it just has this org-babel-haskell-eoe error and never returns control to my org session.
Any chance this is because I have a custom prompt? Using the lambda instead of Prelude> ?
This is not a complete answer: in particular, it does not even mention Stack. But I (a complete ignoramus on Haskell) wanted to find out what it would take to get the OP's test program to run in babel. Here's what I found:
You need a haskell interpreter ;-) I'm on Fedora 24, so I installed the ghc-compiler package and I got ghci.
You need haskell-mode. I installed that from MELPA, using the emacs package manager. That also installed inf-haskell.el
By default, inf-haskell wants to run hugs, so I customized haskell-program-name and set it to "ghci".
M-x load-library RET ob-haskell RET
C-c C-c on the code block: the first time it fails and the Messages buffer shows "Buffer haskell.org does not exist or has no process".
But if you do it C-c C-c on the code block again, it succeeds!
Obviously, ob-haskell.el needs some work - and that's before we even get to Stack, of which I know even less than I know of Haskell, so I'll leave that as an exercise for the interested reader :-)
EDIT: Re. version info (requested in a comment): I keep close to the bleeding edge. At this point in time (2017-05-01), I run Org mode version 9.0.5 (release_9.0.5-444-g998576 # /home/nick/elisp/org-mode/lisp/) and GNU Emacs 26.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.20.10) of 2017-04-14
It is probably because of the custom prompt: I had the same issue, and when I removed ":set +t" and ":set prompt "GHCI >" from ~/.ghci, it worked. I fiddled with it some, and it seems it will work so long as your custom prompt doesn't have any spaces in it except the end (I changes mine to "GHCI> " and it works). It seems to be that the regular expression it parses the information from assumes the prompt will have no spaces in it.
In my case it was .ghci, like Testare's. As soon as I commented out :set prompt "λ ", emacs stopped freezing but gave the message ‘org-babel-script-escape’ expects a string. It was necessary to comment out :set +t for it to work.
what i did after installed stack and ghci (with stack itself)), was to install intero in emacs and then add those to init.el:
(setq haskell-process-type 'stack-ghci)
((org-babel-do-load-languages
'org-babel-load-languages
'((haskell . t)))
after that i can C-c C-c in haskell code block and i get a result under the code block.

Loading Haskell Modules that have dependencies

I'm unsure what my issue is here. I have a trio of modules A.hs, B.hs, and C.hs. All are located at C:\..path...\folder and modules B and C both import from A.
That is, both modules B and C contain the line import A
I can :l C:\..path..\folder\A.hs in gchi and play with its contents; however, ghci gives the following error when I try to :l C:\..path..\folder\B.hs or :l C:\..path..\folder\C.hs
Could not find module `A'
Use -v to see a list of the files searched for.
Failed, modules loaded: none.
Which I find odd because I had no trouble compiling B.hs to B.exe and running the executable. How can I compile and run a module that I can't load into ghci? Or, why would an import succeed at compile time but fail in loading; especially when that very module being imported is itself load-able?
By default, ghci searches only in the current directory for imported modules. To start with, the current directory is the one used to launch ghci; but it can be changed from within ghci with the :cd command. Thus, you could
> :cd C:\...path...\folder
> :l B.hs
and this should find both B.hs and A.hs in what is now the current directory. Alternately (and especially if you have modules in multiple directories) you can launch ghci with the -i command line option to add directories to its module search path. For example, in your command prompt you might
% ghci -iC:\...path...\folder
> :l B.hs
which will instruct ghci to include C:\...path...\folder in its search path, and therefore find B.hs and A.hs there even if it is not the current directory.

Resources