Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explain what type errors have to do with purity #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions src/HL/View/Home/Features.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,26 @@ purefunc =
\ mutate any of its arguments.")
haskellPre "square :: Int -> Int\n\
\square x = x * x"
p_ (do "The following string concatenation is okay:")
haskellPre "\"Hello: \" ++ \"World!\" "
p_ (do "The following string concatenation is a type error:")
rejectedHaskellPre "Type error" "\"Name: \" ++ getLine"
p_ (do "Because "
code_ "getLine"
" has type "
code_ "IO String"
" and not "
code_ "String"
", like "
code_ "\"Name: \""
" is. So by the type system you cannot mix and \
\match purity with impurity.")
p_ (do "The following function takes an integer, prints the list of all numbers \
\from zero to that number, and returns its argument. Printing is an "
code_ "IO"
" side-effect, so the function's type must indicate that it describes an "
code_ "IO"
"computation.")
haskellPre "printUpTo :: Int -> IO Int\n\
\printUpTo x = do print [0..x]\n\
\ return x"
p_ (do "Since "
code_ "Int"
" and "
code_ "IO Int"
" are different types, the type system can guarantee that a function of type "
code_ "Int -> Int"
" will not trigger any surprising side-effect.")
haskellPre "printUpToSquare :: Int -> IO Int\n\
\printUpToSquare x = printUpTo (x * x) -- typechecks"
rejectedHaskellPre "Type error" "printUpToSquare :: Int -> Int\n\
\printUpToSquare x = printUpTo (x * x)"

statically :: Html ()
statically =
Expand Down