Guest User

Untitled

a guest
Feb 19th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. # (Compose) Compose (Compose)
  2.  
  3. ## Motivation
  4. Multiple times I've wanted to understand how to derive the following by hand:
  5.  
  6. ```hs
  7. (.) . (.) :: (c -> d) -> (a -> b -> c) -> a -> b -> d
  8. ```
  9. Which is the point free version of:
  10.  
  11. ```hs
  12. compose2 :: (c -> d) -> (a -> b -> c) -> a -> b -> d
  13. compose2 f1 f2 = (f1 . ) . f2
  14. ```
  15.  
  16. Here `f2` takes 2 parameters and `f1` takes one.
  17.  
  18. The default `(.)` composes functions that take one parameter. If you want a function like `compose2` which lets the first function exectuted, i.e. `f2`, take 2 parameters you need something like `compose2` or better yet `(.).(.)`.
  19.  
  20. ## Derivation
  21.  
  22. First thing to do when doing Type Tetris™ is to make sure that you don't get confused by all the `a`'s, `b`'s and `c`'s. So let's first rewrite the function prefixed and lets label the two different compose functions with subscripts:
  23.  
  24. ```hs
  25. (.)ₐ . (.)ₓ == (.) (.)ₐ (.)ₓ
  26. ```
  27.  
  28. Then let's create unique type parameter names for each compose function:
  29.  
  30. ```hs
  31. (.)ₓ :: (y -> z) -> (x -> y) -> (x -> z)
  32. (.)ₐ :: (b -> c) -> (a -> b) -> (a -> c)
  33. (.) :: (s -> t) -> (r -> s) -> (r -> t)
  34. ```
  35.  
  36. Now we're ready to apply `(.)ₐ` to `(.)`:
  37.  
  38. ```hs
  39. (.) (.)ₐ :: ???
  40. ```
  41.  
  42. Here's where it can can confusing. Let's make it easier by adding redundant parenthesis to `(.)` and `(.)ₐ` and line them up with each other to help us keep track of things:
  43.  
  44. ```hs
  45. (.) :: ( s -> t ) -> ((r -> s) -> (r -> t))
  46. (.)ₐ :: ((b -> c) -> ((a -> b) -> (a -> c))
  47. ```
  48.  
  49. Once we do the application, we can see the following is true:
  50.  
  51. ```hs
  52. s = b -> c
  53. t = ((a -> b) -> (a -> c))
  54. ```
  55.  
  56. Next we substitute into the remaining parameters of `(.)`, viz. `(r -> s) -> (r -> t)`:
  57.  
  58. ```hs
  59. (.) (.)ₐ :: (r -> (b -> c)) -> (r -> ((a -> b) -> (a -> c)))
  60. ```
  61.  
  62. Now to make things easier we remove redundant parenthesis. This can only be done on the right-hand side since `->` commutes to the right. To make sure that we understand how this is done, we'll do it one step at a time:
  63.  
  64. ```hs
  65. (.) (.)ₐ :: (r -> b -> c) -> (r -> ((a -> b) -> (a -> c)))
  66.  
  67. (.) (.)ₐ :: (r -> b -> c) -> r -> ((a -> b) -> (a -> c))
  68.  
  69. (.) (.)ₐ :: (r -> b -> c) -> r -> ((a -> b) -> a -> c)
  70.  
  71. (.) (.)ₐ :: (r -> b -> c) -> r -> (a -> b) -> a -> c
  72. ```
  73.  
  74. This simplified form will help us with the next step which is to apply `(.)ₓ` to `(.) (.)ₐ`. We line things up again to get the substitution values:
  75.  
  76. ```hs
  77. (.) (.)ₐ :: ( r -> b -> c ) -> r -> (a -> b) -> a -> c
  78. (.)ₓ :: (y -> z) -> (x -> y) -> (x -> z)
  79. ```
  80.  
  81. So our new substitutions are:
  82.  
  83. ```hs
  84. r = y -> z
  85. b = x -> y
  86. c = x -> z
  87. ```
  88. So applying those substitutions to the remaining parts of `(.) (.)ₐ`, viz. `r -> (a -> b) -> a -> c` we get:
  89.  
  90. ```hs
  91. (.) (.)ₐ (.)ₓ :: (y -> z) -> (a -> (x -> y)) -> a -> (x -> z)
  92. ```
  93. And removing redundant parenthesis like before one step at a time yields:
  94.  
  95. ```hs
  96. (.) (.)ₐ (.)ₓ :: (y -> z) -> (a -> x -> y) -> a -> (x -> z)
  97.  
  98. (.) (.)ₐ (.)ₓ :: (y -> z) -> (a -> x -> y) -> a -> x -> z
  99. ```
  100.  
  101. Now we should see if we can make it look like the very first signature which is:
  102.  
  103. ```hs
  104. (.) . (.) :: (c -> d) -> (a -> b -> c) -> a -> b -> d
  105. ```
  106. To do that, the following substitutions are needed:
  107.  
  108. ```hs
  109. y = c
  110. z = d
  111. a = a
  112. x = b
  113. ```
  114. Giving us:
  115.  
  116. ```hs
  117. (.) (.)ₐ (.)ₓ :: (c -> d) -> (a -> b -> c) -> a -> b -> d
  118. ```
  119. And we have successfully survived Type Tetris™.
Add Comment
Please, Sign In to add comment