/
Model.hs
509 lines (416 loc) · 14.2 KB
/
Model.hs
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveFoldable #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
module Diagrams.RubiksCube.Model (
-- * Constructing cubes
Side (..)
, topLeft, topCenter, topRight
, middleLeft, middleCenter, middleRight
, bottomLeft, bottomCenter, bottomRight
, rotateSideCW, rotateSideCCW
, Cube (..), frontSide, backSide, leftSide, rightSide, upSide, downSide
, RubiksCube (..), cube
-- * Selecting rows and columns
, Vec3 (..)
, topRow, middleRow, bottomRow
, leftCol, centerCol, rightCol
-- * Traversing facets
-- ** By layer
, topLayerFacets, middleLayerFacets, bottomLayerFacets
-- ** By position
, centerFacets, cornerFacets, edgeFacets
, sideCorners, sideEdges
-- ** By rows
, upRows
, middleRows
, downRows
-- ** By cols
, leftCols
, centerCols
, rightCols
, frontCols
, betweenCols
, backCols
-- * Rotating the whole cube
, Aut
, rotateLeft, rotateRight
, rotateDown, rotateUp
, rotateCW, rotateCCW
-- * Moving layers of the cube
, move, doMoves, undoMoves
) where
import Control.Lens
import Diagrams.RubiksCube.Move (Move (..))
import Data.Foldable (Foldable)
import Control.Applicative (Applicative (..), (<$>))
import Data.Distributive (Distributive (..))
import Data.Functor.Rep (Representable (..), distributeRep, tabulated)
-- | The type of automorphisms
type Aut a = Iso' a a
-- Natural numbers
data N = Z | S N
-- Some type synonyms for natural numbers
type Zero = 'Z
type One = 'S Zero
type Two = 'S One
type Three = 'S Two
type Four = 'S Three
-- | Finite type with n inhabitants
data Fin :: N -> * where
FinZ :: Fin ('S n)
FinS :: Fin n -> Fin ('S n)
zero :: Fin ('S n)
zero = FinZ
one :: Fin ('S ('S n))
one = FinS zero
two :: Fin ('S ('S ('S n)))
two = FinS one
three :: Fin ('S ('S ('S ('S n))))
three = FinS two
-- | A list of fixed length 3.
data Vec3 a
= Vec3 a a a
deriving (Show, Eq, Functor, Foldable, Traversable)
instance Applicative Vec3 where
pure v = Vec3 v v v
Vec3 f1 f2 f3 <*> Vec3 v1 v2 v3 =
Vec3 (f1 v1) (f2 v2) (f3 v3)
instance Reversing (Vec3 a) where
reversing (Vec3 a b c) = Vec3 c b a
instance Distributive Vec3 where
distribute = distributeRep
instance Representable Vec3 where
type Rep Vec3 = Fin Three
tabulate f = Vec3 (f zero) (f one) (f two)
index (Vec3 a b c) fin =
case fin of
FinZ -> a
FinS FinZ -> b
FinS (FinS FinZ) -> c
_ -> error "index@Vec3: cannot happen"
instance Field1 (Vec3 a) (Vec3 a) a a where
_1 f (Vec3 a b c) = (\a' -> Vec3 a' b c) <$> f a
instance Field2 (Vec3 a) (Vec3 a) a a where
_2 f (Vec3 a b c) = (\b' -> Vec3 a b' c) <$> f b
instance Field3 (Vec3 a) (Vec3 a) a a where
_3 f (Vec3 a b c) = (\c' -> Vec3 a b c') <$> f c
-- | A variant of 'inside' that works for
insideRep
:: Representable g
=> Lens s t a b
-> Lens (g s) (g t) (g a) (g b)
insideRep l = from tabulated . inside l . tabulated
-- | A list of fixed length 4.
data Vec4 a = Vec4 a a a a deriving (Show, Eq, Functor, Foldable, Traversable)
instance Applicative Vec4 where
pure v = Vec4 v v v v
Vec4 f1 f2 f3 f4 <*> Vec4 v1 v2 v3 v4 =
Vec4 (f1 v1) (f2 v2) (f3 v3) (f4 v4)
instance Distributive Vec4 where
distribute = distributeRep
instance Representable Vec4 where
type Rep Vec4 = Fin Four
tabulate f = Vec4 (f zero) (f one) (f two) (f three)
index (Vec4 a b c d) fin =
case fin of
FinZ -> a
FinS FinZ -> b
FinS (FinS FinZ) -> c
FinS (FinS (FinS FinZ)) -> d
_ -> error "index@Vec4: cannot happen"
cycRight :: Vec4 a -> Vec4 a
cycRight (Vec4 a b c d) = Vec4 d a b c
cycLeft :: Vec4 a -> Vec4 a
cycLeft (Vec4 a b c d) = Vec4 b c d a
cycleLeft :: Aut (Vec4 a)
cycleLeft = iso cycLeft cycRight
cycleRight :: Aut (Vec4 a)
cycleRight = iso cycRight cycLeft
-- | One side of the Rubik's Cube with 3*3 facets.
data Side a =
Side { _topLeft :: a
, _topCenter :: a
, _topRight :: a
, _middleLeft :: a
, _middleCenter :: a
, _middleRight :: a
, _bottomLeft :: a
, _bottomCenter :: a
, _bottomRight :: a
} deriving (Show, Eq, Functor, Foldable, Traversable)
instance Applicative Side where
pure v = Side v v v v v v v v v
Side f1 f2 f3 f4 f5 f6 f7 f8 f9 <*> Side v1 v2 v3 v4 v5 v6 v7 v8 v9 =
Side (f1 v1) (f2 v2) (f3 v3) (f4 v4) (f5 v5) (f6 v6) (f7 v7) (f8 v8) (f9 v9)
makeLenses ''Side
instance Reversing (Side a) where
reversing (Side tl tc tr ml mc mr bl bc br) =
Side br bc bl mr mc ml tr tc tl
rotCW :: Side a -> Side a
rotCW (Side tl tc tr ml mc mr bl bc br) =
Side bl ml tl bc mc tc br mr tr
rotCCW :: Side a -> Side a
rotCCW (Side tl tc tr ml mc mr bl bc br) =
Side tr mr br tc mc bc tl ml bl
-- | Rotate the side clockwise.
rotateSideCW :: Aut (Side a)
rotateSideCW = iso rotCW rotCCW
-- | Rotate the side counter-clockwise.
rotateSideCCW :: Aut (Side a)
rotateSideCCW = iso rotCCW rotCW
-- | The top three facets (from left to right).
topRow :: Lens' (Side a) (Vec3 a)
topRow = lens getter setter
where
getter (Side tl tc tr _ _ _ _ _ _) = Vec3 tl tc tr
setter (Side _ _ _ ml mc mr bl bc br) (Vec3 tl tc tr) =
Side tl tc tr ml mc mr bl bc br
-- | The middle three facets (from left to right).
middleRow :: Lens' (Side a) (Vec3 a)
middleRow = lens getter setter
where
getter (Side _ _ _ ml mc mr _ _ _) = Vec3 ml mc mr
setter (Side tl tc tr _ _ _ bl bc br) (Vec3 ml mc mr) =
Side tl tc tr ml mc mr bl bc br
-- | The bottom three facets (from left to right).
bottomRow :: Lens' (Side a) (Vec3 a)
bottomRow = lens getter setter
where
getter (Side _ _ _ _ _ _ bl bc br) = Vec3 bl bc br
setter (Side tl tc tr ml mc mr _ _ _) (Vec3 bl bc br) =
Side tl tc tr ml mc mr bl bc br
-- | The left column (from top to down).
leftCol :: Lens' (Side a) (Vec3 a)
leftCol = lens getter setter
where
getter (Side tl _ _ ml _ _ bl _ _) = Vec3 tl ml bl
setter (Side _ tc tr _ mc mr _ bc br) (Vec3 tl ml bl) =
Side tl tc tr ml mc mr bl bc br
-- | The center column (from top to down).
centerCol :: Lens' (Side a) (Vec3 a)
centerCol = lens getter setter
where
getter (Side _ tc _ _ mc _ _ bc _) = Vec3 tc mc bc
setter (Side tl _ tr ml _ mr bl _ br) (Vec3 tc mc bc) =
Side tl tc tr ml mc mr bl bc br
-- | The right column (from top to down).
rightCol :: Lens' (Side a) (Vec3 a)
rightCol = lens getter setter
where
getter (Side _ _ tr _ _ mr _ _ br) = Vec3 tr mr br
setter (Side tl tc _ ml mc _ bl bc _) (Vec3 tr mr br) =
Side tl tc tr ml mc mr bl bc br
-- | The four corners of a side.
sideCorners :: Traversal' (Side a) a
sideCorners f (Side tl tc tr ml mc mr bl bc br) =
(\tl' tr' bl' br' -> Side tl' tc tr' ml mc mr bl' bc br')
<$> f tl <*> f tr <*> f bl <*> f br
-- | The four edges of a side.
sideEdges :: Traversal' (Side a) a
sideEdges f (Side tl tc tr ml mc mr bl bc br) =
(\tc' ml' mr' bc' -> Side tl tc' tr ml' mc mr' bl bc' br)
<$> f tc <*> f ml <*> f mr <*> f bc
-- | A cube with six sides.
--
-- @
-- +---+
-- | u |
-- +---+---+---+---+
-- | l | f | r | b |
-- +---+---+---+---+
-- | d |
-- +---+
-- @
data Cube a =
Cube { _frontSide :: a
, _backSide :: a
, _leftSide :: a
, _rightSide :: a
, _upSide :: a
, _downSide :: a
} deriving (Show, Eq, Functor, Foldable, Traversable)
instance Applicative Cube where
pure v = Cube v v v v v v
Cube ff fb fl fr fu fd <*> Cube vf vb vl vr vu vd =
Cube (ff vf) (fb vb) (fl vl) (fr vr) (fu vu) (fd vd)
rotRight' :: Cube a -> Cube a
rotRight' (Cube f b l r u d) = Cube l r b f u d
rotLeft' :: Cube a -> Cube a
rotLeft' (Cube f b l r u d) = Cube r l f b u d
rotateRight' :: Aut (Cube a)
rotateRight' = iso rotRight' rotLeft'
_rotateLeft' :: Aut (Cube a)
_rotateLeft' = from rotateRight'
rotDown' :: Reversing a => Cube a -> Cube a
rotDown' (Cube f b l r u d) = Cube u (reversing d) l r (reversing b) f
rotUp' :: Reversing a => Cube a -> Cube a
rotUp' (Cube f b l r u d) = Cube d (reversing u) l r f (reversing b)
rotateDown' :: Reversing a => Aut (Cube a)
rotateDown' = iso rotDown' rotUp'
_rotateUp' :: Reversing a => Aut (Cube a)
_rotateUp' = from rotateDown'
makeLenses ''Cube
-- | A normal Rubik's cube with 6 sides with 9 facets each.
newtype RubiksCube a =
RubiksCube { _cube :: Cube (Side a)
} deriving (Show, Eq, Functor)
instance Applicative RubiksCube where
pure = RubiksCube . pure . pure
RubiksCube f <*> RubiksCube v = RubiksCube ((<*>) <$> f <*> v)
makeLenses ''RubiksCube
cong :: Traversal' s a -> Aut a -> Aut s
cong t i = withIso i $ \f g -> iso (over t f) (over t g)
-- | Rotate the whole Rubik's Cube such that the front side becomes the new
-- right side and the top and bottom sides stay fixed.
rotateRight :: Aut (RubiksCube a)
rotateRight =
cong cube $ rotateRight'
. cong upSide rotateSideCCW
. cong downSide rotateSideCW
-- | Rotate the whole Rubik's Cube such that the front side becomes the new
-- left side and the top and bottom sides stay fixed.
rotateLeft :: Aut (RubiksCube a)
rotateLeft = from rotateRight
-- | Rotate the whole Rubik's Cube such that the front side becomes the new
-- bottom side and the left and right sides stay fixed.
rotateDown :: Aut (RubiksCube a)
rotateDown =
cong cube $ rotateDown'
. cong leftSide rotateSideCW
. cong rightSide rotateSideCCW
-- | Rotate the whole Rubik's Cube such that the front side becomes the new
-- top side and the left and right sides stay fixed.
rotateUp :: Aut (RubiksCube a)
rotateUp = from rotateDown
-- | Rotate the whole Rubik's Cube such that the top side becomes the new
-- right side and the front and back sides stay fixed.
rotateCW :: Aut (RubiksCube a)
rotateCW = rotateUp . rotateLeft . rotateDown
-- | Rotate the whole Rubik's Cube such that the top side becomes the new
-- left side and the front and back sides stay fixed.
rotateCCW :: Aut (RubiksCube a)
rotateCCW = from rotateCW
type RowsLens a = Lens' (Cube (Side a)) (Vec4 (Vec3 a))
type ColsLens a = Lens' (Cube (Side a)) (Vec4 (Vec3 a))
horizontalSides :: Lens' (Cube a) (Vec4 a)
horizontalSides = lens getter setter
where
getter (Cube f b l r _u _d) = Vec4 f r b l
setter (Cube _f _b _l _r u d) (Vec4 f' r' b' l') =
Cube f' b' l' r' u d
horizontalRows :: Lens' (Side a) (Vec3 a) -> RowsLens a
horizontalRows rowLens = horizontalSides . insideRep rowLens
upRows :: RowsLens a
upRows = horizontalRows topRow
middleRows :: RowsLens a
middleRows = horizontalRows middleRow
downRows :: RowsLens a
downRows = horizontalRows bottomRow
moveU :: Aut (RubiksCube a)
moveU =
cong cube $ cong upRows cycleLeft
. cong upSide rotateSideCW
moveD :: Aut (RubiksCube a)
moveD =
cong cube $ cong downRows cycleRight
. cong downSide rotateSideCW
verticalCols :: Lens' (Side a) (Vec3 a) -> ColsLens a
verticalCols colLens = lens getter setter
where
getter (Cube f b _l _r u d) =
Vec4 (f ^. colLens) (u ^. colLens) (b ^. reversed . colLens) (d ^. colLens)
setter (Cube f b l r u d) (Vec4 f' u' b' d') =
Cube (set colLens f' f) (set (reversed . colLens) b' b) l r
(set colLens u' u) (set colLens d' d)
leftCols :: ColsLens a
leftCols = verticalCols leftCol
centerCols :: ColsLens a
centerCols = verticalCols centerCol
rightCols :: ColsLens a
rightCols = verticalCols rightCol
moveL :: Aut (RubiksCube a)
moveL =
cong cube $ cong leftCols cycleLeft
. cong leftSide rotateSideCW
moveR :: Aut (RubiksCube a)
moveR =
cong cube $ cong rightCols cycleRight
. cong rightSide rotateSideCW
ringCols :: Lens' (Side a) (Vec3 a) -> ColsLens a
ringCols colLens = lens getter setter
where
getter (Cube _f _b l r u d) =
Vec4 (r ^. colLens) (u ^. rotateSideCW . colLens)
(l ^. reversed . colLens) (d ^. rotateSideCCW . colLens)
setter (Cube f b l r u d) (Vec4 r' u' l' d') =
Cube f b (set (reversed . colLens) l' l) (set colLens r' r)
(set (rotateSideCW . colLens) u' u) (set (rotateSideCCW . colLens) d' d)
frontCols :: ColsLens a
frontCols = ringCols leftCol
betweenCols :: ColsLens a
betweenCols = ringCols centerCol
backCols :: ColsLens a
backCols = ringCols rightCol
moveF :: Aut (RubiksCube a)
moveF =
cong cube $ cong frontCols cycleLeft
. cong frontSide rotateSideCW
moveB :: Aut (RubiksCube a)
moveB =
cong cube $ cong backCols cycleRight
. cong backSide rotateSideCW
-- | Perform a move.
move :: Move -> Aut (RubiksCube a)
move D = moveD
move D' = from moveD
move U = moveU
move U' = from moveU
move L = moveL
move L' = from moveL
move R = moveR
move R' = from moveR
move F = moveF
move F' = from moveF
move B = moveB
move B' = from moveB
-- | Perform a list of moves.
doMoves :: [Move] -> Aut (RubiksCube a)
doMoves [] = iso id id
doMoves (m:ms) = move m . doMoves ms
-- | Undo the actions of a list of moves.
undoMoves :: [Move] -> Aut (RubiksCube a)
undoMoves = from . doMoves
-- | The 21=4*3+9 facets in the top layer.
topLayerFacets :: Traversal' (RubiksCube a) a
topLayerFacets f =
cube $ \c ->
(\upSide' upRows' -> c & upSide .~ upSide' & upRows .~ upRows')
<$> (traverse f (c ^. upSide))
<*> (traverse (traverse f) (c ^. upRows))
-- | The 12=4*3 facets in the middle layer.
middleLayerFacets :: Traversal' (RubiksCube a) a
middleLayerFacets = cube.middleRows.traverse.traverse
-- | The 21=4*3+9 facets in the bottom layer.
bottomLayerFacets :: Traversal' (RubiksCube a) a
bottomLayerFacets f =
cube $ \c ->
(\downSide' downRows' -> c & downSide .~ downSide' & downRows .~ downRows')
<$> (traverse f (c ^. downSide))
<*> (traverse (traverse f) (c ^. downRows))
-- | The six facets that are the center of their side.
centerFacets :: Traversal' (RubiksCube a) a
centerFacets = cube.traverse.middleCenter
-- | The 24=6*4=8*3 corner facets.
cornerFacets :: Traversal' (RubiksCube a) a
cornerFacets = cube.traverse.sideCorners
-- | The 24=6*4=12*2 edge facets.
edgeFacets :: Traversal' (RubiksCube a) a
edgeFacets = cube.traverse.sideEdges