added demo notebook

This commit is contained in:
Andrew Gibiansky 2013-12-30 17:18:10 -05:00
parent 46797e1c88
commit 4a123f0f08
6 changed files with 731 additions and 744 deletions

View File

@ -1,734 +0,0 @@
{
"metadata": {
"language": "haskell",
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"import Diagrams.Prelude\n",
"import Diagrams.Backend.SVG.CmdLine\n",
":extension NoMonomorphismRestriction"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"example = circle 1 # fc blue\n",
" # lw 0.05\n",
" # lc purple\n",
" # dashing [0.2,0.05] 0\n",
"\n",
"test :: Diagram SVG R2 -> Diagram SVG R2\n",
"test = id"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 15
},
{
"cell_type": "code",
"collapsed": false,
"input": [
":t example\n",
":t test example\n",
":t example :: Diagram B R2"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<span style='font-weight: bold; color: green;'>forall b. (TrailLike b, Transformable b, HasStyle b, V b ~ R2) => b</span>"
],
"metadata": {},
"output_type": "display_data",
"text": [
"forall b. (TrailLike b, Transformable b, HasStyle b, V b ~ R2) => b"
]
},
{
"html": [
"<span style='font-weight: bold; color: green;'>Diagram SVG R2</span>"
],
"metadata": {},
"output_type": "display_data",
"text": [
"Diagram SVG R2"
]
},
{
"html": [
"<span style='font-weight: bold; color: green;'>Diagram B R2</span>"
],
"metadata": {},
"output_type": "display_data",
"text": [
"Diagram B R2"
]
}
],
"prompt_number": 16
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"data M = Z Int deriving Show"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print (Z 3)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"text": [
"Z 3"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"ma"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<span style='color: red; font-style: italic;'>Not in scope: `ma'<br/>Perhaps you meant one of these:<br/> `map' (imported from Prelude), `max' (imported from Prelude)</span>"
],
"metadata": {},
"output_type": "display_data",
"text": [
"Not in scope: `ma'\n",
"Perhaps you meant one of these:\n",
" `map' (imported from Prelude), `max' (imported from Prelude)"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"f 3 = 2\n",
"f 2 = 3"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"f 2"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"text": [
"3"
]
}
],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"data X = Y Int\n",
"return (Y 3)\n",
"Y 3"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<span style='color: red; font-style: italic;'>No instance for (Num String)<br/> arising from the literal `3'<br/>Possible fix:<br/> add an instance declaration for (Num String)</span>"
],
"metadata": {},
"output_type": "display_data",
"text": [
"No instance for (GHC.Num.Num GHC.Base.String)\n",
" arising from the literal `3'\n",
"Possible fix:\n",
" add an instance declaration for (GHC.Num.Num GHC.Base.String)"
]
}
],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"data X = Y Int"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 14
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"data X = Y Int deriving Show"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 18
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"Y 3"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<span style='color: red; font-style: italic;'>No instance for (Num String)<br/> arising from the literal `3'<br/>Possible fix:<br/> add an instance declaration for (Num String)</span>"
],
"metadata": {},
"output_type": "display_data",
"text": [
"No instance for (GHC.Num.Num GHC.Base.String)\n",
" arising from the literal `3'\n",
"Possible fix:\n",
" add an instance declaration for (GHC.Num.Num GHC.Base.String)"
]
}
],
"prompt_number": 19
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"test [] = 10"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 20
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"test [3]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<span style='color: red; font-style: italic;'><interactive>:1:1-12: Non-exhaustive patterns in function test</span>"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<interactive>:1:1-12: Non-exhaustive patterns in function test"
]
}
],
"prompt_number": 21
},
{
"cell_type": "code",
"collapsed": false,
"input": [
":tadaf"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<span style='color: red; font-style: italic;'>Parse error (line 1, column 1): Unknown directive: 'tadaf'.</span>"
],
"metadata": {},
"output_type": "display_data",
"text": [
"Parse error (line 1, column 1): Unknown directive: 'tadaf'."
]
}
],
"prompt_number": 22
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"f :: Int -> Int\n",
"f 10 = 11\n",
"f 11 = 10\n",
"f 10"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"text": [
"11"
]
}
],
"prompt_number": 23
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"data X = Y Int"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 24
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"let x = 11\n",
" z = 10 in\n",
" x+z"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"text": [
"21"
]
}
],
"prompt_number": 25
},
{
"cell_type": "code",
"collapsed": false,
"input": [
":extension OverloadedStrings"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import Prelude hiding (div, id)\n",
"import Text.Blaze.Html4.Strict hiding (map, style)\n",
"import Text.Blaze.Html4.Strict.Attributes\n",
"import Control.Monad\n",
"div ! style \"color: red\" $ do\n",
" p \"This is an example of BlazeMarkup syntax.\"\n",
" p \"Hello\"\n",
" b \"Hello\"\n",
" img ! src \"/static/base/images/ipynblogo.png\" ! width \"200\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<div style=\"color: red\">\n",
" <p>\n",
" This is an example of BlazeMarkup syntax.\n",
" </p>\n",
" <p>\n",
" Hello\n",
" </p>\n",
" <b>\n",
" Hello\n",
" </b>\n",
" <img src=\"/static/base/images/ipynblogo.png\" width=\"200\">\n",
"</div>\n"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<div style=\"color: red\">\n",
" <p>\n",
" This is an example of BlazeMarkup syntax.\n",
" </p>\n",
" <p>\n",
" Hello\n",
" </p>\n",
" <b>\n",
" Hello\n",
" </b>\n",
" <img src=\"/static/base/images/ipynblogo.png\" width=\"200\">\n",
"</div>"
]
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"forM_ [100, 200, 300, 400, 500, 600, 700, 800] $ \\size -> do\n",
" let s = show size\n",
" unsafePerformIO $ do\n",
" threadDelay 100000\n",
" putStrLn $ \"Generating size: \" ++ s\n",
" return $ img ! src \"/static/base/images/ipynblogo.png\" ! width (toValue s)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<span style='color: red; font-style: italic;'>Not in scope: `unsafePerformIO'<br/>Not in scope: `threadDelay'</span>"
],
"metadata": {},
"output_type": "display_data",
"text": [
"Not in scope: `unsafePerformIO'\n",
"Not in scope: `threadDelay'"
]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import System.IO.Unsafe"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import Prelude hiding (head, id, div)\n",
"import Text.Blaze.Html4.Strict hiding (map)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 34
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"data X = Y Int\n",
"Y 3"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<span style='color: red; font-style: italic;'>No instance for (Show X)<br/> arising from a use of `print'<br/>Possible fix:<br/> add an instance declaration for (Show X)</span>"
],
"metadata": {},
"output_type": "display_data",
"text": [
"No instance for (GHC.Show.Show :Interactive.X)\n",
" arising from a use of `System.IO.print'\n",
"Possible fix:\n",
" add an instance declaration for (GHC.Show.Show :Interactive.X)"
]
}
],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"forM [1] $ \\x -> return (Y x)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 59
},
{
"cell_type": "code",
"collapsed": false,
"input": [
":t forM [1] $ \\x -> return (Y x)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<span style='font-weight: bold; color: green;'>forall (m :: * -> *). Monad m => m [X]</span>"
],
"metadata": {},
"output_type": "display_data",
"text": [
"forall (m :: * -> *). Monad m => m [X]"
]
}
],
"prompt_number": 60
},
{
"cell_type": "code",
"collapsed": false,
"input": [
":t (Y 3)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<span style='font-weight: bold; color: green;'>X</span>"
],
"metadata": {},
"output_type": "display_data",
"text": [
"X"
]
}
],
"prompt_number": 61
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import Control.Concurrent\n",
"import Control.Monad\n",
"\n",
"forM_ [1..10] $ \\x -> do \n",
" print x\n",
" threadDelay 100000"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"text": [
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"6\n",
"7\n",
"8\n",
"9\n",
"10"
]
}
],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"3"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"text": [
"3"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"data Thing = One Int | Two String deriving Show\n",
"One 3"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<span style='color: red; font-style: italic;'>No instance for (Show Thing)<br/> arising from a use of `print'<br/>Possible fix:<br/> add an instance declaration for (Show Thing)</span>"
],
"metadata": {},
"output_type": "display_data",
"text": [
"No instance for (GHC.Show.Show :Interactive.Thing)\n",
" arising from a use of `System.IO.print'\n",
"Possible fix:\n",
" add an instance declaration for (GHC.Show.Show :Interactive.Thing)"
]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"Two \"hello\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"text": [
"Two \"hello\""
]
}
],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"One 3"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<span style='color: red; font-style: italic;'>No instance for (Show Thing)<br/> arising from a use of `print'<br/>Possible fix:<br/> add an instance declaration for (Show Thing)</span>"
],
"metadata": {},
"output_type": "display_data",
"text": [
"No instance for (GHC.Show.Show :Interactive.Thing)\n",
" arising from a use of `System.IO.print'\n",
"Possible fix:\n",
" add an instance declaration for (GHC.Show.Show :Interactive.Thing)"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"data Thing = One Int\n",
"One 3"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<span style='color: red; font-style: italic;'>No instance for (Show Thing)<br/> arising from a use of `print'<br/>Possible fix:<br/> add an instance declaration for (Show Thing)</span>"
],
"metadata": {},
"output_type": "display_data",
"text": [
"No instance for (GHC.Show.Show :Interactive.Thing)\n",
" arising from a use of `System.IO.print'\n",
"Possible fix:\n",
" add an instance declaration for (GHC.Show.Show :Interactive.Thing)"
]
}
],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"data Thing = One Int deriving Show\n",
"One 3"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<span style='color: red; font-style: italic;'>No instance for (Show Thing)<br/> arising from a use of `print'<br/>Possible fix:<br/> add an instance declaration for (Show Thing)</span>"
],
"metadata": {},
"output_type": "display_data",
"text": [
"No instance for (GHC.Show.Show :Interactive.Thing)\n",
" arising from a use of `System.IO.print'\n",
"Possible fix:\n",
" add an instance declaration for (GHC.Show.Show :Interactive.Thing)"
]
}
],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}

710
IHaskell.ipynb Normal file

File diff suppressed because one or more lines are too long

View File

@ -69,7 +69,7 @@ typeCleaner = useStringType . foldl' (.) id (map (`replace` "") fullPrefixes)
useStringType = replace "[Char]" "String"
write :: GhcMonad m => String -> m ()
write x = when debug $ liftIO $ hPutStrLn stderr x
write x = when debug $ liftIO $ hPutStrLn stderr $ "DEBUG: " ++ x
type Interpreter = Ghc
@ -466,8 +466,10 @@ evalCommand output (Expression expr) state = do
-- implement the Show typeclass.
isShowError errs = case find isPlain errs of
Just (Display PlainText msg) ->
startswith "No instance for (GHC.Show.Show" msg &&
isInfixOf " arising from a use of `System.IO.print'" msg
-- Note that we rely on this error message being 'type cleaned', so
-- that `Show` is not displayed as GHC.Show.Show.
startswith "No instance for (Show" msg &&
isInfixOf " arising from a use of `print'" msg
Nothing -> False
where isPlain (Display mime _) = mime == PlainText
@ -479,7 +481,9 @@ evalCommand output (Expression expr) state = do
-- attempting to do this without the serialization to binary and
-- back gives very strange errors - all the types match but it
-- refuses to decode back into a [DisplayData].
runStmt displayExpr RunToCompletion
-- Suppress output, so as not to mess up console.
capturedStatement (const $ return ()) displayExpr
displayedBytestring <- dynCompileExpr "IHaskell.Display.serializeDisplay it"
case fromDynamic displayedBytestring of
Nothing -> error "Expecting lazy Bytestring"

