Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. {-# LANGUAGE DataKinds #-}
  2. {-# LANGUAGE PolyKinds #-}
  3. {-# LANGUAGE ScopedTypeVariables #-}
  4. {-# LANGUAGE TypeFamilies #-}
  5. {-# LANGUAGE TypeOperators #-}
  6. {-# LANGUAGE UndecidableInstances #-}
  7. module Nub where
  8.  
  9. import Data.Proxy
  10. import Data.Type.Bool
  11. import GHC.TypeLits
  12.  
  13. -- | Check whether a type is a member of a list of types.
  14. -- This is a type-level analogue of @'elem'@.
  15. type family Elem x xs where
  16. Elem x '[] = 'False
  17. Elem x (x ': xs) = 'True
  18. Elem x (y ': xs) = Elem x xs
  19.  
  20. -- | Remove duplicates from a type-level list.
  21. type family Nub xs where
  22. Nub '[] = '[]
  23. Nub (x ': xs) = If (Elem x xs) (Nub xs) (x ': Nub xs)
  24.  
  25. class KnownNats ns where
  26. natVals :: proxy ns -> [Integer]
  27.  
  28. instance KnownNats '[] where
  29. natVals _ = []
  30.  
  31. instance (KnownNat n, KnownNats ns) => KnownNats (n ': ns) where
  32. natVals _ = natVal (Proxy :: Proxy n) : natVals (Proxy :: Proxy ns)
  33.  
  34. type Example = '[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 1, 1, 1, 1]
  35.  
  36. exampleVals :: [Integer]
  37. exampleVals = natVals (Proxy :: Proxy (Nub Example))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement