Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
115
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.  RankNTypes
  9. #-}
  10.  
  11. data Nat = Z | S Nat
  12.            deriving (Show, Eq, Ord)
  13.  
  14. data SNat (n :: Nat) where
  15.   SZ :: SNat Z
  16.   SS :: SNat n -> SNat (S n)
  17.  
  18. sZ :: SNat Z
  19. sZ = case singInstance SZ of
  20.        SingInstance -> SZ
  21.  
  22. sS :: SNat n -> SNat (S n)
  23. sS n = case singInstance n of
  24.          SingInstance -> SS n
  25.  
  26. --
  27.  
  28. class Sing n where
  29.   sing :: SNat n
  30.  
  31. instance Sing Z where
  32.   sing = SZ
  33.  
  34. instance Sing n => Sing (S n) where
  35.   sing = SS sing
  36.  
  37. data SingInstance a where
  38.   SingInstance :: Sing a => SingInstance a
  39.  
  40. singInstance :: SNat n -> SingInstance n
  41. singInstance SZ = SingInstance
  42. singInstance (SS n) =
  43.     case singInstance n of
  44.       SingInstance -> SingInstance
  45.  
  46. --
  47.  
  48. type family Head (xs :: [a]) :: a where
  49.  Head (x ': xs) = x
  50.  
  51. type family Drop (n :: Nat) (xs :: [a]) :: [a] where
  52. Drop (S n) (x ': xs) = Drop n xs
  53.  Drop Z xs = xs
  54.  
  55. type family (!!) (xs :: [a]) (n :: Nat) :: a where
  56.  (!!) xs n = Head (Drop n xs)
  57.  
  58. type family Elem (x :: a) (xs :: [a]) :: Bool where
  59.  Elem _ '[] = 'False
  60.  Elem x (x ': _) = 'True
  61.  Elem x (_ ': xs) = Elem x xs
  62.  
  63. data Proxy n = Proxy
  64.  
  65. data SumType (ls :: [*]) where
  66.  SumType :: xs !! s ~ x => SNat s -> x -> Proxy xs -> SumType xs
  67.  
  68. type Eg = [Bool,Int,Bool]
  69.  
  70. eg1 :: SumType Eg
  71. eg1 = SumType SZ False (Proxy :: Proxy Eg)
  72.  
  73. eg2 :: SumType Eg
  74. eg2 = SumType (SS SZ) 1 (Proxy :: Proxy Eg)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement