bordacount-0.1.0.0: Implementation of the Borda count election method.

Safe HaskellNone
LanguageHaskell2010

Data.Voting.BordaCount

Contents

Description

Implementation of the modified Borda count election method.

The implementation implements the modified Borda count election method, optionally with different weights for different participants.

See https://en.wikipedia.org/wiki/Borda_count.

The election runs in two phases. First individual Votes on options from a certain participant are gathered and ranked into the participant's Ballot using the function ballot.

Then all ballots are gathered and an election is held, resulting in a list of election Results, one for each option.

Except for the Vote data constructor, you should not use other data constructors, as they do not reflect invariants correctly. However, the ballot and election will enforce variants.

Synopsis

Voting

data Vote o Source #

A vote is an attribution of a number of points to an option

Constructors

Vote 

Fields

Instances

Eq o => Eq (Vote o) Source # 

Methods

(==) :: Vote o -> Vote o -> Bool #

(/=) :: Vote o -> Vote o -> Bool #

Show o => Show (Vote o) Source # 

Methods

showsPrec :: Int -> Vote o -> ShowS #

show :: Vote o -> String #

showList :: [Vote o] -> ShowS #

data Ballot p o Source #

A ballot with options of type o filled in by a participant of type p.

Constructors

Ballot 

Fields

Instances

(Eq o, Eq p) => Eq (Ballot p o) Source # 

Methods

(==) :: Ballot p o -> Ballot p o -> Bool #

(/=) :: Ballot p o -> Ballot p o -> Bool #

(Show o, Show p) => Show (Ballot p o) Source # 

Methods

showsPrec :: Int -> Ballot p o -> ShowS #

show :: Ballot p o -> String #

showList :: [Ballot p o] -> ShowS #

data BallotError o Source #

Errors that can occur when creating a Ballot using ballot.

Constructors

DuplicateRanking o o

The given options have the same ranking

DuplicateOption o

The given option was voted on twice.

Instances

ballot :: Eq o => p -> [Vote o] -> Either (BallotError o) (Ballot p o) Source #

Construct a new ballot for a participant from a collection of votes.

This function constructs a valid ballot or returns an error if the collection of votes is invalid.

Election

data Result o Source #

Result of a certain option of type o of an election.

Constructors

Result o Score Zeros 

Instances

Eq o => Eq (Result o) Source # 

Methods

(==) :: Result o -> Result o -> Bool #

(/=) :: Result o -> Result o -> Bool #

Show o => Show (Result o) Source # 

Methods

showsPrec :: Int -> Result o -> ShowS #

show :: Result o -> String #

showList :: [Result o] -> ShowS #

newtype Score Source #

The weighted score of a Result.

Constructors

Score Double 

newtype Zeros Source #

The number of weighted zeros a Result got.

Constructors

Zeros Double 

newtype ElectionError p Source #

Error that can occur when holding an election.

Constructors

DuplicateParticipant p

The given participant voted twice.

election Source #

Arguments

:: (Eq p, Ord o) 
=> (p -> Double)

Weighing function for participants.

-> [Ballot p o]

All ballots.

-> Either (ElectionError p) [Result o]

Election result

Hold an election, collect all the ballots and produce the Result.

This functions assumes that the Ballots are well formed, as returned by the function ballot.

Thee function holds the election, and might return an ElectionError on any irregularity.

election' Source #

Arguments

:: (Eq p, Ord o) 
=> [Ballot p o]

All ballots

-> Either (ElectionError p) [Result o]

Election result

Hold an election, but with a weight of 1 for all participants.

See election for more information.

Internal

findFirstDuplicateBy :: (a -> a -> Bool) -> [a] -> Maybe (a, a) Source #

Find the first duplicates, filtered by a function.