1 -- | WARNING: the 'MajorityGauge' is a simplified 'MajorityValue'
2 -- which is sufficient to determine the 'MajorityRanking'
3 -- when the number of judges is large.
4 -- It is an approximation, it can perfectly lead to a wrong ranking
5 -- wrt. the 'MajorityRanking' done by using 'majorityValue'.
6 module Majority.Gauge where
9 import Data.Eq (Eq(..))
10 import Data.Function (($), (.))
11 import Data.Functor ((<$>))
12 import Data.Maybe (Maybe(..), listToMaybe)
13 import Data.Ord (Ord(..), Ordering(..), Down(..))
14 import Data.Tuple (snd)
15 import Prelude (Num(..))
16 import Text.Show (Show(..))
17 import qualified Data.HashMap.Strict as HM
18 import qualified Data.List as List
19 import qualified Data.Map.Strict as Map
23 -- * Type 'MajorityGauge'
24 -- | The 'MajorityGauge' is a simplification of the 'majorityValue'
25 -- from which may be deduced the 'majorityRanking'
26 -- among the propositions in many cases;
27 -- in particular, when there are many judges.
29 -- However, when two propositions are tied with the same 'MajorityGauge',
30 -- they are not necessarily tied according to their 'majorityValue's.
33 { mgLower :: Share -- ^ Number of 'grade's given which are worse than 'mgGrade'.
34 , mgGrade :: g -- ^ 'majorityGrade'.
35 , mgHigher :: Share -- ^ Number of 'grade's given which are better than 'mgGrade'.
37 instance Show g => Show (MajorityGauge g) where
38 showsPrec p (MajorityGauge w g b) = showsPrec p (w,g,b)
41 data Sign = Minus | Plus
44 -- | If 'mgHigher' is higher than 'mgLower'
45 -- then the 'majorityGrade' is completed by a 'Plus';
46 -- otherwise the 'majorityGrade' is completed by a 'Minus'.
48 -- This indicates the side of the next 'majorityGrade'
49 -- which is different than the current one:
50 -- 'Minus' when it is lower and 'Plus' otherwise.
51 mgSign :: MajorityGauge g -> Sign
52 mgSign g = if mgHigher g > mgLower g then Plus else Minus
54 -- | The 'MajorityGauge'-ranking, first tries to rank
55 -- according to the 'majorityGrade' 'mgGrade'.
57 -- If both 'MajorityGauge's have the same 'mgGrade',
58 -- it tries to rank according to the 'mgSign' of both 'MajorityGauge's:
59 -- a 'Plus' is ahead of a 'Minus'.
61 -- If both 'mgSign' are 'Plus',
62 -- the one having the higher 'mgHigher' is ahead,
63 -- or if both 'mgSign' are 'Minus',
64 -- the one having the higher 'mgLower' is behind.
66 -- Otherwise, the 'MajorityGauge'-ranking is a tie.
67 instance Ord g => Ord (MajorityGauge g) where
69 case mgGrade x `compare` mgGrade y of
71 case (mgSign x, mgSign y) of
74 (Plus , Plus) -> mgHigher x `compare` mgHigher y
75 (Minus, Minus) -> mgLower y `compare` mgLower x
78 majorityGauge :: Ord grade => Merit grade -> Maybe (MajorityGauge grade)
79 majorityGauge = listToMaybe . majorityGauges
81 majorityGauges :: Ord grade => Merit grade -> [MajorityGauge grade]
82 majorityGauges (Merit m) = go Map.empty m
84 go done gs = case snd (Map.foldlWithKey untilMajGrade (0,[]) gs) of
86 (mg,c):_ -> add mg done:go (Map.insert (mgGrade mg) c done) (Map.delete (mgGrade mg) gs)
88 add = Map.foldrWithKey $ \g c (MajorityGauge w mg b) ->
89 if g >= mg then MajorityGauge w mg (b+c)
90 else MajorityGauge (w+c) mg b
92 untilMajGrade (t,[]) g c | 2*tc >= total = (tc,[(MajorityGauge t g 0,c)])
95 untilMajGrade (t,(mg,c):_) _g c' = (t,[(mg{mgHigher=mgHigher mg + c'},c)])
97 -- * Type 'MajorityGaugeRanking'
98 type MajorityGaugeRanking choice grade = [(choice, [MajorityGauge grade])]
100 majorityGaugesByChoice :: Ord grade => MeritByChoice choice grade -> HM.HashMap choice [MajorityGauge grade]
101 majorityGaugesByChoice (MeritByChoice ms) = majorityGauges <$> ms
103 majorityGaugeRanking :: Ord grade => MeritByChoice choice grade -> MajorityGaugeRanking choice grade
104 majorityGaugeRanking = List.sortOn (Down . snd) . HM.toList . majorityGaugesByChoice