Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

running ghci on a module that needs LANGUAGE CPP

I'm running into problems with this sequence of commands:

wget http://hackage.haskell.org/package/github-0.7.1/github-0.7.1.tar.gz
tar zxf github-0.7.1.tar.gz
cd github-0.7.1
ghci samples/Users/ShowUser.hs

The error I am getting is:

Github/Private.hs:142:0:
     error: missing binary operator before token "("

Github/Private.hs:148:0:
     error: missing binary operator before token "("
phase `C pre-processor' failed (exitcode = 1)

and that's because the module Github/Private.hs uses cpp directives in two places:

#if MIN_VERSION_http_conduit(1, 9, 0)
    successOrMissing s@(Status sci _) hs cookiejar
#else
    successOrMissing s@(Status sci _) hs
#endif
      | (200 <= sci && sci < 300) || sci == 404 = Nothing
#if MIN_VERSION_http_conduit(1, 9, 0)
      | otherwise = Just $ E.toException $ StatusCodeException s hs cookiejar
#else
      | otherwise = Just $ E.toException $ StatusCodeException s hs
#endif

It appears that ghci is choking on these CPP directives. However, cabal install successfully compiles and installs the package. Using ghci -XCPP doesn't help.

My question is: how can I run a sample program (i.e. like one in the samples directory) with ghci using the library code that's in the Github directory of this package?

I'd like to experiment with tweaking both the sample programs and the library code, so I'd like to run everything in ghci.

One thing that does work is:

cabal install
cd samples
ghci Users/ShowUser.hs

but, again, I'd rather not have to install the library code just to test it out.

like image 467
ErikR Avatar asked Oct 27 '13 19:10

ErikR


People also ask

How do I run a Ghci file?

GHCi is the interactive interface to GHC. From the command line, enter "ghci" (or "ghci -W") followed by an optional filename to load. Note: We recommend using "ghci -W", which tells GHC to output useful warning messages in more situations. These warnings help to avoid common programming errors.

How do I run Ghci in terminal?

If you have installed the Haskell Platform, open a terminal and type ghci (the name of the executable of the GHC interpreter) at the command prompt. Alternatively, if you are on Windows, you may choose WinGHCi in the Start menu. And you are presented with a prompt. The Haskell system now attentively awaits your input.

What is the difference between GHC and Ghci?

Introduction. GHCi is GHC's interactive environment, in which Haskell expressions can be interactively evaluated and programs can be interpreted.

How do you write multiple lines in Ghci?

The :set +m stuff is shorthand for the multiline :{ code :} construct. Whitespace is also significant in blocks, so you have to indent your function definition after your type definition by four spaces to account for the four spaces in let . Save this answer.


1 Answers

The following command works:

ghci -optP-include -optPdist/build/autogen/cabal_macros.h samples/Users/ShowUser.hs

It tells the C preprocessor to read the file dist/build/autogen/cabal_macros.h. This file is generated by cabal build, but you can abort it after the preprocessing step:

Resolving dependencies...
Configuring github-0.7.1...
Building github-0.7.1...
Preprocessing library github-0.7.1...
[ 1 of 24] Compiling Github.Data.Definitions ( Github/Data/Definitions.hs,    dist/build/Github/Data/Definitions.o )
^C

If you want those options to be set automatically when you launch ghci in that directory, create a .ghci file with this content:

:set -optP-include -optPdist/build/autogen/cabal_macros.h
like image 177
bennofs Avatar answered Oct 29 '22 06:10

bennofs