Phil,

> Where is there a description of the IO request that lets the
> continuation model emulate the stream model???

Here is mine.  Nigel will send his.

Let (IO a) be the type of I/O requests which can return values of type "a"
(after carrying out some I/O action).

    ref     :: a -> (Ref a -> IO b) -> IO b
    deref   :: Ref a -> (a -> IO b) -> IO b
    assign  :: Ref a -> a -> IO b -> b

    suspend :: IO a -> (a -> IO b) -> IO b
    return  :: a -> IO a

Let the remaining builtin I/O requests be the usual ones (with modified types),
e.g.

    writeFile :: Name -> String -> (IOError -> IO a) -> IO a -> IO a

We can easily (in standard Haskell) write a function to perform a single
Request by using the corresponding continuation request, and generating the
appropriate Response.

    perform :: Request -> (Response -> IO a) -> IO a

Now let us define the efficient emulation of stream I/O by continuation I/O.

    emulate :: Dialogue -> IO ()

    emulate program = ref []
     (\ respsVar -> responses (\ resps -> run (program resps))

        where

        responses cont = suspend (get (\ resp ->
                                  responses (\ resps ->
                                  return (resp:resps)))) cont

        run []         = done
        run (req:reqs) = perform req (\ resp -> put resp (run reqs))

        get cont       = deref respsVar (\ resps -> case resps of
                         [] -> error "Request/Response Synchronisation Error"
                         (resp:resps) -> assign respsVar resps (cont resp))

        put resp cont  = deref respsVar (\ resps ->
                         assign respsVar (resps++[resp]) cont)

     )

(Notice the nice error message which appears when the user attempts to examine
the response to a request that has not yet been carried out.)

References:

(1) My own unpublished work.  In the long term, I am planning to develop
    asynchronous I/O for functional languages.  In the short term, I am working
    on portable implementation.

(2) My MSc thesis (for references/assignments).

    @mastersthesis{ireland:msc,
            Author = "Evan Peter Ireland",
            Title = "Writing Interactive and File-Processing Functional Programs: a
 Method and Implementation",
            School = "Victoria University of Wellington",
            Month = "March",
            Year = 1989
    }

(3) Andrew Gordon's PFL+ (for suspend/return).

    @techreport{gordon:pfl+,
            Author = "Andrew Gordon",
            Title = "{PFL+}: A Kernel Scheme for Functional {I/O}",
            Institution = "University of Cambridge Computer Laboratory",
            Year = 1989,
            Type = "Technical Report",
            Number = 160,
            Address = "New Museums Site, Pembroke Street, Cambridge CB2 3QG",
            Month = "February"
    }

Reply via email to