Skip to content

Instantly share code, notes, and snippets.

@qnikst
Created July 2, 2015 09:07
Show Gist options
  • Save qnikst/6a4e4693e817a48ac650 to your computer and use it in GitHub Desktop.
Save qnikst/6a4e4693e817a48ac650 to your computer and use it in GitHub Desktop.
typelevel sort
type Sort a = SortI a '[]
type family SortI a b where
SortI '[] b = b
SortI (a:?n ': as) '[] = SortI as '[ a :? n]
SortI (a:?n ': as) bs = SortI as (Insert (a:?n) bs)
type family Insert a b where
Insert (a :? n) '[] = '[ a :? n ]
Insert (a :? n) ( b :? m ': bs) = UnOrdering (CmpSymbol n m)
((a :? n) ': (b :? m) ': bs)
((a :? n) ': (b :? m) ': bs)
((b :? m) ': Insert (a :? n) bs)
type family UnOrdering x a b c where
UnOrdering 'LT a b c = a
UnOrdering 'EQ a b c = b
UnOrdering 'GT a b c = c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment