-
-
Save roelvandijk/f115c6b85a3961e1b689 to your computer and use it in GitHub Desktop.
Incredibly slow type-level Nub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE DataKinds #-} | |
{-# LANGUAGE PolyKinds #-} | |
{-# LANGUAGE ScopedTypeVariables #-} | |
{-# LANGUAGE TypeFamilies #-} | |
{-# LANGUAGE TypeOperators #-} | |
{-# LANGUAGE UndecidableInstances #-} | |
module Nub where | |
import Data.Proxy | |
import Data.Type.Bool | |
import GHC.TypeLits | |
-- | Remove duplicates from a type-level list. | |
type family Nub xs where | |
Nub '[] = '[] | |
Nub (x ': xs) = x ': Nub (Remove x xs) | |
type family Remove x xs where | |
Remove x '[] = '[] | |
Remove x (x ': ys) = Remove x ys | |
Remove x (y ': ys) = y ': Remove x ys | |
class KnownNats ns where | |
natVals :: proxy ns -> [Integer] | |
instance KnownNats '[] where | |
natVals _ = [] | |
instance (KnownNat n, KnownNats ns) => KnownNats (n ': ns) where | |
natVals _ = natVal (Proxy :: Proxy n) : natVals (Proxy :: Proxy ns) | |
type Example = '[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 1, 1, 1, 1] | |
exampleVals :: [Integer] | |
exampleVals = natVals (Proxy :: Proxy (Nub Example)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment