ghci

Ghc: partially compile Haskell code?

安稳与你 提交于 2019-11-30 08:17:36
When I compile a Haskell file with ghci , typically with :load , and if there is no type error, all the expressions are loaded in the ghc interpreter. It's very nice: I can play around with :t to figure out the type of various expressions. My problem is: if there is a tiny error somewhere, ghci is not able to load anything (not even the imported modules!!), which makes finding the right types even more difficult. I always do the same: comment out all the bits that do not typecheck, find the relevant types wiht :t in ghci, and de-comment. But this is so tedious! Is there a better workflow for

Why can't I define a new type in ghci?

99封情书 提交于 2019-11-30 07:52:20
I get an error in ghci when I try to define a new type: Prelude> data Point = Pt Int Int <interactive>:1:0: parse error on input `data' Prelude> let data Point = Pt Int Int <interactive>:1:4: parse error on input `data' What am I doing wrong? titaniumdecoy , I remember being helped with this sort of GHCi mystery when I learned the frequently made point that writing things like 'let square x = x * x' inside the GHCi is like writing let a = f b with do notation in the IO monad -- say in this sort of example: palindromize :: IO () palindromize = do a <- readFile "foo.txt" let b = reverse a

Haskell line of code not compiling: “Illegal datatype context”

我与影子孤独终老i 提交于 2019-11-30 04:36:47
I am not able to get this line of code compiled in Haskell but it works on my professor's system. I use ghci version 7.6.2. data Eq a => Shape a = Shape a More precisely, this is the error I am getting [1 of 1] Compiling Main ( test.hs, interpreted ) test.hs:1:6: Illegal datatype context (use -XDatatypeContexts): Eq a => Failed, modules loaded: none. What is the mistake here? Thanks Your professor is probably using an older version of GHC. The line you posted uses a feature which has quite recently been removed. The possible solutions are: Remove Eq a => and write data Shape a = Shape a . As

Why am I able to use my value constructor even though I don't export it?

半世苍凉 提交于 2019-11-30 04:15:22
问题 For practice, I'm implementing a queue data type in a module called "Queue". My data type is also called "Queue", as is its only value constructor: module Queue (Queue, enq, emptyQueue) where data Queue a = Queue { inbox :: [a], outbox :: [a] } deriving (Eq, Show) emptyQueue :: Queue a emptyQueue = Queue [] [] enq :: a -> Queue a -> Queue a enq x (Queue inb out) = Queue (x:inb) out -- other function definitions (irrelevant here)... As far as I understand, because I wrote Queue , not Queue(..)

How can I load optimized code in GHCI?

本秂侑毒 提交于 2019-11-30 04:13:44
I am writing a module that relies on optimization. I want to test this module in ghci. But starting ghc in --interactive mode automatically disables optimization; if I compile the module with -O and then try to load it in an interactive session, ghc insists on loading it in interpreted mode. For a simple test case to distinguish optimized and unoptimized modules, isOptimized below evaluates to True with optimization on, but False with optimization off: isOptimized :: Bool isOptimized = g g :: Bool g = False {-# NOINLINE g #-} {-# RULES "g/True" g = True #-} Either use ghci -fobject-code -O

Haskell Error: parse error on input `='

﹥>﹥吖頭↗ 提交于 2019-11-30 03:56:57
问题 Specs GHC 6.12.1 Mac OS X 10.6.4 x64 MacBook Pro Problem I'm having trouble using let syntax. The following code refuses to compile: module Main where main = let x = 1 y = 2 z = 3 in putStrLn $ "X = " ++ show x ++ "\nY = " ++ show y ++ "\nZ = " ++ show z I tried tabbing in y = 2 and z = 3 even more. No dice. (Undesirable) Solutions The only way I've gotten the code to compile is either Replacing hard tabs with spaces. Replacing the let clause with a where clause. 回答1: Saizan on #haskell

How to provide explicit type declarations for functions when using GHCi?

霸气de小男生 提交于 2019-11-29 22:35:09
How to I define the equivalent of this function (taken from learnyouahaskell ) inside GHCi? import Data.List numUniques :: (Eq a) => [a] -> Int numUniques = length . nub Without the type declaration, GHCi accepts the function definition, but it ends up with an unhelpful type: Prelude Data.List> import Data.List Prelude Data.List> let numUniques' = length . nub Prelude Data.List> :t numUniques' numUniques' :: [()] -> Int The resulting function only accepts a list of units as a parameter. Is there a way provide type declarations in GHCi? Or is there another way to define functions like these

How to configure GHCi to automatically import modules

扶醉桌前 提交于 2019-11-29 22:12:33
When I use GHCi, I almost always end up importing Control.Applicative , Data.List , etc. . Is there a way to configure GHCi to automatically import those modules. Also, after importing them, how do I keep the prompt from being insanely long? Prelude Control.Applicative Data.List Database.HDBC Database.HDBC.Sqlite3 System.Directory> GHCi looks for its configuration file at ~/.ghc/ghci.conf on Unix-like systems. %APPDATA%\ghc\ghci.conf on Windows. The configuration file syntax is simple: it's a list of GHCi commands to execute on startup. For example, your ghci.conf could contain: import Control

Haskell : display/get list of all user defined functions

独自空忆成欢 提交于 2019-11-29 17:14:14
问题 Is there a command in Haskell which displays (or get as a list of) all the user defined functions which have been loaded/defined in the GHCi? Thanks 回答1: To see bindings you've made at the ghci prompt (e.g. with let or <- ), try :show bindings . If you've loaded some modules, you can use :show modules to get the names of loaded modules and then :browse ModuleName to list everything in scope from that module. 回答2: When in ghci, use :browse or just :bro after loading the file. You may also