OK, so you have just finished reading your first Haskell book, you understand most of (if not all!) compilation errors GHC throws at you and you start thinking creatively about the language. If you are at this stage and wondering what's next, welcome to my team! Not so long ago I finished Learn you a Haskell and, while reading The Haskell Road to Logic, Maths and Programming, I decided I was ready to start playing with Hackage. My package of choice was Rasterific - mostly because I wanted to get something on the screen quickly.
In this update I'm going to walk you through installing a package of your choice (+ all its dependencies) in your local cabal sandbox and using it in your own Haskell program. Let's get started!
First of all, let's make sure you have the latest versions of
cabal install Cabal cabal-installThis will install the latest versions of both packages.
Once it's done, let's update cabal packages using the following command:
Now, a special note for the Haskell Platform users: the cabal you are using from the command line might not (and it wasn't in my case) be the one being updated! In my case, the cabal my PATH variable was pointing at was located in the Haskell Platform folder
...\Haskell Platform\2013.2.0.0\lib\Cabal-1.16.0but executing those commands made the cabal install its latest version in
C:\Users\Piotr\AppData\Roaming\cabal\bin (version 126.96.36.199)I had to come up with a solution and I ended up changing my PATH variable to use the cabal installed in my AppData.
Once you have the PATH variable pointing at the updated cabal, navigate to the folder you want to place your experimental project in (still using the command line) and type:
cabal sandbox initThis will create a folder-local sandbox environment for packages that could have otherwise damaged your global cabal repository. In case anything goes wrong in the sandbox, you can always delete it and start fresh - no need to modify your global set of packages.
Once you have the local sandbox prepared, you are ready to install your package of choice and its dependencies. Just in case, make the first run dry (just list the required dependencies):
cabal install Rasterific --dry-runYou should see something like this:
Resolving dependencies... In order, the following would be installed: base-orphans-0.4.0 (new package) bytestring-0.10.6.0 (new version) Win32-188.8.131.52 (new version) binary-0.7.5.0 (new version) text-184.108.40.206 -integer-simple (new version) hashable-220.127.116.11 (new version) nats-1 (reinstall) changes: hashable-18.104.22.168 -> 22.214.171.124 time-126.96.36.199 (new version) directory-188.8.131.52 (new version) FontyFruity-0.5.1.1 (new package) mwc-random-0.13.3.2 (new package) unordered-containers-0.2.5.1 (new version) semigroups-0.16.2.2 (reinstall) changes: bytestring-0.10.0.2 -> 0.10.6.0, hashable-184.108.40.206 -> 220.127.116.11, text-0.11.3.1 -> 18.104.22.168, unordered-containers-0.2.3.0 -> 0.2.5.1 bifunctors-5 (new version) vector-algorithms-0.7 (new package) void-0.7 (reinstall) changes: hashable-22.214.171.124 -> 126.96.36.199 contravariant-188.8.131.52 (new version) comonad-184.108.40.206 (new version) profunctors-5.1.1 (new version) semigroupoids-220.127.116.11 (new version) free-4.12.1 (new version) zlib-0.6.1.1 (new version) JuicyPixels-18.104.22.168 (new version) Rasterific-0.6.1 (new package) Warning: The following packages are likely to be broken by the reinstalls: linear-22.214.171.124 force-layout-0.4.0.0 diagrams-contrib-1.3.0 diagrams-1.3 diagrams-lib-1.3 diagrams-svg-1.3 diagrams-core-1.3 active-0.2.0.1 lens-4.9.1 contravariant-1.3.1 semigroupoids-4.3 profunctors-4.4.1 free-4.11 kan-extensions-4.2.1 adjunctions-4.2 monoid-extras-0.4.0.0 dual-tree-0.2.0.6 bifunctors-4.2.1 comonad-4.2.5 bytes-0.15 Use --force-reinstalls if you want to install anyway.
Once you're happy with required dependencies, simply run:
cabal install Rasterific
If everything goes well, you are ready to start playing with the package! You can find Rasterific documentation here. I used it to write a small program drawing rectangles into a file:
import Codec.Picture(PixelRGBA8( .. ), writePng) import Graphics.Rasterific import Graphics.Rasterific.Texture main :: IO () main = do let backgroundColour = PixelRGBA8 234 247 217 255 -- Cilantro Creme foregroundColour1 = PixelRGBA8 195 214 170 255 -- Mint Sherbert foregroundColour2 = PixelRGBA8 142 168 108 255 -- Pesto Paste foregroundColour3 = PixelRGBA8 77 100 45 255 -- Simple Green foregroundColour4 = PixelRGBA8 40 58 16 255 -- Dark Water image = renderDrawing 400 400 backgroundColour $ do withTexture (uniformTexture foregroundColour1) . fill $ rectangle (V2 50 50) 145 145 withTexture (uniformTexture foregroundColour2) . fill $ rectangle (V2 205 50) 145 145 withTexture (uniformTexture foregroundColour3) . fill $ rectangle (V2 50 205) 145 145 withTexture (uniformTexture foregroundColour4) . fill $ rectangle (V2 205 205) 145 145 writePng "test.png" image -- palette taken from: http://www.colourlovers.com/palette/110443/Summer_GrassYou can also find the code here. To build it, execute the following command (and this is where a local sandbox comes very handy):
ghc -package-db=.cabal-sandbox\i386-windows-ghc-7.6.3-packages.conf.d main.hsOnce the program compiles, you can run it and start drawing. Here is my result:
And that's it! You have just installed your first package and used it in your own program. Please leave a comment if the installation process was different for you or if you experienced some other problems.
Let the adventure with Hackage begin!