Guest User

Untitled

a guest
Aug 29th, 2020
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {-# LANGUAGE DataKinds #-}
  2. module Test where
  3.  
  4. import Data.WorldPeace (OpenUnion, openUnionLift, relaxOpenUnion)
  5. import Data.Bifunctor (first)
  6.  
  7. data ErrorOne = ErrorOne
  8. data ErrorTwo = ErrorTwo Int
  9. data ErrorThree = ErrorThree String
  10.  
  11. checkOne :: Int -> Either (OpenUnion '[ErrorOne]) Int
  12. checkOne x = case x of
  13. 1 -> Left $ openUnionLift ErrorOne
  14. otherwise -> Right x
  15.  
  16. checkTwo :: Int -> Either (OpenUnion '[ErrorOne, ErrorTwo]) Int
  17. checkTwo x = case x of
  18. 2 -> Left $ openUnionLift ErrorOne
  19. 3 -> Left $ openUnionLift $ ErrorTwo 3
  20. otherwise -> Right x
  21.  
  22. checkThree :: Int -> Either (OpenUnion '[ErrorThree]) Int
  23. checkThree x = case x of
  24. 4 -> Left $ openUnionLift $ ErrorThree "error"
  25. otherwise -> Right x
  26.  
  27. checkAll :: Int -> Either (OpenUnion '[ErrorOne, ErrorTwo, ErrorThree]) Int
  28. checkAll x = do
  29. a <- first (relaxOpenUnion) $ checkOne x
  30. b <- first (relaxOpenUnion) $ checkTwo a
  31. c <- first (relaxOpenUnion) $ checkThree b
  32. pure b
Add Comment
Please, Sign In to add comment