How to debug a stack process being 'killed'? - haskell

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

Related

stack ghci with error module ‘main:Main’ is defined in multiple files:

I have a small haskell program which builds and executes with stack ok. When I start it with stack ghci I have an error message which I do not understand and cannot proceed.
GHCi, version 8.10.4: https://www.haskell.org/ghc/ :? for help
[1 of 3] Compiling Lib ( /home/frank/Workspace11/primo/src/Lib.hs, interpreted )
[2 of 3] Compiling YamlRead ( /home/frank/Workspace11/primo/src/YamlRead.hs, interpreted )
[3 of 3] Compiling Main ( /home/frank/Workspace11/primo/app/Main.hs, interpreted )
Ok, three modules loaded.
Loaded GHCi configuration from /home/frank/Workspace11/primo/.ghci
<no location info>: error:
module ‘main:Main’ is defined in multiple files: /home/frank/Workspace11/primo/app/Main.hs
/home/frank/Workspace11/primo/app/Main.hs
I do not see why the same Main is listed twice in the exact same file.
I had a somewhat similar warning message about Paths_primo which is a known bug ( Stack issue #5439 ) and I fixed following the advice see.
What is the cure against this error? I have not used stack much - am I doing something wrong?
This looks like a sign that Main.hs or Main is inadvertently listed multiple times in your Stack package.yaml, in such a way that ghc is invoked with multiple occurrences of it.
This error is possible to reproduce easily with GHC alone, for example:
> echo 'main = putStrLn "hello"' > Hello.hs
> ghc Hello Hello.hs
<no location info>: error:
module ‘main:Main’ is defined in multiple files: Hello.hs Hello.hs
I would run Stack with --verbose and see how GHCi is being invoked, and double-check the package.yaml and generated Cabal file. (If you edit your question to include that, we may be able to offer more specific help with it.)
I can think of several possible reasons for this, such as literally listing Main or Main.hs multiple times (e.g. in exposed-modules, other-modules, main-is); or an interaction like a missing option value in the ghc-options field causing subsequent flags to be misinterpreted.
Main.hs can be listed multiple times in GHCi invocation when it is present in multiple components. Typical way when this happens is:
You create a project with stack new which contains single src/Main.hs file, so package.yaml has an executable entry and no library entry.
Then you add a library and use source-dir: src. Now Main.hs is listed in two components: myapp-lib and myapp-lib-exe. Assuming you package name is myapp.
To solve the problem you should move Main.hs to app dir and update executables:source-dirs to app in package.yaml.
So the problem may happen when Main.hs is listed in multiple components in package.yaml, for example in executables and library.

Emacs haskell intero mode, import could not find module

Edit: this error occurs only when using emacs haskell mode and intero mode (C-c C-l to load into ghci). It works in command line using stack ghc Log.hs LogAnalysis.hs.
I'm learning Haskell through CIS 194 (Spring 2013), and am running into this problem when doing homework 2. The homework folder is very simple:
homework2
|-- LogAnalysis.hs
|-- Log.hs
|-- error.log
|-- sample.log
Some data types are defined in Log.hs, and need to be imported into LogAnalysis.hs, which is the file I need to work on. The first few lines in LogAnalysis.hs are like this:
{-# OPTIONS_GHC -Wall #-}
module LogAnalysis where
import Log
-- Exercise 1: parse an individual message
...
However, I got error message like this in my emacs using haskell mode with intero:
error:
Could not find module 'Log'
Use -v to see a list of the files searched for.
(Hit 'C-c C-r' in the Haskell buffer to apply suggestions.)
Same message appears when using 'C-c C-l' to load to ghci within emacs.
But this error doesn't appear when loading LogAnalysis.hs in command line using stack ghci, the message is instead:
Prelude> :l LogAnalysis.hs
[1 of 2] Compiling Log ( Log.hs, interpreted )
[2 of 2] Compiling LogAnalysis ( LogAnalysis.hs, interpreted )
Ok, two modules loaded.
*LogAnalysis>
So I'm guessing this error has something to do with my emacs' setup of haskell mode and intero mode, but couldn't find any available solution yet.
Thanks for reading this question, your help will be appreciated!
It seems that intero needs a package.yaml and a stack.yaml in order to locate your source files. You can just run stack init or stack new to auto matically generate these project files.
I met this problem several times. The above method solved my problem on my Windows and Fedora, so I hope this will help you.
With the help of Krantz's answer and some more reading, the problem is solved by creating a new project with stack so intero knows the location of my source files.
Thus this problem is caused by intero's not knowing where to look for local modules when in intero-global-mode in emacs.
Here I'll write my own answer here to expand Krantz's answer a little more, and to document the process of solving this problem as a beginner to Haskell:
For intero to be able to import local modules, we should avoid using intero-global-mode and instead creating a local project (which in hindsight makes more sense to me).
So in the case of this Homework 2, instead of moving files to a homework folder as described in the question, I'll stack new homework2, and move the sources files into homework2\src. Then when using emacs to load LogAnalysis.hs, instead of this previous message I got in *intero:global-project::repl*:
Loaded GHCi configuration from /Users/[username]/.stack/global-project/.stack-work/intero/intero-[script]
and error message when loading LogAnalysis.hs, I'm now able to get:
Loaded GHCi configuration from /path/to/homework2/.stack-work/intero/intero-[script]
in *intero:homework2:homework2:repl*. And using C-c C-l to load LogAnalysis.hs now gets:
[2 of 2] Compiling LogAnalysis ( /path/to/homework2/src/LogAnalysis.hs, interpreted ) [flags changed]
Ok, two modules loaded.
So problem solved.

Stack: Compile stand-alone source file

Once you've installed Stack, you can use it to install GHC for you. Great!
...now how do I compile a file with it?
To be clear: What you're supposed to do is write a package specification and have Stack build that. But surely there must be a way to trivially compile a tiny helper file inside that project? It seems silly to have to construct an entire project just so I can build a tiny helper program and have it easily runnable from the shell.
I know I can run any Haskell file using Stack. But I cannot for the life of me figure out how to compile the thing...
You can use stack ghc -- <file names> to compile and link a set of files (it's briefly mentioned in Stack's user guide):
You'll sometimes want to just compile (or run) a single Haskell source file, instead of creating an entire Cabal package for it. You can use stack exec ghc or stack exec runghc for that. As simple helpers, we also provide the stack ghc and stack runghc commands, for these common cases.
The -- is to ensure the arguments we pass are sent to ghc, rather than being parsed as arguments to stack exec. It's the same thing as when trying to pass arguments to an executable you've made using the normal stack toolchain: stack exec myExe -foo passes -foo to exec, not myExe, stack exec myExe -- -foo behaves as desired.
For example:
Bar.hs
module Bar where
bar :: Int
bar = 5
Foo.hs
import Bar
main :: IO ()
main = print bar
Compilation (don't even need to specify Bar.hs in the build files, it's sourced automatically):
> stack ghc -- Foo.hs
[1 of 2] Compiling Bar ( Bar.hs, Bar.o )
[2 of 2] Compiling Main ( Foo.hs, Foo.o )
Linking Foo ...
> ./Foo
5
There's no problem with dependencies either - it looks like all the packages installed locally are available to the build (you can use containers or QuickCheck without any additional build params).

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.

How to display a backtrace for an Error thrown in Haskell

I'm currently debugging an algorithm I implemented in Haskell for my Diploma thesis. It seems to work correctly for most inputs, yet I found one input which makes GHC throw the error
*** Exception: Map.find: element not in the map
Since I have many Map lookups in my code, I need to find the line throwing this error to make any sense of it. I read through this guide, but although I set the flag fbreak-on-exception (and -error), all GHCi gives me after tracing the function I'm testing is:
[...]> :trace test
[...]
Stopped at <exception thrown>
_exception ::
e = GHC.Exception.SomeException (GHC.Exception.D:Exception _
(GHC.Show.D:Show ...) ....)
(GHC.Exception.ErrorCall ['M',....])
Unable to list source for <exception thrown>
Try rerunning with :trace, :back then :list
[<exception thrown>] [...]> :history
Empty history. Perhaps you forgot to use :trace?
Trying :trace again doesn't seem to help either.
So, can someone tell me what is going wrong or offer another way of finding the offending line? Thanks in advance!
PS: I'm using GHC version 7.0.3, so the linked guide should apply.
Maybe this will help you
http://www.haskell.org/haskellwiki/Debugging
LocH provides wrappers over assert for generating source-located exceptions and errors.
...
adding: import Debug.Trace.Location and then recompiling with the preprocessor on:
$ ghc A.hs --make -pgmF loch -F -no-recomp
[1 of 1] Compiling Main ( A.hs, A.o )
Linking A ...
$ ./A
A: A.hs:14:14-19: Maybe.fromJust: Nothing
There are also other tips on the wiki, like e.g. using the Safe-Library.

Resources