Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected semi-colons in conditional

Tags:

haskell

cabal

I have the following line of code which when compiled with GHC it goes without a hitch:

addRDF c (Just (FILE))  = do
  (_:file:_) <- getArgs
  check <- doesFileExist file
  if check then do rdfG <- TLI.readFile file >>= (return . parseN3fromText)
                   case rdfG of (Left s)  -> putStrLn s
                                (Right g) -> storeRDF c g
  else do putStrLn "Specified files does not exist"

But when I run it through the cabal build process, it dumps out the following error.

Repository/Adder.hs:46:35:
    Unexpected semi-colons in conditional:
    if check then do { rdfG <- TLI.readFile file
                             >>=
                               (return . parseN3fromText);
                       case rdfG of {
                         (Left s) -> putStrLn s
                         (Right g) -> storeRDF c g } }; else do { putStrLn
                                                                    "Specified files does not exist" }
Perhaps you meant to use -XDoAndIfThenElse?

I can see the additional semicolon in the error but I don't understand where that comes from.

Here is my cabal configuration file:

cabal-version: >= 1.2
build-type: Simple

library
  build-depends:
    base,
    containers,
    HTTP >= 4000.2.2,
    directory >= 1.1.0.0,
    text >= 0.11.1.13,
    swish >= 0.6.5.2
  exposed-modules: Repository.Adder, Repository.Configuration
  ghc-options: -Wall

executable repository-add
  main-is: repository-add.hs
  build-depends:
    MissingH,
    swish >= 0.6.5.2,
    split >= 0.1.4.2
  ghc-options: -Wall

UPDATE

With correct indentation for if:

addRDF c (Just (FILE))  = do (_:file:_) <- getArgs
  check <- doesFileExist file
  if check
  then do rdfG <- TLI.readFile file >>= (return . parseN3fromText)
          case rdfG of (Left s)  -> putStrLn s
                       (Right g) -> storeRDF c g
  else do putStrLn "Specified files does not exist"

I get a semicolon after check now as well:

Repository/Adder.hs:46:35:
    Unexpected semi-colons in conditional:
        if check; then do { rdfG <- TLI.readFile file
                                  >>=
                                    (return . parseN3fromText);
                            case rdfG of {
                              (Left s) -> putStrLn s
                              (Right g) -> storeRDF c g } }; else do { putStrLn
                                                                         "Specified files does not exist" }
    Perhaps you meant to use -XDoAndIfThenElse?
like image 829
mhitza Avatar asked Apr 09 '12 16:04

mhitza


1 Answers

Your indentation is incorrect, but it works when you use the raw GHC compiler because it automatically turns on the syntactic extension mentioned in the error message (DoAndIfThenElse).

In Cabal, you have to specify the language extensions that you use manually, either at the top of your code files, or in your Cabal files; otherwise, they will not be enabled by the compiler.

One correct version of the indentation for if-clauses is like this:

if check
  then do
    rdfG <- TLI.readFile file >>= (return . parseN3fromText)
    case rdfG of
      (Left s)  -> putStrLn s
      (Right g) -> storeRDF c g
  else putStrLn "Specified files does not exist"

You have to keep the then part and the else part at deeper indentation levels than the block they're a part of.

like image 101
dflemstr Avatar answered Sep 21 '22 14:09

dflemstr