Guest User

Untitled

a guest
May 25th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. {-# LANGUAGE AllowAmbiguousTypes #-}
  2. {-# LANGUAGE CPP #-}
  3. {-# LANGUAGE FlexibleContexts #-}
  4. {-# LANGUAGE FlexibleInstances #-}
  5. {-# LANGUAGE GADTs #-}
  6. {-# LANGUAGE RankNTypes #-}
  7. {-# LANGUAGE ScopedTypeVariables #-}
  8. {-# LANGUAGE TypeApplications #-}
  9. {-# LANGUAGE TypeFamilyDependencies #-}
  10. {-# LANGUAGE TypeInType #-}
  11. {-# LANGUAGE TypeOperators #-}
  12. module DependentComposition where
  13.  
  14. import Data.Kind
  15. import Data.Proxy
  16.  
  17. data family Sing (a :: k)
  18. data SomeSing :: Type -> Type where
  19. SomeSing :: Sing (a :: k) -> SomeSing k
  20.  
  21. class SingKind k where
  22. type Demote k = (r :: Type) | r -> k
  23. fromSing :: Sing (a :: k) -> Demote k
  24. toSing :: Demote k -> SomeSing k
  25.  
  26. withSomeSing :: forall k r
  27. . SingKind k
  28. => Demote k
  29. -> (forall (a :: k). Sing a -> r)
  30. -> r
  31. withSomeSing x f =
  32. case toSing x of
  33. SomeSing x' -> f x'
  34.  
  35. newtype instance Sing (f :: k1 ~> k2) =
  36. SLambda { applySing :: forall t. Sing t -> Sing (f @@ t) }
  37.  
  38. instance (SingKind k1, SingKind k2) => SingKind (k1 ~> k2) where
  39. type Demote (k1 ~> k2) = Demote k1 -> Demote k2
  40. fromSing sFun x = withSomeSing x (fromSing . applySing sFun)
  41. toSing = undefined
  42.  
  43. data TyFun :: Type -> Type -> Type
  44. type a ~> b = TyFun a b -> Type
  45. infixr 0 ~>
  46.  
  47. type family Apply (f :: k1 ~> k2) (x :: k1) :: k2
  48. type f @@ x = Apply f x
  49. infixl 9 @@
  50.  
  51. dapply1 :: forall (a :: Type)
  52. (b :: a ~> Type)
  53. (x :: a).
  54. (forall (x :: a). b @@ x)
  55. -> Sing x
  56. -> b @@ x
  57. dapply1 f _ = f @x
  58.  
  59. dapply2 :: forall (a :: Type)
  60. (b :: a ~> Type)
  61. (f :: forall (x :: a). Proxy x ~> b @@ x)
  62. (x :: a).
  63. SingKind (b @@ x)
  64. => (forall (xx :: a). Sing (f @@ ('Proxy :: Proxy xx)))
  65. -> Sing x
  66. -> Demote (b @@ x)
  67. dapply2 sf _ = fromSing $ sf @x
  68.  
  69. dcomp1 :: forall (a :: Type)
  70. (b :: a ~> Type)
  71. (c :: forall (x :: a). Proxy x ~> b @@ x ~> Type)
  72. (g :: forall (x :: a). Proxy x ~> b @@ x)
  73. (x :: a).
  74. (forall (xx :: a) (yy :: b @@ xx). c @@ ('Proxy :: Proxy xx) @@ yy)
  75. -> (forall (xx :: a). Sing (g @@ ('Proxy :: Proxy xx)))
  76. -> Sing x
  77. -> c @@ ('Proxy :: Proxy x) @@ (g @@ ('Proxy :: Proxy x))
  78. dcomp1 f _ _ = f @x @(g @@ ('Proxy :: Proxy x))
  79.  
  80. #if __GLASGOW_HASKELL__ >= 805
  81. dcomp2 :: forall (a :: Type)
  82. (b :: a ~> Type)
  83. (c :: forall (x :: a). Proxy x ~> b @@ x ~> Type)
  84. (f :: forall (x :: a) (y :: b @@ x). Proxy x ~> Proxy y ~> c @@ ('Proxy :: Proxy x) @@ y)
  85. (g :: forall (x :: a). Proxy x ~> b @@ x)
  86. (x :: a).
  87. SingKind (c @@ ('Proxy :: Proxy x) @@ (g @@ ('Proxy :: Proxy x)))
  88. => (forall (xx :: a) (yy :: b @@ xx). Sing (f @@ ('Proxy :: Proxy xx) @@ ('Proxy :: Proxy yy)))
  89. -> (forall (xx :: a). Sing (g @@ ('Proxy :: Proxy xx)))
  90. -> Sing x
  91. -> Demote (c @@ ('Proxy :: Proxy x) @@ (g @@ ('Proxy :: Proxy x)))
  92. dcomp2 sf _ _ = fromSing $ sf @x @(g @@ ('Proxy :: Proxy x))
  93. #endif
Add Comment
Please, Sign In to add comment