Safe Haskell | None |
---|---|
Language | Haskell2010 |
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 Vote
s 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 Result
s, 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.
- data Vote o = Vote {
- voteRanking :: Int
- voteOption :: o
- data Ballot p o = Ballot {
- ballotParticipant :: p
- ballotVotes :: [Vote o]
- data BallotError o
- = DuplicateRanking o o
- | DuplicateOption o
- ballot :: Eq o => p -> [Vote o] -> Either (BallotError o) (Ballot p o)
- data Result o = Result o Score Zeros
- newtype Score = Score Double
- newtype Zeros = Zeros Double
- newtype ElectionError p = DuplicateParticipant p
- election :: (Eq p, Ord o) => (p -> Double) -> [Ballot p o] -> Either (ElectionError p) [Result o]
- election' :: (Eq p, Ord o) => [Ballot p o] -> Either (ElectionError p) [Result o]
- findFirstDuplicateBy :: (a -> a -> Bool) -> [a] -> Maybe (a, a)
Voting
A vote is an attribution of a number of points to an option
Vote | |
|
A ballot with options of type o
filled in by a participant of type p
.
Ballot | |
|
data BallotError o Source #
DuplicateRanking o o | The given options have the same ranking |
DuplicateOption o | The given option was voted on twice. |
Eq o => Eq (BallotError o) Source # | |
Show o => Show (BallotError o) Source # | |
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
Result of a certain option of type o
of an election.
The weighted score of a Result
.
The number of weighted zeros a Result
got.
newtype ElectionError p Source #
Error that can occur when holding an election
.
DuplicateParticipant p | The given participant voted twice. |
Eq p => Eq (ElectionError p) Source # | |
Show p => Show (ElectionError p) Source # | |
:: (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 Ballot
s are well formed, as
returned by the function ballot
.
Thee function holds the election, and might return an ElectionError
on any irregularity.
Internal
findFirstDuplicateBy :: (a -> a -> Bool) -> [a] -> Maybe (a, a) Source #
Find the first duplicates, filtered by a function.