Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bind a outside value to an interpreted variable #116

Open
gelisam opened this issue Mar 3, 2021 · 0 comments
Open

bind a outside value to an interpreted variable #116

gelisam opened this issue Mar 3, 2021 · 0 comments

Comments

@gelisam
Copy link
Contributor

gelisam commented Mar 3, 2021

It is easy to move those values from the interpreter to the enclosing program using interpret. But what about moving values from the enclosing program to the interpreter?

If we are only interested in interpreting a single expression which refers to one or more outside values, there are currently two solutions:

  1. if the value has a Show instance, we can interpret that String representation.
  2. if the value has a Typeable instance, we can interpret a function, and then pass our value to the function.

Now that we have runStmt, we can also give a name to a value so that it is available in future expressions. This time, there is only one solution:

  1. if the value has a Show instance, we can run a statement of the form var <- expr using that String representation.

It would be nice to complete the picture using a solution for Typeable instances. I figured out how to implement that solution using the existing primitives, and I think the trick is sufficiently non-obvious that it's worth including in the library:

-- Bind a variable inside the interpreter to a value from outside the
-- interpreter.
bind :: forall a. Typeable a => String -> a -> M ()
bind var value = do
  liftHint $ Hint.runStmt
               ( "tmpIORef <- newIORef (undefined :: "
              ++ show (typeOf (undefined :: a))
              ++ ")"
               )
  tmpIORef <- liftHint
            $ Hint.interpret
                "tmpIORef"
                (Hint.as :: IORef a)
  liftIO $ writeIORef tmpIORef value
  liftHint $ Hint.runStmt (var ++ " <- readIORef tmpIORef")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant