Skip to content

Commit ff2d676

Browse files
committed
fix: Remove exponential type level if statement.
1 parent 8d30680 commit ff2d676

File tree

1 file changed

+41
-33
lines changed

1 file changed

+41
-33
lines changed

src/DataFrame/Typed/Schema.hs

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ import GHC.TypeLits
6666
import Type.Reflection (SomeTypeRep, Typeable, someTypeRep)
6767

6868
import DataFrame.Internal.Column (Columnable)
69-
import DataFrame.Internal.Types (If)
7069
import DataFrame.Typed.Types (Column)
7170

7271
-- | Look up the element type of a column by name.
@@ -121,10 +120,11 @@ type family SubsetSchema (names :: [Symbol]) (cols :: [Type]) :: [Type] where
121120
type family ExcludeSchema (names :: [Symbol]) (cols :: [Type]) :: [Type] where
122121
ExcludeSchema names '[] = '[]
123122
ExcludeSchema names (Column n a ': rest) =
124-
If
125-
(IsElem n names)
126-
(ExcludeSchema names rest)
127-
(Column n a ': ExcludeSchema names rest)
123+
ExcludeSchemaHelper (IsElem n names) n a names rest
124+
125+
type family ExcludeSchemaHelper (found :: Bool) (n :: Symbol) (a :: Type) (names :: [Symbol]) (rest :: [Type]) :: [Type] where
126+
ExcludeSchemaHelper 'True n a names rest = ExcludeSchema names rest
127+
ExcludeSchemaHelper 'False n a names rest = Column n a ': ExcludeSchema names rest
128128

129129
-- | Type-level elem for Symbols
130130
type family IsElem (x :: Symbol) (xs :: [Symbol]) :: Bool where
@@ -197,14 +197,14 @@ type family
197197
-- | Assert that a column name is present in the schema.
198198
type family AssertAllPresent (name :: [Symbol]) (cols :: [Type]) :: Constraint where
199199
AssertAllPresent (name ': rest) cols =
200-
If
201-
(HasName name cols)
202-
(AssertAllPresent rest cols)
203-
( TypeError
204-
('Text "Column '" ':<>: 'Text name ':<>: 'Text "' not found in schema")
205-
)
200+
AssertAllPresentHelper (HasName name cols) name rest cols
206201
AssertAllPresent '[] cols = ()
207202

203+
type family AssertAllPresentHelper (found :: Bool) (name :: Symbol) (rest :: [Symbol]) (cols :: [Type]) :: Constraint where
204+
AssertAllPresentHelper 'True name rest cols = AssertAllPresent rest cols
205+
AssertAllPresentHelper 'False name rest cols =
206+
TypeError ('Text "Column '" ':<>: 'Text name ':<>: 'Text "' not found in schema")
207+
208208
{- | Strip 'Maybe' from all columns. Used by 'filterAllJust'.
209209
210210
@Column "x" (Maybe Double)@ becomes @Column "x" Double@.
@@ -232,13 +232,21 @@ type family StripMaybeAt (name :: Symbol) (cols :: [Type]) :: [Type] where
232232
type family SharedNames (left :: [Type]) (right :: [Type]) :: [Symbol] where
233233
SharedNames '[] right = '[]
234234
SharedNames (Column n _ ': rest) right =
235-
If (HasName n right) (n ': SharedNames rest right) (SharedNames rest right)
235+
SharedNamesHelper (HasName n right) n rest right
236+
237+
type family SharedNamesHelper (found :: Bool) (n :: Symbol) (rest :: [Type]) (right :: [Type]) :: [Symbol] where
238+
SharedNamesHelper 'True n rest right = n ': SharedNames rest right
239+
SharedNamesHelper 'False n rest right = SharedNames rest right
236240

237241
-- | Columns from @left@ whose names do NOT appear in @right@.
238242
type family UniqueLeft (left :: [Type]) (rightNames :: [Symbol]) :: [Type] where
239243
UniqueLeft '[] _ = '[]
240244
UniqueLeft (Column n a ': rest) rn =
241-
If (IsElem n rn) (UniqueLeft rest rn) (Column n a ': UniqueLeft rest rn)
245+
UniqueLeftHelper (IsElem n rn) n a rest rn
246+
247+
type family UniqueLeftHelper (found :: Bool) (n :: Symbol) (a :: Type) (rest :: [Type]) (rn :: [Symbol]) :: [Type] where
248+
UniqueLeftHelper 'True n a rest rn = UniqueLeft rest rn
249+
UniqueLeftHelper 'False n a rest rn = Column n a ': UniqueLeft rest rn
242250

243251
-- | Wrap column types in Maybe.
244252
type family WrapMaybe (cols :: [Type]) :: [Type] where
@@ -249,23 +257,26 @@ type family WrapMaybe (cols :: [Type]) :: [Type] where
249257
type family WrapMaybeColumns (names :: [Symbol]) (cols :: [Type]) :: [Type] where
250258
WrapMaybeColumns names '[] = '[]
251259
WrapMaybeColumns names (Column n a ': rest) =
252-
If
253-
(IsElem n names)
254-
(Column n (Maybe a) ': WrapMaybeColumns names rest)
255-
(Column n a ': WrapMaybeColumns names rest)
260+
WrapMaybeColumnsHelper (IsElem n names) n a names rest
261+
262+
type family WrapMaybeColumnsHelper (found :: Bool) (n :: Symbol) (a :: Type) (names :: [Symbol]) (rest :: [Type]) :: [Type] where
263+
WrapMaybeColumnsHelper 'True n a names rest = Column n (Maybe a) ': WrapMaybeColumns names rest
264+
WrapMaybeColumnsHelper 'False n a names rest = Column n a ': WrapMaybeColumns names rest
256265

257266
-- | Columns in left whose names collide with right (excluding keys).
258267
type family CollidingColumns (left :: [Type]) (right :: [Type]) (keys :: [Symbol]) :: [Type] where
259268
CollidingColumns '[] _ _ = '[]
260269
CollidingColumns (Column n a ': rest) right keys =
261-
If
262-
(IsElem n keys)
263-
(CollidingColumns rest right keys)
264-
( If
265-
(HasName n right)
266-
(Column n (These a (Lookup n right)) ': CollidingColumns rest right keys)
267-
(CollidingColumns rest right keys)
268-
)
270+
CollidingColumnsHelper1 (IsElem n keys) n a rest right keys
271+
272+
type family CollidingColumnsHelper1 (isKey :: Bool) (n :: Symbol) (a :: Type) (rest :: [Type]) (right :: [Type]) (keys :: [Symbol]) :: [Type] where
273+
CollidingColumnsHelper1 'True n a rest right keys = CollidingColumns rest right keys
274+
CollidingColumnsHelper1 'False n a rest right keys =
275+
CollidingColumnsHelper2 (HasName n right) n a rest right keys
276+
277+
type family CollidingColumnsHelper2 (inRight :: Bool) (n :: Symbol) (a :: Type) (rest :: [Type]) (right :: [Type]) (keys :: [Symbol]) :: [Type] where
278+
CollidingColumnsHelper2 'True n a rest right keys = Column n (These a (Lookup n right)) ': CollidingColumns rest right keys
279+
CollidingColumnsHelper2 'False n a rest right keys = CollidingColumns rest right keys
269280

270281
-- | Inner join result schema.
271282
type family InnerJoinSchema (keys :: [Symbol]) (left :: [Type]) (right :: [Type]) :: [Type] where
@@ -322,18 +333,15 @@ type family
322333
)
323334
)
324335

325-
-------------------------------------------------------------------------------
326-
-- GroupBy helpers
327-
-------------------------------------------------------------------------------
328-
329336
-- | Extract Column entries from a schema whose names appear in @keys@.
330337
type family GroupKeyColumns (keys :: [Symbol]) (cols :: [Type]) :: [Type] where
331338
GroupKeyColumns keys '[] = '[]
332339
GroupKeyColumns keys (Column n a ': rest) =
333-
If
334-
(IsElem n keys)
335-
(Column n a ': GroupKeyColumns keys rest)
336-
(GroupKeyColumns keys rest)
340+
GroupKeyColumnsHelper (IsElem n keys) n a keys rest
341+
342+
type family GroupKeyColumnsHelper (found :: Bool) (n :: Symbol) (a :: Type) (keys :: [Symbol]) (rest :: [Type]) :: [Type] where
343+
GroupKeyColumnsHelper 'True n a keys rest = Column n a ': GroupKeyColumns keys rest
344+
GroupKeyColumnsHelper 'False n a keys rest = GroupKeyColumns keys rest
337345

338346
-- | Provides runtime evidence of a schema: a list of (name, TypeRep) pairs.
339347
class KnownSchema (cols :: [Type]) where

0 commit comments

Comments
 (0)