<span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; border-collapse: collapse; "><div>I&#39;m trying to receive small segments of Haskell code over a socket, and be able to evaluate them in real time in GHCI.</div>
<div>I&#39;ve already downloaded Hint and have run the test code, and it&#39;s working great.  I&#39;m also using the socket server code from Ch.27 of &quot;Real World Haskell&quot;</div><div>and that is working well also.  </div>
<div><br></div><div>     directly below is the function from the socket server code that handles the incoming messages.</div><div>     Instead of doing this: &quot;putStrLn msg&quot;... I want to send whatever is captured in &quot;msg&quot; to the GHC interpreter that is used in the Hint code, something like this:  &quot;eval msg&quot;.</div>
<div>     I&#39;m not sure how to combine both of these functionalities to get them to work with each other..</div><div><br></div><div><div>      -- A simple handler that prints incoming packets</div><div>      plainHandler :: HandlerFunc</div>
<div>      plainHandler addr msg = </div><div>         putStrLn msg </div></div><div><br></div><div><br></div><div>Below is the full  code for the socket server, then below that is &quot;SomeModule&quot; used in the Hint example test below that.</div>
<div><br></div><div>-- file: ch27/syslogserver.hs</div><div>import Data.Bits</div><div>import Network.Socket</div><div>import Network.BSD</div><div>import Data.List</div><div><br></div><div>type HandlerFunc = SockAddr -&gt; String -&gt; IO ()</div>
<div><br></div><div>serveLog :: String              -- ^ Port number or name; 514 is default</div><div>         -&gt; HandlerFunc         -- ^ Function to handle incoming messages</div><div>         -&gt; IO ()</div><div>
serveLog port handlerfunc = withSocketsDo $</div><div>    do -- Look up the port.  Either raises an exception or returns</div><div>       -- a nonempty list.  </div><div>       addrinfos &lt;- getAddrInfo </div><div>                    (Just (defaultHints {addrFlags = [AI_PASSIVE]}))</div>
<div>                    Nothing (Just port)</div><div>       let serveraddr = head addrinfos</div><div><br></div><div>       -- Create a socket</div><div>       sock &lt;- socket (addrFamily serveraddr) Datagram defaultProtocol</div>
<div><br></div><div>       -- Bind it to the address we&#39;re listening to</div><div>       bindSocket sock (addrAddress serveraddr)</div><div><br></div><div>       -- Loop forever processing incoming data.  Ctrl-C to abort.</div>
<div>       procMessages sock</div><div>    where procMessages sock =</div><div>              do -- Receive one UDP packet, maximum length 1024 bytes,</div><div>                 -- and save its content into msg and its source</div>
<div>                 -- IP and port into addr</div><div>                 (msg, _, addr) &lt;- recvFrom sock 1024</div><div>                 -- Handle it</div><div>                 handlerfunc addr msg</div><div>                 -- And process more messages</div>
<div>                 procMessages sock</div><div><br></div><div>-- A simple handler that prints incoming packets</div><div>plainHandler :: HandlerFunc</div><div>plainHandler addr msg = </div><div>    putStrLn msg</div><div>
   </div><div><br></div><div>-- main = serveLog &quot;8008&quot; plainHandler</div><div>----------------------------------------------------------------------------------------------------------------</div><div><br></div>
<div><div>module SomeModule(g, h) where</div><div><br></div><div>f = head</div><div><br></div><div>g = f [f]</div><div><br></div><div>h = f</div><div><br></div><div>----------------------------------------------------------------------------------------------------------------</div>
</div><div><br></div><div><div>import Control.Monad</div><div>import Language.Haskell.Interpreter</div><div><br></div><div>main :: IO ()</div><div>main = do r &lt;- runInterpreter testHint</div><div>          case r of</div>
<div>            Left err -&gt; printInterpreterError err</div><div>            Right () -&gt; putStrLn &quot;that&#39;s all folks&quot;</div><div><br></div><div>-- observe that Interpreter () is an alias for InterpreterT IO ()</div>
<div>testHint :: Interpreter ()</div><div>testHint =</div><div>    do</div><div>      say &quot;Load SomeModule.hs&quot;</div><div>      loadModules [&quot;SomeModule.hs&quot;]</div><div>      --</div><div>      say &quot;Put the Prelude, Data.Map and *SomeModule in scope&quot;</div>
<div>      say &quot;Data.Map is qualified as M!&quot;</div><div>      setTopLevelModules [&quot;SomeModule&quot;]</div><div>      setImportsQ [(&quot;Prelude&quot;, Nothing), (&quot;Data.Map&quot;, Just &quot;M&quot;)]</div>
<div>      --</div><div>      say &quot;Now we can query the type of an expression&quot;</div><div>      let expr1 = &quot;M.singleton (f, g, h, 42)&quot;</div><div>      say $ &quot;e.g. typeOf &quot; ++ expr1</div><div>
      say =&lt;&lt; typeOf expr1</div><div>      --</div><div>      say $ &quot;Observe that f, g and h are defined in SomeModule.hs, &quot; ++</div><div>            &quot;but f is not exported. Let&#39;s check it...&quot;</div>
<div>      exports &lt;- getModuleExports &quot;SomeModule&quot;</div><div>      say (show exports)</div><div>      --</div><div>      say &quot;We can also evaluate an expression; the result will be a string&quot;</div><div>
      let expr2 = &quot;length $ concat [[f,g],[h]]&quot;</div><div>      say $ concat [&quot;e.g. eval &quot;, show expr1]</div><div>      a &lt;- eval expr2</div><div>      say (show a)</div><div>      --</div><div>      say &quot;Or we can interpret it as a proper, say, int value!&quot;</div>
<div>      a_int &lt;- interpret expr2 (as :: Int)</div><div>      say (show a_int)</div><div>      --</div><div>      say &quot;This works for any monomorphic type, even for function types&quot;</div><div>      let expr3 = &quot;\\(Just x) -&gt; succ x&quot;</div>
<div>      say $ &quot;e.g. we interpret &quot; ++ expr3 ++</div><div>            &quot; with type Maybe Int -&gt; Int and apply it on Just 7&quot;</div><div>      fun &lt;- interpret expr3 (as :: Maybe Int -&gt; Int)</div>
<div>      say . show $ fun (Just 7)</div><div>      --</div><div>      say &quot;And sometimes we can even use the type system to infer the expected type (eg Maybe Bool -&gt; Bool)!&quot;</div><div>      bool_val &lt;- (interpret expr3 infer `ap` (return $ Just False))</div>
<div>      say (show $ not bool_val)</div><div>      --</div><div>      say &quot;Here we evaluate an expression of type string, that when evaluated (again) leads to a string&quot;</div><div>      res &lt;- interpret &quot;head $ map show [\&quot;Worked!\&quot;, \&quot;Didn&#39;t work\&quot;]&quot; infer &gt;&gt;= flip interpret infer</div>
<div>      say res</div><div><br></div><div><br></div><div>say :: String -&gt; Interpreter ()</div><div>say = liftIO . putStrLn</div><div><br></div><div>printInterpreterError :: InterpreterError -&gt; IO ()</div><div>printInterpreterError e = putStrLn $ &quot;Ups... &quot; ++ (show e)</div>
</div></span>