Last active
August 29, 2015 14:25
-
-
Save seanhess/109e104c6e13aab71f13 to your computer and use it in GitHub Desktop.
Typed holes and inline typing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
main = do | |
foo <- getLine | |
return () | |
-- the type of `getLine` is IO String, but how can we figure that out? | |
-- let's put the wrong type inline and let GHC yell at us | |
-- "Couldn't match expected type ‘()’ with actual type ‘IO String’" | |
main2 = do | |
-- we're checking the type of getLine here | |
foo <- getLine :: () | |
-- we are checking the type of bar | |
(bar :: ()) <- getLine | |
return () | |
-- Typed holes don't really solve this problem, but they're very cool | |
-- they tell you what you need to make the types fit | |
-- here, I want to print out the 4. Why doesn't it work? | |
main3 = putStrLn $ 4 | |
-- so let's put in a typed hole. As in. What do I need to fill in the blank to make my code work. | |
-- GHC says: Found hole ‘_’ with type: Integer -> String | |
-- so I need to replace _ with some function of type: Integer -> String | |
main4 = putStrLn $ _ 4 | |
-- what converts an Integer to a String? `show`. Replace _ with show. | |
main5 = putStrLn $ show 4 | |
Here's trace
which lets you print out debugging stuff if you need. http://dev.stephendiehl.com/hask/#trace
Here's a cool trick to use typed holes to figure out the first question: http://stackoverflow.com/questions/30138726/what-is-the-nearest-equivalent-of-type-in-ghci-in-a-ghc-source-file
Whoa, this is awesome! Thanks for such a detailed answer! This is very understandable.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Typed holes: http://dev.stephendiehl.com/hask/#typed-holes