View File

@ -80,10 +80,11 @@ htmlSuggestions suggests = table (concatMap toHtml suggests)
++
row (
el (style severityClass "Found:" ++
style "code" (found suggest))
-- Things that look like this get highlighted.
styleId "highlight-code" "haskell" (found suggest))
++
el (style severityClass "Why Not:" ++
style "code" (whyNot suggest)))
styleId "highlight-code" "haskell" (whyNot suggest)))
where
severityClass = case severity suggest of
LintWarning -> "warning"
@ -91,6 +92,9 @@ htmlSuggestions suggests = table (concatMap toHtml suggests)
style :: String -> String -> String
style cls thing = [i| <div class="suggestion-${cls}">${thing}</div> |]
styleId :: String -> String -> String -> String
styleId cls id thing = [i| <div class="${cls}" id="${id}">${thing}</div> |]
table :: String -> String
table thing = [i| <table class="suggestion-table">${thing}</table> |]

Binary file not shown.

View File

@ -81,22 +81,25 @@ $([IPython.events]).on('app_initialized.NotebookApp', function(){
IPython.CodeCell.options_default['cm_config']['mode'] = 'haskell';
});
var highlightHlint = function() {
var highlightCodes = function() {
// Add logic here that should be run once per reply.
$('.suggestion-code').each(function() {
$('.highlight-code').each(function() {
var $this = $(this),
$code = $this.html(),
$unescaped = $('<div/>').html($code).text();
$this.empty();
// Never highlight this block again.
this.className = "";
CodeMirror(this, {
value: $unescaped,
mode: 'haskell',
mode: this.id,
lineNumbers: false,
readOnly: true
});
});
};
$([IPython.events]).on('shell_reply.Kernel', highlightHlint);
$([IPython.events]).on('shell_reply.Kernel', highlightCodes);