Advertisement
Guest User

Untitled

a guest
Oct 6th, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {-# Language UndecidableInstances,MultiParamTypeClasses,TypeOperators , DataKinds,GADTs,TypeFamilies,TypeFamilyDependencies#-}
  2. {-
  3. fold a hlist of kliesli types
  4. passing previous return type into next input
  5. needs a hlist type with adjacently coupled types...
  6. this can be generated by a zipWIth . tail pattern
  7. -}
  8.  
  9. {-
  10. eg = -- cant really use list, intersperse with Xcons instead
  11. [\int        
  12.   -> return $ show int
  13. ,\string
  14.   -> return $ (reverse string,read string)
  15. ,\(string,int)
  16.   -> return $ replicate int string
  17. ,\strings
  18.   -> return $ concat strings
  19.  ]
  20. eg :: HList (fmap (Uncurry . fmap IO) (Stagger xs))
  21. type xs = '[Int,String,(String,Int),[String],String]
  22.  
  23. then have some function to fold this up into an Int -> IO String
  24. -}
  25.  
  26. --foldl (>>=) :: (Foldable t, Monad m) => m a -> t (a -> m a) -> m a
  27. -- a -> m b,b -> m c etc
  28.  
  29. infixr 6 :-:
  30.  
  31. data HList xs where
  32.  HEmpty :: HList '[]
  33. (:-:)  :: x -> HList xs -> HList (x ': xs)
  34.  
  35. type XList m xs = HList (FX m xs)
  36.  
  37. -- should really use Map, but would have to defunctionalise and write Uncurry...
  38. type family FX (m :: * -> *) (xs :: [*]) = (ys :: [*]) | ys -> m xs where
  39. -- FX m (x ': y ': '[]) = '[x -> m y]
  40. -- FX m (x ': y ':  xs) =  (x -> m y) ': FX m (y ': xs)
  41.  
  42. eg :: Monad m => XList m '[Int,String,(String,Int),[String],String]
  43. eg =     (\int        
  44.   -> return $ show int)
  45. :-: (\string
  46.   -> return $ (reverse string,read string))
  47. :-: (\(string,int)
  48.  -> return $ replicate int string)
  49. :-: (\strings
  50.  -> return $ concat strings)
  51. :-: HEmpty
  52.  
  53. eg1 :: XList IO '[Int,String,Bool]
  54. eg1 = ((return . show) :: Int -> IO String) :-: ((return . null) :: String -> IO Bool) :-: HEmpty
  55.  
  56. type family Head xs where
  57.  Head (x ': _) = x
  58.  
  59. type family Last xs where
  60. Last (x ': '[]) = x
  61. Last (x ': xs) = Last xs
  62.  
  63. {-
  64. class RunXList m xs where
  65.  runXList :: XList m xs -> (Head xs -> m (Last xs))
  66.  
  67.     * Couldn't match type `Head xs' with `Head xs0'
  68.       Expected type: XList m xs -> Head xs -> m (Last xs)
  69.         Actual type: XList m xs0 -> Head xs0 -> m (Last xs0)
  70.       NB: `Head' is a non-injective type family
  71.       The type variable `xs0' is ambiguous
  72.     * In the ambiguity check for `runXList'
  73.       To defer the ambiguity check to use sites, enable AllowAmbiguousTypes
  74.       When checking the class method:
  75.         runXList :: forall (m :: * -> *) (xs :: [*]).
  76.                     RunXList m xs =>
  77.                     XList m xs -> Head xs -> m (Last xs)
  78.       In the class declaration for `RunXList'
  79.    |
  80. 64 |  runXList :: XList m xs -> (Head xs -> m (Last xs))
  81.    |  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  82. runXList (x :-: HEmpty) = x
  83. runXList (x :-: xs) = x >>= (runXList xs)
  84. -}
  85.  
  86. -- should really use nonempty list, eg. FX2 basecase
  87.  
  88. infixr 6 :--:
  89.  
  90. data HList2 (zs :: (*,[*],*)) where
  91.  HLast2 :: (i -> o) -> HList2 '(i , '[] , o)
  92.  (:--:)  :: i -> HList2 '(i' , xs , o) -> HList2 '(i, (i ': xs), o)
  93.  
  94. type XList2 m i xs o = HList2 (FX2 m i xs o)
  95.  
  96. type family FX2 m i (xs :: [*]) o where
  97.  FX2 m i '[] o = '(i , '[] , m o) -- '[i -> m o]
  98.  FX2 m i (x ': xs) o = InsertFX2 (i -> m x) (FX2 m x xs o)
  99.  
  100. type family InsertFX2 f xs where
  101. InsertFX2 f '(i,xs,o) = '(i,f ':xs,o)
  102.  
  103. {-
  104. eg2 :: Monad m => XList2 m Int '[String,(String,Int),[String]] String
  105. eg2 =     (\int        
  106.    -> return $ show int)
  107.  :--: (\string
  108.    -> return $ (reverse string,read string))
  109.  :--: (\(string,int)
  110.   -> return $ replicate int string)
  111.  (HLast2 (\strings -> return $ concat strings))
  112. -}
  113.  
  114. eg2 :: Monad m => XList2 m [String] '[] String
  115. eg2 = (\strings -> return $ concat strings) :-: HEmpty
  116.  
  117. class RunXList m xs where
  118. runXList :: XList2 m i xs o -> (i -> m o)
  119.  
  120. {-
  121.    * Couldn't match type `FX2 m i xs o' with `FX2 m i xs0 o'
  122.       Expected type: XList2 m i xs o -> i -> m o
  123.         Actual type: XList2 m i xs0 o -> i -> m o
  124.       NB: `FX2' is a non-injective type family
  125.      The type variable `xs0' is ambiguous
  126.     * In the ambiguity check for `runXList'
  127.      To defer the ambiguity check to use sites, enable AllowAmbiguousTypes
  128.      When checking the class method:
  129.        runXList :: forall (m :: * -> *) (xs :: [*]) i o.
  130.                    RunXList m xs =>
  131.                    XList2 m i xs o -> i -> m o
  132.      In the class declaration for `RunXList'
  133.     |
  134. 118 |  runXList :: XList2 m i xs o -> (i -> m o)
  135.     |  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  136. -}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement