Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.03 KB | None | 0 0
  1. object InstantInsanity extends App {
  2.  
  3. // scalastyle:off
  4.  
  5. def undefined[T]: T = ???
  6. def ⊥[T]: T = undefined
  7.  
  8. trait R
  9. trait G
  10. trait B
  11. trait W
  12.  
  13. trait Cube[u, f, b, r, l, d]
  14.  
  15. type Cube1 = Cube[B, G, W, G, B, R]
  16. type Cube2 = Cube[W, G, B, W, R, R]
  17. type Cube3 = Cube[G, W, R, B, R, R]
  18. type Cube4 = Cube[B, R, G, G, W, W]
  19.  
  20. trait Transform[u, f, r, b, l, d] {
  21. def rot: Cube[u, f, r, b, l, d] => Cube[u, r, b, l, f, d]
  22. def twist: Cube[u, f, r, b, l, d] => Cube[f, r, u, l, d, b]
  23. def flip: Cube[u, f, r, b, l, d] => Cube[d, l, b, r, f, u]
  24. }
  25.  
  26. implicit def transform[u, f, r, b, l, d] = new Transform[u, f, r, b, l, d] {
  27. def rot: (Cube[u, f, r, b, l, d]) => Cube[u, r, b, l, f, d] = ⊥
  28. def twist: (Cube[u, f, r, b, l, d]) => Cube[f, r, u, l, d, b] = ⊥
  29. def flip: (Cube[u, f, r, b, l, d]) => Cube[d, l, b, r, f, u] = ⊥
  30. }
  31.  
  32. def rot[u, f, r, b, l, d](cube: Cube[u, f, r, b, l, d])(implicit t: Transform[u, f, r, b, l, d]) = t.rot(cube)
  33. def twist[u, f, r, b, l, d](cube: Cube[u, f, r, b, l, d])(implicit t: Transform[u, f, r, b, l, d]) = t.twist(cube)
  34. def flip[u, f, r, b, l, d](cube: Cube[u, f, r, b, l, d])(implicit t: Transform[u, f, r, b, l, d]) = t.flip(cube)
  35.  
  36. // :type rot(⊥[Cube1])
  37. // :type twist(flip(rot(⊥[Cube1])))
  38.  
  39. trait True
  40. trait False
  41.  
  42. trait And[l, r, o]
  43.  
  44. implicit object ttt extends And[True, True, True]
  45. implicit object tff extends And[True, False, False]
  46. implicit object ftf extends And[False, True, False]
  47. implicit object fff extends And[False, False, False]
  48.  
  49. def and[l, r, o](l: l, r: r)(implicit and: And[l, r, o]): o = ⊥
  50.  
  51. // :type and(⊥[True], ⊥[False])
  52.  
  53. trait Nil
  54. trait :::[x, xs]
  55.  
  56. trait ListConcat[l1, l2, l]
  57.  
  58. implicit def nilConcat[l]: ListConcat[Nil, l, l] = ⊥
  59. implicit def notNilConcat[x, xs, ys, zs](implicit
  60. lc: ListConcat[xs, ys, zs]
  61. ): ListConcat[x ::: xs, ys, x ::: zs] = ⊥
  62.  
  63. def listConcat[xs, ys, zs](l1: xs, l2: ys)(implicit
  64. lc: ListConcat[xs, ys, zs]
  65. ): zs = ⊥
  66.  
  67. // :type listConcat(⊥[R ::: Nil], ⊥[G ::: W ::: Nil])
  68.  
  69. trait Rotation
  70. trait Twist
  71. trait Flip
  72.  
  73. trait Apply[f, a, b]
  74.  
  75. implicit def apRotation[u, f, r, b, l, d]: Apply[Rotation, Cube[u, f, r, b, l, d], Cube[u, r, b, l, f, d]] = ⊥
  76. implicit def apTwist[u, f, r, b, l, d]: Apply[Twist, Cube[u, f, r, b, l, d], Cube[f, r, u, l, d, b]] = ⊥
  77. implicit def apFlip[u, f, r, b, l, d]: Apply[Flip, Cube[u, f, r, b, l, d], Cube[d, l, b, r, f, u]] = ⊥
  78.  
  79. def ap[t, u, f, r, b, l, d, o](r: t, c1: Cube[u, f, r, b, l, d])(implicit
  80. ap: Apply[t, Cube[u, f, r, b, l, d], o]
  81. ): o = ⊥
  82.  
  83. // :type ap(⊥[Rotation], ⊥[Cube1])
  84.  
  85. trait Map[f, xs, zs]
  86.  
  87. implicit def mapNil[f]: Map[f, Nil, Nil] = ⊥
  88. implicit def mapCons[f, x, z, xs, zs](implicit
  89. ap: Apply[f, x, z],
  90. map: Map[f, xs, zs]
  91. ): Map[f, x ::: xs, z ::: zs] = ⊥
  92.  
  93. def map[f, xs, zs](f: f, xs: xs)(implicit
  94. map: Map[f, xs, zs]
  95. ): zs = ⊥
  96.  
  97. // :type map(⊥[Flip], ⊥[Cube1 ::: Cube2 ::: Nil])
  98.  
  99. trait AppendIf[b, x, ys, zs]
  100.  
  101. implicit def appendIfTrue[x, ys]: AppendIf[True, x, ys, x ::: ys] = ⊥
  102. implicit def appendIfFalse[x, ys]: AppendIf[False, x, ys, ys] = ⊥
  103.  
  104. def append[b, x, ys, zs](b: b, x: x, ys: ys)(implicit
  105. apn: AppendIf[b, x, ys, zs]
  106. ): zs = ⊥
  107.  
  108. // :type append(⊥[True], ⊥[R], ⊥[G ::: W ::: Nil])
  109. // :type append(⊥[False], ⊥[R], ⊥[G ::: W ::: Nil])
  110.  
  111.  
  112. trait Filter[f, xs, zs]
  113.  
  114. implicit def filterNil[f]: Filter[f, Nil, Nil] = ⊥
  115. implicit def filterCons[f, x, b, xs, ys, zs](implicit
  116. ap: Apply[f, x, b],
  117. f: Filter[f, xs, ys],
  118. apn: AppendIf[b, x, ys, zs]
  119. ): Filter[f, x ::: xs, zs] = ⊥
  120.  
  121.  
  122. //
  123.  
  124. trait MapAppend[f, xs, zs]
  125.  
  126. implicit def mapAppendNil[f]: MapAppend[f, Nil, Nil] = ⊥
  127. implicit def mapAppendCons[f, xs, ys, zs](implicit
  128. m: Map[f, xs, ys],
  129. lc: ListConcat[xs, ys, zs]
  130. ): MapAppend[f, xs, zs] = ⊥
  131.  
  132. trait MapAppend2[f, xs, zs]
  133.  
  134. implicit def mapAppend2Nil[f]: MapAppend2[f, Nil, Nil] = ⊥
  135. implicit def mapAppend2Cons[f, xs, ys, _ys, zs](implicit
  136. m: Map[f, xs, ys],
  137. ma: MapAppend[f, ys, _ys],
  138. lc: ListConcat[xs, _ys, zs]
  139. ): MapAppend2[f, xs, zs] = ⊥
  140.  
  141. trait MapAppend3[f, xs, zs]
  142.  
  143. implicit def mapAppend3Nil[f]: MapAppend3[f, Nil, Nil] = ⊥
  144. implicit def mapAppend3Cons[f, xs, ys, _ys, zs](implicit
  145. m: Map[f, xs, ys],
  146. ma2: MapAppend2[f, ys, _ys],
  147. lc: ListConcat[xs, _ys, zs]
  148. ): MapAppend3[f, xs, zs] = ⊥
  149.  
  150.  
  151. trait Orientations
  152.  
  153. implicit def orientations[c, fs, ts, zs](implicit
  154. ma: MapAppend[Flip, c ::: Nil, fs],
  155. ma2: MapAppend2[Twist, fs, ts],
  156. ma3: MapAppend3[Rotation, ts, zs]
  157. ): Apply[Orientations, c, zs] = ⊥
  158.  
  159. // :type ap(⊥[Orientations], ⊥[Cube1])
  160.  
  161. trait NE[x, y, b]
  162.  
  163. implicit object neRR extends NE[R, R, False]
  164. implicit object neRG extends NE[R, G, True]
  165. implicit object neRB extends NE[R, B, True]
  166. implicit object neRW extends NE[R, W, True]
  167.  
  168. implicit object neGR extends NE[G, R, True]
  169. implicit object neGG extends NE[G, G, False]
  170. implicit object neGB extends NE[G, B, True]
  171. implicit object neGW extends NE[G, W, True]
  172.  
  173. implicit object neBR extends NE[B, R, True]
  174. implicit object neBG extends NE[B, G, True]
  175. implicit object neBB extends NE[B, B, False]
  176. implicit object neBW extends NE[B, W, True]
  177.  
  178. implicit object neWR extends NE[W, R, True]
  179. implicit object neWG extends NE[W, G, True]
  180. implicit object neWB extends NE[W, B, True]
  181. implicit object neWW extends NE[W, W, False]
  182.  
  183. trait All[l, b]
  184.  
  185. implicit def allNil: All[Nil, True]
  186.  
  187. = ⊥
  188. implicit def allFalse[xs]: All[False ::: xs, False] = ⊥
  189. implicit def allTrue[b, xs](implicit all: All[xs, b]): All[True ::: xs, b] = ⊥
  190.  
  191. def all[b, xs](l: xs)(implicit all: All[xs, b]): b = ⊥
  192.  
  193. // :type all(⊥[Nil])
  194. // :type all(⊥[False ::: Nil])
  195. // :type all(⊥[True ::: False ::: Nil])
  196. // :type all(⊥[True ::: True ::: Nil])
  197.  
  198.  
  199. trait Compatible[c1, c2, b]
  200.  
  201. implicit def compatibleInstance[f1, f2, bF, r1, r2, bR, b1, b2, bB, l1, l2, bL, u1, u2, d1, d2, b](implicit
  202. ne1: NE[f1, f2, bF],
  203. ne2: NE[r1, r2, bR],
  204. ne3: NE[b1, b2, bB],
  205. ne4: NE[l1, l2, bL],
  206. all: All[bF ::: bR ::: bB ::: bL ::: Nil, b]
  207. ): Compatible[Cube[u1, f1, r1, b1, l1, d1], Cube[u2, f2, r2, b2, l2, d2], b] = ⊥
  208.  
  209. def compatible[c1, c2, b](c1: c1, c2: c2)(implicit
  210. c: Compatible[c1, c2, b]
  211. ): b = ⊥
  212.  
  213. // :type compatible(⊥[Cube[R, R, R, R, R, R]], ⊥[Cube[B, B, B, B, B, B]])
  214. // :type compatible(⊥[Cube[R, R, G, R, R, R]], ⊥[Cube[B, B, G, B, B, B]])
  215.  
  216. trait Allowed[c, cs, b]
  217.  
  218. implicit def allowedNil[c]: Allowed[c, Nil, True] = ⊥
  219. implicit def allowedCons[c, y, b1, ys, b2, b](implicit
  220. c: Compatible[c, y, b1],
  221. allowed: Allowed[c, ys, b2],
  222. and: And[b1, b2, b]
  223. ): Allowed[c, y ::: ys, b] = ⊥
  224.  
  225. def allowed[c, cs, b](c: c, cs: cs)(implicit a: Allowed[c, cs, b]): b = ⊥
  226.  
  227. type CubeRed = Cube[R, R, R, R, R, R]
  228. type CubeBlue = Cube[B, B, B, B, B, B]
  229.  
  230. // :type allowed(⊥[CubeRed], ⊥[CubeBlue ::: CubeRed ::: Nil])
  231.  
  232. trait MatchingOrientations[os, sols, zs]
  233.  
  234. implicit def matchingOrientationsNil[sol]: MatchingOrientations[Nil, sol, Nil] = ⊥
  235. implicit def matchingOrientationsCons[os, sol, as, o, b, zs](implicit
  236. mo: MatchingOrientations[os, sol, as],
  237. a: Allowed[o, sol, b],
  238. apn: AppendIf[b, o ::: sol, as, zs]
  239. ): MatchingOrientations[o ::: os, sol, zs] = ⊥
  240.  
  241. trait AllowedCombinations[os, sols, zs]
  242.  
  243. implicit def allowedCombinationsNil[os]: AllowedCombinations[os, Nil, Nil] = ⊥
  244. implicit def allowedCombinationsCons[os, sols, as, s, bs, zs](implicit
  245. ac: AllowedCombinations[os, sols, as],
  246. mo: MatchingOrientations[os, s, bs],
  247. lc: ListConcat[as, bs, zs]
  248. ): AllowedCombinations[os, s ::: sols, zs] = ⊥
  249.  
  250. class Solutions[cs, ss]
  251.  
  252. implicit def solutionsNil: Solutions[Nil, Nil ::: Nil] = ⊥
  253. implicit def solutionsCons[cs, sols, c, os, zs](implicit
  254. s: Solutions[cs, sols],
  255. ap: Apply[Orientations, c, os],
  256. ac: AllowedCombinations[os, sols, zs]
  257. ): Solutions[c ::: cs, zs] = ⊥
  258.  
  259. type Cubes = (Cube1 ::: Cube2 ::: Cube3 ::: Cube4 ::: Nil)
  260.  
  261. def solutions[cs, ss](cs: cs)(implicit sol: Solutions[cs, ss]): ss = ⊥
  262.  
  263. // :type solutions(⊥[Cubes])
  264.  
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement