module Diagrams.RubiksCube.Model (
Side (..)
, topLeft, topCenter, topRight
, middleLeft, middleCenter, middleRight
, bottomLeft, bottomCenter, bottomRight
, rotateSideCW, rotateSideCCW
, Cube (..), frontSide, backSide, leftSide, rightSide, upSide, downSide
, RubiksCube (..), cube
, Vec3 (..)
, topRow, middleRow, bottomRow
, leftCol, centerCol, rightCol
, topLayerFacets, middleLayerFacets, bottomLayerFacets
, centerFacets, cornerFacets, edgeFacets
, Aut
, rotateLeft, rotateRight
, rotateDown, rotateUp
, rotateCW, rotateCCW
, 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)
type Aut a = Iso' a a
data N = Z | S N
type Zero = 'Z
type One = 'S Zero
type Two = 'S One
type Three = 'S Two
type Four = 'S Three
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
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
insideRep
:: Representable g
=> Lens s t a b
-> Lens (g s) (g t) (g a) (g b)
insideRep l = from tabulated . inside l . tabulated
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
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
rotateSideCW :: Aut (Side a)
rotateSideCW = iso rotCW rotCCW
rotateSideCCW :: Aut (Side a)
rotateSideCCW = iso rotCCW rotCW
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
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
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
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
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
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
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
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
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
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)
rotateRight :: Aut (RubiksCube a)
rotateRight =
cong cube $ rotateRight'
. cong upSide rotateSideCCW
. cong downSide rotateSideCW
rotateLeft :: Aut (RubiksCube a)
rotateLeft = from rotateRight
rotateDown :: Aut (RubiksCube a)
rotateDown =
cong cube $ rotateDown'
. cong leftSide rotateSideCW
. cong rightSide rotateSideCCW
rotateUp :: Aut (RubiksCube a)
rotateUp = from rotateDown
rotateCW :: Aut (RubiksCube a)
rotateCW = rotateUp . rotateLeft . rotateDown
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
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
doMoves :: [Move] -> Aut (RubiksCube a)
doMoves [] = iso id id
doMoves (m:ms) = move m . doMoves ms
undoMoves :: [Move] -> Aut (RubiksCube a)
undoMoves = from . doMoves
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))
middleLayerFacets :: Traversal' (RubiksCube a) a
middleLayerFacets = cube.middleRows.traverse.traverse
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))
centerFacets :: Traversal' (RubiksCube a) a
centerFacets = cube.traverse.middleCenter
cornerFacets :: Traversal' (RubiksCube a) a
cornerFacets = cube.traverse.sideCorners
edgeFacets :: Traversal' (RubiksCube a) a
edgeFacets = cube.traverse.sideEdges