Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {-# Language
  2.  DataKinds,
  3.  TypeOperators,
  4.  PolyKinds,
  5.  TypeFamilies,
  6.  UndecidableInstances,
  7.  GADTs
  8. #-}
  9.  
  10. data Nat = Z | S Nat
  11.            deriving (Show, Eq, Ord)
  12.  
  13. data SNat (n :: Nat) where
  14.   SZ :: SNat Z
  15.   SS :: SNat n -> SNat (S n)
  16.  
  17. sZ :: SNat Z
  18. sZ = case singInstance SZ of
  19.        SingInstance -> SZ
  20.  
  21. sS :: SNat n -> SNat (S n)
  22. sS n = case singInstance n of
  23.          SingInstance -> SS n
  24.  
  25. --
  26.  
  27. class Sing n where
  28.   sing :: SNat n
  29.  
  30. instance Sing Z where
  31.   sing = SZ
  32.  
  33. instance Sing n => Sing (S n) where
  34.   sing = SS sing
  35.  
  36. data SingInstance a where
  37.   SingInstance :: Sing a => SingInstance a
  38.  
  39. singInstance :: SNat n -> SingInstance n
  40. singInstance SZ = SingInstance
  41. singInstance (SS n) =
  42.     case singInstance n of
  43.       SingInstance -> SingInstance
  44.  
  45. --
  46.  
  47. type family Head (xs :: [a]) :: a where
  48.  Head (x ': xs) = x
  49.  
  50. type family Drop (n :: Nat) (xs :: [a]) :: [a] where
  51. Drop (S n) (x ': xs) = Drop n xs
  52.  Drop Z xs = xs
  53.  
  54. type family (!!) (xs :: [a]) (n :: Nat) :: a where
  55.  (!!) xs n = Head (Drop n xs)
  56.  
  57. type SumType (a :: [*]) = (n @ SNat,a !! n)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement