Advertisement
Guest User

Untitled

a guest
Jul 8th, 2015
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. {-# LANGUAGE DataKinds #-}
  2. {-# LANGUAGE KindSignatures #-}
  3. {-# LANGUAGE GADTs #-}
  4. module Main where
  5.  
  6. data UserType
  7. = VirtualUser
  8. | RealUser
  9.  
  10. data User (userType :: UserType) where
  11. MkVirtualUser :: String -> User 'VirtualUser
  12. MkRealUser :: String -> User 'RealUser
  13.  
  14. deliverPackage :: User 'RealUser -> String -> IO ()
  15. deliverPackage (MkRealUser name) package =
  16. putStrLn $ "Delivering package " ++ package ++ " for user " ++ name
  17.  
  18. sendEmail :: User 'VirtualUser -> String -> IO ()
  19. sendEmail (MkVirtualUser email) body =
  20. putStrLn $ "Sending email to " ++ email ++ " with body: \n" ++ body
  21.  
  22. userData :: User userType -> IO ()
  23. userData (MkRealUser name) = putStrLn $ "User with name: " ++ name
  24. userData (MkVirtualUser email) = putStrLn $ "User with email: " ++ email
  25.  
  26. main :: IO ()
  27. main = do
  28. let vuser = MkVirtualUser "romanandreg@gmail.com"
  29. ruser = MkRealUser "Roman Gonzalez"
  30.  
  31.  
  32. -- Compiles with all types of users
  33. userData vuser
  34. userData ruser
  35.  
  36. --------------------------------------------------------------------------------
  37. -- Compiles only with types of user @'RealUser@
  38.  
  39. deliverPackage ruser "foobar"
  40.  
  41. {-
  42. deliverPackage vuser "barfoo" -- <- Compile Error
  43. -- Couldn't match type ‘'VirtualUser’ with ‘'RealUser’
  44. -- Expected type: User 'RealUser
  45. -- Actual type: User 'VirtualUser
  46. -- In the first argument of ‘deliverPackage’, namely ‘vuser’
  47. -- In a stmt of a 'do' block: deliverPackage vuser "barfoo"
  48. -}
  49.  
  50. --------------------------------------------------------------------------------
  51. -- Compiles only with types of user @'VirtualUser@
  52.  
  53. sendEmail vuser "romanandreg@gmail.com"
  54.  
  55. {-
  56. sendEmail ruser "other@mail.com" -- <- Compile Error
  57.  
  58. -- Couldn't match type ‘'RealUser’ with ‘'VirtualUser’
  59. -- Expected type: User 'VirtualUser
  60. -- Actual type: User 'RealUser
  61. -- In the first argument of ‘sendEmail’, namely ‘ruser’
  62. -- In a stmt of a 'do' block: sendEmail ruser "other@mail.com"
  63. -}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement