如何只用纯函数表达定界的延续?

时间:2019-01-06 12:00:06

标签: haskell continuations continuation-passing delimited-continuations

我通过Oleg的定界延续tutorial

newtype Cont r a = Cont{runCont :: (a -> r) -> r}

instance Monad (Cont r) where
  return x = Cont (\k -> k x)
  Cont m >>= f = Cont (\k -> m (\v -> runCont (f v) k))

runC :: Cont r r -> r
runC m = runCont m id

reset :: Cont a a -> Cont r a
reset = return . runC

shift :: ((a -> r) -> Cont r r) -> Cont r a
shift f = Cont (runC . f)

liftM2 (-)
  (reset
    (liftM2 (+) (return 3)
      (shift (\k -> return (5*2))))) -- drop the continuation
        (return 1) -- 9

由于延续基本上是函数,reset / shift甚至不是monad api的一部分,所以我想知道如何在没有newtype和monad机制的情况下实现定界的延续。

这是到目前为止我要提出的:

reset :: Cont a a -> Cont r a -- becomes
reset :: ((a -> a) -> a) -> (a -> r) -> r
reset k f = f $ k id

shift :: ((a -> r) -> Cont r r) -> Cont r a -- becomes
shift :: ((a -> r) -> (r -> r) -> r) -> (a -> r) -> r
shift f k = f k id

我很确定这是完全错误的,如果不是这样,我不知道如何正确应用运算符:

(1-) (reset ((3+) (shift (\k -> 5*2)))) -- yields

• Non type-variable argument in the constraint: Num ((a -> a) -> a)
  (Use FlexibleContexts to permit this)
• When checking the inferred type
    t1 :: forall a r.
          (Num ((a -> a) -> a), Num ((a -> r) -> r)) =>
          (a -> r) -> r

1 个答案:

答案 0 :(得分:1)

继续前进!

import Prelude hiding (return)

-- reset :: Cont a a -> Cont r a
reset :: ((a -> a) -> a) -> (a -> r) -> r
reset k f = f $ k id

-- shift :: ((a -> r) -> Cont r r) -> Cont r a
shift :: ((a -> r) -> (r -> r) -> r) -> (a -> r) -> r
shift f k = f k id

-- return :: a -> Cont r a
return :: a -> (a -> r) -> r
return a k = k a

-- liftM2 :: (a -> b -> c) -> Cont r a -> Cont r b -> Cont r c
liftM2 :: (a -> b -> c) -> ((a -> r) -> r) -> ((b -> r) -> r) -> (c -> r) -> r
liftM2 f ma mb k = ma $ \a -> mb $ \b -> k (f a b)

example :: Num a => (a -> r) -> r
example = liftM2 (-) (reset (liftM2 (+) (return 3) (shift (\k -> return (5*2))))) (return 1)

(1-) (reset ((3+) (shift (\k -> 5*2))))的一个问题是,您实际上将Cont的{​​{1}}的{​​{1}}替换为return

id

通常,当ghci说“我们需要能够将函数视为数字才能使此代码起作用”时,这意味着您在某个地方犯了错误:)