Guest User

Untitled

a guest
Jun 23rd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. We saw that every data type is inhabited by a value ⊥ pronounced "bottom". It is very important in a non-strict language such
  2. as Haskell to represent how much knowledge you have about a value. You can either know exactly what the value is, know
  3. only a part of it, or know nothing at all. Bottom represents the latter, but you could as well only know that a list starts
  4. with 1, i.e. _1 : ⊥_.
  5.  
  6. In fact, all data types in Haskell respect an approximation function _⊑_, where _x ⊑ y_ means "_x_ is an approximation
  7. of _y_". For example with booleans, _x ⊑ y iff ⇔ x ≡ ⊥ or x ≡ y_ in other words either you don't know anything about a
  8. particular boolean, or you know exactly what it is. This approximation ordering is important when reasoning about recursive
  9. algorithms, which is why it is important to understand the notion of "nothingness" in Haskell.
  10.  
  11. In the next session we'll see talk about cyclic data structures and fixed points, where it plays a big role.
  12.  
  13. ## Exercise for next session
  14.  
  15. The _fusion law_ of `map` is well known and is commonly used to reduce the runtime complexity of an algorithm:
  16. ```hs
  17. map f . map g = map (f . g)
  18. ```
  19.  
  20. Folds and unfolds have similar laws. The `foldr` fusion law states that
  21. ```hs
  22. foldr f a . g = foldr h b
  23. ```
  24. provided three properties are satisfied.
  25.  
  26. Can you find out what these properties are? To do that, prove that the equality holds for all lists.
  27.  
  28. ### Example of a derivation
  29.  
  30. Just wanted to provide an example of how to do it. Given the following definition of `reverse`:
  31. ```
  32. reverse [] = []
  33. reverse (x:xs) = reverse xs ++ [x]
  34. ```
  35. We want to prove the following law holds:
  36. ```
  37. reverse (reverse xs) == xs
  38. ```
  39.  
  40. #### Case undefined
  41. ```hs
  42. reverse (reverse undefined)
  43. = {- no match in the definition of reverse -}
  44. reverse undefined
  45. = {- again -}
  46. undefined
  47. ```
  48.  
  49. #### Case []
  50. ```hs
  51. reverse (reverse [])
  52. = {- definition of reverse -}
  53. reverse []
  54. = {- again -}
  55. []
  56. ```
  57.  
  58. #### Case (x:xs)
  59. ```hs
  60. reverse (reverse (x:xs))
  61. = {- def of reverse -}
  62. reverse (reverse xs ++ [x])
  63. = {- we claim reverse xs ++ [x] = x : reverse xs -}
  64. reverse (x:reverse xs)
  65. = {- definition of reverse -}
  66. reverse (reverse xs) ++ [x]
  67. = {- claim -}
  68. x:reverse (reverse xs)
  69. = {- induction hyposethis -}
  70. x:xs
  71. ```
  72.  
  73. The claim above can be proved similarly.
Add Comment
Please, Sign In to add comment