HCard-0.0: A library for implementing a Deck of Cards



Author : Joe Fredette License : BSD3 Copyright : Joe Fredette

Maintainer : Joe Fredette jfredett.at.gmail.dot.com Stability : Unstable Portability : portable



class (Eq s, Eq i, Show s, Show i) => Card s i whereSource

The Main class, this is -- effectively -- a type-indexed record. Specifically, it requires two types, one representing the suit, the other representing the index/rank. The suit, index, and construct functions are generic forms of the record accessors.

The bulk of the implementation takes place in generic type instances, supporting equality irrelevant of ordering, ordering, parsing from a normal form (index-suit) and enum/bounded

TODO: Write deriving instance?

Associated Types

data CardT s i :: *Source


suit :: CardT s i -> sSource

index :: CardT s i -> iSource

construct :: i -> s -> CardT s iSource

(@@) :: i -> s -> CardT s iSource


type Cards s i = [CardT s i]Source

class Parse a whereSource


parse :: String -> aSource


Parse Index 
Parse Suit 
(Parse s, Parse i, Card s i) => Parse (CardT s i) 
(Parse s, Parse i, Card s i) => Parse (Hand s i) 

newtype Deck s i Source

Separate Deck from Hand, even though the types are isomorphic, we don't want shuffling to be to liberal.

TODO: Make Deck clever enough to support reshuffling when the deck runs out -- it should store cards it has already seen till it runs out of the main deck, reshuffle, redeal.


Deck (Cards s i) 


Card s i => Eq (Deck s i) 
Card s i => Show (Deck s i) 
(Arbitrary s, Arbitrary i, Card s i) => Arbitrary (Deck s i) 

type DeckST s i = State (Deck s i)Source

Type wrapper for stateful decks, useful for sorting

mkDeck :: (Bounded s, Bounded i, Enum s, Enum i, Card s i) => Deck s iSource

Creates a deck, used as in: `mkDeck::your deck type here`, or w/ inference.

shuffleDeck :: (Card s i, RandomGen g) => Deck s i -> g -> Deck s iSource

Shuffles a deck given a generator

shuffleDeckIO :: Card s i => Deck s i -> IO (Deck s i)Source

Shuffles using the standard generator

dealHands :: Card s i => Int -> Int -> DeckST s i [Hand s i]Source

Deals n hands of qty cards, written in the state monad.

dealHand :: Card s i => Int -> DeckST s i (Hand s i)Source

Helper for dealHands, also somewhat useful, equiv. to `dealHands 1 qty`

newtype Hand s i Source

A type to separate Hands from Decks.


Hand (Cards s i) 


Card s i => Eq (Hand s i) 
Card s i => Show (Hand s i) 
(Parse s, Parse i, Card s i) => Parse (Hand s i)