daily pastebin goal
48%
SHARE
TWEET

Untitled

a guest Aug 10th, 2018 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Polymorphic Return Types Depending on Context
  2. get :: (RedisValue a) => Key -> Redis a
  3. get k = decodeValue <$> sendCommand ["GET", key]
  4.    
  5. data Redis a = ...
  6.  
  7. instance MonadIO Redis where ...
  8. instance Monad Redis where ...
  9.  
  10. sendCommand :: [String] -> Redis String
  11.  
  12. class RedisValue a where
  13.     decodeValue :: String -> a
  14.  
  15. -- example instances
  16. instance RedisValue String where ...
  17. instance RedisValue ByteString where ...
  18.    
  19. get :: (RedisStatus a) => Key -> RedisTransaction a
  20. get k = decodeStatus <$> sendCommand ["GET", key]
  21.    
  22. class RedisGet m a where
  23.     get :: Key -> m a
  24.  
  25. instance (RedisValue a) => RedisGet Redis a where...
  26.  
  27. instance (RedisStatus a) => RedisGet RedisTransaction a where...
  28.    
  29. {-# LANGUAGE GeneralizedNewtypeDeriving #-}
  30. {-# LANGUAGE FlexibleInstances          #-}
  31. {-# LANGUAGE FunctionalDependencies     #-}
  32. {-# LANGUAGE MultiParamTypeClasses      #-}
  33.  
  34. newtype Redis a = Redis (IO a) deriving Monad
  35. newtype RedisTransaction a = RedisTransaction (IO a) deriving Monad
  36.  
  37. newtype Key    = Key {unKey :: String}
  38. newtype Value  = Value {unValue :: String}
  39. newtype Status = Status {unStatus :: String}
  40.  
  41. class Monad m => RedisMonad m a | m -> a where
  42.   sendCommand :: [String] -> m a
  43.  
  44. instance RedisMonad Redis Value where
  45.   sendCommand = undefined -- TODO: provide implementation                      
  46.  
  47. instance RedisMonad RedisTransaction Status where
  48.   sendCommand = undefined -- TODO: provide implementation                      
  49.  
  50. class Decodable a b where
  51.   decode :: a -> b
  52.  
  53. instance Decodable Status String where
  54.   decode = unStatus
  55.  
  56. instance Decodable Value String where
  57.   decode = unValue
  58.  
  59. get :: (RedisMonad m a, Decodable a b) => Key -> m b
  60. get k = do
  61.   response <- sendCommand ["GET", unKey k]
  62.   return (decode response)
  63.    
  64. get :: (ContextValue (f a)) => Key -> f a
  65.  
  66. class ContextValue a
  67. instance (RedisValue a) => ContextValue (Redis a)
  68. instance (RedisStatus a) => ContextValue (RedisTransaction a)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top