Your SlideShare is downloading. ×
GHCi: More Awesome Than You Thought
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.


Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

GHCi: More Awesome Than You Thought


Published on



Published in: Technology

  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide
  • Comic Sans and Powerpoint!A beginner talk, from the perspective of someone trying to learn haskellI’m going to talk a lot about the things that I found difficult, and some of the tricks I’ve discovered. Please raise your hand if you have a better workaround and we’ll bring you the mic
  • Multiline mode seems like the best if you don’t have a fancy IDE, but I’d rather just highlight and goThe lack of type info bugged me a lot at first. Most ML languages show type and result by default.The different syntax for GHCi was extremely frustrating when I was starting out.I found this out on stackoverflow of all places. It doesn’t seem to be in the docs anywhere.
  • Fixed up
  • I added this because I hated not being able to just highlight and execute things in the REPL (a standard feature of most ML languages)Started trying to write haskell with nested functions to execute in the replThen added the wrapping syntax so I didn’t need to leave on the multi-line syntax (it’s annoying)Finally, it occurred to me that I could simply prepend let and indent You can disable any of these features independently*show it*Future: Type sigs, more than one function at a time
  • Haskell scripts seem great for mutli-platform targets.
  • Because haskell is so configurable, it’s probably best to keep a separate .ghci per projectput the :set +m and :set +t to be auto run hereWe could do a whole talk on pimping your GHCi, but for now let’s focus on how to use it, for more examples check out the wiki
  • Monomorphism Restriction: Cannot overload a function unless you provide an explicit type sigNote that some things can’t be unset, such as this particular flag
  • Modules can be loaded in either compiled or interpreted mode. Compiled modules can only depend on other compiled, so for now we’ll be loaded everything interpreted.
  • Moral of the story, if you’re new and just fiddling around you should probably just stick with interpreted
  • Modules can be loaded in either compiled or interpreted mode. Compiled modules can only depend on other compiled, so for now we’ll be loaded everything interpreted.
  • Info usually does the right thing for what you’re looking atPrint will show you the types of the unevaluated thunksForce will show you the result
  • 1) Monads, but with type declarations and lots of special commands?2) Because the bindings will frequently disappear on you, it seems best to work with a temporary file outside of the repl3) You may find yourself needing to use type annotations4) A haskell thing, not ghci, but you may find yourself hitting a breakpoint in the process of debugging (especially recursive functions)5) You can hit breakpoints with the print command or force
  • Most of the oddities stem from laziness.
  • With breakpoints you can target just about anywhere, be it the name or a specific character in a fileStep sets implicit breakpoints everywhere, steplocal just within the current scope, and stepmodule just within the current module
  • fbreak-on-exception will break on any exception fbreak-on-error will break on uncaught exceptions
  • I’ve found that getting a trace where I want it can be somewhat difficult.
  • Transcript

    • 1. -- GHCi --More AwesomeThan You (I?) ThoughtRichard MinerichSenior Researcher at Bayard Rock@Rickasaurus
    • 2. Noob Answers forNoob QuestionsHow do I allow multi-line definitions?Wrap it in “:{ … :}” or “:set +m”Why doesn’t it show the type of my result?“:set +t” will enable type info.Why won’t my Haskell code execute in GHCi?GHCi uses the monad “let” syntax.How do I specify the type of something?Follow it with “:: type” (e.g x = 5 :: Int)
    • 3. Setup for this talkSublime Text 2 with- SublimeHaskell- SublimeREPL (head)- Tabs to Spaces onHaskell Platform (2012.4.0.0) withaeson, haskell-src-exts, haddock,ghc-mod, stylish-haskell, haskell-docs
    • 4. SublimeREPLSublimeREPL (as of last week) will now:- Intelligently remove extra whitespace infront of nested functions- Prepends “let “ to functions sent to the REPLand indents the remainder (as needed)- Automatically wraps multi-line functions withthe new block syntax :{ .. :}
    • 5. Haskell for Scripting-- script.hs --#!/usr/bin/env runhaskellmain = putStrLn “Hello, or something…”On Windows:runhaskell script.hsOn Posix:./script.hs
    • 6. GHCi Custom ConfigsSearch Paths::set +t:def hoogle str ->return $ ":! hoogle --count=15 "" ++ str ++ """Tons of useful macros at the wiki: $HOME/.ghc/ghci.confappdata/ghc/ghci.conf $HOME/.ghci
    • 7. Play with Language Flags{-# LANGUAGE NoMonomorphismRestriction #-}Vs.:set –XNoMonomorphismRestrictionVs.(in latest Haskell Platform):seti –XNoMonomorphismRestriction
    • 8. Loading ModulesBy default module Dir.Module is Dir/Module.hs:load Dir.Module Load compiled if exists:load *Dir.Module Load interpreted forced:reload Reloads target set:show modules List loaded modules
    • 9. Compiled vs. InterpretedCompiled Interpreted~10x Faster Execution ~2x Faster LoadDependencies must becompiledDependencies compiledor interpretedOnly exports available Exports or full scope“:! ghc –c Module.hs” Noop
    • 10. Controlling GHCi Scope:show importsShows what’s in scope:module – Dir1.Module1 …Remove “./Dir1/Module1.hs” from scope:module + Dir1.Module1 *Dir2.Module2 …Open exports from “./Dir1/Module1.hs”Open full scope from “./Dir2/Module2.hs”Standard import syntax applies as well
    • 11. Your Eyes and Ears:browse Module - Shows a module’s exports:browse *Module – Shows full Module scope:show bindings – List bound variables with type:type <expr> - Type information for an expr:info <name> - Extended information:print <id> - Print without forced evaluation:force <id> - Print with forced evaluation
    • 12. GHCi Funny BusinessGHCi is a monad, monad syntax applies (kind of)Bindings go away after “:load” or “:reload”Type defaults (are strange)It’s thunks all the way down“print” vs “:print” vs “:sprint” vs “:force”
    • 13. Things to know about DebuggingAll the standard stuff:- Breakpoints, Break on Exception, SteppingAnd some fancy stuff:- Time Travel, Subexpression BreakpointsBut, some oddities (coming from other langs):- Scope, Execution Ordering, :Print Breakpoints
    • 14. Review: Debugging Basics:set stop :list:break [<id>] or [<module?> <line?> <col?>]:show breaks:delete <breakpoint_id|*>:step <expr?> / :steplocal / :stepmodule:abandon / :continue:print <id> / :force <id>
    • 15. Review: Time Travel Wizardry:trace <expr>:hist <num?>:back / :forward:show context:set –fbreak-on-exception:set –fbreak-on-error_result / _exception
    • 16. How the heck do I trace?Remember, Haskell is lazy. So be thoughtful:- Break inside, near the end- Leverage assertions with error(import Control.Exception (assert))- Give BangPatterns a go- Debug from a secondary .hs file- Implement conditional breakpoints
    • 17. That was just the tip of the iceburg, see theDocs and Wiki for MUCH more information.