@@ -137,7 +137,6 @@ import Data.List
137137import Data.Proxy
138138import Data.Void
139139import GHC.TypeLits
140−import Data.Type.Bool
141140import Data.Type.Equality
142141import GHC.Generics
143142
@@ -168,13 +167,15 @@ type family Count (d :: Type) where
168167 Count d = Count (Rep d R)
169168
170169-- | Type family that computes of a given type dimension from a given natural
170+-- Thanks to Li-Yao Xia this type family is super fast.
171171type family FromNat (n :: Nat) where
172172 FromNat 0 = Void
173173 FromNat 1 = ()
174− FromNat n = If (Mod n 2 == 0)
175− (Either (FromNat (Div n 2)) (FromNat (Div n 2)))
176− (Either () (FromNat (n - 1)))
177− -- Either () (FromNat (n - 1))
174+ FromNat n = FromNat' (Mod n 2 == 0) (FromNat (Div n 2))
175+
176+type family FromNat' (b :: Bool) (m :: Type) :: Type where
177+ FromNat' 'True m = Either m m
178+ FromNat' 'False m = Either () (Either m m)
178179
179180-- | Type family that normalizes the representation of a given data
180181-- structure
@@ -182,10 +183,12 @@ type family Normalize (d :: Type) where
182183 Normalize d = FromNat (Count d)
183184
184185instance Eq e => Eq (Matrix e cols rows) where
185− Empty == Empty = True
186− (One a) == (One b) = a == b
187− (Junc a b) == (Junc c d) = a == c && b == d
188− (Split a b) == (Split c d) = a == c && b == d
186+ Empty == Empty = True
187+ (One a) == (One b) = a == b
188+ (Junc a b) == (Junc c d) = a == c && b == d
189+ (Split a b) == (Split c d) = a == c && b == d
190+ x@(Split a b) == y@(Junc c d) = x == abideJS y
191+ x@(Junc a b) == y@(Split c d) = abideJS x == y
189192
190193instance Num e => Num (Matrix e cols rows) where
191194
@@ -268,7 +271,7 @@ instance {-# OVERLAPPING #-} FromLists e () () where
268271 fromLists [[e]] = One e
269272 fromLists _ = error "Wrong dimensions"
270273
271−instance (FromLists e cols ()) => FromLists e (Either () cols) () where
274+instance {-# OVERLAPPING #-} (FromLists e cols ()) => FromLists e (Either () cols) () where
272275 fromLists [h : t] = Junc (One h) (fromLists [t])
273276 fromLists _ = error "Wrong dimensions"
274277
@@ -278,7 +281,7 @@ instance {-# OVERLAPPABLE #-} (FromLists e a (), FromLists e b (), KnownNat (Cou
278281 in Junc (fromLists [take rowsA l]) (fromLists [drop rowsA l])
279282 fromLists _ = error "Wrong dimensions"
280283
281−instance (FromLists e () rows) => FromLists e () (Either () rows) where
284+instance {-# OVERLAPPING #-} (FromLists e () rows) => FromLists e () (Either () rows) where
282285 fromLists ([h] : t) = Split (One h) (fromLists t)
283286 fromLists _ = error "Wrong dimensions"
284287