Craft3e: cabal install not in scope: Applicative - haskell

I am attempting to install the code for "Haskell: The Craft of Functional Programming", 3rd edition.
I'm using GHCi, version 7.6.3 on Centos version 7.
Then:
cabal unpack Craft3e
cd Craft3e-0.1.1.0/
cabal install
...
[29 of 67] Compiling CalcParseLib ( Calculator/CalcParseLib.hs, dist/build/CalcParseLib.o )
Calculator/CalcParseLib.hs:132:10:
Not in scope: type constructor or class `Applicative'
Failed to install Craft3e-0.1.1.0
cabal: Error: some packages failed to install:
Craft3e-0.1.1.0 failed during the building phase. The exception was:
ExitFailure 1
I have attempted this installation multiple times, but cannot
overcome this error.
Could I use something other than "cabal install"?
I have plenty to learn about Haskell before I get to this example,
but it would be great to know the installation is fine! :)

You have three options:
Install an older version of the Craft3e package, with e.g. cabal unpack Craft3e-0.1.0.10.
Find Calculator/CalcParseLib.hs in the source files you have downloaded with cabal unpack and add a...
import Control.Applicative
... line next to the other import lines at the beginning. I suspect you will have to do the same with other modules, if the same error shows up elsewhere after you do this change, and there might be other issues of a similar nature.
Install a newer version of GHC (7.6.3 is from 2013). Though the default CentOS repositories won't offer that, there are other options, such as an unofficial Fedora repository and a manual installation. See this page for instructions.
#3 is the definitive solution. If you just want to get started with the book right now, though, you can go for #1 and leave the reinstall for later.
For the sake of reference, here is a brief explanation of the problem (I will use some unfamiliar terms, but you will soon enough learn about them as you study Haskell). There is an important type class called Applicative which, for historical reasons, wasn't as well integrated with the rest of the core libraries as it should be. This situation was corrected in GHC 7.10, which both included Applicative in the Prelude (the module which is imported by default in Haskell programs) and made it necessary to add Applicative instances in a number of places where they were missing. The code in the Craft3e package was updated so that these Applicative instances were in place (cf. this entry in the book's blog), but the import Control.Applicative line, which would be necessary to make the updated code work in older GHCs that do not have Applicative in the Prelude, wasn't added, leading to the error that you are seeing.

Related

Building the Spock tutorial example fails

I wanted to get going with Haskell a little bit and therefore took a look at the Spock framework. To start clean, I uninstalled everything Haskell related from my Arch Linux machine and installed ghcup, Cabal and Stack using the install scripts from their respective websites.
Now I want to follow Spock's Tutorial. Trying to install Spock globally with cabal install Spock as suggested gives me an error (abbreviated):
src/Web/Spock/Internal/Wire.hs:43:1: error:
Could not find module ‘Web.Routing.AbstractRouter’
Use -v (or `:set -v` in ghci) to see a list of the files searched for.
|
43 | import Web.Routing.AbstractRouter
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
cabal: Failed to build Spock-0.9.0.1. See the build log above for details.
I already found a question on reddit on the topic, but the solution does not apply because I'm not trying to use a specific version of the libraries as implied.
So I try to follow along and build only locally.
But when I reach the point where it says stack build --fast --pedantic, the build plan can not be constructed and Stack suggests to add another dependency, stm-containers. Doing so, I am presented with two additional suggestions for focus and primitive. When I add these, the plan fails again, this time without a simple solution:
In the dependencies for primitive-0.6.4.0:
base-4.13.0.0 from stack configuration does not match >=4.5 && <4.13 (latest matching version is 4.12.0.0)
needed due to Spock-example-0.1.0.0 -> primitive-0.6.4.0
I can do a little thing with Haskell, but with the build system(s), I am way out of my comfort zone. Help and hints appreciated. Oh, and all versions of course are the latest by the time of this post.
Due to incompatible versions of dependencies, Spock won't build with GHC 8.8 and above. A similar problem is described in Spock issue #149, though I'm not fully sure it is exactly the same incompatibility. The error you got from Stack hints at that, as base-4.13.0.0 is the version of base that is bundled with GHC 8.8. cabal-install failed in a more obscure way because, upon noting the incompatibility, it tries to solve the dependencies using older versions of Spock, eventually picking 0.9.0.1, attempting and, thanks to a missing version upper bound for the reroute dependency, failing to build it.
(Shortly after this answer was posted, the missing upper bound was retrofitted to the old Spock version, so attempting to reproduce the problem now will lead to an easier to understand failure.)
Casting the tutorial aside, the most straightforward way to use Spock given those complications is probably through cabal-install 3+. Begin by using ghcup to switch to GHC 8.6.5:
$ ghcup install 8.6.5
$ ghcup set 8.6.5
Then, create a blank project with cabal-install:
$ mkdir myproject
$ cd myproject
$ cabal init
Add Spock to the build-depends section of myproject.cabal:
build-depends: base >=4.12 && <4.13
, Spock == 0.13.*
Finally, you can run:
$ cabal build
Which will install Spock and its dependencies before building the project. (Note that you generally don't need to use cabal install to install libraries with cabal-install 3.)
It is presumably possible to make it work with Stack as well, by changing to the lts-14.27 resolver (the latest one that uses GHC 8.6.5), tracking down all dependency versions that need to be overriden (as you had began to do) and manually adding them to the extra-deps of stack.yaml.

Why is GHC not importing Semigroup ((<>))

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.
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(())’
That means, that the Data.Semigroup module exists in your ghc
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:
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
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:
You tried to compile your code
GHC complained Could not find module ‘Data.Semigroup’. That's because you're running an old compiler.
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.

How to use haste / hplayground with stack

I have some familiarity with Haskell, the language, but not so much with the toolchain. (I played around with Haskell before cabal and stack existed.) I'm told that stack is the tool I should be using to manage Haskell projects. I'm trying to learn the haste library, and I'm getting stymied on the first tutorial I've tried, because I can't get hplayground installed. I created a stack project; my stack.yaml has
extra-deps:
- ghc-simple-0.3
- haste-compiler-0.5.3
- shellmate-0.2.3
- haste-perch-0.1.0.9
- hplayground-0.1.3.1
and my .cabal file has hplayground listed in the build-depends. But when I run stack build, I get these errors:
Configuring haste-perch-0.1.0.9...
Building haste-perch-0.1.0.9...
Preprocessing library haste-perch-0.1.0.9...
Haste/Perch.hs:17:15: Warning:
-XOverlappingInstances is deprecated: instead use per-instance pragmas OVERLAPPING/OVERLAPPABLE/OVERLAPS
Haste/App/Perch.hs:18:15: Warning:
-XOverlappingInstances is deprecated: instead use per-instance pragmas OVERLAPPING/OVERLAPPABLE/OVERLAPS
[1 of 2] Compiling Haste.App.Perch ( Haste/App/Perch.hs, .stack-work/dist/x86_64-osx/Cabal-1.22.4.0/build/Haste/App/Perch.o )
Haste/App/Perch.hs:61:15: Not in scope: ‘newTextElem’
Haste/App/Perch.hs:71:9:
Not in scope: ‘setAttr’
Perhaps you meant ‘jsSetAttr’ (imported from Haste.App)
Haste/App/Perch.hs:76:15:
Not in scope: ‘newElem’
Perhaps you meant one of these:
‘nelem’ (line 75), ‘notElem’ (imported from Prelude)
and a whole lot of similar errors. Any thoughts on what I'm doing wrong?
More broadly: what is a fast, easy way to get up and running with haste for someone who's not experienced with the Haskell toolchain?
Haste's event APIs got overhauled between the 0.4 and 0.5 series, and HPlayground is still on 0.4. If you want to use it, you will unfortunately have to fall back to 0.4 until HPlayground gets patched for 0.5.
For getting started in general, you should install the pre-build binaries if you're on a non-Linux platform (and probably if you're on a Linux platform as well, since you get man-pages and other niceties); the build process can be tricky and is prone to errors.
Once you've done that, you can refer to the docs and resources page on haste-lang.org, which contains links to API docs, video tutorials, source code examples and more.
Once you have installed ghc and cabal installed you will need to install the haste compiler as follows ( from http://haste-lang.org/downloads/ ):
$ cabal update
$ cabal install haste-compiler
$ haste-boot
After doing this, "hastec" ( the haste compiler ) should be available to use to compile haskell to javascript. In addition, "haste-cabal" ( the haste version of cabal ) should be available to use to install libraries such as haste-perch for use in your programs.
The readme file for haste-perch (https://github.com/agocorona/haste-perch) contains the instructions for installing haste-perch. Those instructions use "haste-inst" to install haste-perch but "haste-inst" is obsolete ( and no longer exists ). Use the modified instructions below to install haste-perch:
>git clone http://github.com/agocorona/haste-perch.git
>cd haste-perch
>haste-cabal install
I was able to install haste-perch and successfully build the example that it came with.
I also tried building hplayground but ran into compile problems that looked as if they were due to the code not being updated to use haste 0.5 . For example, "OnClick" was a valid identifier in earlier versions of haste but not anymore:
src/Haste/HPlay/View.hs:820:45:
Not in scope: data constructor ‘OnClick’
Perhaps you meant ‘Click’ (line 1017)

GTK2HS fails to install with recent cabal versions

I'm starting a new project that will hopefully use gtk2hs. However, I can not get this package to install on my fairly typical Linux box. Here is the failure :
[1 of 2] Compiling SetupWrapper ( /tmp/cairo-0.12.4-4201/cairo-0.12.4/SetupWrapper.hs, /tmp/cairo-0.12.4-4201/cairo-0.12.4/dist/dist-sandbox-58b5f9c6/setup/SetupWrapper.o )
/tmp/cairo-0.12.4-4201/cairo-0.12.4/SetupWrapper.hs:94:45:
Ambiguous occurrence `moreRecentFile'
It could refer to either `SetupWrapper.moreRecentFile',
defined at /tmp/cairo-0.12.4-4201/cairo-0.12.4/SetupWrapper.hs:149:1
or `Distribution.Simple.Utils.moreRecentFile',
imported from `Distribution.Simple.Utils' at /tmp/cairo-0.12.4-4201/cairo-0.12.4/SetupWrapper.hs:8:1-32
/tmp/cairo-0.12.4-4201/cairo-0.12.4/SetupWrapper.hs:95:45:
Ambiguous occurrence `moreRecentFile'
It could refer to either `SetupWrapper.moreRecentFile',
defined at /tmp/cairo-0.12.4-4201/cairo-0.12.4/SetupWrapper.hs:149:1
or `Distribution.Simple.Utils.moreRecentFile',
imported from `Distribution.Simple.Utils' at /tmp/cairo-0.12.4-4201/cairo-0.12.4/SetupWrapper.hs:8:1-32
Failed to install cairo-0.12.4
[1 of 2] Compiling SetupWrapper ( /tmp/glib-0.12.4-4201/glib-0.12.4/SetupWrapper.hs, /tmp/glib-0.12.4-4201/glib-0.12.4/dist/dist-sandbox-58b5f9c6/setup/SetupWrapper.o )
/tmp/glib-0.12.4-4201/glib-0.12.4/SetupWrapper.hs:94:45:
Ambiguous occurrence `moreRecentFile'
It could refer to either `SetupWrapper.moreRecentFile',
defined at /tmp/glib-0.12.4-4201/glib-0.12.4/SetupWrapper.hs:149:1
or `Distribution.Simple.Utils.moreRecentFile',
imported from `Distribution.Simple.Utils' at /tmp/glib-0.12.4-4201/glib-0.12.4/SetupWrapper.hs:8:1-32
/tmp/glib-0.12.4-4201/glib-0.12.4/SetupWrapper.hs:95:45:
Ambiguous occurrence `moreRecentFile'
It could refer to either `SetupWrapper.moreRecentFile',
defined at /tmp/glib-0.12.4-4201/glib-0.12.4/SetupWrapper.hs:149:1
or `Distribution.Simple.Utils.moreRecentFile',
imported from `Distribution.Simple.Utils' at /tmp/glib-0.12.4-4201/glib-0.12.4/SetupWrapper.hs:8:1-32
Failed to install glib-0.12.4
It seems to be related to recent cabal versions, since that used to work before. It seems to be a known bug (http://trac.haskell.org/gtk2hs/ticket/1292 , http://trac.haskell.org/gtk2hs/ticket/1291 and http://trac.haskell.org/gtk2hs/ticket/1289), however it has not moved for weeks. I am not sufficiently competent to suggest a good fix, so I would like your advice. This is very annoying since I am stuck on this important project, and it pains me to think that the main GUI lib of our language has been broken for many weeks now.
cabal-install version 1.18.0.2 using version 1.18.1.1 of the Cabal library / ghc 7.4.1
Thanks for your help
The correct solution for now is to build from darcs. Detailed instructions are given in the Getting the latest and greatest section of the installation instructions; the short version is:
darcs get --lazy http://code.haskell.org/gtk2hs
cd gtk2hs
sh bootstrap.sh
EDIT: The official 0.12.5 release of gtk2hs from December 2013 supports cabal 1.18, so the above darcs instructions are no longer needed. The full installation instructions are still available from the Gtk2Hs download page; the short version is:
cabal install gtk2hs-buildtools
cabal install gtk gtk3
You can try with older Cabal version using cabal install --cabal-lib-version=1.16.0 gtk.

Haskell Web Framework

I'm creating simple web application using haskell. First I used Snap in front and I was able to run the application, but I want to add user input to the application.
I couldn't find a way to get user input parameters to the function. How might I do that?
Other thing, I also used Happstack framework, I can not import "Happstack.Server". I use cabal installation configure Happstack. It was successfully installed, but when I try to import to "Happstack.Server", it gives me an error:
<no location info>:
Could not find module `Happstack.Server':
it is not a module in the current program, or in any known package.
If I run my program using ghc --make HelloWorld.hs -v, I get:
Glasgow Haskell Compiler, Version 6.12.1, for Haskell 98, stage 2 booted by GHC version 6.12.1
Using binary package database: /usr/lib/ghc-6.12.1/package.conf.d/package.cache
Using binary package database: /home/udeshika/.ghc/i386-linux-6.12.1/package.conf.d/package.cache
package happstack-6.0.0-0f0c2507d590ebd01e8601c8667ec809 is unusable due to missing or recursive dependencies:
happstack-ixset-6.0.0-4e1b5476a551c4501c5734b22e0b280d happstack-server-6.0.3-6d71e7bb09489130538fb851a694b927 happstack-state-6.0.0-0e753e61d7092b6a5139e473113877a1 happstack-util-6.0.0-4156bd1331b7a0d62e0087101c9eba1c
package happstack-ixset-6.0.0-4e1b5476a551c4501c5734b22e0b280d is unusable due to missing or recursive dependencies:
happstack-util-6.0.0-4156bd1331b7a0d62e0087101c9eba1c
package happstack-server-6.0.3-6d71e7bb09489130538fb851a694b927 is unusable due to missing or recursive dependencies:
happstack-util-6.0.0-4156bd1331b7a0d62e0087101c9eba1c hslogger-1.1.4-90c801c802eec92e4e6a6f83d24d58d9 network-2.2.1.7-72dad7eb07ee7a683982f7475b8a449f network-bytestring-0.1.3.4-937fd511949a2d5ef21e86ec5306c791 sendfile-0.7.3-137cf51cc81a277d724637a7cd1e6b09
package happstack-state-6.0.0-0e753e61d7092b6a5139e473113877a1 is unusable due to missing or recursive dependencies:
happstack-util-6.0.0-4156bd1331b7a0d62e0087101c9eba1c hslogger-1.1.4-90c801c802eec92e4e6a6f83d24d58d9
package happstack-util-6.0.0-4156bd1331b7a0d62e0087101c9eba1c is unusable due to missing or recursive dependencies:
hslogger-1.1.4-90c801c802eec92e4e6a6f83d24d58d9 network-2.2.1.7-72dad7eb07ee7a683982f7475b8a449f
package hslogger-1.1.4-90c801c802eec92e4e6a6f83d24d58d9 is unusable due to missing or recursive dependencies:
...................
Snap's getParam function allows you to get HTTP request parameters specified by the user. These can come in the post body of a form submission or from the query string. For example, consider the following code:
site = dir "mypage" pageHandler
pageHandler = do
val <- getParam "foo"
writeBS $ maybe "no value" id val
If I request the url "myapp.com/mypage?foo=bar", then I will see "bar" as the response. If I leave off the "?foo=bar" part, then it will return "no value".
1) Check out that Happstack is actually installed and exposed:
ghc-pkg list | grep Happstack
2) What if you try to import Happstack.Server in ghci ?
3) Better use cabal-dev or capri for installing such things and making a sandbox
4) Snap and Yesod a quite new and they are under heavy development, Happstack is pretty
stable and solid.
5) What OS are you using? How did your installed the haskell? Ubuntu + haskell 6.12 + Happstack work out of the box
Regarding to the log, seems that cabal packages are messed up somehow. I recommend you to remove all packages from the .cabal and .ghc, reinstall cabal, then install cabal-dev as explained here:
http://www.reddit.com/r/haskell/comments/f3ykj/psa_use_cabaldev_to_solve_dependency_problems/
and then have fun with Happstack. Some guys actually use capri, it works as well.
Your installation is messed up. Unfortunately, cabal does that sometimes.
Your best bet is use 'ghc-pkg unregister ' to remove all the happstack packages, hslogger, and sendfile. Make sure they are removed from the user and global package databases. (ghc-pkg list will show you what is installed). Once you have the old versions removed, do, cabal update and the cabal install happstack. Then things should work.
What do you mean by 'user input' ? Do you mean html forms? Or something else ?
This section of the crash course may address your needs:
http://www.happstack.com/docs/crashcourse/RqData.html
It is also possible to use digestive-functors with Happstack to get type-safe form processing. Unfortunately, this is not well documented yet. Though I believe the disgestive-functors source code does include a working Happstack example.

Resources