@@ -137,7 +137,6 @@ import Data.List
137import Data.Proxy
137import Data.Proxy
138import Data.Void
138import Data.Void
139import GHC.TypeLits
139import GHC.TypeLits
140−import Data.Type.Bool
141import Data.Type.Equality
140import Data.Type.Equality
142import GHC.Generics
141import GHC.Generics
@@ -168,13 +167,15 @@ type family Count (d :: Type) where
168 Count d = Count (Rep d R)
167 Count d = Count (Rep d R)
170-- | Type family that computes of a given type dimension from a given natural
169-- | 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.
171type family FromNat (n :: Nat) where
171type family FromNat (n :: Nat) where
172 FromNat 0 = Void
172 FromNat 0 = Void
173 FromNat 1 = ()
173 FromNat 1 = ()
174− FromNat n = If (Mod n 2 == 0)
174+ FromNat n = FromNat' (Mod n 2 == 0) (FromNat (Div n 2))
175− (Either (FromNat (Div n 2)) (FromNat (Div n 2)))
175+
176− (Either () (FromNat (n - 1)))
176+type family FromNat' (b :: Bool) (m :: Type) :: Type where
177− -- Either () (FromNat (n - 1))
177+ FromNat' 'True m = Either m m
178+ FromNat' 'False m = Either () (Either m m)
179-- | Type family that normalizes the representation of a given data
180-- | Type family that normalizes the representation of a given data
180-- structure
181-- structure
@@ -182,10 +183,12 @@ type family Normalize (d :: Type) where
182 Normalize d = FromNat (Count d)
183 Normalize d = FromNat (Count d)
184instance Eq e => Eq (Matrix e cols rows) where
185instance Eq e => Eq (Matrix e cols rows) where
185− Empty == Empty = True
186+ Empty == Empty = True
186− (One a) == (One b) = a == b
187+ (One a) == (One b) = a == b
187− (Junc a b) == (Junc c d) = a == c && b == d
188+ (Junc a b) == (Junc c d) = a == c && b == d
188− (Split a b) == (Split 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
190instance Num e => Num (Matrix e cols rows) where
193instance Num e => Num (Matrix e cols rows) where
@@ -268,7 +271,7 @@ instance {-# OVERLAPPING #-} FromLists e () () where
268 fromLists [[e]] = One e
271 fromLists [[e]] = One e
269 fromLists _ = error "Wrong dimensions"
272 fromLists _ = error "Wrong dimensions"
271−instance (FromLists e cols ()) => FromLists e (Either () cols) () where
274+instance {-# OVERLAPPING #-} (FromLists e cols ()) => FromLists e (Either () cols) () where
272 fromLists [h : t] = Junc (One h) (fromLists [t])
275 fromLists [h : t] = Junc (One h) (fromLists [t])
273 fromLists _ = error "Wrong dimensions"
276 fromLists _ = error "Wrong dimensions"
@@ -278,7 +281,7 @@ instance {-# OVERLAPPABLE #-} (FromLists e a (), FromLists e b (), KnownNat (Cou
278 in Junc (fromLists [take rowsA l]) (fromLists [drop rowsA l])
281 in Junc (fromLists [take rowsA l]) (fromLists [drop rowsA l])
279 fromLists _ = error "Wrong dimensions"
282 fromLists _ = error "Wrong dimensions"
281−instance (FromLists e () rows) => FromLists e () (Either () rows) where
284+instance {-# OVERLAPPING #-} (FromLists e () rows) => FromLists e () (Either () rows) where
282 fromLists ([h] : t) = Split (One h) (fromLists t)
285 fromLists ([h] : t) = Split (One h) (fromLists t)
283 fromLists _ = error "Wrong dimensions"
286 fromLists _ = error "Wrong dimensions"