ghci

Can a GHCI config file use CPP macros?

好久不见. 提交于 2019-12-05 15:33:40
I was thinking it would be nice to set up my global GHCI config such that my commonly-used imports occur automatically when the packages that provide them are present. I tried adding this to ~/.ghc/ghci.conf : :set -XCPP #ifdef MIN_VERSION_containers import Data.Set (Set) import qualified Data.Set as Set import Data.Map (Map) import qualified Data.Map as Map #endif But apparently that does not work. > stack repl Configuring GHCi with the following packages: GHCi, version 8.0.2: http://www.haskell.org/ghc/ :? for help <interactive>:24:1: error: parse error on input ‘#’ <interactive>:29:1: error

How do I make lenses from a record in GHCi

拥有回忆 提交于 2019-12-05 11:13:23
问题 I want to play around with the Lens library a bit. I've loaded it into GHCi and created a record data type with the appropriate underscores: > data Foo a = Foo {_arg1 :: Int, _arg2 :: [a]} I would like to make the lenses for Foo using the makeLenses template. I would like to do this without needing to read through the entire set of Template-Haskell docs. What incantation can I type in at the GHCi prompt to get this to work? 回答1: Tested in GHCi 7.8.3: :set -XTemplateHaskell :m +Control.Lens :{

Why can ghci see non-exported types and constructors? How can I fix it?

假装没事ソ 提交于 2019-12-05 11:12:07
I am a novice in Haskell. Here's some simple code: module Src ( -- The 'Answer' type isn't exported Shape(Circle), -- i.e 'Rectangle' data constructor isn't exported Point(..), area, nudge ) where data Answer = Yes | No deriving (Show) data Point = Point Float Float deriving (Show) data Shape = Circle Point Float | Rectangle Point Point deriving (Show) area :: Shape -> Float area (Circle _ r) = pi * r ^ 2 area (Rectangle (Point x1 y1) (Point x2 y2)) = (abs $ x2 - x1) * (abs $ y2 - y1) nudge::Shape->Float->Float->Shape nudge (Rectangle(Point x1 y1)(Point x2 y2)) dx dy = Rectangle (Point (x1 +

Haskell's type inference strangeness

十年热恋 提交于 2019-12-05 09:35:43
Look at this output from ghci: Prelude> :t Data.Map.lookup Data.Map.lookup :: Ord k => k -> Data.Map.Map k a -> Maybe a Prelude> :t flip Data.Map.lookup flip Data.Map.lookup :: Ord a => Data.Map.Map a a1 -> a -> Maybe a1 Prelude> let look = flip Data.Map.lookup Loading package array-0.3.0.2 ... linking ... done. Loading package containers-0.4.0.0 ... linking ... done. Prelude> :t look look :: Data.Map.Map () a -> () -> Maybe a Why look 's inferred type differs from type of flip Data.Map.lookup ? To give you some context. Initially I had small program and was trying to figure out why it

GHCi runtime linker issue when using FFI declarations

让人想犯罪 __ 提交于 2019-12-05 08:25:02
问题 I have a problem regarding FFI in Haskell and the interactive mode of GHC again. Consider FFISo.hs : {-# LANGUAGE OverloadedStrings #-} module Main where import qualified Data.ByteString.Char8 as B import FFIFun.Foo main :: IO () main = do B.putStrLn "main" callMeFromC callMeFromHaskell return () c.c : #include <stdio.h> void callMeFromC(void); void callMeFromHaskell(void) { printf("callMeFromHaskell\n"); callMeFromC(); } FFIFun/Foo.hs : {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE

“No instance for” error

[亡魂溺海] 提交于 2019-12-05 08:05:41
Following an example in http://en.wikibooks.org/wiki/Haskell/Beginning Prelude> let abs x = if x < 0 then -x else x Prelude> abs 5 5 Prelude> abs -3 <interactive>:1:6: No instance for (Num (a0 -> a0)) arising from the literal `3' Possible fix: add an instance declaration for (Num (a0 -> a0)) In the second argument of `(-)', namely `3' In the expression: abs - 3 In an equation for `it': it = abs - 3 What's wrong? Haskell thinks you're trying to subtract 3 from abs , and is complaining that abs is not a number. You need to add parenthesis when using the unary negation operator: abs (-3) The

How can I get `ghci` to use my `show` function?

纵饮孤独 提交于 2019-12-05 03:51:17
Let's say you want to use your own show function (for example, let show = take 1000 . Prelude.show ). How can you allow ghci to use that for printing instead of the built in show ? Lee You can define your own interactive print function e.g: module BetterPrint betterPrint a = putStrLn (take 1000 $ show a) then start ghci as ghci -interactive-print=BetterPrint.betterPrint 来源: https://stackoverflow.com/questions/35613612/how-can-i-get-ghci-to-use-my-show-function

Using ghci to find type

◇◆丶佛笑我妖孽 提交于 2019-12-05 03:35:19
When I do something simple in ghci, like the following: let x = 7 + 2 I expect ghci to give a response of the type that x holds, like: x :: Integer When I run ghci, I do not get that the above line. How do I get that response? To show types automatically use :set +t : μ> :set +t μ> let x = 7 + 2 x :: Integer μ> Use the ghci :t command, like so: Prelude> let x = 7 + 2 Prelude> :t x x :: Integer Prelude> Taneb To find the type of something in GHCi, you can use the :type command, or (as is much more common), the abbreviated :t . With this, you can do something like: Prelude> let x = 7 + 2 Prelude

Testing FFI Code (with “foreign import”s) with GHCi

不羁的心 提交于 2019-12-05 01:48:36
Good (your local time of day), everyone. I went through Real World Haskell's chapter on the Foreign Function Interface, and did some follow-up reading here . I'm now experimenting with binding to C functions, and I'd like some clarification on some things. The following is fairly clear: foreign import ccall unsafe "math.h sin" c_sin :: CDouble -> CDouble I can load this and code that uses it in ghci, and everything is fine. It even loads in the embedded ghci in emacs's Haskell mode. I find this great for testing. math is a system library so this is straight-forward. Now an example from Real

GHCi ignores type signature

ⅰ亾dé卋堺 提交于 2019-12-04 16:45:33
问题 Prelude> let myprint = putStrLn . show Prelude> :t myprint myprint :: () -> IO () OK, nothing too unusual here. Just GHCi type defaulting rules, I guess... Prelude> let myprint = (putStrLn . show) :: Show x => x -> IO () Prelude> :t myprint myprint :: () -> IO () What sorcery is this?? You're point-blank ignoring my type declaration?! O_O Is there some way I can convince GHCi to do what I actually intended? 回答1: We can do the following, with monomorphism restriction on: >let myprint :: Show x