{"id":373843,"date":"2024-05-21T05:45:38","date_gmt":"2024-05-21T05:45:38","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=373843"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=373843","title":{"rendered":"<span>\u0420\u0430\u0437\u0431\u043e\u0440 \u043c\u043e\u043d\u0430\u0434\u044b ContT, \u0438\u043b\u0438 \u043a\u0430\u043a \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0440\u043e\u0434\u0432\u0438\u043d\u0443\u0442\u044b\u0439 goto, \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u044b \u0438 \u0444\u0430\u0439\u0431\u0435\u0440\u044b \u043d\u0430 \u0445\u0430\u0441\u043a\u0435\u043b\u0435<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/webt\/qw\/fa\/eq\/qwfaeqfrnalaebixj5spvbdmbbs.png\" data-src=\"https:\/\/habrastorage.org\/webt\/qw\/fa\/eq\/qwfaeqfrnalaebixj5spvbdmbbs.png\"\/><\/figure>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0440\u0430\u0437\u0431\u0435\u0440\u0443 \u043c\u043e\u043d\u0430\u0434\u0443 <code>ContT<\/code>, \u0438 \u043f\u043e\u043a\u0430\u0436\u0443 \u043a\u0430\u043a \u0432\u0435\u0440\u043d\u0443\u0442\u044c <code>return<\/code> \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 control-flow \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u044b \u0438\u0437 \u0438\u043c\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u0445 \u044f\u0437\u044b\u043a\u043e\u0432 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043c\u043d\u0435 \u0442\u0430\u043a \u043d\u0435\u0445\u0432\u0430\u0442\u0430\u043b\u043e, \u043a\u043e\u0433\u0434\u0430 \u044f \u043d\u0430\u0447\u0438\u043d\u0430\u043b \u0438\u0437\u0443\u0447\u0430\u0442\u044c \u0445\u0430\u0441\u043a\u0435\u043b\u044c.<\/p>\n<h3>0. \u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0432 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u044f (continuations)<\/h3>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435 <code>f (g x)<\/code>. \u0422\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u043a \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u043d\u0435 \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u0443\u0435\u0442\u0441\u044f. \u0415\u0441\u043b\u0438 \u0438\u043c\u0435\u043d\u0430 \u0433\u0440\u043e\u043c\u043e\u0437\u0434\u043a\u0438\u0435 \u0438 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0430\u044f, \u043a\u043e\u0434 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u043d\u043e \u043d\u0435\u0447\u0438\u0442\u0430\u0435\u043c\u044b\u043c:<\/p>\n<pre><code class=\"haskell\">largeFunctionFoo (largeFunctionBar (largeFunctionBaz x)) <\/code><\/pre>\n<p>\u0415\u0441\u0442\u044c \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u0431\u043e\u0440\u044c\u0431\u044b \u0441 \u044d\u0442\u0438\u043c, \u043d\u043e \u0434\u0432\u0430 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u0445 &#8212; \u044d\u0442\u043e \u043f\u0430\u0439\u043f\u044b <code>x |> g |> f<\/code> \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0439 (CPS &#8212; Continuation passing style) <code>($ x) g' f' id<\/code>.<\/p>\n<p>\u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u044f \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u0445\u043e\u0436\u0438 \u043d\u0430 \u043a\u043e\u043b\u0431\u044d\u043a\u0438. \u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0441 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u043e\u0439 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0439 \u043f\u0440\u043e\u0432\u043e\u0434\u0438\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f, \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0435\u0433\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>g'<\/code> \u043c\u043e\u0436\u043d\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0442\u0430\u043a:<\/p>\n<pre><code class=\"haskell\">g' x cont = cont (g x) <\/code><\/pre>\n<p>\u0421\u0440\u0430\u0437\u0443 \u043e\u0442\u043c\u0435\u0442\u0438\u043c \u043e\u0434\u043d\u0443 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0443\u044e \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c: CPS \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0437\u043d\u0430\u0435\u0442 \u043f\u0440\u043e \u0441\u0432\u043e\u0451 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435. \u0412 \u0442\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u0430\u043a \u0432 <code>f (g x)<\/code> \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>g<\/code> \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e \u043f\u0440\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>f<\/code>, \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>g'<\/code> \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442\u0441\u044f \u044f\u0432\u043d\u043e. \u042d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0435\u0439 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435\u043c, \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437 \u0438\u043b\u0438 \u043d\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0432\u043e\u0432\u0441\u0435.<\/p>\n<h3>1. \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u044f \u0432 \u0445\u0430\u0441\u043a\u0435\u043b\u044c<\/h3>\n<p>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0442\u0438\u043f \u0434\u043b\u044f \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439:<\/p>\n<pre><code class=\"haskell\">data ContT r m a = ContT {runCont :: (a -> m r) -> m r} <\/code><\/pre>\n<p>\u0414\u0430\u043b\u044c\u0448\u0435 \u043f\u043e \u0442\u0435\u043a\u0441\u0442\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>a -> m r<\/code> \u0447\u0430\u0441\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f <code>cc<\/code> &#8212; current continuation<\/p>\n<p>\u0427\u0442\u043e\u0431\u044b \u043f\u0440\u0438\u0432\u044b\u043a\u043d\u0443\u0442\u044c \u043a \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044e \u0440\u0430\u0437\u0431\u0435\u0440\u0451\u043c \u043f\u0430\u0440\u0443 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432:<\/p>\n<p>\u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c\u043e\u0435 \u0447\u0438\u0441\u043b\u043e 5:<\/p>\n<pre><code class=\"haskell\">cont5 :: ContT r m Integer cont5 = ContT $ \\cc -> cc 5 -- ContT ($ 5), \u0435\u0441\u043b\u0438 \u043a\u0440\u0430\u0442\u043a\u043e <\/code><\/pre>\n<p><code>cont5<\/code> &#8212; \u044d\u0442\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 \u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u0435\u0433\u043e \u0441 \u0447\u0438\u0441\u043b\u043e\u043c 5.<\/p>\n<p>\u041c\u043e\u0436\u043d\u043e \u0443\u0431\u0435\u0434\u0438\u0442\u044c\u0441\u044f, \u0447\u0442\u043e <code>cont5<\/code> \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0445\u0440\u0430\u043d\u0438\u0442 \u043d\u0443\u0436\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e, \u0432\u044b\u0437\u0432\u0430\u0432 <code>cont5<\/code> \u0441 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435\u043c <code>print<\/code>:<\/p>\n<pre><code>ghci> runCont cont5 print 5 <\/code><\/pre>\n<p>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043f\u0440\u0438\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0435\u0434\u0438\u043d\u0438\u0446\u044b \u043a \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c\u043e\u043c\u0443 \u0447\u0438\u0441\u043b\u0443:<\/p>\n<pre><code class=\"haskell\">addOne :: ContT r m Integer -> ContT r m Integer addOne contX = ContT $   \\cc -> runCont contX $     \\x -> cc (x + 1) <\/code><\/pre>\n<p><code>addOne<\/code> \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c\u043e\u0435 \u0447\u0438\u0441\u043b\u043e, \u0438 \u043d\u0430 \u0435\u0433\u043e \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0442\u0440\u043e\u0438\u0442 \u043d\u043e\u0432\u043e\u0435 \u0447\u0438\u0441\u043b\u043e. \u0412\u043d\u0443\u0442\u0440\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 <code>cc<\/code>, \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u0441\u0442\u0430\u0440\u043e\u0435 \u0447\u0438\u0441\u043b\u043e <code>contX<\/code> \u0441 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435\u043c <code>\\x -> cc (x + 1)<\/code>, \u043a\u043e\u0442\u043e\u0440\u043e\u0435, \u0432 \u0441\u0432\u043e\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c, \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 <code>cc<\/code> \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c <code>x + 1<\/code>.<\/p>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u0438\u043c, \u0447\u0442\u043e <code>(addOne cont5)<\/code> \u0445\u0440\u0430\u043d\u0438\u0442 \u0447\u0438\u0441\u043b\u043e 6:<\/p>\n<pre><code>ghci> runCont (addOne cont5) print 6 <\/code><\/pre>\n<p>\u0427\u0442\u043e\u0431\u044b \u0441 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438 \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0443\u0434\u043e\u0431\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0434\u043b\u044f \u043d\u0438\u0445 \u0438\u043d\u0441\u0442\u0430\u043d\u0441\u044b \u043a\u043b\u0430\u0441\u0441\u043e\u0432 <code>Functor<\/code>, <code>Applicative<\/code> \u0438 <code>Monad<\/code>:<\/p>\n<pre><code class=\"haskell\">instance Functor (ContT r m) where   fmap f contX = ContT $     \\cc -> runCont contX $       \\x -> cc (f x)  instance Applicative (ContT r m) where   pure a = ContT ($ a)    contF &lt;*> contX = ContT $     \\cc -> runCont contF $       \\f -> runCont contX $         \\x -> cc (f x)  instance Monad (ContT r m) where   return = pure    contX >>= f = ContT $     \\cc -> runCont contX $       \\x -> runCont (f x) $         \\res -> cc res <\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0441 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u044f\u043c\u0438 \u043c\u043e\u0436\u043d\u043e \u043f\u0438\u0441\u0430\u0442\u044c \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u043f\u0440\u043e\u0449\u0435. \u0424\u0443\u043d\u043a\u0446\u0438\u0438 \u0438\u0437 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0442\u0430\u043a:<\/p>\n<pre><code class=\"haskell\">cont5 :: ContT r m Integer cont5 = return 5  addOne :: ContT r m Integer -> ContT r m Integer addOne contX = do   x &lt;- contX   return (x + 1) <\/code><\/pre>\n<p>\u041f\u043e\u043a\u0430 \u0447\u0442\u043e \u0432\u0441\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c <code>ContT<\/code> \u0431\u044b\u043b\u0438 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u044f\u043c\u043e\u043b\u0438\u043d\u0435\u0439\u043d\u044b\u043c\u0438. \u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u044f \u0432\u044b\u0437\u044b\u0432\u0430\u043b\u0438\u0441\u044c \u0440\u043e\u0432\u043d\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437, \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u043e\u0432 \u043d\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u043b\u043e. \u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0433\u043b\u0430\u0432\u0435 \u043c\u044b \u044d\u0442\u043e \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u043c<\/p>\n<h3>2. \u0421\u0442\u0440\u043e\u0438\u043c callCC<\/h3>\n<p>\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0435\u0449\u0451 \u0440\u0430\u0437 \u043d\u0430 \u0442\u0438\u043f <code>ContT<\/code>:<\/p>\n<pre><code class=\"haskell\">data ContT r m a = ContT {runCont :: (a -> m r) -> m r} <\/code><\/pre>\n<p>\u0417\u0430\u043c\u0435\u0442\u0438\u043b\u0438? \u0415\u0441\u043b\u0438 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0442\u0438\u043f\u0430 <code>m r<\/code>, \u043c\u043e\u0436\u043d\u043e \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e, \u043d\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u044f <code>cc<\/code>. \u0422\u043e\u0433\u0434\u0430 \u0432\u0441\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f (\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u043b\u0438\u0441\u044c \u0432\u043d\u0443\u0442\u0440\u0438 <code>cc<\/code>) \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u043e\u043f\u0443\u0449\u0435\u043d\u044b.<\/p>\n<p>\u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0437\u0430\u0444\u0438\u043a\u0441\u0438\u0440\u0443\u0435\u043c <code>r = ()<\/code>, <code>m = IO<\/code> \u0438 \u043d\u0430\u043f\u0438\u0448\u0435\u043c skip3 &#8212; \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0438 \u043f\u0435\u0447\u0430\u0442\u0430\u0435\u0442 3:<\/p>\n<pre><code class=\"haskell\">skip3 :: ContT () IO a skip3 = ContT $ \\cc -> print 3 -- ContT (const $ print 3) <\/code><\/pre>\n<p>\u041e\u0442\u043c\u0435\u0442\u0438\u043c, \u0447\u0442\u043e \u0442\u0438\u043f <code>a<\/code> \u043d\u0435 \u0432\u0430\u0436\u0435\u043d, \u0442\u0430\u043a \u043a\u0430\u043a <code>cc<\/code> \u043d\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f. \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u043c, \u0447\u0442\u043e <code>skip3<\/code> \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442:<\/p>\n<pre><code class=\"haskell\">test1 = do   return 5  test2 = do   skip3   return 5 <\/code><\/pre>\n<pre><code>ghci> runCont test1 print 5 ghci> runCont test2 print 3 <\/code><\/pre>\n<p>\u042d\u0442\u043e \u0434\u0430\u0451\u0442 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u0448\u0430\u0433\u0438, \u043d\u043e \u0438\u0437-\u0437\u0430 \u0444\u0438\u043a\u0441\u0430\u0446\u0438\u0438 <code>r<\/code> \u0438 <code>m<\/code>, <code>skip3<\/code> \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0441\u044f \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u044b\u043c. \u041d\u0430\u043f\u0438\u0448\u0435\u043c \u0431\u043e\u043b\u0435\u0435 \u0433\u0438\u0431\u043a\u0438\u0439 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c.<\/p>\n<p>\u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e \u0438\u0437\u043d\u0443\u0442\u0440\u0438 <code>ContT<\/code>:<\/p>\n<pre><code class=\"haskell\">cont :: ContT r m a cont = ContT $ \\cc -> _ -- ? <\/code><\/pre>\n<p><code>cc<\/code> \u0432\u043d\u0443\u0442\u0440\u0438 <code>cont<\/code> \u0438\u043c\u0435\u0435\u0442 \u0442\u0438\u043f <code>a -> m r<\/code>. \u042d\u0442\u043e \u0434\u0430\u0451\u0442 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c <code>m r<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0430. \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 <code>skip<\/code> \u0432\u043d\u0443\u0442\u0440\u044c <code>cont<\/code>:<\/p>\n<pre><code class=\"haskell\">cont :: ContT r m a cont = ContT $   \\cc ->     let skip a = ContT (const $ cc a)      in _ -- ? <\/code><\/pre>\n<p><code>skip<\/code> \u0438\u043c\u0435\u0435\u0442 \u0442\u0438\u043f <code>a -> ContT r m b<\/code>, <code>skip a<\/code> \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e <code>skip3<\/code>. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0432\u043d\u0443\u0442\u0440\u0438 <code>cont<\/code> \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u0443\u044e &#171;\u043c\u0430\u043b\u0443\u044e&#187; \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043f\u0435\u0440\u0435\u0434\u0430\u0434\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>skip<\/code> \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u0432\u043e \u0432\u043d\u0435\u0448\u043d\u0438\u0439 <code>ContT r m a<\/code>.<\/p>\n<p>\u0422\u0430\u043a \u043c\u044b \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u043c \u043a \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>callCC<\/code> (call with current continuation):<\/p>\n<pre><code class=\"haskell\">type Switch r m a = forall b. a -> ContT r m b  callCC :: (Switch r m a -> ContT r m a) -> ContT r m a callCC f = ContT $   \\cc ->     let switchToCC = \\a -> ContT (const $ cc a)      in runCont (f switchToCC) cc <\/code><\/pre>\n<p>\u042d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u043e\u0441\u043d\u043e\u0432\u043e\u0439 \u043f\u043e\u0447\u0442\u0438 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0438\u0445 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0439.<\/p>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u0441 <code>callCC<\/code>.<\/p>\n<p>\u041d\u0430\u0447\u043d\u0451\u043c \u0441 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043e\u0441\u0440\u043e\u0447\u043d\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e:<\/p>\n<pre><code class=\"haskell\">test = callCC $ \\exit -> do   lift $ putStrLn \"Reachable\"   exit ()   lift $ putStrLn \"Unreachable\" <\/code><\/pre>\n<pre><code>ghci> runCont test (const $ return ()) Reachable <\/code><\/pre>\n<p>\u041c\u043e\u0436\u043d\u043e \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u044b <code>break<\/code> \u0438 <code>continue<\/code>:<\/p>\n<pre><code class=\"haskell\">test :: ContT r IO () test = do   forM_ [1 .. 10] $ \\i -> do     callCC $ \\continue -> do       when (i == 5) $ do         continue ()        lift $ print i <\/code><\/pre>\n<pre><code>ghci> runCont test (const $ return ()) 1 2 3 4 6 7 8 9 10 <\/code><\/pre>\n<pre><code class=\"haskell\">test :: ContT r IO () test = do   callCC $ \\break -> do     forM_ [1 .. 10] $ \\i -> do       when (i == 5) $ do         break ()        lift $ print i <\/code><\/pre>\n<pre><code>ghci> runCont test (const $ return ()) 1 2 3 4 <\/code><\/pre>\n<p>\u041e\u0442\u043c\u0435\u0442\u0438\u043c \u043e\u0434\u043d\u0443 \u043e\u0447\u0435\u043d\u044c \u0432\u0430\u0436\u043d\u0443\u044e \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c: \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>switchToCC<\/code> \u043d\u0435 \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u0435\u0442 &#171;\u043c\u0430\u043b\u0443\u044e&#187; \u0444\u0443\u043d\u043a\u0446\u0438\u044e. \u041e\u043d\u0430 \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u0438 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0442\u043e, \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0431\u044b\u043b\u0430 \u0432\u044b\u0437\u0432\u0430\u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>callCC<\/code>. \u0415\u0441\u043b\u0438 \u043a\u0430\u043a\u0438\u043c-\u0442\u043e \u043e\u0431\u0440\u0430\u0437\u043e\u043c <code>switchToCC<\/code> \u0441\u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0439\u0442\u0438 \u0437\u0430 \u043f\u0440\u0435\u0434\u0435\u043b\u044b <code>callCC<\/code>, \u0432\u044b\u0437\u043e\u0432 \u044d\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442 \u044d\u0442\u043e \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435:<\/p>\n<pre><code class=\"haskell\">test = do   val {- label -} &lt;- callCC $ \\exit -> do     exit 10   -- rest  <\/code><\/pre>\n<p><code>exit<\/code> \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0438\u0442 \u043d\u0430\u0441 \u043a \u0442\u043e\u0447\u043a\u0435 <code>label<\/code>, \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u0442\u0441\u044f \u0441 \u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u043d\u0438\u044f <code>val<\/code>.<\/p>\n<p>\u041f\u0435\u0440\u0435\u0439\u0434\u0451\u043c \u043a \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u043e\u0434\u0432\u0438\u043d\u0443\u0442\u044b\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0438\u043c \u0432\u044b\u0437\u043e\u0432\u044b <code>switchToCC<\/code> \u0441\u043d\u0430\u0440\u0443\u0436\u0438 <code>callCC<\/code><\/p>\n<h3>3. \u0424\u0443\u043d\u043a\u0446\u0438\u044f label (\u043e\u043d\u0430 \u0436\u0435 goto, \u043e\u043d\u0430 \u0436\u0435 \u0430\u043d\u0430\u043b\u043e\u0433 \u0445\u0443\u043a\u0430 useState)<\/h3>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0433\u043b\u0430\u0432\u0435 \u043c\u044b \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>label<\/code>. \u041e\u043d\u0430 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043f\u0430\u0440\u0443 <code>(restart, value)<\/code>.<\/p>\n<ul>\n<li>\n<p><code>restart<\/code> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u0441 \u0434\u0440\u0443\u0433\u0438\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c<\/p>\n<\/li>\n<li>\n<p><code>value<\/code> &#8212; \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435<\/p>\n<\/li>\n<\/ul>\n<p>\u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u044d\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0435:<\/p>\n<pre><code class=\"haskell\">label :: a -> ContT r m (a -> ContT r m b, a) label init = callCC $   \\switch ->     let restart val = switch (restart, val)      in return (restart, init) <\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u0435\u0449\u0438 \u0432 \u0438\u043c\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u043e\u043c \u0441\u0442\u0438\u043b\u0435<\/p>\n<p>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0446\u0438\u043a\u043b\u044b:<\/p>\n<pre><code class=\"haskell\">test = do   (restart, counter) &lt;- label 0    lift $ print counter    when (counter &lt; 10) $ do     restart $ counter + 1 <\/code><\/pre>\n<pre><code>ghci> runCont test (const $ return ()) 0 1 2 3 4 5 6 7 8 9 10 <\/code><\/pre>\n<p>\u041c\u043e\u0436\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0430\u0440\u0443 <code>setjmp<\/code>\/<code>longjmp<\/code>. \u042d\u0442\u043e \u0434\u0430\u0436\u0435 \u043f\u0440\u043e\u0449\u0435 \u0447\u0435\u043c <code>label<\/code> \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0438\u043c \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0435\u0441\u0442\u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435:<\/p>\n<pre><code class=\"haskell\">setjmp = do   (restart, _) &lt;- label ()    let longjmp = restart ()    return longjmp  test = do   longjmp &lt;- setjmp   lift $ print 10    longjmp <\/code><\/pre>\n<pre><code>ghci> runCont test (const $ return ()) 10 10 10 10 10 10 ... <\/code><\/pre>\n<h3>4. \u0413\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u044b, \u0444\u0430\u0439\u0431\u0435\u0440\u044b \u0438 \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a<\/h3>\n<p>\u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0435\u0449\u0451 \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u043e\u0434\u0432\u0438\u043d\u0443\u0442\u044b\u0435 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u044b \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043e\u0439.<\/p>\n<p>\u041f\u043e\u0440\u0430\u0441\u0441\u0443\u0436\u0434\u0430\u0435\u043c \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u043c. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043a\u0430\u043a \u043e\u043d\u0438 \u0441\u0434\u0435\u043b\u0430\u043d\u044b \u0432 \u0434\u0440\u0443\u0433\u0438\u0445 \u044f\u0437\u044b\u043a\u0430\u0445. \u041e\u0431\u044b\u0447\u043d\u043e \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 \u0441\u043b\u043e\u0432\u043e <code>yield<\/code>, \u043e\u0434\u043d\u0430\u043a\u043e \u0435\u0441\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043d\u044e\u0430\u043d\u0441\u043e\u0432:<\/p>\n<ol start=\"0\">\n<li>\n<p>\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 \u0441\u043b\u043e\u0432\u043e \u0443 \u043d\u0430\u0441 \u043d\u0435\u0442, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 <code>yield<\/code> \u0431\u0443\u0434\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439<\/p>\n<\/li>\n<li>\n<p>\u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e, \u0447\u0442\u043e\u0431\u044b \u0438\u0437 <code>yield x<\/code> \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 <code>yield<\/code>. \u0415\u0441\u043b\u0438 \u0434\u0432\u0430\u0436\u0434\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 <code>yield<\/code>, \u043c\u044b \u0432\u044b\u0439\u0434\u0435\u043c \u0432 \u043e\u0434\u043d\u043e\u0439 \u0438 \u0442\u043e\u0439 \u0436\u0435 \u0442\u043e\u0447\u043a\u0435, \u0447\u0442\u043e \u043d\u0430\u043c \u043d\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442<\/p>\n<\/li>\n<li>\n<p>\u041d\u0443\u0436\u043d\u0430 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0432\u044b\u0437\u0432\u0430\u0442\u044c <code>exit<\/code> (\u0435\u0433\u043e \u0442\u043e\u0436\u0435 \u043d\u0443\u0436\u043d\u043e \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 <code>yield<\/code>) \u0447\u0442\u043e\u0431\u044b \u0432\u0435\u0440\u043d\u0443\u0442\u044c\u0441\u044f \u0432 \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a<\/p>\n<\/li>\n<\/ol>\n<p>\u041f\u043e\u043b\u043e\u0436\u0438\u043c \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443:<\/p>\n<pre><code class=\"haskell\">data Controls r m x = Controls   { yield :: x -> ContT r m (Controls r m x)   , exit :: forall b. ContT r m b   } <\/code><\/pre>\n<p>\u0422\u043e\u0433\u0434\u0430 \u043a\u043e\u0434 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u0430 \u043c\u043e\u0433 \u0431\u044b \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a:<\/p>\n<pre><code class=\"haskell\">test controls = do   controls &lt;- yield controls 1   controls &lt;- yield controls 2   controls &lt;- yield controls 3   exit controls <\/code><\/pre>\n<p>\u041c\u043e\u0436\u0435\u0442 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u0441\u0442\u0440\u0430\u043d\u043d\u044b\u043c, \u0447\u0442\u043e \u043c\u044b \u044f\u0432\u043d\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c <code>exit controls<\/code> \u0434\u043b\u044f \u0432\u044b\u0445\u043e\u0434\u0430 \u0432 \u043a\u043e\u043d\u0446\u0435 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u0430, \u043d\u043e \u0434\u0430\u043b\u044c\u0448\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043d\u044f\u0442\u043d\u043e \u0437\u0430\u0447\u0435\u043c \u044d\u0442\u043e.<br \/> \u041f\u043e\u043a\u0430 \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u0442\u0438\u043f\u043e\u0432 \u0437\u0430\u043f\u0440\u0435\u0442\u0438\u043c \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u0443 \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0442\u044c\u0441\u044f \u0431\u0435\u0437 \u0432\u044b\u0437\u043e\u0432\u0430 <code>exit<\/code>:<\/p>\n<pre><code class=\"haskell\">type Generator r m x = Controls r m x -> ContT r m Void <\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u0430 \u0434\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e <code>yield<\/code>.<br \/> \u041e\u043d\u0430 \u0431\u0443\u0434\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u043f\u0430\u0440\u0443 \u0438\u0437 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0447\u0430\u0441\u0442\u0438 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u0430 \u0438\u043b\u0438 <code>Nothing<\/code> \u0435\u0441\u043b\u0438 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u043b\u0441\u044f:<\/p>\n<pre><code class=\"haskell\">runToYield :: Generator r m x -> ContT r m (Maybe (x, Generator r m x)) runToYield generator = callCC $ \\exitContext -> do   let exit = exitContext Nothing       yield value = callCC $ \\continueGenerator ->         exitContext $ Just (value, continueGenerator)        controls = Controls{yield, exit}    generator controls   -- \u0415\u0441\u043b\u0438 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440 \u0434\u043e\u0431\u0440\u0430\u043b\u0441\u044f \u0441\u044e\u0434\u0430, \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0447\u0442\u043e-\u0442\u043e \u043f\u043e\u0445\u043e\u0436\u0435\u0435 \u043d\u0430 \u043f\u0440\u043e\u0432\u0430\u043b \u0441\u0442\u0435\u043a\u0430 \u0432   -- \u0438\u043c\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u0445 \u044f\u0437\u044b\u043a\u0430\u0445. \u0414\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0435 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435, \u0435\u0441\u043b\u0438 \u0443\u0431\u0440\u0430\u0442\u044c error, \u0445\u043e\u0442\u044c \u0438   -- \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043e, \u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u043e\u0447\u0435\u043d\u044c \u0441\u0442\u0440\u0430\u043d\u043d\u044b\u043c \u0438 \u043a\u043e\u043d\u0442\u0440\u0438\u043d\u0442\u0443\u0438\u0442\u0438\u0432\u043d\u044b\u043c   error \"Generator exit invariant violated\" <\/code><\/pre>\n<p>\u0420\u0430\u0437\u0431\u0435\u0440\u0451\u043c \u044d\u0442\u043e\u0442 \u043a\u043e\u0434 \u043f\u043e\u0441\u0442\u0440\u043e\u0447\u043d\u043e:<\/p>\n<ul>\n<li>\n<p><code>callCC $ \\exitContext -> do<\/code> &#8212; \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u043d\u043e\u0432\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440<\/p>\n<\/li>\n<li>\n<p><code>exit = exitContext Nothing<\/code> &#8212; \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u043b\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u0432 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442<\/p>\n<\/li>\n<li>\n<p><code>yield value<\/code> &#8212; \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f<\/p>\n<\/li>\n<li>\n<p><code>callCC $ \\continueGenerator -><\/code> &#8212; \u0438\u0437\u043d\u0443\u0442\u0440\u0438 \u0437\u0430\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u0430<\/p>\n<\/li>\n<li>\n<p><code>exitContext $ Just (value, continueGenerator)<\/code> &#8212; \u0432\u044b\u0445\u043e\u0434\u0438\u043c \u0432 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0447\u0430\u0441\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u0430<\/p>\n<\/li>\n<li>\n<p><code>controls = Controls{yield, exit}<\/code> &#8212; \u043f\u0440\u043e\u0441\u0442\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u0443\u0434\u043e\u0431\u043d\u044b\u0439 \u0441\u0438\u043d\u043e\u043d\u0438\u043c<\/p>\n<\/li>\n<li>\n<p><code>generator controls<\/code> &#8212; \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440<\/p>\n<\/li>\n<li>\n<p><code>error \"Generator exit invariant violated\"<\/code> &#8212; \u043c\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0442\u0438\u043f\u0430 <code>Void<\/code> \u0437\u0430\u043f\u0440\u0435\u0442\u0438\u043b\u0438 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u0443 \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0442\u044c\u0441\u044f \u043a\u0430\u043a-\u043b\u0438\u0431\u043e \u043f\u043e\u043c\u0438\u043c\u043e \u0432\u044b\u0437\u043e\u0432\u0430 <code>exit<\/code>. \u042d\u0442\u0430 \u0441\u0442\u0440\u043e\u0447\u043a\u0430 \u043a\u043e\u0434\u0430 \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c unreachable<\/p>\n<\/li>\n<\/ul>\n<p>\u0424\u0430\u0439\u0431\u0435\u0440\u044b &#8212; \u044d\u0442\u043e \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u044b, \u0443 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 <code>yield<\/code> \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043d\u0430\u0437\u0430\u0434 \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0443 (\u0431\u0435\u0437 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u043a\u0430\u043a\u0438\u0445-\u043b\u0438\u0431\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439).<br \/> \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u043f\u0430\u0440\u0443 \u0443\u0434\u043e\u0431\u043d\u044b\u0445 \u0441\u0438\u043d\u043e\u043d\u0438\u043c\u043e\u0432 \u0434\u043b\u044f \u043d\u0438\u0445:<\/p>\n<pre><code class=\"haskell\">type Fiber r m = Generator r m ()  suspend :: Controls r m () -> ContT r m (Controls r m ()) suspend controls = yield controls () <\/code><\/pre>\n<p>\u041d\u0430\u043f\u0438\u0448\u0435\u043c \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a \u0434\u043b\u044f \u0443\u0434\u043e\u0431\u043d\u043e\u0433\u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0444\u0430\u0439\u0431\u0435\u0440\u043e\u0432. \u0420\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u043e: \u0431\u0435\u0440\u0451\u043c \u0441\u043f\u0438\u0441\u043e\u043a \u0444\u0430\u0439\u0431\u0435\u0440\u043e\u0432, \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0432\u0441\u0435 \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u0440\u0430\u0437\u0443, \u0443\u0431\u0438\u0440\u0430\u0435\u043c \u0442\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u043b\u0438\u0441\u044c. \u041f\u043e\u0432\u0442\u043e\u0440\u044f\u0442\u044c \u043f\u043e\u043a\u0430 \u043e\u0441\u0442\u0430\u043b\u0438\u0441\u044c \u043d\u0435\u0437\u0430\u0432\u0435\u0440\u0448\u0451\u043d\u043d\u044b\u0435 \u0444\u0430\u0439\u0431\u0435\u0440\u044b:<\/p>\n<pre><code class=\"haskell\">scheduler :: [Fiber r m] -> ContT r m () scheduler threads = do   let round threads = do         nextThreads &lt;- forM threads $ \\thread -> do           res &lt;- runToYield thread           return $ snd &lt;$> res          return $ catMaybes nextThreads -- \u0424\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044f \u0444\u0430\u0439\u0431\u0435\u0440\u043e\u0432    (loop, threads) &lt;- label threads   threadsLeft &lt;- round threads    when (length threadsLeft \/= 0) $ do     loop threadsLeft <\/code><\/pre>\n<p>\u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043b\u0438\u0448\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0447\u0442\u043e \u0444\u0430\u0439\u0431\u0435\u0440\u044b \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442:<\/p>\n<pre><code class=\"haskell\">debug :: (MonadIO m) => String -> m () debug str = liftIO $ putStrLn str  fiberA :: Fiber r IO fiberA controls = do   debug \"Started fiber A\"   controls &lt;- suspend controls   debug \"Running fiber A\"   controls &lt;- suspend controls   debug \"Exiting fiber A\"   exit controls  fiberB :: Fiber r IO fiberB controls = do   debug \"Started fiber B\"   controls &lt;- suspend controls   debug \"Running fiber B\"   controls &lt;- suspend controls   debug \"Running fiber B again\"   controls &lt;- suspend controls   debug \"Exiting fiber B\"   exit controls <\/code><\/pre>\n<pre><code>ghci> runCont (scheduler [fiberA, fiberB]) (const $ return ()) Started fiber A Started fiber B Running fiber A Running fiber B Exiting fiber A Running fiber B again Exiting fiber B <\/code><\/pre>\n<p>\u041d\u0443 \u0432\u043e\u0442 \u0438 \u0432\u0441\u0451. \u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/808241\/\"> https:\/\/habr.com\/ru\/articles\/808241\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"\"><\/figure>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0440\u0430\u0437\u0431\u0435\u0440\u0443 \u043c\u043e\u043d\u0430\u0434\u0443 <code>ContT<\/code>, \u0438 \u043f\u043e\u043a\u0430\u0436\u0443 \u043a\u0430\u043a \u0432\u0435\u0440\u043d\u0443\u0442\u044c <code>return<\/code> \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 control-flow \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u044b \u0438\u0437 \u0438\u043c\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u0445 \u044f\u0437\u044b\u043a\u043e\u0432 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043c\u043d\u0435 \u0442\u0430\u043a \u043d\u0435\u0445\u0432\u0430\u0442\u0430\u043b\u043e, \u043a\u043e\u0433\u0434\u0430 \u044f \u043d\u0430\u0447\u0438\u043d\u0430\u043b \u0438\u0437\u0443\u0447\u0430\u0442\u044c \u0445\u0430\u0441\u043a\u0435\u043b\u044c.<\/p>\n<h3>0. \u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0432 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u044f (continuations)<\/h3>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435 <code>f (g x)<\/code>. \u0422\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u043a \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u043d\u0435 \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u0443\u0435\u0442\u0441\u044f. \u0415\u0441\u043b\u0438 \u0438\u043c\u0435\u043d\u0430 \u0433\u0440\u043e\u043c\u043e\u0437\u0434\u043a\u0438\u0435 \u0438 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0430\u044f, \u043a\u043e\u0434 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u043d\u043e \u043d\u0435\u0447\u0438\u0442\u0430\u0435\u043c\u044b\u043c:<\/p>\n<pre><code class=\"haskell\">largeFunctionFoo (largeFunctionBar (largeFunctionBaz x)) <\/code><\/pre>\n<p>\u0415\u0441\u0442\u044c \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u0431\u043e\u0440\u044c\u0431\u044b \u0441 \u044d\u0442\u0438\u043c, \u043d\u043e \u0434\u0432\u0430 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u0445 &#8212; \u044d\u0442\u043e \u043f\u0430\u0439\u043f\u044b <code>x |> g |> f<\/code> \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0439 (CPS &#8212; Continuation passing style) <code>($ x) g' f' id<\/code>.<\/p>\n<p>\u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u044f \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u0445\u043e\u0436\u0438 \u043d\u0430 \u043a\u043e\u043b\u0431\u044d\u043a\u0438. \u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0441 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u043e\u0439 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0439 \u043f\u0440\u043e\u0432\u043e\u0434\u0438\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f, \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0435\u0433\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>g'<\/code> \u043c\u043e\u0436\u043d\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0442\u0430\u043a:<\/p>\n<pre><code class=\"haskell\">g' x cont = cont (g x) <\/code><\/pre>\n<p>\u0421\u0440\u0430\u0437\u0443 \u043e\u0442\u043c\u0435\u0442\u0438\u043c \u043e\u0434\u043d\u0443 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0443\u044e \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c: CPS \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0437\u043d\u0430\u0435\u0442 \u043f\u0440\u043e \u0441\u0432\u043e\u0451 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435. \u0412 \u0442\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u0430\u043a \u0432 <code>f (g x)<\/code> \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>g<\/code> \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e \u043f\u0440\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>f<\/code>, \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>g'<\/code> \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442\u0441\u044f \u044f\u0432\u043d\u043e. \u042d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0435\u0439 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435\u043c, \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0437 \u0438\u043b\u0438 \u043d\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0432\u043e\u0432\u0441\u0435.<\/p>\n<h3>1. \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u044f \u0432 \u0445\u0430\u0441\u043a\u0435\u043b\u044c<\/h3>\n<p>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0442\u0438\u043f \u0434\u043b\u044f \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439:<\/p>\n<pre><code class=\"haskell\">data ContT r m a = ContT {runCont :: (a -> m r) -> m r} <\/code><\/pre>\n<p>\u0414\u0430\u043b\u044c\u0448\u0435 \u043f\u043e \u0442\u0435\u043a\u0441\u0442\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>a -> m r<\/code> \u0447\u0430\u0441\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f <code>cc<\/code> &#8212; current continuation<\/p>\n<p>\u0427\u0442\u043e\u0431\u044b \u043f\u0440\u0438\u0432\u044b\u043a\u043d\u0443\u0442\u044c \u043a \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044e \u0440\u0430\u0437\u0431\u0435\u0440\u0451\u043c \u043f\u0430\u0440\u0443 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432:<\/p>\n<p>\u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c\u043e\u0435 \u0447\u0438\u0441\u043b\u043e 5:<\/p>\n<pre><code class=\"haskell\">cont5 :: ContT r m Integer cont5 = ContT $ \\cc -> cc 5 -- ContT ($ 5), \u0435\u0441\u043b\u0438 \u043a\u0440\u0430\u0442\u043a\u043e <\/code><\/pre>\n<p><code>cont5<\/code> &#8212; \u044d\u0442\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 \u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u0435\u0433\u043e \u0441 \u0447\u0438\u0441\u043b\u043e\u043c 5.<\/p>\n<p>\u041c\u043e\u0436\u043d\u043e \u0443\u0431\u0435\u0434\u0438\u0442\u044c\u0441\u044f, \u0447\u0442\u043e <code>cont5<\/code> \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0445\u0440\u0430\u043d\u0438\u0442 \u043d\u0443\u0436\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e, \u0432\u044b\u0437\u0432\u0430\u0432 <code>cont5<\/code> \u0441 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435\u043c <code>print<\/code>:<\/p>\n<pre><code>ghci> runCont cont5 print 5 <\/code><\/pre>\n<p>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043f\u0440\u0438\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0435\u0434\u0438\u043d\u0438\u0446\u044b \u043a \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c\u043e\u043c\u0443 \u0447\u0438\u0441\u043b\u0443:<\/p>\n<pre><code class=\"haskell\">addOne :: ContT r m Integer -> ContT r m Integer addOne contX = ContT $   \\cc -> runCont contX $     \\x -> cc (x + 1) <\/code><\/pre>\n<p><code>addOne<\/code> \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c\u043e\u0435 \u0447\u0438\u0441\u043b\u043e, \u0438 \u043d\u0430 \u0435\u0433\u043e \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0442\u0440\u043e\u0438\u0442 \u043d\u043e\u0432\u043e\u0435 \u0447\u0438\u0441\u043b\u043e. \u0412\u043d\u0443\u0442\u0440\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 <code>cc<\/code>, \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u0441\u0442\u0430\u0440\u043e\u0435 \u0447\u0438\u0441\u043b\u043e <code>contX<\/code> \u0441 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435\u043c <code>\\x -> cc (x + 1)<\/code>, \u043a\u043e\u0442\u043e\u0440\u043e\u0435, \u0432 \u0441\u0432\u043e\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c, \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 <code>cc<\/code> \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c <code>x + 1<\/code>.<\/p>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u0438\u043c, \u0447\u0442\u043e <code>(addOne cont5)<\/code> \u0445\u0440\u0430\u043d\u0438\u0442 \u0447\u0438\u0441\u043b\u043e 6:<\/p>\n<pre><code>ghci> runCont (addOne cont5) print 6 <\/code><\/pre>\n<p>\u0427\u0442\u043e\u0431\u044b \u0441 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438 \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0443\u0434\u043e\u0431\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0434\u043b\u044f \u043d\u0438\u0445 \u0438\u043d\u0441\u0442\u0430\u043d\u0441\u044b \u043a\u043b\u0430\u0441\u0441\u043e\u0432 <code>Functor<\/code>, <code>Applicative<\/code> \u0438 <code>Monad<\/code>:<\/p>\n<pre><code class=\"haskell\">instance Functor (ContT r m) where   fmap f contX = ContT $     \\cc -> runCont contX $       \\x -> cc (f x)  instance Applicative (ContT r m) where   pure a = ContT ($ a)    contF &lt;*> contX = ContT $     \\cc -> runCont contF $       \\f -> runCont contX $         \\x -> cc (f x)  instance Monad (ContT r m) where   return = pure    contX >>= f = ContT $     \\cc -> runCont contX $       \\x -> runCont (f x) $         \\res -> cc res <\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0441 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u044f\u043c\u0438 \u043c\u043e\u0436\u043d\u043e \u043f\u0438\u0441\u0430\u0442\u044c \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u043f\u0440\u043e\u0449\u0435. \u0424\u0443\u043d\u043a\u0446\u0438\u0438 \u0438\u0437 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0442\u0430\u043a:<\/p>\n<pre><code class=\"haskell\">cont5 :: ContT r m Integer cont5 = return 5  addOne :: ContT r m Integer -> ContT r m Integer addOne contX = do   x &lt;- contX   return (x + 1) <\/code><\/pre>\n<p>\u041f\u043e\u043a\u0430 \u0447\u0442\u043e \u0432\u0441\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c <code>ContT<\/code> \u0431\u044b\u043b\u0438 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u044f\u043c\u043e\u043b\u0438\u043d\u0435\u0439\u043d\u044b\u043c\u0438. \u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u044f \u0432\u044b\u0437\u044b\u0432\u0430\u043b\u0438\u0441\u044c \u0440\u043e\u0432\u043d\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437, \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u043e\u0432 \u043d\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u043b\u043e. \u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0433\u043b\u0430\u0432\u0435 \u043c\u044b \u044d\u0442\u043e \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u043c<\/p>\n<h3>2. \u0421\u0442\u0440\u043e\u0438\u043c callCC<\/h3>\n<p>\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0435\u0449\u0451 \u0440\u0430\u0437 \u043d\u0430 \u0442\u0438\u043f <code>ContT<\/code>:<\/p>\n<pre><code class=\"haskell\">data ContT r m a = ContT {runCont :: (a -> m r) -> m r} <\/code><\/pre>\n<p>\u0417\u0430\u043c\u0435\u0442\u0438\u043b\u0438? \u0415\u0441\u043b\u0438 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0442\u0438\u043f\u0430 <code>m r<\/code>, \u043c\u043e\u0436\u043d\u043e \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e, \u043d\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u044f <code>cc<\/code>. \u0422\u043e\u0433\u0434\u0430 \u0432\u0441\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f (\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u043b\u0438\u0441\u044c \u0432\u043d\u0443\u0442\u0440\u0438 <code>cc<\/code>) \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u043e\u043f\u0443\u0449\u0435\u043d\u044b.<\/p>\n<p>\u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0437\u0430\u0444\u0438\u043a\u0441\u0438\u0440\u0443\u0435\u043c <code>r = ()<\/code>, <code>m = IO<\/code> \u0438 \u043d\u0430\u043f\u0438\u0448\u0435\u043c skip3 &#8212; \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0438 \u043f\u0435\u0447\u0430\u0442\u0430\u0435\u0442 3:<\/p>\n<pre><code class=\"haskell\">skip3 :: ContT () IO a skip3 = ContT $ \\cc -> print 3 -- ContT (const $ print 3) <\/code><\/pre>\n<p>\u041e\u0442\u043c\u0435\u0442\u0438\u043c, \u0447\u0442\u043e \u0442\u0438\u043f <code>a<\/code> \u043d\u0435 \u0432\u0430\u0436\u0435\u043d, \u0442\u0430\u043a \u043a\u0430\u043a <code>cc<\/code> \u043d\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f. \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u043c, \u0447\u0442\u043e <code>skip3<\/code> \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442:<\/p>\n<pre><code class=\"haskell\">test1 = do   return 5  test2 = do   skip3   return 5 <\/code><\/pre>\n<pre><code>ghci> runCont test1 print 5 ghci> runCont test2 print 3 <\/code><\/pre>\n<p>\u042d\u0442\u043e \u0434\u0430\u0451\u0442 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u0448\u0430\u0433\u0438, \u043d\u043e \u0438\u0437-\u0437\u0430 \u0444\u0438\u043a\u0441\u0430\u0446\u0438\u0438 <code>r<\/code> \u0438 <code>m<\/code>, <code>skip3<\/code> \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0441\u044f \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u044b\u043c. \u041d\u0430\u043f\u0438\u0448\u0435\u043c \u0431\u043e\u043b\u0435\u0435 \u0433\u0438\u0431\u043a\u0438\u0439 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c.<\/p>\n<p>\u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e \u0438\u0437\u043d\u0443\u0442\u0440\u0438 <code>ContT<\/code>:<\/p>\n<pre><code class=\"haskell\">cont :: ContT r m a cont = ContT $ \\cc -> _ -- ? <\/code><\/pre>\n<p><code>cc<\/code> \u0432\u043d\u0443\u0442\u0440\u0438 <code>cont<\/code> \u0438\u043c\u0435\u0435\u0442 \u0442\u0438\u043f <code>a -> m r<\/code>. \u042d\u0442\u043e \u0434\u0430\u0451\u0442 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c <code>m r<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0430. \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 <code>skip<\/code> \u0432\u043d\u0443\u0442\u0440\u044c <code>cont<\/code>:<\/p>\n<pre><code class=\"haskell\">cont :: ContT r m a cont = ContT $   \\cc ->     let skip a = ContT (const $ cc a)      in _ -- ? <\/code><\/pre>\n<p><code>skip<\/code> \u0438\u043c\u0435\u0435\u0442 \u0442\u0438\u043f <code>a -> ContT r m b<\/code>, <code>skip a<\/code> \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e <code>skip3<\/code>. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0432\u043d\u0443\u0442\u0440\u0438 <code>cont<\/code> \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u0443\u044e &#171;\u043c\u0430\u043b\u0443\u044e&#187; \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043f\u0435\u0440\u0435\u0434\u0430\u0434\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>skip<\/code> \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u0432\u043e \u0432\u043d\u0435\u0448\u043d\u0438\u0439 <code>ContT r m a<\/code>.<\/p>\n<p>\u0422\u0430\u043a \u043c\u044b \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u043c \u043a \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>callCC<\/code> (call with current continuation):<\/p>\n<pre><code class=\"haskell\">type Switch r m a = forall b. a -> ContT r m b  callCC :: (Switch r m a -> ContT r m a) -> ContT r m a callCC f = ContT $   \\cc ->     let switchToCC = \\a -> ContT (const $ cc a)      in runCont (f switchToCC) cc <\/code><\/pre>\n<p>\u042d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u043e\u0441\u043d\u043e\u0432\u043e\u0439 \u043f\u043e\u0447\u0442\u0438 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0438\u0445 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0439.<\/p>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u0441 <code>callCC<\/code>.<\/p>\n<p>\u041d\u0430\u0447\u043d\u0451\u043c \u0441 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043e\u0441\u0440\u043e\u0447\u043d\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e:<\/p>\n<pre><code class=\"haskell\">test = callCC $ \\exit -> do   lift $ putStrLn \"Reachable\"   exit ()   lift $ putStrLn \"Unreachable\" <\/code><\/pre>\n<pre><code>ghci> runCont test (const $ return ()) Reachable <\/code><\/pre>\n<p>\u041c\u043e\u0436\u043d\u043e \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u044b <code>break<\/code> \u0438 <code>continue<\/code>:<\/p>\n<pre><code class=\"haskell\">test :: ContT r IO () test = do   forM_ [1 .. 10] $ \\i -> do     callCC $ \\continue -> do       when (i == 5) $ do         continue ()        lift $ print i <\/code><\/pre>\n<pre><code>ghci> runCont test (const $ return ()) 1 2 3 4 6 7 8 9 10 <\/code><\/pre>\n<pre><code class=\"haskell\">test :: ContT r IO () test = do   callCC $ \\break -> do     forM_ [1 .. 10] $ \\i -> do       when (i == 5) $ do         break ()        lift $ print i <\/code><\/pre>\n<pre><code>ghci> runCont test (const $ return ()) 1 2 3 4 <\/code><\/pre>\n<p>\u041e\u0442\u043c\u0435\u0442\u0438\u043c \u043e\u0434\u043d\u0443 \u043e\u0447\u0435\u043d\u044c \u0432\u0430\u0436\u043d\u0443\u044e \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c: \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>switchToCC<\/code> \u043d\u0435 \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u0435\u0442 &#171;\u043c\u0430\u043b\u0443\u044e&#187; \u0444\u0443\u043d\u043a\u0446\u0438\u044e. \u041e\u043d\u0430 \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u0438 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0442\u043e, \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0431\u044b\u043b\u0430 \u0432\u044b\u0437\u0432\u0430\u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>callCC<\/code>. \u0415\u0441\u043b\u0438 \u043a\u0430\u043a\u0438\u043c-\u0442\u043e \u043e\u0431\u0440\u0430\u0437\u043e\u043c <code>switchToCC<\/code> \u0441\u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0439\u0442\u0438 \u0437\u0430 \u043f\u0440\u0435\u0434\u0435\u043b\u044b <code>callCC<\/code>, \u0432\u044b\u0437\u043e\u0432 \u044d\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442 \u044d\u0442\u043e \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435:<\/p>\n<pre><code class=\"haskell\">test = do   val {- label -} &lt;- callCC $ \\exit -> do     exit 10   -- rest  <\/code><\/pre>\n<p><code>exit<\/code> \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0438\u0442 \u043d\u0430\u0441 \u043a \u0442\u043e\u0447\u043a\u0435 <code>label<\/code>, \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u0442\u0441\u044f \u0441 \u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u043d\u0438\u044f <code>val<\/code>.<\/p>\n<p>\u041f\u0435\u0440\u0435\u0439\u0434\u0451\u043c \u043a \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u043e\u0434\u0432\u0438\u043d\u0443\u0442\u044b\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0438\u043c \u0432\u044b\u0437\u043e\u0432\u044b <code>switchToCC<\/code> \u0441\u043d\u0430\u0440\u0443\u0436\u0438 <code>callCC<\/code><\/p>\n<h3>3. \u0424\u0443\u043d\u043a\u0446\u0438\u044f label (\u043e\u043d\u0430 \u0436\u0435 goto, \u043e\u043d\u0430 \u0436\u0435 \u0430\u043d\u0430\u043b\u043e\u0433 \u0445\u0443\u043a\u0430 useState)<\/h3>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0433\u043b\u0430\u0432\u0435 \u043c\u044b \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>label<\/code>. \u041e\u043d\u0430 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043f\u0430\u0440\u0443 <code>(restart, value)<\/code>.<\/p>\n<ul>\n<li>\n<p><code>restart<\/code> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u0441 \u0434\u0440\u0443\u0433\u0438\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c<\/p>\n<\/li>\n<li>\n<p><code>value<\/code> &#8212; \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435<\/p>\n<\/li>\n<\/ul>\n<p>\u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u044d\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0435:<\/p>\n<pre><code class=\"haskell\">label :: a -> ContT r m (a -> ContT r m b, a) label init = callCC $   \\switch ->     let restart val = switch (restart, val)      in return (restart, init) <\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u0435\u0449\u0438 \u0432 \u0438\u043c\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u043e\u043c \u0441\u0442\u0438\u043b\u0435<\/p>\n<p>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0446\u0438\u043a\u043b\u044b:<\/p>\n<pre><code class=\"haskell\">test = do   (restart, counter) &lt;- label 0    lift $ print counter    when (counter &lt; 10) $ do     restart $ counter + 1 <\/code><\/pre>\n<pre><code>ghci> runCont test (const $ return ()) 0 1 2 3 4 5 6 7 8 9 10 <\/code><\/pre>\n<p>\u041c\u043e\u0436\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0430\u0440\u0443 <code>setjmp<\/code>\/<code>longjmp<\/code>. \u042d\u0442\u043e \u0434\u0430\u0436\u0435 \u043f\u0440\u043e\u0449\u0435 \u0447\u0435\u043c <code>label<\/code> \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0438\u043c \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0435\u0441\u0442\u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435:<\/p>\n<pre><code class=\"haskell\">setjmp = do   (restart, _) &lt;- label ()    let longjmp = restart ()    return longjmp  test = do   longjmp &lt;- setjmp   lift $ print 10    longjmp <\/code><\/pre>\n<pre><code>ghci> runCont test (const $ return ()) 10 10 10 10 10 10 ... <\/code><\/pre>\n<h3>4. \u0413\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u044b, \u0444\u0430\u0439\u0431\u0435\u0440\u044b \u0438 \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a<\/h3>\n<p>\u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0435\u0449\u0451 \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u043e\u0434\u0432\u0438\u043d\u0443\u0442\u044b\u0435 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u044b \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043e\u0439.<\/p>\n<p>\u041f\u043e\u0440\u0430\u0441\u0441\u0443\u0436\u0434\u0430\u0435\u043c \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u043c. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043a\u0430\u043a \u043e\u043d\u0438 \u0441\u0434\u0435\u043b\u0430\u043d\u044b \u0432 \u0434\u0440\u0443\u0433\u0438\u0445 \u044f\u0437\u044b\u043a\u0430\u0445. \u041e\u0431\u044b\u0447\u043d\u043e \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 \u0441\u043b\u043e\u0432\u043e <code>yield<\/code>, \u043e\u0434\u043d\u0430\u043a\u043e \u0435\u0441\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043d\u044e\u0430\u043d\u0441\u043e\u0432:<\/p>\n<ol start=\"0\">\n<li>\n<p>\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 \u0441\u043b\u043e\u0432\u043e \u0443 \u043d\u0430\u0441 \u043d\u0435\u0442, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 <code>yield<\/code> \u0431\u0443\u0434\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439<\/p>\n<\/li>\n<li>\n<p>\u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e, \u0447\u0442\u043e\u0431\u044b \u0438\u0437 <code>yield x<\/code> \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 <code>yield<\/code>. \u0415\u0441\u043b\u0438 \u0434\u0432\u0430\u0436\u0434\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 <code>yield<\/code>, \u043c\u044b \u0432\u044b\u0439\u0434\u0435\u043c \u0432 \u043e\u0434\u043d\u043e\u0439 \u0438 \u0442\u043e\u0439 \u0436\u0435 \u0442\u043e\u0447\u043a\u0435, \u0447\u0442\u043e \u043d\u0430\u043c \u043d\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442<\/p>\n<\/li>\n<li>\n<p>\u041d\u0443\u0436\u043d\u0430 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0432\u044b\u0437\u0432\u0430\u0442\u044c <code>exit<\/code> (\u0435\u0433\u043e \u0442\u043e\u0436\u0435 \u043d\u0443\u0436\u043d\u043e \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 <code>yield<\/code>) \u0447\u0442\u043e\u0431\u044b \u0432\u0435\u0440\u043d\u0443\u0442\u044c\u0441\u044f \u0432 \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a<\/p>\n<\/li>\n<\/ol>\n<p>\u041f\u043e\u043b\u043e\u0436\u0438\u043c \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443:<\/p>\n<pre><code class=\"haskell\">data Controls r m x = Controls   { yield :: x -> ContT r m (Controls r m x)   , exit :: forall b. ContT r m b   } <\/code><\/pre>\n<p>\u0422\u043e\u0433\u0434\u0430 \u043a\u043e\u0434 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u0430 \u043c\u043e\u0433 \u0431\u044b \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a:<\/p>\n<pre><code class=\"haskell\">test controls = do   controls &lt;- yield controls 1   controls &lt;- yield controls 2   controls &lt;- yield controls 3   exit controls <\/code><\/pre>\n<p>\u041c\u043e\u0436\u0435\u0442 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u0441\u0442\u0440\u0430\u043d\u043d\u044b\u043c, \u0447\u0442\u043e \u043c\u044b \u044f\u0432\u043d\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c <code>exit controls<\/code> \u0434\u043b\u044f \u0432\u044b\u0445\u043e\u0434\u0430 \u0432 \u043a\u043e\u043d\u0446\u0435 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u0430, \u043d\u043e \u0434\u0430\u043b\u044c\u0448\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043d\u044f\u0442\u043d\u043e \u0437\u0430\u0447\u0435\u043c \u044d\u0442\u043e.<br \/> \u041f\u043e\u043a\u0430 \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u0442\u0438\u043f\u043e\u0432 \u0437\u0430\u043f\u0440\u0435\u0442\u0438\u043c \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u0443 \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0442\u044c\u0441\u044f \u0431\u0435\u0437 \u0432\u044b\u0437\u043e\u0432\u0430 <code>exit<\/code>:<\/p>\n<pre><code class=\"haskell\">type Generator r m x = Controls r m x -> ContT r m Void <\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u0430 \u0434\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e <code>yield<\/code>.<br \/> \u041e\u043d\u0430 \u0431\u0443\u0434\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u043f\u0430\u0440\u0443 \u0438\u0437 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0447\u0430\u0441\u0442\u0438 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u0430 \u0438\u043b\u0438 <code>Nothing<\/code> \u0435\u0441\u043b\u0438 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u043b\u0441\u044f:<\/p>\n<pre><code class=\"haskell\">runToYield :: Generator r m x -> ContT r m (Maybe (x, Generator r m x)) runToYield generator = callCC $ \\exitContext -> do   let exit = exitContext Nothing       yield value = callCC $ \\continueGenerator ->         exitContext $ Just (value, continueGenerator)        controls = Controls{yield, exit}    generator controls   -- \u0415\u0441\u043b\u0438 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440 \u0434\u043e\u0431\u0440\u0430\u043b\u0441\u044f \u0441\u044e\u0434\u0430, \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0447\u0442\u043e-\u0442\u043e \u043f\u043e\u0445\u043e\u0436\u0435\u0435 \u043d\u0430 \u043f\u0440\u043e\u0432\u0430\u043b \u0441\u0442\u0435\u043a\u0430 \u0432   -- \u0438\u043c\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u0445 \u044f\u0437\u044b\u043a\u0430\u0445. \u0414\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0435 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435, \u0435\u0441\u043b\u0438 \u0443\u0431\u0440\u0430\u0442\u044c error, \u0445\u043e\u0442\u044c \u0438   -- \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043e, \u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u043e\u0447\u0435\u043d\u044c \u0441\u0442\u0440\u0430\u043d\u043d\u044b\u043c \u0438 \u043a\u043e\u043d\u0442\u0440\u0438\u043d\u0442\u0443\u0438\u0442\u0438\u0432\u043d\u044b\u043c   error \"Generator exit invariant violated\" <\/code><\/pre>\n<p>\u0420\u0430\u0437\u0431\u0435\u0440\u0451\u043c \u044d\u0442\u043e\u0442 \u043a\u043e\u0434 \u043f\u043e\u0441\u0442\u0440\u043e\u0447\u043d\u043e:<\/p>\n<ul>\n<li>\n<p><code>callCC $ \\exitContext -> do<\/code> &#8212; \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u043d\u043e\u0432\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442 \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440<\/p>\n<\/li>\n<li>\n<p><code>exit = exitContext Nothing<\/code> &#8212; \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u043b\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u0432 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442<\/p>\n<\/li>\n<li>\n<p><code>yield value<\/code> &#8212; \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f<\/p>\n<\/li>\n<li>\n<p><code>callCC $ \\continueGenerator -><\/code> &#8212; \u0438\u0437\u043d\u0443\u0442\u0440\u0438 \u0437\u0430\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u0430<\/p>\n<\/li>\n<li>\n<p><code>exitContext $ Just (value, continueGenerator)<\/code> &#8212; \u0432\u044b\u0445\u043e\u0434\u0438\u043c \u0432 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0439 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0447\u0430\u0441\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u0430<\/p>\n<\/li>\n<li>\n<p><code>controls = Controls{yield, exit}<\/code> &#8212; \u043f\u0440\u043e\u0441\u0442\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u0443\u0434\u043e\u0431\u043d\u044b\u0439 \u0441\u0438\u043d\u043e\u043d\u0438\u043c<\/p>\n<\/li>\n<li>\n<p><code>generator controls<\/code> &#8212; \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440<\/p>\n<\/li>\n<li>\n<p><code>error \"Generator exit invariant violated\"<\/code> &#8212; \u043c\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0442\u0438\u043f\u0430 <code>Void<\/code> \u0437\u0430\u043f\u0440\u0435\u0442\u0438\u043b\u0438 \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u0443 \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0442\u044c\u0441\u044f \u043a\u0430\u043a-\u043b\u0438\u0431\u043e \u043f\u043e\u043c\u0438\u043c\u043e \u0432\u044b\u0437\u043e\u0432\u0430 <code>exit<\/code>. \u042d\u0442\u0430 \u0441\u0442\u0440\u043e\u0447\u043a\u0430 \u043a\u043e\u0434\u0430 \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c unreachable<\/p>\n<\/li>\n<\/ul>\n<p>\u0424\u0430\u0439\u0431\u0435\u0440\u044b &#8212; \u044d\u0442\u043e \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u044b, \u0443 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 <code>yield<\/code> \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043d\u0430\u0437\u0430\u0434 \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0449\u0438\u043a\u0443 (\u0431\u0435\u0437 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u043a\u0430\u043a\u0438\u0445-\u043b\u0438\u0431\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439). <\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-373843","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/373843","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=373843"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/373843\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=373843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=373843"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=373843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}