How to display a backtrace for an Error thrown in Haskell - 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.

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.

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

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.

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.

Haskell doctest and FFI

I have a module which binds to a C function using the FFI. How can I make this module use doctest?
The error I get when running doctest Foo.hs is something like this:
ByteCodeLink: can't find label
During interactive linking, GHCi couldn't find the following symbol:
bar
This may be due to you not asking GHCi to load extra object files,
archives or DLLs needed by your current session. Restart GHCi, specifying
the missing library using the -L/path/to/object/dir and -lmissinglibname
flags, or simply by naming the relevant files on the GHCi command line.
Alternatively, this link failure might indicate a bug in GHCi.
If you suspect the latter, please send a bug report to:
glasgow-haskell-bugs#haskell.org
### Failure in Foo.hs:41: expression `foo'
expected: [42]
but got:
<interactive>:24:1: Not in scope: `bar'
Examples: 2 Tried: 2 Errors: 0 Failures: 1
Doctest accepts arbitrary GHC flags. If you want to run Doctest with FFI code you need to pass the exact same flags that you would need to run a GHCi session with that code. Have e.g. a look at the Doctest driver of unix-time.

Resources