<div dir="ltr">Simon,<div><br></div><div>That's good to hear. That addresses one of the complex use cases for dynCompileExpr that we have.</div><div><br></div><div>I'd like to present two more and see how we can address them. They both follow the same pattern so I will just describe the first one.</div><div><br></div><div>In order to let interpreted Haskell code display images and other complex media to the frontend, the interpreted code uses unsafePerformIO to create a TChan. This TChan holds images (roughly speaking). The interpreted Haskell has access to a function which writes to this TChan. </div><div><br></div><div>The compiled Haskell runs the interpreted Haskell in a separate thread. That thread then writes to this TChan (which lives in the interpreted context). In order to access values in the TChan, IHaskell uses dynCompileExpr on a value of type `IO [Image]`. This value is an IO action that reads from the TChan that lives in the interpreted context. This value is then run in the compiled code, giving IHaskell access to the results generated by the interpreter.</div><div><br></div><div>I do not see how this can be easily replicated with a remote GHCi. The value is `IO [Image]`, so it is not serializable. `[Image]` is serializable; however, in order to get the Image values from the TChan, we would have to run interpreted code, and these images are generated during the runtime of interpreted code already, so we would need to be running two separate interpreted threads at once. This has to be completely invisible to the user and seems to me like it might be a logistical nightmare... And, if I understand correctly, GHC/GHCi are not thread safe, so I can't just use the new equivalent of dynCompileExpr from two threads. (Can I?)</div><div><br></div><div><br></div><div>Thanks,</div><div><br></div><div>-- Andrew</div><div><br></div><div><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Nov 19, 2015 at 1:44 AM, Simon Marlow <span dir="ltr"><<a href="mailto:marlowsd@gmail.com" target="_blank">marlowsd@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Andrew -<br>
<br>
Since the interpreted code is running in a separate process that we spawn using createProcess, we can set stdin/stdout/stderr to be whatever we like, including new pipes.  GHC itself needs two pipes to communicate with the sub-process, but those use separate file descriptors from the std Handles.<br>
<br>
So I think the answer is yes, we can support that more easily with remote GHCi.  I'll think about what API we can provide for it.<br>
<br>
Cheers,<br>
Simon<div><div><br>
<br>
On 18/11/2015 16:26, Andrew Gibiansky wrote:<br>
</div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div>
Simon,<br>
<br>
I'd like to hear how we can support what IHaskell does with remote GHCi.<br>
<br>
One core functionality that we use dynCompileExpr for (not quite<br>
dynCompileExpr, but similar) is getting the standard output of code that<br>
is being run. Any time code is run, we<br>
<br>
1. Create a unix pipe.<br>
2. Set stdout to point to that pipe using dupTo.<br>
3. Use hscStmt with unsafeCoerce to get the other end of the pipe in the<br>
compiled context.<br>
4. Run the statement in the interpreted context in a separate thread;<br>
meanwhile, read from the pipe to get the stdout of the code running in<br>
the interpreted context.<br>
5. When it is done running, move stdout back to point to the read stdout<br>
and close the unix pipe file handle.<br>
6. Send the stdout (both intermediate values and the final value) to the<br>
frontend to display to the user.<br>
<br>
The key here is that we can access directly the file handle created by<br>
the interpreted code. If the interpreted code is remote, we clearly<br>
cannot read from a pipe it creates. In your remote GHCi, how could we<br>
solve this problem?<br>
<br>
In general, how would stdin and stdout work? Would there be a clean way<br>
to feed the remote process its stdin and receive its stdout and stderr?<br>
That would effectively mean stdin/stdout/stderr are configurable which<br>
would be a godsend for IHaskell.<br>
<br>
-- Andrew<br>
<br>
On Wed, Nov 18, 2015 at 1:45 AM, Simon Marlow <<a href="mailto:marlowsd@gmail.com" target="_blank">marlowsd@gmail.com</a><br></div></div><div><div>
<mailto:<a href="mailto:marlowsd@gmail.com" target="_blank">marlowsd@gmail.com</a>>> wrote:<br>
<br>
    On 18/11/2015 01:41, Manuel M T Chakravarty wrote:<br>
<br>
        Hi Simon,<br>
<br>
        While this is an interesting proposal, Haskell for Mac strongly<br>
        relies on running interpreted code in the same process. I’m using<br>
        ’dynCompileExpr’ as well as ’hscStmtWithLocation’ and some other<br>
        stuff.<br>
<br>
<br>
    Let me say first of all that I'm not going to remove anything, so<br>
    there's no need to worry.  But I'd like to explore exactly what you<br>
    need, so that we can see whether there's a way to accommodate it<br>
    with a separate-process implementation.<br>
<br>
    hscStmtWithLocation is part of the core GHCi functionality, it is<br>
    definitely supported.  It has a slightly different signature:<br>
<br>
    hscStmtWithLocation :: HscEnv<br>
                         -> String -- ^ The statement<br>
                         -> String -- ^ The source<br>
                         -> Int    -- ^ Starting line<br>
                         -> IO ( Maybe ([Id]<br>
                               , RemoteHValue {- IO [HValue] -}<br>
                               , FixityEnv))<br>
<br>
    RemoteHValue is a reference to a value in the interpreter's context.<br>
    These have to be evaluated via an explicit API, rather than just<br>
    unsafeCoercing HValue as we do now.  (this is not strictly speaking<br>
    part of the GHC API, so a separate but interesting question is: why<br>
    did you need to use this directly, and what should we add to the GHC<br>
    API?)<br>
<br>
    I believe that many uses of dynCompileExpr can be changed so that<br>
    the code using the resulting value is moved into the interpreter's<br>
    context, and then there's no problem.<br>
<br>
        This is quite crucial for some of the interactive<br>
        functionality. Imagine a game where the game engine is in Swift<br>
        linked into the main application and the game logic is in<br>
        *interpreted* Haskell code. The engine calls into the Haskell code<br>
        multiple times per frame of the animation and for all<br>
        keyboard/mouse/etc input (using StablePtr and ForeignPtr to<br>
        construct<br>
        the scene graph across the Swift and Haskell heap).<br>
<br>
<br>
    So my question is, why wouldn't you run the whole game engine in the<br>
    interpreter's context?  That's what would happen if you were to load<br>
    the program into GHCi and run it.  Directly calling back and forth<br>
    between the client of the GHC API and the program being interpreted<br>
    is arguably a strange thing to do, and it's kind of accidental that<br>
    we allow it.<br>
<br>
        I actually also might have a use for the architecture that you are<br>
        proposing. However, I really would like to keep the ability to, at<br>
        least, optionally run interpreted code in the same process (without<br>
        profiling etc). Do you think we could have both?<br>
<br>
<br>
    We can certainly have both, it's straightforward to implement, but I<br>
    don't get to throw away some of the hacks we have to support<br>
    same-process execution, which would be a shame.  We just add more<br>
    code rather than<br>
<br>
<br>
        Cheers,<br>
        Manuel<br>
<br></div></div>
            Simon Marlow <<a href="mailto:marlowsd@gmail.com" target="_blank">marlowsd@gmail.com</a> <mailto:<a href="mailto:marlowsd@gmail.com" target="_blank">marlowsd@gmail.com</a>>>:<span><br>
<br>
            Hi folks - I've been thinking about changing the way we run<br>
            interpreted code so that it would be run in a separate<br>
            process.  It turns out this has quite a few benefits, and<br>
            would let us kill some of the really awkward hacks we have<br>
            in GHC to work around problems that arise because we're<br>
            running interpreted code and the compiler on the same runtime.<br>
<br>
            I summarised the idea here:<br>
            <a href="https://ghc.haskell.org/trac/ghc/wiki/RemoteGHCi" rel="noreferrer" target="_blank">https://ghc.haskell.org/trac/ghc/wiki/RemoteGHCi</a><br>
<br>
            I'd be interested to hear if anyone has any thoughts around<br>
            this, particularly if doing this would make your life<br>
            difficult in some way. Are people relying on dynCompileExpr<br>
            for anything?<br>
<br>
            Cheers,<br>
            Simon<br>
            _______________________________________________<br>
            ghc-devs mailing list<br></span>
            <a href="mailto:ghc-devs@haskell.org" target="_blank">ghc-devs@haskell.org</a> <mailto:<a href="mailto:ghc-devs@haskell.org" target="_blank">ghc-devs@haskell.org</a>><span><br>
            <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs</a><br>
<br>
<br>
    _______________________________________________<br>
    ghc-devs mailing list<br></span>
    <a href="mailto:ghc-devs@haskell.org" target="_blank">ghc-devs@haskell.org</a> <mailto:<a href="mailto:ghc-devs@haskell.org" target="_blank">ghc-devs@haskell.org</a>><br>
    <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/ghc-devs</a><br>
<br>
<br>
</blockquote>
</blockquote></div><br></div></div>