Search code examples
haskelltypescontinuations

Continuation function implementations based upon input type?


I have a very simple continuation function (avoided using Monads for simplicity sake):

data C a = C {unwrap :: (a -> a) -> a}

Essentially, I'm trying to perform different implementations based on input type, something similar to (sudo-code):

data Gadt a where
  AString :: String -> Gadt Bool
  AInt :: Int -> Gadt Bool

data C a = C {unwrap :: (a -> Gadt a) -> a}

example :: a -> C a
example v = C $ \f -> | (v :: Int) == True      = f (AInt v)
                      | (v :: String) == True   = f (AString v)

cont :: Gadt a -> a
cont (AInt v)       = ...
cont (AString v)    = ...

Am I overlooking a basic solution here? I'm new to continuations so I may of just overlooked something simple.


Solution

  • I've come across a potential solution, following advice from here:

    newtype C r a = C {runC :: (a -> r) -> r}
    
    data Hole = Hole1 Int | Hole2 String | Hole3 Bool
    
    example :: String -> C Bool a
    example s = C $ \f -> do
       x <- f (Hole1 22)
       y <- f (Hole2 "string")
       k (Hole3 False)
    
    cont :: Hole -> Bool
    cont (Hole1 x) = ...
    cont (Hole2 x) = ...
    cont (Hole3 x) = ...
    

    This enables specific implementations based on type by wrapping the input type inside the Hole data structure.