Guest User

Untitled

a guest
Nov 20th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. type Flat api = Reassoc (Flatten api)
  2.  
  3. -- | Completely flattens an API type by applying a few simple transformations.
  4. -- The goal is to end up with an aPI type where things like @a :> (b :<|> c)@
  5. -- are rewritten to @a :> b :<|> a :> c@, so as to have client with very simple
  6. -- types, instead of "nested clients".
  7. type family Flatten (api :: k) :: k where
  8. Flatten ((a :: k) :> (b :<|> c)) = a :> Flatten b :<|> Flatten (a :> c)
  9. Flatten ((a :<|> b) :> c) = a :> Flatten c :<|> (Flatten (b :> c))
  10. Flatten ((a :: k) :> b) = Redex b (Flatten b) a
  11. Flatten (a :<|> b) = Flatten a :<|> Flatten b
  12. Flatten (a :: k) = a
  13.  
  14. type family Redex a b (c :: k) :: * where
  15. Redex a a first = Flatten first :> a
  16. Redex a b first = Flatten (first :> b)
  17.  
  18. -- | Get the endpoints with given indices in the all-flat
  19. -- representation of the API type, glueing them together
  20. -- with ':<|>'.
  21. type family Nths (idxs :: [Nat]) api where
  22. Nths '[i] api = Nth i api
  23. Nths (i ': is) api = Nth i api :<|> Nths is api
  24.  
  25. -- | Get the endpoint with given index in the all-flat representation
  26. -- of the API type.
  27. type family Nth (i :: Nat) api where
  28. Nth 0 (a :<|> b) = a
  29. Nth 0 a = a
  30. Nth n (a :<|> b) = Nth (n - 1) b
  31.  
  32. -- | Reassociates ':<|>'.
  33. type family Reassoc api where
  34. Reassoc ((a :<|> b) :<|> c) = Reassoc a :<|> Reassoc (b :<|> c)
  35. Reassoc (a :<|> b) = a :<|> Reassoc b
  36. Reassoc a = a
Add Comment
Please, Sign In to add comment