spacepaste

  1.  
  2. {-# LANGUAGE GADTs #-}
  3. {-# LANGUAGE FlexibleInstances #-}
  4. {-# LANGUAGE TypeFamilyDependencies #-}
  5. {-# LANGUAGE ScopedTypeVariables #-}
  6. {-# LANGUAGE MultiParamTypeClasses #-}
  7. {-# LANGUAGE PolyKinds #-}
  8. {-# LANGUAGE RankNTypes #-}
  9. {-# LANGUAGE ExistentialQuantification #-}
  10. {-# LANGUAGE KindSignatures #-}
  11. {-# LANGUAGE DataKinds #-}
  12. {-# LANGUAGE TypeOperators #-}
  13. {-# LANGUAGE TypeFamilies #-}
  14. {-# LANGUAGE UndecidableInstances #-}
  15. {-# LANGUAGE ConstraintKinds #-}
  16. {-# LANGUAGE TypeApplications #-}
  17. import Data.Proxy
  18. import GHC.TypeLits
  19. import Data.Constraint
  20. import Unsafe.Coerce
  21. data Box (xs::[*]) = forall n a . (KnownNat n, Elem n xs ~ a) => Box (Proxy n) a
  22. type family Elem (n::Nat) (xs::[k]) = result where
  23. Elem 0 (a ': _) = a
  24. Elem n (a ': as) = Elem (n-1) as
  25. type family ForAll (cxt :: * -> Constraint) (xs :: [k]) :: Constraint where
  26. ForAll ctx '[] = ()
  27. ForAll ctx (x ': xs) = (ctx x, ForAll ctx xs)
  28. data CBox ctx (xs::[*]) where
  29. Z :: CBox ctx '[]
  30. S :: Dict (ctx a) -> CBox ctx as -> CBox ctx (a ': as)
  31. class MkCBox ctx xs where mkCB :: Proxy ctx -> Proxy xs -> CBox ctx xs
  32. instance MkCBox ctx '[] where mkCB _ _ = Z
  33. instance (ctx x, MkCBox ctx xs) => MkCBox ctx (x ': xs) where mkCB ctx p = S Dict (mkCB ctx (Proxy :: Proxy xs))
  34. ithCBox :: forall ctx xs n a . (KnownNat n, Elem n xs ~ a) => Proxy n -> CBox ctx xs -> Dict (ctx a)
  35. ithCBox pn cb = go i cb
  36. where i = natVal pn
  37. go :: Integer -> CBox ctx zs -> Dict (ctx a)
  38. go 0 (S d _) = unsafeCoerce d
  39. go i (S _ r) = go (i-1) r
  40. go _ _ = error "Wooooo!"
  41. applyCBox :: Proxy ctx -> CBox ctx xs -> Box xs -> (forall a. ctx a => a -> t) -> t
  42. applyCBox _ cbox box f = case box of
  43. Box p x -> case ithCBox p cbox of
  44. Dict -> f x
  45. withBox :: (MkCBox ctx xs, ForAll ctx xs) => Proxy ctx -> Box xs -> (forall a. ctx a => a -> t) -> t
  46. withBox ctx box = applyCBox ctx cbox box where cbox = mkCB ctx (Proxy :: Proxy xs)
  47. {-
  48. type family ForAll1 (cxt :: * -> Constraint) (xs :: [k]) :: [Constraint] where
  49. ForAll1 ctx '[] = '[]
  50. ForAll1 ctx (x ': xs) = (ctx x) ': (ForAll1 ctx xs)
  51. type family ForAll2 (cxt :: * -> Constraint) (xs :: [k]) :: [(*,Constraint)] where
  52. ForAll2 ctx '[] = '[]
  53. ForAll2 ctx (x ': xs) = '(x, ctx x) ': (ForAll2 ctx xs)
  54. class MkD (c::Constraint) a where mkD :: proxy a -> Dict c
  55. instance ctx => MkD ctx (a ': as) where mkD _ = Dict
  56. instance MkD (ctx a) as => MkD (ctx a) (b ': as) where mkD p = mkD (Proxy :: Proxy as)
  57. class MkD (ctx a) a => A (ctx :: * -> Constraint) (a :: *)
  58. instance MkD (ctx a) a => A ctx a
  59. -}
  60. {-
  61. q :: forall ctx xs t. ForAll (A ctx) xs => Proxy (ctx :: * -> Constraint) -> Box xs -> (forall a. ctx a => a -> t) -> t
  62. q pctx box f = case box of
  63. Box (n::Proxy n) (x::a) -> case mkD (Proxy :: Proxy (Elem n xs)) of
  64. (Dict :: Dict (ctx a)) -> f x
  65. -}
  66. {-
  67. t :: Proxy xs -> ctx -> (Proxy n -> (foral a . ctx a => a -> t) -> t)
  68. t
  69. -}
  70. {-
  71. class Has ctx x xs
  72. instance ctx x => Has ctx x (x ':xs)
  73. instance Has ctx x xs => Has ctx x (y ':xs)
  74. -}
  75. {-
  76. q :: (ForAll ctx xs, Elem (n::Nat) xs ~ a, Has ctx a xs) => Proxy ctx -> Proxy xs -> Proxy n -> Proxy a -> Dict (ctx a)
  77. q _ _ _ _ = Dict
  78. -}
  79. p :: ((a ': as) ~ xs, ForAll ctx xs) => Proxy ctx -> Proxy xs -> Dict (ctx a)
  80. p _ _ = Dict
  81.