Structuring continuation types?

I move on to continuations, and I came across two different approaches to structuring continuation types:

newtype C ra = C {runC :: (a -> r) -> r} exampleFunction :: String -> C Bool String exampleFunction s = C $ \t -> if length s > 10 then ts else False continuationFunction :: String -> Bool continuationFunction s = True main = do let suspendedFunc = exampleFunction "testing" let completedFunc = runC suspendedFunc $ continuationFunction 

compared to the approach adopted by Poor Mans Concurrency :

 type C ra = (a -> r) -> r exampleFunction :: String -> C Bool String exampleFunction s = \t -> if length s > 10 then ts else False ... 

I understand that the latter approach does not use an explicit data constructor.

  • What are the practical differences between these approaches?
  • Will it affect when I try to use it in a generic way with a monad? For instance:

     data Hole = Hole1 Int | Hole2 String type C rma = (a -> mr) -> mr exampleFunction :: String -> C Bool Maybe Hole exampleFunction s = \t -> do x <- t (Hole1 11) y <- t (Hole2 "test") ... continuationFunction :: Hole -> Bool continuationFunction (Hole1 x) = False continuationFunction (Hole2 y) = True 
+6
source share
1 answer

Differences are the usual differences between type and newtype .

A type A synonym is simply a new name for an existing type. type synonyms cannot be partially applied because the compiler extends the definition during type checking. For example, this is not good, even with TypeSynonymInstances :

 type TypeCont ra = (a -> r) -> r instance Monad (TypeCont r) where -- "The type synonym 'TypeCont' should have 2 arguments, but has been given 1" return x = ($ x) k >>= f = \q -> k (\x -> (fx) q) 

newtype s, while operatively equivalent to their types, are separate objects in the type system. This means that newtype can be partially applied.

 newtype NewtypeCont ra = Cont { runCont :: (a -> r) -> r } instance Monad (NewtypeCont r) where return x = Cont ($ x) Cont k >>= f = Cont $ \q -> k (\x -> runCont (fx) q) 
+3
source

Source: https://habr.com/ru/post/1015055/


All Articles