1 -- | The Cardinal module contains the Cardinal data type, representing
2 -- a cardinal direction (one of the 26 directions surrounding the
3 -- center of a cube. In addition to those 26 directions, we also
4 -- include the interior point and a number of composite types that
5 -- allow us to perform arithmetic on directions.
19 import Control.Monad (liftM, liftM2)
25 Fractional( (/), fromRational, recip ),
26 Num( (+), (-), (*), abs, negate, signum, fromInteger ),
32 import Test.Tasty ( TestTree, testGroup )
33 import Test.Tasty.HUnit ( Assertion, assertEqual, testCase )
34 import Test.Tasty.QuickCheck (
35 Arbitrary( arbitrary ),
42 data Cardinal = F -- ^ Front
60 | FLD -- ^ Front Left Down
61 | FLT -- ^ Front Left Top
62 | FRD -- ^ Front Right Down
63 | FRT -- ^ Front Right Top
64 | BLD -- ^ Back Left Down
65 | BLT -- ^ Back Left Top
66 | BRD -- ^ Back Right Down
67 | BRT -- ^ Back Right Top
69 | Scalar Double -- ^ A wrapper around a scalar value.
70 | Sum Cardinal Cardinal -- ^ The sum of two directions.
71 | Difference Cardinal Cardinal
72 -- ^ The difference of two directions, the first minus the second.
73 | Product Cardinal Cardinal -- ^ The product of two directions.
74 | Quotient Cardinal Cardinal
75 -- ^ The quotient of two directions, the first divided by the
80 -- | By making Cardinal an instance of 'Num', we gain the ability to
81 -- add, subtract, and multiply directions. The results of these
82 -- operations are never actually calculated; the types just keep
83 -- track of which operations were performed in which order.
84 instance Num Cardinal where
86 x - y = Difference x y
88 negate = Product (Scalar (-1))
91 fromInteger x = Scalar (fromIntegral x)
94 -- | Like the Num instance, the 'Fractional' instance allows us to
95 -- take quotients of directions.
96 instance Fractional Cardinal where
98 recip = Quotient (Scalar 1)
99 fromRational x = Scalar (fromRational x)
103 instance Arbitrary Cardinal where
104 arbitrary = oneof [f,b,l,r,d,t,fl,fr,fd,ft,bl,br,bd,bt,ld,lt,
105 rd,rt,fld,flt,frd,frt,bld,blt,brd,brt,i,
106 scalar,csum,cdiff,cprod,cquot]
108 f = return F :: Gen Cardinal
109 b = return B :: Gen Cardinal
110 l = return L :: Gen Cardinal
111 r = return R :: Gen Cardinal
112 d = return D :: Gen Cardinal
113 t = return T :: Gen Cardinal
114 fl = return FL :: Gen Cardinal
115 fr = return FR :: Gen Cardinal
116 fd = return FD :: Gen Cardinal
117 ft = return FT :: Gen Cardinal
118 bl = return BL :: Gen Cardinal
119 br = return BR :: Gen Cardinal
120 bd = return BD :: Gen Cardinal
121 bt = return BT :: Gen Cardinal
122 ld = return LD :: Gen Cardinal
123 lt = return LT :: Gen Cardinal
124 rd = return RD :: Gen Cardinal
125 rt = return RT :: Gen Cardinal
126 fld = return FLD :: Gen Cardinal
127 flt = return FLT :: Gen Cardinal
128 frd = return FRD :: Gen Cardinal
129 frt = return FRT :: Gen Cardinal
130 bld = return BLD :: Gen Cardinal
131 blt = return BLT :: Gen Cardinal
132 brd = return BRD :: Gen Cardinal
133 brt = return BRT :: Gen Cardinal
134 i = return I :: Gen Cardinal
135 scalar = liftM Scalar arbitrary
136 csum = liftM2 Sum arbitrary arbitrary
137 cdiff = liftM2 Difference arbitrary arbitrary
138 cprod = liftM2 Product arbitrary arbitrary
139 cquot = liftM2 Quotient arbitrary arbitrary
142 -- | Rotate a cardinal direction counter-clockwise about the x-axis.
143 ccwx :: Cardinal -> Cardinal
171 ccwx (Scalar s) = (Scalar s)
172 ccwx (Sum c0 c1) = Sum (ccwx c0) (ccwx c1)
173 ccwx (Difference c0 c1) = Difference (ccwx c0) (ccwx c1)
174 ccwx (Product c0 c1) = Product (ccwx c0) (ccwx c1)
175 ccwx (Quotient c0 c1) = Quotient (ccwx c0) (ccwx c1)
177 -- | Rotate a cardinal direction clockwise about the x-axis.
178 cwx :: Cardinal -> Cardinal
179 cwx = ccwx . ccwx . ccwx
182 -- | Rotate a cardinal direction counter-clockwise about the y-axis.
183 ccwy :: Cardinal -> Cardinal
211 ccwy (Scalar s) = (Scalar s)
212 ccwy (Sum c0 c1) = Sum (ccwy c0) (ccwy c1)
213 ccwy (Difference c0 c1) = Difference (ccwy c0) (ccwy c1)
214 ccwy (Product c0 c1) = Product (ccwy c0) (ccwy c1)
215 ccwy (Quotient c0 c1) = Quotient (ccwy c0) (ccwy c1)
217 -- | Rotate a cardinal direction clockwise about the y-axis.
218 cwy :: Cardinal -> Cardinal
219 cwy = ccwy . ccwy . ccwy
222 -- | Rotate a cardinal direction counter-clockwise about the z-axis.
223 ccwz :: Cardinal -> Cardinal
251 ccwz (Scalar s) = (Scalar s)
252 ccwz (Sum c0 c1) = Sum (ccwz c0) (ccwz c1)
253 ccwz (Difference c0 c1) = Difference (ccwz c0) (ccwz c1)
254 ccwz (Product c0 c1) = Product (ccwz c0) (ccwz c1)
255 ccwz (Quotient c0 c1) = Quotient (ccwz c0) (ccwz c1)
257 -- | Rotate a cardinal direction clockwise about the z-axis.
258 cwz :: Cardinal -> Cardinal
259 cwz = ccwz . ccwz . ccwz
264 -- | We know what (c t6 2 1 0 0) should be from Sorokina and
265 -- Zeilfelder, p. 87. This test checks that the directions are
266 -- rotated properly. The order of the letters has to be just right
267 -- since I haven't defined a proper Eq instance for Cardinals.
268 test_c_tilde_2100_rotation_correct :: Assertion
269 test_c_tilde_2100_rotation_correct =
270 assertEqual "auto-rotate equals manual rotate" ((ccwz . ccwz . cwy) expr1) expr2
274 (1/12)*(T + R + L + D) +
275 (1/64)*(FT + FR + FL + FD) +
278 (1/96)*(RT + LD + LT + RD) +
279 (1/192)*(BT + BR + BL + BD)
283 (1/12)*(F + L + R + B) +
284 (1/64)*(FT + LT + RT + BT) +
287 (1/96)*(FL + BR + FR + BL) +
288 (1/192)*(FD + LD + RD + BD)
290 -- | A list of all directions, sans the interior and composite types.
291 all_directions :: [Cardinal]
292 all_directions = [L, R, F, B, D, T, FL, FR, FD, FT,
293 BL, BR, BD, BT, LD, LT, RD, RT, FLD,
294 FLT, FRD, FRT, BLD, BLT, BRD, BRT]
297 -- | If we rotate a direction (other than front or back)
298 -- counter-clockwise with respect to the x-axis, we should get a new
300 prop_ccwx_rotation_changes_direction :: Cardinal -> Property
301 prop_ccwx_rotation_changes_direction c =
302 c `elem` [L, R, D, T, FL, FR, FD, FT, BL, BR, BD, BT, LD, LT,
303 RD, RT, FLD, FLT, FRD, FRT, BLD, BLT, BRD, BRT]
306 -- | If we rotate a direction (other than front or back) clockwise
307 -- with respect to the x-axis, we should get a new direction.
308 prop_cwx_rotation_changes_direction :: Cardinal -> Property
309 prop_cwx_rotation_changes_direction c =
310 -- The front and back faces are unchanged by x-rotation.
311 c `elem` [L, R, D, T, FL, FR, FD, FT, BL, BR, BD, BT, LD, LT,
312 RD, RT, FLD, FLT, FRD, FRT, BLD, BLT, BRD, BRT]
315 -- | If we rotate a direction (other than left or right)
316 -- counter-clockwise with respect to the y-axis, we should get a new
318 prop_ccwy_rotation_changes_direction :: Cardinal -> Property
319 prop_ccwy_rotation_changes_direction c =
320 c `elem` [F, B, D, T, FL, FR, FD, FT, BL, BR, BD, BT, LD, LT,
321 RD, RT, FLD, FLT, FRD, FRT, BLD, BLT, BRD, BRT]
325 -- | If we rotate a direction (other than left or right) clockwise
326 -- with respect to the y-axis, we should get a new direction.
327 prop_cwy_rotation_changes_direction :: Cardinal -> Property
328 prop_cwy_rotation_changes_direction c =
329 c `elem` [F, B, D, T, FL, FR, FD, FT, BL, BR, BD, BT, LD, LT,
330 RD, RT, FLD, FLT, FRD, FRT, BLD, BLT, BRD, BRT]
334 -- | If we rotate a direction (other than top or down)
335 -- counter-clockwise with respect to the z-axis, we should get a new
337 prop_ccwz_rotation_changes_direction :: Cardinal -> Property
338 prop_ccwz_rotation_changes_direction c =
339 c `elem` [L, R, F, B, FL, FR, FD, FT, BL, BR, BD, BT, LD, LT,
340 RD, RT, FLD, FLT, FRD, FRT, BLD, BLT, BRD, BRT]
344 -- | If we rotate a direction (other than top or down) clockwise with
345 -- respect to the z-axis, we should get a new direction.
346 prop_cwz_rotation_changes_direction :: Cardinal -> Property
347 prop_cwz_rotation_changes_direction c =
348 c `elem` [L, R, F, B, FL, FR, FD, FT, BL, BR, BD, BT, LD, LT,
349 RD, RT, FLD, FLT, FRD, FRT, BLD, BLT, BRD, BRT]
353 -- | If we are given a direction c, there should only be one direction
354 -- d which, when rotated counter-clockwise with respect to the
355 -- x-axis, produces c.
356 prop_ccwx_rotation_result_unique :: Cardinal -> Property
357 prop_ccwx_rotation_result_unique c =
358 c `elem` all_directions ==>
359 (length [ d | d <- all_directions, ccwx d == c ]) == 1
361 -- | If we are given a direction c, there should only be one direction
362 -- d which, when rotated clockwise with respect to the x-axis,
364 prop_cwx_rotation_result_unique :: Cardinal -> Property
365 prop_cwx_rotation_result_unique c =
366 c `elem` all_directions ==>
367 (length [ d | d <- all_directions, cwx d == c ]) == 1
370 -- | If we are given a direction c, there should only be one direction
371 -- d which, when rotated counter-clockwise with respect to the
372 -- y-axis, produces c.
373 prop_ccwy_rotation_result_unique :: Cardinal -> Property
374 prop_ccwy_rotation_result_unique c =
375 c `elem` all_directions ==>
376 (length [ d | d <- all_directions, ccwy d == c ]) == 1
379 -- | If we are given a direction c, there should only be one direction
380 -- d which, when rotated clockwise with respect to the y-axis,
382 prop_cwy_rotation_result_unique :: Cardinal -> Property
383 prop_cwy_rotation_result_unique c =
384 c `elem` all_directions ==>
385 (length [ d | d <- all_directions, cwy d == c ]) == 1
388 -- | If we are given a direction c, there should only be one direction
389 -- d which, when rotated counter-clockwise with respect to the
390 -- z-axis, produces c.
391 prop_ccwz_rotation_result_unique :: Cardinal -> Property
392 prop_ccwz_rotation_result_unique c =
393 c `elem` all_directions ==>
394 (length [ d | d <- all_directions, ccwz d == c ]) == 1
397 -- | If we are given a direction c, there should only be one direction
398 -- d which, when rotated clockwise with respect to the z-axis,
400 prop_cwz_rotation_result_unique :: Cardinal -> Property
401 prop_cwz_rotation_result_unique c =
402 c `elem` all_directions ==>
403 (length [ d | d <- all_directions, cwz d == c ]) == 1
406 -- | If you rotate a cardinal direction four times in the clockwise
407 -- (with respect to x) direction, you should wind up with the same
409 prop_four_cwx_is_identity :: Cardinal -> Bool
410 prop_four_cwx_is_identity c =
411 (cwx . cwx . cwx . cwx) c == c
413 -- | If you rotate a cardinal direction four times in the
414 -- counter-clockwise (with respect to x) direction, you should wind up
415 -- with the same direction.
416 prop_four_ccwx_is_identity :: Cardinal -> Bool
417 prop_four_ccwx_is_identity c =
418 (ccwx . ccwx . ccwx . ccwx) c == c
420 -- | If you rotate a cardinal direction four times in the clockwise
421 -- (with respect to y) direction, you should wind up with the same
423 prop_four_cwy_is_identity :: Cardinal -> Bool
424 prop_four_cwy_is_identity c =
425 (cwy . cwy . cwy . cwy) c == c
427 -- | If you rotate a cardinal direction four times in the counter-clockwise
428 -- (with respect to y) direction, you should wind up with the same
430 prop_four_ccwy_is_identity :: Cardinal -> Bool
431 prop_four_ccwy_is_identity c =
432 (ccwy . ccwy . ccwy . ccwy) c == c
434 -- | If you rotate a cardinal direction four times in the clockwise
435 -- (with respect to z) direction, you should wind up with the same
437 prop_four_cwz_is_identity :: Cardinal -> Bool
438 prop_four_cwz_is_identity c =
439 (cwz . cwz . cwz . cwz) c == c
441 -- | If you rotate a cardinal direction four times in the
442 -- counter-clockwise (with respect to z) direction, you should wind up
443 -- with the same direction.
444 prop_four_ccwz_is_identity :: Cardinal -> Bool
445 prop_four_ccwz_is_identity c =
446 (ccwz . ccwz . ccwz . ccwz) c == c
449 cardinal_tests :: TestTree
451 testGroup "Cardinal tests" [
452 testCase "c-tilde_2100 rotation correct"test_c_tilde_2100_rotation_correct ]
455 cardinal_properties :: TestTree
456 cardinal_properties =
457 testGroup "Cardinal properties" [
459 "ccwx rotation changes direction"
460 prop_ccwx_rotation_changes_direction,
462 "cwx rotation changes direction"
463 prop_cwx_rotation_changes_direction,
465 "ccwy rotation changes direction"
466 prop_ccwy_rotation_changes_direction,
468 "cwy rotation changes direction"
469 prop_cwy_rotation_changes_direction,
471 "ccwz rotation changes direction"
472 prop_ccwz_rotation_changes_direction,
474 "cwz rotation changes direction"
475 prop_cwz_rotation_changes_direction,
477 "ccwx rotation result unique"
478 prop_ccwx_rotation_result_unique,
480 "cwx rotation result unique"
481 prop_cwx_rotation_result_unique,
483 "ccwy rotation result unique"
484 prop_ccwy_rotation_result_unique,
486 "cwy rotation result unique"
487 prop_cwy_rotation_result_unique,
489 "ccwz rotation result unique"
490 prop_ccwz_rotation_result_unique,
492 "cwz rotation result unique"
493 prop_cwz_rotation_result_unique,
495 "four cwx is identity"
496 prop_four_cwx_is_identity,
498 "four ccwx is identity"
499 prop_four_ccwx_is_identity,
501 "four cwy is identity"
502 prop_four_cwy_is_identity,
504 "four ccwy is identity"
505 prop_four_ccwy_is_identity,
507 "four cwz is identity"
508 prop_four_cwz_is_identity,
510 "four ccwz is identity"
511 prop_four_ccwz_is_identity ]