Skip to content

Instantly share code, notes, and snippets.

@kcsongor
Last active December 10, 2017 18:19
Show Gist options
  • Save kcsongor/23eac1fe4e1202160243650677aa7111 to your computer and use it in GitHub Desktop.
Save kcsongor/23eac1fe4e1202160243650677aa7111 to your computer and use it in GitHub Desktop.
type family RowToList (r :: * -> *) :: [(Symbol, *)] where
RowToList (l :*: r)
= Merge (RowToList l) (RowToList r)
RowToList (S1 ('MetaSel ('Just name) _ _ _) (Rec0 a))
= '[ '(name, a)]
RowToList (M1 _ m a)
= RowToList a
RowToList _
= '[]
class Ord' t where
type Cmp (a :: t) (b :: t) :: Ordering
instance Ord' Symbol where
type Cmp a b = CmpSymbol a b
instance Ord' k => Ord' (k, v) where
type Cmp '(k1, _) '(k2, _) = Cmp k1 k2
type family Merge (xs :: [t]) (ys :: [t]) :: [t] where
Merge xs '[] = xs
Merge '[] ys = ys
Merge (x ': xs) (y ': ys) = Merge' (Cmp x y) x y xs ys
type family Merge' (ord :: Ordering) (x :: t) (y :: t) (xs :: [t]) (ys :: [t]) :: [t] where
Merge' 'LT x y xs ys = x ': Merge xs (y ': ys)
Merge' _ x y xs ys = y ': Merge (x ': xs) ys
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment