ghci

Debugging infinite loops in Haskell programs with GHCi

Deadly 提交于 2019-11-27 02:08:28
问题 For the first time I've encountered an infinite loop in a Haskell program I'm writing. I've narrowed it down to a quite specific section of code, but I cannot seem to pinpoint exactly where I have a non-terminating recursive definition. I'm vaguely familiar with :trace and :history in GHCi, but the problem is that some branches of my code involve quite a bit of recursive modifications of a Data.Map.Map in the sense that the map x is obtained by adjust ing something in the map x' based on

How to make a Haskell cabal project with library+executables that still run with runhaskell/ghci?

人盡茶涼 提交于 2019-11-26 23:56:39
问题 If you declare a library + executable sections in a cabal file while avoiding double compilation of the library by putting the library into a hs-source-dirs directory, you cannot usually run your project with ghci and runhaskell anymore, especially if the executables have helper modules themselves. What is a recommended project layout that only builds what is needed once allows using runhaskell has a clean structure without hacks? 回答1: Let's assume you have a mylib library, and mylib

How to hack GHCi (or Hugs) so that it prints Unicode chars unescaped?

本秂侑毒 提交于 2019-11-26 22:43:45
问题 Look at the problem: Normally, in the interactive Haskell environment, non-Latin Unicode characters (that make a part of the results) are printed escaped, even if the locale allows such characters (as opposed to direct output through putStrLn , putChar which looks fine and readable)--the examples show GHCi and Hugs98: $ ghci GHCi, version 7.0.1: http://www.haskell.org/ghc/ :? for help Prelude> "hello: привет" "hello: \1087\1088\1080\1074\1077\1090" Prelude> 'Я' '\1071' Prelude> putStrLn

How to run a haskell file in interpreted mode

僤鯓⒐⒋嵵緔 提交于 2019-11-26 19:13:52
问题 I've been told you can interpret haskell files (which I assume means they will work like Ruby/Python/Perl). Can't find the command line option on ghc to do this, though. It always wants to compile my file. Took a look at ghci as well, but it always dumps me into a repl. I'm basically wanting to just do ghc -i MyFile.hs (where -i is a made up flag that I'm pretending correllates to interpreted mode) and have it execute so that I can get quick feedback while I'm trying out ideas and learning.

Seeing Typeclass definition in ghci for a specific type

百般思念 提交于 2019-11-26 17:23:42
问题 Is there a way to see Typeclass definition in ghci for a specific type? For example, Maybe is defined like this: instance Functor Maybe where fmap f (Just x) = Just (f x) fmap f Nothing = Nothing Can I see this in ghci ? When, I use :info in ghci, I get this: Prelude> :i Maybe data Maybe a = Nothing | Just a -- Defined in `Data.Maybe' instance Eq a => Eq (Maybe a) -- Defined in `Data.Maybe' instance Monad Maybe -- Defined in `Data.Maybe' instance Functor Maybe -- Defined in `Data.Maybe'

:sprint for polymorphic values?

≡放荡痞女 提交于 2019-11-26 16:43:24
I am wondering why :sprint reports xs = _ in this case: Prelude> xs = map (+1) [1..10] Prelude> length xs 10 Prelude> :sprint xs xs = _ but not in this case: Prelude> xs = map (+1) [1..10] :: [Int] Prelude> length xs 10 Prelude> :sprint xs xs = [_,_,_,_,_,_,_,_,_,_] Note: I am running ghci with -XNoMonomorphismRestriction . Does it have to do with the fact that the type of xs is polymorphic in the first case but not in the second? I'd like to know what's going on internally. jozefg The gist is that the with the polymorphic xs it has a type of the form xs :: Num a => [a] typeclasses under the

Multi-line commands in GHCi

孤街醉人 提交于 2019-11-26 15:08:34
I am having problem in entering multi-line commands in ghci. The following 2-line code works from a file: addTwo :: Int -> Int -> Int addTwo x y = x + y But when I enter in ghci, I get an error: <interactive>:1:1: error: Variable not in scope: addTwo :: Int -> Int -> Int I also tried putting the code inside :{ ... :} , but they are also not working for this example, because this is just appending the lines into one line, which should not be the case. I am using WinGHCi, version 2011.2.0.1 Most of the time, you can rely on type inference to work out a signature for you. In your example, the

Why :sprint always prints a “_”?

限于喜欢 提交于 2019-11-26 14:35:49
问题 Prelude> let a = 3 Prelude> :sprint a a = _ Prelude> let c = "ab" Prelude> :sprint c c = _ Why does it always print a _ ? I don't quite get the semantics of the :sprint command. 回答1: Haskell is a lazy language. It doesn't evaluate results until they are "needed". Now, just printing a value causes all of it to be "needed". In other words, if you type an expression in GHCi, it will try to print out the result, which causes it all to be evaluated. Usually that's what you want. The sprint command

How to define a function in ghci across multiple lines?

戏子无情 提交于 2019-11-26 12:02:07
I'm trying to define any simple function that spans multiple lines in ghci, take the following as an example: let abs n | n >= 0 = n | otherwise = -n So far I've tried pressing Enter after the first line: Prelude> let abs n | n >= 0 = n Prelude> | otherwise = -n <interactive>:1:0: parse error on input `|' I've also attempted to use the :{ and :} commands but I don't get far: Prelude> :{ unknown command ':{' use :? for help. I'm using GHC Interactive version 6.6 for Haskell 98 on Linux, what am I missing? for guards (like your example), you can just put them all on one line and it works (guards

Prefix form of unary operator in Haskell

我的梦境 提交于 2019-11-26 11:42:11
问题 In GHCi: Prelude> (+3) 2 5 Prelude> (*3) 2 6 Prelude> (/3) 2 0.6666666666666666 Prelude> (-3) 2 No instance for (Num (t -> t1)) arising from the literal 3\' at <interactive>:1:2 Possible fix: add an instance declaration for (Num (t -> t1)) In the expression: 3 In the expression: (- 3) 2 In the definition of it\': it = (- 3) 2 How can I correct the last one to make it return -1? 回答1: Haskell's grammar doesn't allow you to use - like that. Use the subtract function instead: (subtract 3) 2 回答2: