Code Snippet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
--------------------------------------------------------------------------------
--
-- Kleroterion, (c) 2020 SPISE MISU ApS
--
--------------------------------------------------------------------------------
{-# LANGUAGE Trustworthy #-}
--------------------------------------------------------------------------------
module Main (main) where
--------------------------------------------------------------------------------
import Data.List
( sortBy
)
import Data.Maybe
( catMaybes
)
import System.Random
( StdGen
, newStdGen
, randoms
)
--------------------------------------------------------------------------------
{-
KLEROTERION
┌║─────────────────────────────────────────────────┐
│║ Α Β Γ Δ Ε Ζ Η Θ Ι Κ Λ Μ Ν Ξ Ο Π Ρ Σ Τ Υ Φ Χ Ψ Ω │
│║ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ │
│║ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ │
│║ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ │
│║ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ │
│║ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ │
│║ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ │
│║ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ │
│║ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ │
│║ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ │
│║ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▬ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ │
│║ ▬ ▬ ▭ ▭ ▬ ▭ ▬ ▬ ▭ ▬ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ │
│║ ▭ ▬ ▭ ▭ ▬ ▭ ▬ ▬ ▭ ▬ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ │
│║ ▭ ▬ ▭ ▭ ▬ ▭ ▬ ▭ ▭ ▬ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ │
│║ ▭ ▭ ▭ ▭ ▬ ▭ ▭ ▭ ▭ ▬ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ │
│║ ▭ ▭ ▭ ▭ ▬ ▭ ▭ ▭ ▭ ▬ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ │
│║ ▭ ▭ ▭ ▭ ▬ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ │
│║ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ │
│║ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ │
│║ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ │
│║ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ ▭ │
│║ │
│║ │
│║ │
│║ │
│╚═ ▪▫▫▪▫▪▪▫▫▪▫▫▫▫▫│
└──────────────────────────────────────────────────┘
REFERENCES:
* https://en.wikipedia.org/wiki/Kleroterion
* https://www.academia.edu/36510282/
* https://www.youtube.com/watch?v=gt9H7nbZjAw
* https://www.youtube.com/watch?v=1DhgkqJCIBA
-}
--------------------------------------------------------------------------------
data Token a b
= UID a b
deriving
( Eq
, Ord
, Show
)
data Marble
= Black
| White
deriving
( Eq
, Ord
, Show
)
type Pinakia
= Token
type Kyboi
= Marble
type Slot a b
= Maybe (Pinakia a b)
data Kleroterion a b
= Slab [[Slot a b]] [Kyboi]
deriving
Show
--------------------------------------------------------------------------------
main :: IO ()
main =
pinakias >>= \ps ->
kybois >>= \ks ->
( putStrLn "# Pinakias (pre-shuffled eligible people):"
) >>
( mapM_ (putStrLn . ("* " ++)) $
map (concatMap (\ (UID a b) -> "(" ++ [a] ++ "," ++ zeropad b ++ ") ")) $
ps
) >>
( putStrLn ""
) >>
( putStrLn "# Kybois (pre-shuffled binary marbles):"
) >>
( putStrLn . ("* " ++) $
concatMap ((++ " ") . show) $
ks
) >>
( putStrLn ""
) >>
( putStrLn "# Kleroterion (sorted elected people):"
) >>
( putStrLn . ("* " ++) $
concatMap (\ (UID a b) -> "(" ++ [a] ++ "," ++ zeropad b ++ ") ") $
sortBy compare $
elected $
kleroterion ps ks
)
where
pinakias =
go
<$> shuffle αs
<*> shuffle βs
<*> shuffle γs
<*> shuffle δs
<*> shuffle εs
<*> shuffle ζs
<*> shuffle ηs
<*> shuffle θs
<*> shuffle ιs
<*> shuffle κs
<*> shuffle λs
where
go α β γ δ ε ζ η θ ι κ λ =
α:β:γ:δ:ε:ζ:η:θ:ι:κ:λ:[]
--
αs = map (UID 'Α') $ take 11 ns
βs = map (UID 'Β') $ take 13 ns
γs = map (UID 'Γ') $ take 10 ns
δs = map (UID 'Δ') $ take 10 ns
εs = map (UID 'Ε') $ take 10 ns
ζs = map (UID 'Ζ') $ take 16 ns
ηs = map (UID 'Η') $ take 13 ns
θs = map (UID 'Θ') $ take 12 ns
ιs = map (UID 'Ι') $ take 10 ns
κs = map (UID 'Κ') $ take 15 ns
λs = map (UID 'Λ') $ take 10 ns
--
ns = [ 0 :: Integer .. ]
kybois =
shuffle ms
where
ms =
take 5 bs ++ take 5 ws
where
bs = repeat Black
ws = repeat White
kleroterion ps ks =
Slab ss ks
where
ss = map (map Just) ps
zeropad =
leftpad 2 '0' . show
--------------------------------------------------------------------------------
-- HELPERS
shuffle
:: [a]
-> IO [a]
shuffle xs =
aux <$> newStdGen
where
num = randoms :: StdGen -> [Integer]
len = length xs
fip = flip zip
ord = \ x y -> fst x `compare` fst y
aux =
map snd .
sortBy ord .
fip xs .
take len .
num
elected
:: Kleroterion a b
-> [Pinakia a b]
elected (Slab ss ks) =
catMaybes $ aux ks ss
where
aux [ ] __ = []
aux (x:xs) ys =
case x of
Black -> zs
White -> concatMap (take 1) ys ++ zs
where
zs = aux xs $ map (drop 1) ys
leftpad
:: Int
-> Char
-> String
-> String
leftpad n c x =
if l > n
then x
else replicate (n-l) c ++ x
where
l = length x