Add notebook for continuations

Signed-off-by: Sanchayan Maity <maitysanchayan@gmail.com>
This commit is contained in:
Sanchayan Maity 2020-04-21 20:33:03 +05:30
parent 0c904479a5
commit b1ef1705d3

174
continuations.ipynb Normal file
View file

@ -0,0 +1,174 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<style>/* Styles used for the Hoogle display in the pager */\n",
".hoogle-doc {\n",
"display: block;\n",
"padding-bottom: 1.3em;\n",
"padding-left: 0.4em;\n",
"}\n",
".hoogle-code {\n",
"display: block;\n",
"font-family: monospace;\n",
"white-space: pre;\n",
"}\n",
".hoogle-text {\n",
"display: block;\n",
"}\n",
".hoogle-name {\n",
"color: green;\n",
"font-weight: bold;\n",
"}\n",
".hoogle-head {\n",
"font-weight: bold;\n",
"}\n",
".hoogle-sub {\n",
"display: block;\n",
"margin-left: 0.4em;\n",
"}\n",
".hoogle-package {\n",
"font-weight: bold;\n",
"font-style: italic;\n",
"}\n",
".hoogle-module {\n",
"font-weight: bold;\n",
"}\n",
".hoogle-class {\n",
"font-weight: bold;\n",
"}\n",
".get-type {\n",
"color: green;\n",
"font-weight: bold;\n",
"font-family: monospace;\n",
"display: block;\n",
"white-space: pre-wrap;\n",
"}\n",
".show-type {\n",
"color: green;\n",
"font-weight: bold;\n",
"font-family: monospace;\n",
"margin-left: 1em;\n",
"}\n",
".mono {\n",
"font-family: monospace;\n",
"display: block;\n",
"}\n",
".err-msg {\n",
"color: red;\n",
"font-style: italic;\n",
"font-family: monospace;\n",
"white-space: pre;\n",
"display: block;\n",
"}\n",
"#unshowable {\n",
"color: red;\n",
"font-weight: bold;\n",
"}\n",
".err-msg.in.collapse {\n",
"padding-top: 0.7em;\n",
"}\n",
".highlight-code {\n",
"white-space: pre;\n",
"font-family: monospace;\n",
"}\n",
".suggestion-warning { \n",
"font-weight: bold;\n",
"color: rgb(200, 130, 0);\n",
"}\n",
".suggestion-error { \n",
"font-weight: bold;\n",
"color: red;\n",
"}\n",
".suggestion-name {\n",
"font-weight: bold;\n",
"}\n",
"</style><span class='get-type'>(>>-) :: forall r a. MCont r a -> (a -> r) -> r</span>"
],
"text/plain": [
"(>>-) :: forall r a. MCont r a -> (a -> r) -> r"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"newtype MCont r a = MCont { (>>-) :: (a -> r) -> r }\n",
"\n",
":t (>>-)\n",
"\n",
"instance Functor (MCont r) where\n",
" fmap f cra = MCont $ \\k -> cra >>- \\a -> k (f a)\n",
" \n",
"instance Applicative (MCont r) where\n",
" pure a = MCont $ \\aTor -> aTor a\n",
" cf <*> cx = MCont $ \\bTor ->\n",
" cx >>- \\a ->\n",
" cf >>- \\f ->\n",
" bTor (f a)\n",
" \n",
"instance Monad (MCont r) where\n",
" return = pure\n",
" cra >>= f = MCont $ \\bTor ->\n",
" cra >>- \\a ->\n",
" (f a) >>- \\b ->\n",
" bTor b\n",
" \n",
"join' :: MCont r (MCont r a) -> MCont r a\n",
"join' ccx = MCont $ \\k ->\n",
" ccx >>- \\cx -> -- extract inner continuation\n",
" cx >>- \\x -> -- extract value of inner continuation\n",
" k x -- wrap value in k again"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"square :: Int -> MCont r Int\n",
"square x = MCont $ \\k -> k (x ^ 2)\n",
"\n",
"add :: Int -> Int -> MCont r Int\n",
"add x y = MCont $ \\k -> k (x + y)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"pythagoras :: Int -> Int -> MCont r Int\n",
"pythagoras x y = do\n",
" xsquare <- square x\n",
" ysquare <- square y\n",
" add xsquare ysquare"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Haskell",
"language": "haskell",
"name": "haskell"
},
"language_info": {
"codemirror_mode": "ihaskell",
"file_extension": ".hs",
"name": "haskell",
"pygments_lexer": "Haskell",
"version": "8.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}