Haskell package vector-space fails at compile time - haskell

I tried to install the Haskell diagrams library and one of the dependencies cannot be installed. Being not that experienced to haskell, I cannot quite see how to resolve this error.
I removed warnings that I believe are not related to the failed build (------ warnings ommitted (-Winvalid-pp-token) -----)
[hsenv]➜ ~HSENV cabal install vector-space-0.8.5
Resolving dependencies...
Downloading vector-space-0.8.5...
Configuring vector-space-0.8.5...
/var/folders/nq/fx4vf3v14jz9b8mldvcyv1xr0000gn/T/23218.c:1:12:
warning: control reaches end of non-void function [-Wreturn-type]
int foo() {}
^
1 warning generated.
Building vector-space-0.8.5...
Preprocessing library vector-space-0.8.5...
------ warnings ommitted (-Winvalid-pp-token) -----
src/Data/VectorSpace.hs:4:3:
error: invalid preprocessing directive
#-}
^
------ warnings ommitted (-Winvalid-pp-token) -----
2 warnings and 1 error generated.
Failed to install vector-space-0.8.5
cabal: Error: some packages failed to install:
vector-space-0.8.5 failed during the building phase. The exception was:
ExitFailure 1

I presume you’re on OS X with Xcode 5 installed? There are instructions for that.
Xcode 5 supplies Clang as the C compiler, and with respect to pre-processing there are some “differences of interpretation” between it and GCC. These differences can affect some Haskell code that uses the CPP extension. An alternate approach for users with Xcode 5 is to install a GCC and direct GHC at that. See this page for instructions.

As Jon Purdy says, this has to do with the preprocessing. In particular, some of the modules in vector-space have their list of required language extensions on top like this:
{-# LANGUAGE MultiParamTypeClasses, TypeOperators
, TypeFamilies, UndecidableInstances, CPP
, FlexibleContexts
#-}
{-# OPTIONS_GHC -Wall #-}
----------------------------------------------------------------------
-- |
-- Module : Data.VectorSpace
...
i.e. a {#- ... -#} over multiple lines, and the delimiter is thus a line starting with #. Which then triggers the preprocessor, to obviously no good result!
You can change this to
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
or probably this will also do:
{-# LANGUAGE MultiParamTypeClasses, TypeOperators
, TypeFamilies, UndecidableInstances, CPP
, FlexibleContexts #-}
Here's a script that does this transformation automatically:
import System.IO.Strict as S
import System.Environment
import Control.Monad
rmLeadHash :: [String] -> [String]
rmLeadHash [] = []
rmLeadHash [l] = [l]
rmLeadHash (l:l':ls)
| ('#':_) <- dropWhile (==' ') l' = (l ++ l') : rmLeadHash ls
| otherwise = l : rmLeadHash (l':ls)
main = do
files <- getArgs
forM_ files $ \f -> do
hs <- S.readFile f
writeFile f $ unlines . rmLeadHash . lines $ hs
You can with this do
$ git clone git://github.com/conal/vector-space.git # or cabal fetch vector-space
$ cd vector-space
$ cabal install strict # needed for my script
$ find src -name '*.hs' | xargs runhaskell remLeadingHashes.hs
# ^- the script I've posted
$ cabal install # installs the modified vector-space package
If it works, you may want to push the changes.

Related

Why is stack not auto detecting my module?

I am trying to write a script that uses Diagrams but the stack package detection feature is confusing me. I have two files and when the support module imports SVGFonts stack doesn't add it to the list of modules that are found. Instead, it's hidden. When the import is in the main file, however, it downloads it and finds it for the build just fine.
Main.hs
-- stack --resolver lts-19.0 script
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}
import Diagrams.Prelude
import MyMod (val)
import Diagrams.Backend.SVG
main = print val
MyMod.hs
module MyMod where
import Diagrams.Backend.SVG
val = 1
And I am getting this error
Could not load module ‘Diagrams.Backend.SVG’
It is a member of the hidden package ‘diagrams-svg-1.4.3.1’.
You can run ‘:set -package diagrams-svg’ to expose it.
(Note: this unloads all the modules in the current scope.)
Use -v (or `:set -v` in ghci) to see a list of the files searched for.
|
3 | import Diagrams.Backend.SVG (loadFont)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
How can I get stack to automatically find that I need SVGFonts and import it properly without adding the import to my main file?

Detecting unused functions at build time in Haskell

I'm trying to get the GHC to warn me whenever I write a new function and forget to use it anywhere. I've added the following function in my Main.hs:
unused :: () -> ()
unused _ = ()
and of course not used it.
I have the following in my *.cabal file:
ghc-options:
-W
-Wall
-Werror
-Wcompat
-Wincomplete-uni-patterns
-Wincomplete-record-updates
-Wredundant-constraints
-Wpartial-fields
-Wunused-binds
I've specifically added -Wunused-binds to try to force the compiler to fail on this but it does not report any warning about an unused function.
Any idea what am I doing wrong here?
EDIT
Just to give a bigger picture, here is the beggining of the main file:
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Main where
import Apecs
import Linear ( V2(..) )
import Control.Monad
import Control.Monad.IO.Class
import Control.Monad.Catch
import qualified System.Terminal as T
import System.Terminal.Internal
import qualified Data.Map as M
import System.Random
Exporting only main function from module fixed the issue:
module Main(main) where
EDIT
Just for reference, this flag is already contained in -Wall so it would be enough to have this in cabal to catch it:
ghc-options:
-Wall
-Werror

How to run yesod hello world app with stack?

I've created a yesod project with following command:
stack new yeplay yesodweb/simple
and I've added a helloworld.hs file to app directory. The app directory contains the following files:
~/haskell/yeplay/app$ ls
devel.hs DevelMain.hs helloworld.hs main.hs
The helloworld.hs file has the following content:
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
import Yesod
data HelloWorld = HelloWorld
mkYesod "HelloWorld" [parseRoutes|
/ HomeR GET
|]
instance Yesod HelloWorld
getHomeR :: Handler Html
getHomeR = defaultLayout [whamlet|Hello World!|]
main :: IO ()
main = warp 3000 HelloWorld
Then I tried to run the file with the command:
runhaskell helloworld.hs
Command 'runhaskell' not found, but can be installed with:
sudo apt install ghc
as described in the documentation.
How can I start the app?

Setting import path in an OPTIONS pragma

According to the GHC 8.4.3 flag reference, the -i flag is dynamic, which means it should be settable by an OPTIONS pragma.
So I tried the following:
.
├── Main.hs
└── imp
└── Imported.hs
Contents of imp/Imported.hs:
module Imported (foo) where
foo :: String
foo = "Foo"
Contents of Main.hs:
{-# OPTIONS_GHC -iimp #-}
import Imported (foo)
main :: IO ()
main = putStrLn foo
However, if I try to run Main.hs using runhaskell, it complains that Imported cannot be found:
$ runhaskell -v Main.hs
...
Main.hs:2:1: error:
Could not find module ‘Imported’
Locations searched:
Imported.hs
Imported.lhs
Imported.hsig
Imported.lhsig
How do I specify the -i flag in an OPTIONS pragma?
This appears to be a regression of a documentation bug that was fixed in 2007 and then re-broken in 2014 when a bunch of "static"s were changed to "dynamic"s in the flag reference table. As per the linked bug report, the -i flag is not fully dynamic. It can be :set in GHCi but can't be specified in an OPTIONS_GHC line.

parse error in haskell file with unicode symbol

It's probably very stupid, but I get a
parse error on input ‘⊥’
when I process this file
#!/usr/bin/env stack
-- stack --install-ghc --resolver lts-5.13 runghc --package http-conduit
{-# LANGUAGE OverloadedStrings, RankNTypes, MultiParamTypeClasses, FunctionalDependencies #-}
{-# LANGUAGE ScopedTypeVariables, UnicodeSyntax, PartialTypeSignatures #-}
module MainImplicitConfiguration where
a = ⊥
Is there a reason why that should be a parse error ?
Try:
a = (⊥)
(along with LANGUAGE UnicodeSyntax)

Resources