Advertisement
Guest User

Untitled

a guest
Oct 5th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {-# Language MultiParamTypeClasses,TypeOperators , DataKinds,GADTs,TypeFamilies#-}
  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. --foldl (>>=) :: (Foldable t, Monad m) => m a -> t (a -> m a) -> m a
  10. -- a -> m b,b -> m c etc
  11.  
  12. infixr 6 :-:
  13.  
  14. data HList xs where
  15.  HEmpty :: HList '[]
  16. (:-:)  :: x -> HList xs -> HList (x ': xs)
  17.  
  18. type XList m xs = HList (FX m xs)
  19.  
  20. -- should really use Map, but would have to defunctionalise and write Uncurry...
  21. type family FX m (xs :: [*]) :: [*] where
  22.  FX m (x ': y ': '[]) = '[x -> m y]
  23.  FX m (x ': y ':  xs) =  (x -> m y) ': FX m (y ': xs)
  24.  
  25. eg :: Monad m => XList m '[Int,String,(String,Int),[String],String]
  26. eg = (\int        
  27.  -> return $ show int)
  28. :-: (\string
  29.  -> return $ (reverse string,read string))
  30. :-: (\(string,int)
  31.  -> return $ replicate int string)
  32. :-: (\strings
  33.  -> return $ concat strings)
  34. :-: HEmpty
  35.  
  36. type family Head xs where
  37. Head (x ': _) = x
  38.  
  39. type family Last xs where
  40.  Last (x ': '[]) = x
  41.  Last (x ': xs) = Last xs
  42.  
  43. class RunXList m xs where
  44. runXList :: XList m xs -> (Head xs -> m (Last xs))
  45. {-
  46. runXList (x :-: HEmpty) = x
  47. runXList (x :-: xs) = x >>= (runXList xs)
  48. -}
  49.  
  50. {-
  51.    * Couldn't match type `Head xs' with `Head xs0'
  52.       Expected type: XList m xs -> Head xs -> m (Last xs)
  53.         Actual type: XList m xs0 -> Head xs0 -> m (Last xs0)
  54.       NB: `Head' is a non-injective type family
  55.      The type variable `xs0' is ambiguous
  56.     * In the ambiguity check for `runXList'
  57.      To defer the ambiguity check to use sites, enable AllowAmbiguousTypes
  58.      When checking the class method:
  59.        runXList :: forall (m :: * -> *) (xs :: [*]).
  60.                    RunXList m xs =>
  61.                    XList m xs -> Head xs -> m (Last xs)
  62.      In the class declaration for `RunXList'
  63.    |
  64. 64 |  runXList :: XList m xs -> (Head xs -> m (Last xs))
  65.    |  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  66. -}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement