Skip to content

Conversation

meipp
Copy link

@meipp meipp commented Sep 19, 2025

Addresses #47

@alt-romes
Copy link
Collaborator

I've rebased this on master. Looking into the segfault now.

@alt-romes
Copy link
Collaborator

The issue was evalStmt receives an EvalExpr ForeignHValue representing a IO [a] action.

With your code, if you make the method of the instance return an IO [a] action then evalStmt will find no problem in executing it:

module Main where

import qualified Data.Text as T

class Debug a where
  debugDisplayTree :: a -> IO [String]

instance Debug T.Text where
  debugDisplayTree txt = print ("Text: " ++ T.unpack txt) >> return []

f :: T.Text -> T.Text
f x = x

main :: IO ()
main = do
  print (f $ T.pack "Hello, Haskell!")

You should see what the result of debugDisplayTree being printed out only by stopping at f, because the method is being called on x!

If you look in GHC.Runtime.Interpreter you will find other functions which wrap "foreign" evaluation. They may be closer to what you intended to explore. Namely:

-- | Execute an action of type @IO ()@
evalIO :: Interp -> ForeignHValue -> IO ()
evalIO interp fhv =
  liftIO $ withForeignRef fhv $ \fhv ->
    interpCmd interp (EvalIO fhv) >>= fromEvalResult

-- | Execute an action of type @IO String@
evalString :: Interp -> ForeignHValue -> IO String
evalString interp fhv =
  liftIO $ withForeignRef fhv $ \fhv ->
    interpCmd interp (EvalString fhv) >>= fromEvalResult

-- | Execute an action of type @String -> IO String@
evalStringToIOString :: Interp -> ForeignHValue -> String -> IO String
evalStringToIOString interp fhv str =
  liftIO $ withForeignRef fhv $ \fhv ->
    interpCmd interp (EvalStringToString fhv str) >>= fromEvalResult

Feel free to keep exploring. I still haven't had the design discussion about the type class we'd want to have, at least to a first approximation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants