2

I've been given code by my lecturer that doesn't build in GHCI. As far as I know, it has been building correctly for my classmates.

The code I'm refering to is

import Data.Semigroup  (Semigroup ((<>)))

GHCI is throwing this error at me

Module ‘Data.Semigroup’ does not export ‘Semigroup((<>))’

Should this work? Is there perhaps something wrong with my version of GHC? All other import statements are working.

Ewan Brown
  • 143
  • 1
  • 9
  • 4
    Locally this works. What ghc version do you use? – Willem Van Onsem Jul 19 '19 at 19:56
  • 1
    @thatotherguy not really; prior to GHC-8 the `Data.Semigroup` module was not in the `base` package but that would give a different error. With the `semigroups` package installed, the behaviour should be the same. – leftaroundabout Jul 19 '19 at 20:04
  • 1
    @WillemVanOnsem I'm currently running 7.10.3. I'll try updating it? – Ewan Brown Jul 19 '19 at 20:34
  • 5
    Eeep, yeah that's ancient. GHC isn't like gcc, the open source packages and community assume you're within 3 releases of the latest. – Thomas M. DuBuisson Jul 19 '19 at 20:51
  • @ThomasM.DuBuisson but why does it give that error message and not `Could not find module ‘Data.Semigroup’`? Or if `semigroups` is installed, why does it not just work? – leftaroundabout Jul 19 '19 at 20:55
  • @leftaroundabout Not sure why you'd ask me but... likely because it _could_ find the module Data.Semigroup. Don't forget `Data.Semigroup` was a module in non-base packages for a while before base finally tamed things. – Thomas M. DuBuisson Jul 19 '19 at 20:59
  • 1
    @ThomasM.DuBuisson of course, but then the import should have worked just fine. – leftaroundabout Jul 19 '19 at 21:17
  • 1
    @leftaroundabout Not at all, the `semigroup` package has a module `Data.Semigroup` but it does not have export a symbol `(<>)`. It probably isn't alone. – Thomas M. DuBuisson Jul 19 '19 at 21:56
  • 1
    @ThomasM.DuBuisson The `semigroup` package doesn't exist on Hackage as far as I can tell. The `semigroups` package does exist, but for every version I could be bothered to check, its `Data.Semigroup`, if it exists at all (CPP is used to prevent it from existing on newer GHCs), exports the symbol `(<>)`. So what gives? – Daniel Wagner Jul 20 '19 at 02:10
  • 2
    @danielwagner what gives? Searching is hard, let's go linking: http://hackage.haskell.org/package/Semigroup-0.0.7/docs/Data-Semigroup.html – Thomas M. DuBuisson Jul 20 '19 at 02:35
  • @ThomasM.DuBuisson Phew! Nice find. – Daniel Wagner Jul 20 '19 at 05:18
  • 1
    @ThomasM.DuBuisson I don't know how you found that archaeological piece of documentation, but I really would like to read that comment before... hahaha, I was losing my mind trying to find in which version of ghc I could reproduce the error – developer_hatch Jul 20 '19 at 06:46
  • @leftaroundabout it turns out that the ghc version that is using should be `0.0.7`, is the only explanaition – developer_hatch Jul 20 '19 at 07:00
  • @DamianLattenero actually more like GHC-6.12 / base-4.2 (from 2010, which is the time of the `Semigroup` package). Still, I'd say “archaeological document” is a fair description! Do you want to integrate this in your (already great) answer, or should I write it up as Community Wiki? – leftaroundabout Jul 20 '19 at 07:04
  • @leftaroundabout fantastic! Yes I can add it of course! And can you also write a Community wiki? I don't know if it can be both things, I don't have much knowledge about community wiki to be honest – developer_hatch Jul 20 '19 at 07:08
  • 1
    @WillemVanOnsem locally works depending on the ghc version, your question is the key in this issue! – developer_hatch Jul 20 '19 at 07:49

2 Answers2

8

Final Edit

Is there perhaps something wrong with my version of GHC?

Absolutely yes, there is something wrong, is too old, to be precise:

GHC-6.12 / base-4.2 (from 2010, which is the time of the Semigroup package) -- thanks so much @leftaroundabout and @Thomas M. DuBuisson for the contribution!

And that package has not the (Semigroup ((<>))) function. Hence the error you see.

Edit 2

After comments, I tried to reproduce the OP environment to reproduce the error too, I installed in stack the ghc version 7.10.3, this is how looks the stack.yaml file:

resolver: lts-6.27 
system-ghc: false
packages:
- .

And after ruining a base stack project with a Main.hs file containing:

module Main where

import Data.Semigroup

main :: IO ()
main = do
  putStrLn "Hello"
  putStrLn "World"

the error I got is

/home/damian/test-semigroup/app/Main.hs:3:8:

Could not find module ‘Data.Semigroup’

Use -v to see a list of the files searched for.    
--  While building package test-semigroup-0.1.0.0 using:
      /home/damian/.stack/setup-exe-cache/x86_64-linux/Cabal-simple_mPHDZzAJ_1.22.5.0_ghc-7.10.3 --builddir=.stack-work/dist/x86_64-linux/Cabal-1.22.5.0 build lib:test-semigroup exe:test-semigroup-exe --ghc-options ""
    Process exited with code: ExitFailure 1

The same happend with older versions:

  • LTS 3.22 for ghc-7.10.2,

I couldt try with:

  • LTS 2.22 for ghc-7.8.4, published 4 years ago Because they where to old to run with cabal

and - LTS 0.7 for ghc-7.8.3, published 5 years ago Because they I cannot install it in a 64bit OS...

So, to be absolutely sure I tried with all the newer LTS versions one by one, yes... one by one, It took some time but worth the shot:

  • LTS 13.29 for ghc-8.6.5, published today
  • LTS 13.19 for ghc-8.6.4, published 3 months ago
  • LTS 13.11 for ghc-8.6.3, published 4 months ago
  • LTS 12.26 for ghc-8.4.4, published 7 months ago
  • LTS 12.14 for ghc-8.4.3, published 9 months ago
  • LTS 11.22 for ghc-8.2.2, published 11 months ago
  • LTS 9.21 for ghc-8.0.2, published a year ago
  • LTS 7.24 for ghc-8.0.1, published 2 years ago

None of those ghc version could reproduce your log error, so I thought to try a typo maybe:

import Data.Semigroup (Semigroup ((<$>)))

or

import Data.Semigroup (Semigroup ((=>>)))

And those gave me your error:

/home/damian/test-semigroup/app/Main.hs:3:24: error:

Module ‘Data.Semigroup’ does not export ‘Semigroup(())’

  1. That means, that the Data.Semigroup module exists in your ghc
  2. Semigroup((<>)) is not part of that module

Meaning, you must have one of the oldest ghc versions...

So, I just can think that you can try:

  • Reinstall your ghc, and try it to run it again.
  • Please, consider using some tool such as stack
  • Check your code, look out for some typos or something odd

I stand that my first answer was close though:

Edit 1

It has been added in ghc version 8.0.1

A quick search in hoogle always helps:

Here first link is semigroup <>, (<>) :: Semigroup a => a -> a -> a

and it says:

enter image description here

This versions is able since May 2016, and it ghc version is 8.0.1.

To see all versions of ghc with its base versions:

https://wiki.haskell.org/Base_package

developer_hatch
  • 15,898
  • 3
  • 42
  • 75
4

ThomasM.DuBuisson found what must have been the problem (discussion in comments): there are three different packages defining a Data.Semigroup module –

  • base. As of GHC-8, Haskell ships with the semigroup class and -module out of the box, so you don't need to take care for anything. See Damian Lattenero's answer for details.
  • semigroups. In GHC-7, there was no semigroups class in base but the semigroups package was semi-official part of the base libraries. In fact, this exact module was just copied over to base. The way to write really backwards-compatible code with semigroups is still to depend on the semigroups package: when compiling against new GHC versions, that package just uses the base module, only in old versions does it provide the module itself. See the .cabal configuration

      if impl(ghc < 7.11.20151002)
        -- starting with GHC 8 these modules are provided by `base`
        hs-source-dirs: src-ghc7
        exposed-modules:
          Data.Semigroup
          Data.List.NonEmpty
    
  • Semigroup is an obsolete package back from 2010. It too exports a Data.Semigroup module, but unlike the semigroups one this is not compatible with the official base module. It does have a Semigroups class, but its method is called .++. rather than <> (probably, to not clash with the operator from Data.Monoid – that is by now not an issue, because in very new versions <> is now in the prelude and already works on semigroups).

So, what happened for you is probably the following:

  1. You tried to compile your code
  2. GHC complained Could not find module ‘Data.Semigroup’. That's because you're running an old compiler.
  3. At this point, what you should have done would be installing/depending on the semigroups package. Instead you installed the Semigroup package, which is unfortunately incompatible.

There are two solutions:

  • Use Cabal-install and allow for your old compiler. IMO this is ok, though you should be prepared to run into other dependency troubles in the future. If you do this, you need to add the semigroups package in the build-depends of your .cabal file.
  • Or use Stack to enforce a recent compiler. Select e.g. lts-12.14.
leftaroundabout
  • 117,950
  • 5
  • 174
  • 319