Commit dbbcb06f authored by Andrew Gibiansky's avatar Andrew Gibiansky

Update notebooks to v4 format

parent 5bf953f0
{ {
"metadata": {
"language": "haskell",
"name": "",
"signature": "sha256:8332eed5b1a2647ecfe6b707d1d07de0e8798861c517cac876970de5eb31e43c"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [ "cells": [
{ {
"cell_type": "markdown", "cell_type": "markdown",
...@@ -168,22 +159,26 @@ ...@@ -168,22 +159,26 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 1,
"input": [ "metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"-- Extensions and imports we'll need later.\n", "-- Extensions and imports we'll need later.\n",
":set -XTypeFamilies -XFlexibleContexts -XMultiParamTypeClasses -XDoAndIfThenElse -XFlexibleInstances\n", ":set -XTypeFamilies -XFlexibleContexts -XMultiParamTypeClasses -XDoAndIfThenElse -XFlexibleInstances\n",
"import Control.Monad.Writer\n", "import Control.Monad.Writer\n",
"import Text.Printf" "import Text.Printf"
], ]
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 2,
"input": [ "metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"class Monad m => GradientDescent m a where\n", "class Monad m => GradientDescent m a where\n",
" -- Type to represent the parameter space.\n", " -- Type to represent the parameter space.\n",
" data Params a :: *\n", " data Params a :: *\n",
...@@ -196,11 +191,7 @@ ...@@ -196,11 +191,7 @@
" -> Params a -- Direction vector.\n", " -> Params a -- Direction vector.\n",
" -> Params a -- Original location.\n", " -> Params a -- Original location.\n",
" -> m (Params a) -- New location." " -> m (Params a) -- New location."
], ]
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
...@@ -216,8 +207,12 @@ ...@@ -216,8 +207,12 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 3,
"input": [ "metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"-- We will call this a vector space, though the definition actually\n", "-- We will call this a vector space, though the definition actually\n",
"-- requires an inner product, since it requires an implementation of `dot`.\n", "-- requires an inner product, since it requires an implementation of `dot`.\n",
"class VectorSpace v where\n", "class VectorSpace v where\n",
...@@ -233,11 +228,7 @@ ...@@ -233,11 +228,7 @@
" -- For convenience.\n", " -- For convenience.\n",
" minus :: v -> v -> v\n", " minus :: v -> v -> v\n",
" minus a b = add a (scale (-1) b)" " minus a b = add a (scale (-1) b)"
], ]
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
...@@ -248,19 +239,19 @@ ...@@ -248,19 +239,19 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 4,
"input": [ "metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"class (Monad m, VectorSpace (Params a)) => GradientDescent m a where\n", "class (Monad m, VectorSpace (Params a)) => GradientDescent m a where\n",
" -- Type to represent the parameter space.\n", " -- Type to represent the parameter space.\n",
" data Params a :: *\n", " data Params a :: *\n",
" \n", " \n",
" -- Compute the gradient at a location in parameter space.\n", " -- Compute the gradient at a location in parameter space.\n",
" grad :: a -> Params a -> m (Params a)" " grad :: a -> Params a -> m (Params a)"
], ]
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
...@@ -273,8 +264,12 @@ ...@@ -273,8 +264,12 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 5,
"input": [ "metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"-- A point consisting of a value and the function at that value.\n", "-- A point consisting of a value and the function at that value.\n",
"-- The stopping condition is implemented as a function\n", "-- The stopping condition is implemented as a function\n",
"-- Point -> Point -> Bool\n", "-- Point -> Point -> Bool\n",
...@@ -305,11 +300,7 @@ ...@@ -305,11 +300,7 @@
" if signum midvalue /= signum aValue\n", " if signum midvalue /= signum aValue\n",
" then bisectionSearch f midpoint a stop\n", " then bisectionSearch f midpoint a stop\n",
" else bisectionSearch f midpoint b stop" " else bisectionSearch f midpoint b stop"
], ]
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
...@@ -320,8 +311,12 @@ ...@@ -320,8 +311,12 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 6,
"input": [ "metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"newtype StopCondition m a = StopWhen (Params a -> Params a -> m Bool)\n", "newtype StopCondition m a = StopWhen (Params a -> Params a -> m Bool)\n",
"\n", "\n",
"conjugateGradient :: GradientDescent m a =>\n", "conjugateGradient :: GradientDescent m a =>\n",
...@@ -369,11 +364,7 @@ ...@@ -369,11 +364,7 @@
" if shouldStop\n", " if shouldStop\n",
" then return xNew\n", " then return xNew\n",
" else go xNew $ Just (x, gradVec, dir)" " else go xNew $ Just (x, gradVec, dir)"
], ]
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 6
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
...@@ -384,8 +375,12 @@ ...@@ -384,8 +375,12 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 7,
"input": [ "metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"-- We need FlexibleInstances for declarations like these!\n", "-- We need FlexibleInstances for declarations like these!\n",
"-- We must declare these instances together, because they have recursive dependencies on each other.\n", "-- We must declare these instances together, because they have recursive dependencies on each other.\n",
"instance VectorSpace (Params (Double -> Double -> Double)) where\n", "instance VectorSpace (Params (Double -> Double -> Double)) where\n",
...@@ -408,11 +403,7 @@ ...@@ -408,11 +403,7 @@
" return $ uncurry Arg gradient\n", " return $ uncurry Arg gradient\n",
" where epsilon = 0.0001\n", " where epsilon = 0.0001\n",
" show' (x, y) = printf \"%.5f, \\t%.5f \" x y" " show' (x, y) = printf \"%.5f, \\t%.5f \" x y"
], ]
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 7
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
...@@ -434,41 +425,23 @@ ...@@ -434,41 +425,23 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 8,
"input": [ "metadata": {
"-- Create a stop condition that respects a given error tolerance.\n", "collapsed": false
"stopCondition f tolerance = StopWhen stop\n", },
" where stop (Arg a b) (Arg x y) = do\n",
" Arg dx dy <- grad f (Arg x y)\n",
" return $ abs dx < tolerance && abs dy < tolerance\n",
"\n",
"-- A demo function with minimum at (0, 0)\n",
"function x y = x^2 + y^2 + 3\n",
"\n",
"-- Note that we don't need to set an alpha!\n",
"let tolerance = 1e-2\n",
"let initValue = Arg 3 2\n",
"let writer = conjugateGradient function (stopCondition function tolerance) initValue\n",
" (minLoc, messages) = runWriter writer :: (Params (Double -> Double -> Double), [String])\n",
"\n",
"printf \"Min at x = %.5f, y = %.5f\\n\" (x minLoc) (y minLoc)\n",
"mapM_ putStrLn (take 10 messages)\n",
"printf \"... and so on ... (%d evaluations)\\n\" $ length messages"
],
"language": "python",
"metadata": {},
"outputs": [ "outputs": [
{ {
"metadata": {}, "data": {
"output_type": "display_data", "text/plain": [
"text": [
"Min at x = 0.00078, y = 0.00054" "Min at x = 0.00078, y = 0.00054"
] ]
}, },
{
"metadata": {}, "metadata": {},
"output_type": "display_data", "output_type": "display_data"
"text": [ },
{
"data": {
"text/plain": [
"Gradient at\t3.00000, \t2.00000 \tis\t5.99990, \t3.99990 \n", "Gradient at\t3.00000, \t2.00000 \tis\t5.99990, \t3.99990 \n",
"Gradient at\t3.00000, \t2.00000 \tis\t5.99990, \t3.99990 \n", "Gradient at\t3.00000, \t2.00000 \tis\t5.99990, \t3.99990 \n",
"Gradient at\t0.00005, \t0.00005 \tis\t-0.00000, \t0.00000 \n", "Gradient at\t0.00005, \t0.00005 \tis\t-0.00000, \t0.00000 \n",
...@@ -481,15 +454,39 @@ ...@@ -481,15 +454,39 @@
"Gradient at\t0.37504, \t0.25004 \tis\t0.74999, \t0.49999" "Gradient at\t0.37504, \t0.25004 \tis\t0.74999, \t0.49999"
] ]
}, },
{
"metadata": {}, "metadata": {},
"output_type": "display_data", "output_type": "display_data"
"text": [ },
{
"data": {
"text/plain": [
"... and so on ... (39 evaluations)" "... and so on ... (39 evaluations)"
] ]
},
"metadata": {},
"output_type": "display_data"
} }
], ],
"prompt_number": 8 "source": [
"-- Create a stop condition that respects a given error tolerance.\n",
"stopCondition f tolerance = StopWhen stop\n",
" where stop (Arg a b) (Arg x y) = do\n",
" Arg dx dy <- grad f (Arg x y)\n",
" return $ abs dx < tolerance && abs dy < tolerance\n",
"\n",
"-- A demo function with minimum at (0, 0)\n",
"function x y = x^2 + y^2 + 3\n",
"\n",
"-- Note that we don't need to set an alpha!\n",
"let tolerance = 1e-2\n",
"let initValue = Arg 3 2\n",
"let writer = conjugateGradient function (stopCondition function tolerance) initValue\n",
" (minLoc, messages) = runWriter writer :: (Params (Double -> Double -> Double), [String])\n",
"\n",
"printf \"Min at x = %.5f, y = %.5f\\n\" (x minLoc) (y minLoc)\n",
"mapM_ putStrLn (take 10 messages)\n",
"printf \"... and so on ... (%d evaluations)\\n\" $ length messages"
]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
...@@ -499,7 +496,17 @@ ...@@ -499,7 +496,17 @@
] ]
} }
], ],
"metadata": {} "metadata": {
"kernelspec": {
"display_name": "Haskell",
"language": "haskell",
"name": "haskell"
},
"language_info": {
"name": "haskell",
"version": "7.8.3"
} }
] },
"nbformat": 4,
"nbformat_minor": 0
} }
This diff is collapsed.
{ {
"metadata": {
"language": "haskell",
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [ "cells": [
{ {
"cell_type": "markdown", "cell_type": "markdown",
...@@ -52,8 +44,12 @@ ...@@ -52,8 +44,12 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 1,
"input": [ "metadata": {
"collapsed": false
},
"outputs": [],
"source": [
":set -XTypeFamilies\n", ":set -XTypeFamilies\n",
"class GradientDescent a where\n", "class GradientDescent a where\n",
" -- Type to represent the parameter space.\n", " -- Type to represent the parameter space.\n",
...@@ -67,11 +63,7 @@ ...@@ -67,11 +63,7 @@
" -> Params a -- Direction vector.\n", " -> Params a -- Direction vector.\n",
" -> Params a -- Original location.\n", " -> Params a -- Original location.\n",
" -> Params a -- New location." " -> Params a -- New location."
], ]
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
...@@ -90,8 +82,12 @@ ...@@ -90,8 +82,12 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 2,
"input": [ "metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"-- We need flexible instances for declarations like these.\n", "-- We need flexible instances for declarations like these.\n",
":set -XFlexibleInstances\n", ":set -XFlexibleInstances\n",
"\n", "\n",
...@@ -104,11 +100,7 @@ ...@@ -104,11 +100,7 @@
" where epsilon = 0.0001\n", " where epsilon = 0.0001\n",
" \n", " \n",
" paramMove scale (Arg vec) (Arg old) = Arg $ old + fromRational (toRational scale) * vec" " paramMove scale (Arg vec) (Arg old) = Arg $ old + fromRational (toRational scale) * vec"
], ]
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
...@@ -119,18 +111,18 @@ ...@@ -119,18 +111,18 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 3,
"input": [ "metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"-- Define a way to decide when to stop.\n", "-- Define a way to decide when to stop.\n",
"-- This lets the user specify an error tolerance easily.\n", "-- This lets the user specify an error tolerance easily.\n",
"-- The function takes the previous two sets of parameters and returns\n", "-- The function takes the previous two sets of parameters and returns\n",
"-- `True` to continue the descent and `False` to stop.\n", "-- `True` to continue the descent and `False` to stop.\n",
"newtype StopCondition a = StopWhen (Params a -> Params a -> Bool)" "newtype StopCondition a = StopWhen (Params a -> Params a -> Bool)"
], ]
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
...@@ -141,8 +133,12 @@ ...@@ -141,8 +133,12 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 4,
"input": [ "metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"gradientDescent :: GradientDescent a => a -- What to optimize.\n", "gradientDescent :: GradientDescent a => a -- What to optimize.\n",
" -> StopCondition a -- When to stop.\n", " -> StopCondition a -- When to stop.\n",
" -> Double -- Step size (alpha).\n", " -> Double -- Step size (alpha).\n",
...@@ -162,11 +158,7 @@ ...@@ -162,11 +158,7 @@
" let gradients = grad function params in\n", " let gradients = grad function params in\n",
" -- And move against the gradient with a step size alpha.\n", " -- And move against the gradient with a step size alpha.\n",
" paramMove (-alpha) gradients params" " paramMove (-alpha) gradients params"
], ]
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
...@@ -177,18 +169,18 @@ ...@@ -177,18 +169,18 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 5,
"input": [ "metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"-- Create a stop condition that respects a given error tolerance.\n", "-- Create a stop condition that respects a given error tolerance.\n",
"stopCondition :: (Double -> Double) -> Double -> StopCondition (Double -> Double)\n", "stopCondition :: (Double -> Double) -> Double -> StopCondition (Double -> Double)\n",
"stopCondition f tolerance = StopWhen stop\n", "stopCondition f tolerance = StopWhen stop\n",
" where stop (Arg prev) (Arg cur) =\n", " where stop (Arg prev) (Arg cur) =\n",
" abs (f prev - f cur) < tolerance " " abs (f prev - f cur) < tolerance "
], ]
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
...@@ -205,15 +197,15 @@ ...@@ -205,15 +197,15 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 6,
"input": [ "metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"-- A demo function with minimum at -3/2\n", "-- A demo function with minimum at -3/2\n",
"function x = x^2 + 3 * x" "function x = x^2 + 3 * x"
], ]
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 6
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
...@@ -224,25 +216,27 @@ ...@@ -224,25 +216,27 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 7,
"input": [ "metadata": {
"let alpha = 1e-1\n", "collapsed": false
"let tolerance = 1e-4\n", },
"let initValue = 12.0\n",
"unArg $ gradientDescent function (stopCondition function tolerance) alpha (Arg initValue)"
],
"language": "python",
"metadata": {},
"outputs": [ "outputs": [
{ {
"metadata": {}, "data": {
"output_type": "display_data", "text/plain": [
"text": [
"-1.4892542376755242" "-1.4892542376755242"
] ]
},
"metadata": {},
"output_type": "display_data"
} }
], ],
"prompt_number": 7 "source": [
"let alpha = 1e-1\n",
"let tolerance = 1e-4\n",
"let initValue = 12.0\n",
"unArg $ gradientDescent function (stopCondition function tolerance) alpha (Arg initValue)"
]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
...@@ -256,8 +250,12 @@ ...@@ -256,8 +250,12 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 8,
"input": [ "metadata": {
"collapsed": false
},
"outputs": [],
"source": [
":set -XMultiParamTypeClasses\n", ":set -XMultiParamTypeClasses\n",
"class Monad m => GradientDescent m a where\n", "class Monad m => GradientDescent m a where\n",
" -- Type to represent the parameter space.\n", " -- Type to represent the parameter space.\n",
...@@ -275,11 +273,7 @@ ...@@ -275,11 +273,7 @@
" \n", " \n",
"-- Since we've redefined GradientDescent, we need to redefine StopCondition.\n", "-- Since we've redefined GradientDescent, we need to redefine StopCondition.\n",
"newtype StopCondition a = StopWhen (Params a -> Params a -> Bool)" "newtype StopCondition a = StopWhen (Params a -> Params a -> Bool)"
], ]
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 8
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
...@@ -290,8 +284,12 @@ ...@@ -290,8 +284,12 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 9,
"input": [ "metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"gradientDescent :: (GradientDescent m a) => \n", "gradientDescent :: (GradientDescent m a) => \n",
" a -- What to optimize.\n", " a -- What to optimize.\n",
" -> StopCondition a -- When to stop.\n", " -> StopCondition a -- When to stop.\n",
...@@ -310,11 +308,7 @@ ...@@ -310,11 +308,7 @@
" takeStep params = do\n", " takeStep params = do\n",
" gradients <- grad function params\n", " gradients <- grad function params\n",
" paramMove (-alpha) gradients params" " paramMove (-alpha) gradients params"
], ]
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 9
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
...@@ -325,8 +319,12 @@ ...@@ -325,8 +319,12 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 10,
"input": [ "metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"instance (Ord a, Floating a) => GradientDescent Maybe (a -> a) where\n", "instance (Ord a, Floating a) => GradientDescent Maybe (a -> a) where\n",
" -- The parameter for a function is just its argument.\n", " -- The parameter for a function is just its argument.\n",
" data Params (a -> a) = Arg { unArg :: a }\n", " data Params (a -> a) = Arg { unArg :: a }\n",
...@@ -339,11 +337,7 @@ ...@@ -339,11 +337,7 @@
" where epsilon = 0.0001\n", " where epsilon = 0.0001\n",
" \n", " \n",
" paramMove scale (Arg vec) (Arg old) = Just $ Arg $ old + fromRational (toRational scale) * vec" " paramMove scale (Arg vec) (Arg old) = Just $ Arg $ old + fromRational (toRational scale) * vec"
], ]
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 10
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
...@@ -354,8 +348,22 @@ ...@@ -354,8 +348,22 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"collapsed": false, "execution_count": 11,
"input": [ "metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"Nothing!"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"stopCondition f tolerance = StopWhen stop\n", "stopCondition f tolerance = StopWhen stop\n",
" where stop (Arg prev) (Arg cur) =\n", " where stop (Arg prev) (Arg cur) =\n",
" abs (f prev - f cur) < tolerance \n", " abs (f prev - f cur) < tolerance \n",
...@@ -365,19 +373,7 @@ ...@@ -365,19 +373,7 @@
"case gradientDescent function stopper alpha x0 of\n", "case gradientDescent function stopper alpha x0 of\n",
" Just x -> print $ unArg x\n", " Just x -> print $ unArg x\n",
" Nothing -> putStrLn \"Nothing!\"" " Nothing -> putStrLn \"Nothing!\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"text": [
"Nothing!"
] ]
}
],
"prompt_number": 11
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
...@@ -389,7 +385,17 @@ ...@@ -389,7 +385,17 @@
] ]
} }
], ],
"metadata": {} "metadata": {
"kernelspec": {
"display_name": "Haskell",
"language": "haskell",
"name": "haskell"
},
"language_info": {
"name": "haskell",
"version": "7.8.3"
} }
] },
"nbformat": 4,
"nbformat_minor": 0
} }
This diff is collapsed.
{ {
"metadata": {
"language": "haskell",
"name": "",
"signature": "sha256:95812c4aac52ed0e9f86f92d457f4647bf9adb83e02b30f67ce5a9362a823c07"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [ "cells": [
{ {
"cell_type": "markdown", "cell_type": "markdown",
...@@ -117,15 +108,20 @@ ...@@ -117,15 +108,20 @@
" display :: a -> IO Display\n", " display :: a -> IO Display\n",
"```\n", "```\n",
"\n", "\n",
"In order to display a data type, create an instance of `IHaskellDisplay` for your data type \u2013 then, any expression that results in your data type will generate a corresponding display. \n", "In order to display a data type, create an instance of `IHaskellDisplay` for your data type – then, any expression that results in your data type will generate a corresponding display. \n",
"\n", "\n",
"Let's go ahead and do this for `CanvasFree a` from the `static-canvas` package." "Let's go ahead and do this for `CanvasFree a` from the `static-canvas` package."
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false, "collapsed": false,
"input": [ "hidden": false
},
"outputs": [],
"source": [
"-- Start with necessary imports.\n", "-- Start with necessary imports.\n",
"import IHaskell.Display -- From the 'ihaskell' package.\n", "import IHaskell.Display -- From the 'ihaskell' package.\n",
"import IHaskell.IPython.Types(MimeType(..))\n", "import IHaskell.IPython.Types(MimeType(..))\n",
...@@ -134,13 +130,7 @@ ...@@ -134,13 +130,7 @@
"-- Text conversion functions.\n", "-- Text conversion functions.\n",
"import Data.Text.Lazy.Builder(toLazyText)\n", "import Data.Text.Lazy.Builder(toLazyText)\n",
"import Data.Text.Lazy(toStrict)" "import Data.Text.Lazy(toStrict)"
], ]
"language": "python",
"metadata": {
"hidden": false
},
"outputs": [],
"prompt_number": 12
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
...@@ -153,8 +143,13 @@ ...@@ -153,8 +143,13 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": false, "collapsed": false,
"input": [ "hidden": false
},
"outputs": [],
"source": [
"-- Since CanvasFree is a type synonym, we need a language pragma.\n", "-- Since CanvasFree is a type synonym, we need a language pragma.\n",
"{-# LANGUAGE TypeSynonymInstances #-}\n", "{-# LANGUAGE TypeSynonymInstances #-}\n",
"\n", "\n",
...@@ -165,13 +160,7 @@ ...@@ -165,13 +160,7 @@
" in Display [DisplayData MimeHtml src]\n", " in Display [DisplayData MimeHtml src]\n",
" \n", " \n",
" where (height, width) = (200, 600)" " where (height, width) = (200, 600)"
], ]
"language": "python",
"metadata": {
"hidden": false
},
"outputs": [],
"prompt_number": 24
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
...@@ -184,8 +173,23 @@ ...@@ -184,8 +173,23 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 34,
"metadata": {
"collapsed": false, "collapsed": false,
"input": [ "hidden": false
},
"outputs": [
{
"data": {
"text/html": [
"<canvas id=\"theStaticCanvas\" width=\"600\" height=\"200\"></canvas><script>var canvas = document.getElementById('theStaticCanvas');var ctx = canvas.getContext('2d');ctx.font = ('italic 60pt Calibri');ctx.lineWidth = (6.0000);ctx.strokeStyle = ('rgb(0,0,255)');ctx.fillStyle = ('rgb(218,165,32)');ctx.textBaseline = ('middle');ctx.strokeText('Hello',150.0000,100.0000);ctx.fillText('Hello World!',150.0000,100.0000);</script>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"{-# LANGUAGE OverloadedStrings #-}\n", "{-# LANGUAGE OverloadedStrings #-}\n",
"import Graphics.Static.ColorNames\n", "import Graphics.Static.ColorNames\n",
"\n", "\n",
...@@ -200,21 +204,7 @@ ...@@ -200,21 +204,7 @@
" fillText \"Hello World!\" 150 100\n", " fillText \"Hello World!\" 150 100\n",
" \n", " \n",
"text" "text"
], ]
"language": "python",
"metadata": {
"hidden": false
},
"outputs": [
{
"html": [
"<canvas id=\"theStaticCanvas\" width=\"600\" height=\"200\"></canvas><script>var canvas = document.getElementById('theStaticCanvas');var ctx = canvas.getContext('2d');ctx.font = ('italic 60pt Calibri');ctx.lineWidth = (6.0000);ctx.strokeStyle = ('rgb(0,0,255)');ctx.fillStyle = ('rgb(218,165,32)');ctx.textBaseline = ('middle');ctx.strokeText('Hello',150.0000,100.0000);ctx.fillText('Hello World!',150.0000,100.0000);</script>"
],
"metadata": {},
"output_type": "display_data"
}
],
"prompt_number": 34
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
...@@ -227,20 +217,19 @@ ...@@ -227,20 +217,19 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": false, "collapsed": false,
"input": [ "hidden": false
},
"outputs": [],
"source": [
"data Canvas = Canvas {\n", "data Canvas = Canvas {\n",
" width :: Int,\n", " width :: Int,\n",
" height :: Int,\n", " height :: Int,\n",
" canvas :: CanvasFree ()\n", " canvas :: CanvasFree ()\n",
" }" " }"
], ]
"language": "python",
"metadata": {
"hidden": false
},
"outputs": [],
"prompt_number": 26
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
...@@ -253,21 +242,20 @@ ...@@ -253,21 +242,20 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": false, "collapsed": false,
"input": [ "hidden": false
},
"outputs": [],
"source": [
"{-# LANGUAGE TypeSynonymInstances #-}\n", "{-# LANGUAGE TypeSynonymInstances #-}\n",
"instance IHaskellDisplay Canvas where\n", "instance IHaskellDisplay Canvas where\n",
" -- display :: Canvas -> IO Display\n", " -- display :: Canvas -> IO Display\n",
" display cnv = return $\n", " display cnv = return $\n",
" let src = toStrict $ toLazyText $ buildScript (width cnv) (height cnv) (canvas cnv)\n", " let src = toStrict $ toLazyText $ buildScript (width cnv) (height cnv) (canvas cnv)\n",
" in Display [DisplayData MimeHtml src]" " in Display [DisplayData MimeHtml src]"
], ]
"language": "python",
"metadata": {
"hidden": false
},
"outputs": [],
"prompt_number": 27
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
...@@ -287,7 +275,7 @@ ...@@ -287,7 +275,7 @@
" fillText \"Hello World!\" 150 100\n", " fillText \"Hello World!\" 150 100\n",
"```\n", "```\n",
"\n", "\n",
"Sadly, it seems that the `static-canvas` library currently only supports having *one* generated canvas on the page \u2013 if you try to add another one, it simply modifies the pre-existing one. This is probably a bug that should be fixed, though!" "Sadly, it seems that the `static-canvas` library currently only supports having *one* generated canvas on the page – if you try to add another one, it simply modifies the pre-existing one. This is probably a bug that should be fixed, though!"
] ]
}, },
{ {
...@@ -310,7 +298,17 @@ ...@@ -310,7 +298,17 @@
] ]
} }
], ],
"metadata": {} "metadata": {
"kernelspec": {
"display_name": "Haskell",
"language": "haskell",
"name": "haskell"
},
"language_info": {
"name": "haskell",
"version": "7.8.3"
} }
] },
"nbformat": 4,
"nbformat_minor": 0
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment