Skip to content

Instantly share code, notes, and snippets.

@ruescasd
Last active August 24, 2017 04:52
Show Gist options
  • Save ruescasd/2f65978f529a34ec3ada63f5b4abe329 to your computer and use it in GitHub Desktop.
Save ruescasd/2f65978f529a34ec3ada63f5b4abe329 to your computer and use it in GitHub Desktop.
import scala.collection.SortedSet
trait Method {
// options to choose from
type Option
// a choice made on the ballot
type Choice
// a completed ballot
type Ballot
// the voting rule (algorithm)
type Rule <: Seq[RuleIn] => RuleOut
// the election result
type Result
type ConverterIn <: Ballot => RuleIn
type RuleIn
type RuleOut
type ConverterOut <: RuleOut => Result
type Id[X] = X => X
}
trait Rules { self: Method =>
type Plurality = Seq[Set[Choice]] => SortedSet[Option]
type Stv = Seq[SortedSet[Choice]] => Set[Option]
type Irv = Seq[SortedSet[Choice]] => Option
type Borda = Seq[SortedSet[Choice]] => SortedSet[Option]
type PairwiseBeta = Seq[Set[Choice]] => SortedSet[Option]
type Score = Seq[Set[Choice]] => SortedSet[Option]
type Copeland = Seq[SortedSet[Choice]] => SortedSet[Option]
}
trait SingleWinner extends Method with Rules {
type RuleOut = SortedSet[Option]
type ConverterOut = SortedSet[Option] => Option
type Result = Option
}
trait SingleWinnerId extends Method with Rules {
type RuleOut = Option
type ConverterOut = Id[Option]
type Result = Option
}
trait MultipleWinner extends Method with Rules {
type RuleOut = SortedSet[Option]
type ConverterOut = SortedSet[Option] => Set[Option]
type Result = Set[Option]
}
trait MultipleWinnerId extends Method with Rules {
type RuleOut = Set[Option]
type ConverterOut = Id[Set[Option]]
type Result = Set[Option]
}
trait FirstPastThePost extends SingleWinner {
type Choice = Option
type Ballot = Choice
type Rule = Plurality
type ConverterIn = Choice => Set[Choice]
type RuleIn = Set[Choice]
}
trait Approval extends SingleWinner {
type Choice = Option
type Ballot = Set[Choice]
type Rule = Plurality
type ConverterIn = Id[Set[Choice]]
type RuleIn = Set[Choice]
}
trait IRV extends SingleWinnerId {
type Choice = Option
type Ballot = SortedSet[Choice]
type Rule = Irv
type ConverterIn = Id[SortedSet[Choice]]
type RuleIn = SortedSet[Choice]
}
trait PluralityAtLarge extends MultipleWinner {
type Choice = Option
type Ballot = Set[Choice]
type Rule = Plurality
type ConverterIn = Id[Set[Choice]]
type RuleIn = Set[Choice]
}
trait SNTV extends MultipleWinner {
type Choice = Option
type Ballot = Choice
type Rule = Plurality
type ConverterIn = Choice => Set[Choice]
type RuleIn = Set[Choice]
}
trait STV extends MultipleWinnerId {
type Choice = Option
type Ballot = SortedSet[Choice]
type Rule = Stv
type ConverterIn = Id[SortedSet[Choice]]
type RuleIn = SortedSet[Choice]
}
trait Pairwise extends MultipleWinner {
type Choice = (Option, Option)
type Ballot = Set[Choice]
type Rule = PairwiseBeta
type ConverterIn = Id[Set[Choice]]
type RuleIn = Set[Choice]
}
trait BordaCount extends SingleWinner {
type Choice = Option
type Ballot = SortedSet[Choice]
type Rule = Borda
type ConverterIn = Id[SortedSet[Choice]]
type RuleIn = SortedSet[Choice]
}
trait Range extends MultipleWinner {
type Choice = (Option, Int)
type Ballot = Set[Choice]
type Rule = Score
type ConverterIn = Id[Set[Choice]]
type RuleIn = Set[Choice]
}
trait Cumulative extends MultipleWinner {
type Choice = (Option, Int)
type Ballot = Set[Choice]
type Rule = Score
type ConverterIn = Id[Set[Choice]]
type RuleIn = Set[Choice]
}
trait CondorcetCopeland extends SingleWinner {
type Choice = Option
type Ballot = SortedSet[Choice]
type Rule = Copeland
type ConverterIn = Id[SortedSet[Choice]]
type RuleIn = SortedSet[Choice]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment