@@ -59,23 +59,39 @@ statically =
5959 p_ " Every expression in Haskell has a type which is determined at compile time. \
6060 \All the types composed together by function application have to match up. If \
6161 \they don't, the program will be rejected by the compiler. Types become not \
62- \only a form of guarantee, but a language for expressing the construction \
63- \of programs."
62+ \only a form of guarantee, but also a shorthand for documenting proper usage."
6463 p_ [class_ " expand" ] (a_ " Click to expand" )
6564 div_ [class_ " expandable" ] $ do
66- p_ " All Haskell values have a type :"
67- haskellPre " char = 'a' :: Char \n \
68- \int = 123 :: Int \n \
69- \fun = isDigit :: Char -> Bool \n "
65+ p_ " Suppose you have values with the following types :"
66+ haskellPre " fileContents :: ByteString \n \
67+ \decodeUtf8 :: ByteString -> Text \n \
68+ \putStr :: Text -> IO () \n "
7069 p_ " You have to pass the right type of values to functions, or the compiler\
7170 \ will reject the program:"
72- rejectedHaskellPre " Type error" " isDigit 1"
73- p_ " You can decode bytes into text:"
74- haskellPre " bytes = Crypto.Hash.SHA1.hash \" hello\" :: ByteString\n \
75- \text = decodeUtf8 bytes :: Text\n "
76- p_ " But you cannot decode Text, which is already a vector \
77- \of Unicode points:"
78- rejectedHaskellPre " Type error" " doubleDecode = decodeUtf8 (decodeUtf8 bytes)"
71+ rejectedHaskellPre " Type error" " putStr fileContents"
72+ haskellPre " putStr (decodeUtf8 fileContents) -- typechecks"
73+ rejectedHaskellPre " Type error" " putStr (decodeUtf8 (decodeUtf8 fileContents))"
74+ p_ " This eliminates common errors such as forgetting to decode or \
75+ \double-decoding a piece of text."
76+ p_ (do " Notice how the types "
77+ code_ " ByteString"
78+ " and "
79+ code_ " ByteString -> Text"
80+ " tell you that you can combine those values by applying "
81+ code_ " decodeUtf8"
82+ " to "
83+ code_ " fileContents"
84+ " . With more sophisticated types, and with a bit of experience, it's \
85+ \often possible to use a Haskell library without reading any tutorial, \
86+ \just by examining the types of the values it exports." )
87+ p_ " A precise type is also a useful guide when implementing a function."
88+ haskellPre " mySnd :: (a, b) -> b\n \
89+ \mySnd (x, y) = _hole"
90+ p_ (do " Here, the compiler will tell you that you need a value of type "
91+ code_ " b"
92+ " to fill the hole, and that among the values which are in scope, "
93+ code_ " y"
94+ " is the only one which has the required type." )
7995
8096concurrent :: Html ()
8197concurrent =
0 commit comments