Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create and Install a own module

Tags:

haskell

I want to create a module, and load it in the haskell library. I can work with ubuntu 11, or windows 7, using the tools of the haskell-platform. This is my module:

module Pkr.Element(..) where

import Char

data Card = Card Int deriving (Eq)

seme :: Card -> Int
seme (Card x) = mod (div x 13) 4

label :: Card -> Int
label (Card x) = mod x 13

instance Ord Card where
(>) x y     |ix == iy       = False
        |ix == 0        = True
        |iy == 0        = False
        | otherwise     = (ix > iy)
        where 
        ix = label x
        iy = label y

instance Show Card where
show :: Card -> String
show card =     strI(label card)    :   strS(seme card) :[] 
where
strI x  |   (x == 0)    = 'A'
    |   (x == 12)   = 'K' 
    |   (x == 11)   = 'Q'
    |   (x == 10)   = 'J'
    |   (x == 9)    = 'T'
    |   otherwise   = chr (49+x)
strS y  |   (y == 0)    = 'h'
    |   (y == 1)    = 'c'
    |   (y == 2)    = 'd'
    |   (y == 3)    = 's'

data Category = Null | HighCard | Copple | TwoCopple | 
        Tris | Straight | Flush | FullHouse |
         Poker | StraightFlush deriving (Show, Eq, Ord)

type Cards = [Card]
data Rank = Rank Category Cards Cards deriving (Eq, Ord, Show)

I have also some problem with "show" in the ghci, because I get an exception of stack overflow.

like image 448
optimusfrenk Avatar asked Nov 21 '11 09:11

optimusfrenk


1 Answers

Your paste is messing with the code; maybe it should look so: http://hpaste.org/54363 (note the advice from hlint at the bottom.) The following steps are for a simple method of development, but will I think expose most of the relevant factors:

  • Rename the module to the more sensible Poker.Elements, save it as Elements.hs

  • Make a directory named poker with a subdirectory named Poker

  • Move Elements.hs into poker/Poker. Now its hierarchical name, Poker.Elements makes sense. Your directory structure looks so:

    -- poker -- Poker -- Elements.hs 
    
  • poker is now organized properly. Type ghci Poker/Elements.hs from there and ghci will know how to deal with any other Poker.x.y.z modules in a more complicated structure like say this one:

    -- poker -- Poker -- Internal -- Guts.hs (i.e.Poker.Internal.Guts)          
                      |
                      -- Elements.hs  (i.e. Poker.Elements)
    

But our idea was to build and install the poker library with the cabal tool. Nothing simpler.

  • cd into poker if you are not there.

  • Run cabal init. The answers will all be obvious. You are making a Library, pertaining to Games.

  • Edit your new poker.cabal file -- cabal init can't tell what packages you are using.
    In fact, you are only using Prelude and Data.Char which are in base, so extend the Build-depends line to look thus:

    Build-depends:       base > 2
    

    The result will look like this: http://hpaste.org/54364 (If you are missing any other dependencies, this will emerge with the next command.)

  • Your directory now has this structure:

    -- poker   -- poker.cabal
              |
               -- Poker      -- Elements.hs
    
  • You thus now have a cabalized, buildable, indeed hackage-able package. Type cabal install, then cabal clean. The cabal tool has managed configuration, compilation, installation and registration of the package. (By default the compiled library will be deposited in the hidden directory $HOME/.cabal/lib/poker-0.1 or the equivalent for your system.)

  • Open ghci from anywhere on your system; type import Poker.Elements. Enjoy. It is no different if you call ghc directly -- for example, if you make an executable with ghc --make -O2 PokerCalculator.hs -o pokercalculator, ghc will now know how to find Poker.Elements without further instruction.

  • Test your definitions. Reflect. Fret. Reconsider. Test more. Look into quickcheck.

  • When you revise your module, rebuild and reinstall with cabal install. This will over-write the old installation of poker-0.1, as you are still calling it; but in this, simplest, case, no other packages are being built against it so the result is desirable. (If your other experimental library, texas-holdem-0.1 -- the one in the next directory --imports Poker.Elements, as is likely, then rebuild it too to use your more advanced ideas about the Elements of poker.)

  • If you add new modules, or import modules from new packages, specify these in the relevant lines of poker.cabal. If you forget, cabal install will politely remind you to do this when you try to rebuild ...

  • Upload your poker package to github or patch-tag or darcsden. When you have perfected it, upload it to hackage. Note that even on github or patch-tag it belongs to the 'hackaged' universe. If you import modules from fancier packages on Hackage, then when people git clone or darcs get your repository, cabal install will get the right packages for them from hackage.haskell.org.

like image 119
16 revs Avatar answered Sep 20 '22 15:09

16 revs