{ "metadata": { "language": "haskell", "name": "", "signature": "sha256:0866ba3c2a15da31a719190dea10c3c740d34e438328dabcd7614ba286e29a23" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "![](https://raw2.github.com/gibiansky/IHaskell/master/images/ihaskell-logo-small.png)\n", "\n", "IHaskell Notebook\n", "===\n", "Hello, and welcome to the **IHaskell Notebook**. IHaskell Notebook is similar to an interactive shell along the lines of GHCi. However, it is much more powerful, and provides features such as syntax highlighting, autocompletion, multi-line input cells, integrated documentation, rich output visualization, and more. In this notebook, I'd like to demonstrate many of the awesome features IHaskell provides.\n", "\n", "IHaskell is implemented as a language kernel for the [IPython](http://ipython.org) project, which means that although the entire thing is written only in Haskell, we get a beautiful notebook interface practically for free.\n", "\n", "We can start with very simple Haskell expressions:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "-- First of all, we can evaluate simple expressions.\n", "3 + 5\n", "\"Hello, \" ++ \"World!\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As you can see, each input cell get an execution number. The first input cell is labeled `In [1]`. Just like in GHCi, the output of the last executed statement or expression is available via the `it` variable - however, in addition, the output of the $n$th cell is available via the `itN` variable. For example, if we wanted to see what the first cell printed, we can go ahead and output that:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "it1" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "-- Unlike in GHCi, we can have multi-line expressions.\n", "concat [\n", " \"Hello\",\n", " \", \",\n", " \"World!\"\n", " ] :: String" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In addition to multi-line expressions, IHaskell supports most things that you could put in a standard Haskell file. For example, we can have function bindings without the `let` that GHCi requires. (As long as you group type signatures and their corresponding declarations together, you can use pattern matching and put signatures on your top-level declarations!)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "thing :: String -> Int -> Int\n", "thing \"no\" _ = 100\n", "thing str int = int + length str\n", "\n", "thing \"no\" 10\n", "thing \"ah\" 10" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So far we've just looked at pure functions, but nothing is stopping us from doing IO." ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"What's going on?\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Although this doesn't hold everywhere, we've tried to keep IHaskell relatively similar to GHCi in terms of naming. So, just like in GHCi, you can inspect types with `:type` (or shorthands):" ] }, { "cell_type": "code", "collapsed": false, "input": [ "-- We can look at types like in GHCi.\n", ":ty 3 + 3" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The same goes for the `:info` command. However, unlike GHCi, which simply prints info, the IHaskell notebook brings up a separate pane." ] }, { "cell_type": "code", "collapsed": false, "input": [ "-- What is the Integral typeclass?\n", ":info Integral" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can now write slightly more complicated scripts." ] }, { "cell_type": "code", "collapsed": false, "input": [ "-- Results are printed as we go, even from a single expression.\n", "import Control.Monad\n", "import Control.Concurrent\n", "\n", "forM_ [1..5] $ \\x -> do\n", " print x\n", " threadDelay $ 200 * 1000" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is where the similarities with GHCi end, and the particularly shiny features of IHaskell begin.\n", "\n", "Although looking at text outputs is often enough, there are many times where we really want a richer output. Suppose we have a custom data type for color:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "data Color = Red | Green | Blue" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we were playing around with designing GUI applications, for instance, we might want to actually *see* these colors, instead of just seeing the text \"Red\", \"Green\", and \"Blue\" when we are debugging.\n", "\n", "IHaskell lets you define a custom display mechanism for any data type via its `IHaskellDisplay` typeclass. Since you can use IHaskell in console mode as well as notebook mode, you can provide a list of display outputs for any data type, and the frontend will simply choose the best one. Here's how you would implement a very simple display mechanism for this `Color` data type:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import IHaskell.Display\n", "\n", "instance IHaskellDisplay Color where\n", " display color = return $ Display [html code]\n", " where\n", " code = concat [\"
Look!
\"]\n", " css Red = \"red\"\n", " css Blue = \"blue\"\n", " css Green = \"green\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Once we define a custom `display :: a -> IO Display` function, we can simply output a `Color`:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "Red\n", "Green\n", "Blue" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `DisplayData` type has several constructors which let you display your data as plain text, HTML, images (SVG, PNG, JPG), or even as LaTeX code.\n", "\n", "In order to ship an extension for IHaskell, simply create a package named `ihaskell-thing` with a module named `IHaskell.Display.Thing`. As long as `ihaskell-thing` is installed, IHaskell will detect and use it automatically.\n", "\n", "A number of packages already exist, which we can briefly look at. First, in `ihaskell-basic`, we have very simple displays for data types from `Prelude`." ] }, { "cell_type": "code", "collapsed": false, "input": [ "-- We can display Maybes fancily for Show-able types.\n", "Just ()\n", "Nothing\n", "\n", "-- But it dies if it's not showable.\n", "data NoShow = X Int\n", "Just (X 3)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "IHaskell special features" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In addition to displaying outputs in a rich format, IHaskell has a bunch of useful features.\n", "\n", "For instance, the popular linting tool `hlint` is integrated and turned on by default. Let's write some ugly code, and see what it tells us:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "-- There is also hlint integration enabled by default.\n", "-- If you write sketchy code, it will tell you:\n", "f :: Int -> Int\n", "f x = x + 1\n", "\n", "-- Most warnings are orange...\n", "f $ 3\n", "\n", "-- But more severe warnings are red.\n", "putStrLn (show 3)\n", "do\n", " return 3" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you're an experienced Haskeller, though, and don't want `hlint` telling you what to do, you can easily turn it off with `:opt no-lint`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In addition to `hlint` integration, IHaskell also integrates **Hoogle** for documentation searches. IHaskell provides two directives for searching Hoogle. The first of these, `:document` (or shorthands), looks for exact matches." ] }, { "cell_type": "code", "collapsed": false, "input": [ ":doc filterM" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The other provided command is `:hoogle`. This does a normal Hoogle search, and thus lets you use imperfect matching and searching by type signature." ] }, { "cell_type": "code", "collapsed": false, "input": [ ":hoogle :: [a] -> [(a, b)]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you need a refresher on all of the options, you can just use `:help`:" ] }, { "cell_type": "code", "collapsed": false, "input": [ ":help" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "All of the code you normally put into IHaskell is (like in GHCi) interpreted. However, sometimes you've perfected a function, and now need it to run faster. In that case, you can go ahead and define a module in a single cell. As long as your module has a module header along the lines of `module Name where`, IHaskell will recognize it as a module. It will create the file `A/B.hs`, compile it, and load it. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "-- If your code isn't running fast enough, you can just put it into a module.\n", "module A.B where\n", "\n", "fib 0 = 1\n", "fib 1 = 1\n", "fib n = fib (n-1) + fib (n-2)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that the module is by default imported unqualified, as though you had typed `import A.B`." ] }, { "cell_type": "code", "collapsed": false, "input": [ "-- The module is automatically imported unqualified.\n", "print $ A.B.fib 20\n", "print $ fib 20" ], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }