Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Structuring continuation types?

I'm going over continuations and I've come across two different approaches to structuring continuation types:

newtype C r a = C {runC :: (a -> r) -> r}

exampleFunction :: String -> C Bool String
exampleFunction s = C $ \t -> if length s > 10 then t s else False

continuationFunction :: String -> Bool
continuationFunction s = True

main = do
 let suspendedFunc = exampleFunction "testing"
 let completedFunc = runC suspendedFunc $ continuationFunction

versus the approach taken in Poor Mans Concurrency:

type C r a = (a -> r) -> r

exampleFunction :: String -> C Bool String
exampleFunction s = \t -> if length s > 10 then t s else False

...

I understand that the latter approach doesn't use an explicit data constructor.

  1. What are the practical differences of these approaches?
  2. Will this impact when I try to use this over a general type with a monad? Such as:

    data Hole = Hole1 Int | Hole2 String
    
    type C r m a = (a -> m r) -> m r
    
    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  
    
like image 808
Babra Cunningham Avatar asked Feb 17 '17 08:02

Babra Cunningham


People also ask

What is a GP vs LP?

A private equity firm is called a general partner (GP) and its investors that commit capital are called limited partners (LPs). Limited partners generally consist of pension funds, institutional accounts and wealthy individuals.

What is a continuation vehicle transaction?

In a typical Continuation Fund transaction, one or more assets of an existing fund (often one that is nearing the end of its term) are acquired by a new vehicle managed by the same sponsor (the “Continuation Fund”).

What are continuation funds?

The structure of a Continuation Fund involves the formation of a new fund for the purpose of acquiring one or more assets from an original fund. The same fund sponsor continues to manage those assets as part of the investment objectives of the new fund.

What are Ucits and AIFS?

A CCF can be established as a UCITS fund (Undertakings for Collective Investment in Transferable Securities) or an AIF (Alternative Investment Fund). Tax transparency is the main feature, which differentiates the CCF from other types of Irish funds. The CCF is authorised and regulated by the Central Bank.


1 Answers

The differences are the usual differences between type and newtype.

A type synonym is just a new name for an existing type. type synonyms can't be partially applied, because the compiler expands the definition during type checking. For example, this is no good, even with TypeSynonymInstances:

type TypeCont r a = (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 -> (f x) q)

newtypes, while operationally equivalent to the types they wrap, are separate entities in the type system. This means that newtypes can be partially applied.

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

instance Monad (NewtypeCont r) where
    return x = Cont ($ x)
    Cont k >>= f = Cont $ \q -> k (\x -> runCont (f x) q)
like image 63
Benjamin Hodgson Avatar answered Sep 20 '22 16:09

Benjamin Hodgson