Skip to content

A peer-to-peer stack for code collaboration

fix ref mutation in lambdas (#51)

James Haydon4cf026811fd3
diff --git a/src/Radicle/Internal/Core.hs b/src/Radicle/Internal/Core.hs
index d728882..12bf8ad 100644
--- a/src/Radicle/Internal/Core.hs
+++ b/src/Radicle/Internal/Core.hs
@@ -135,24 +135,6 @@ data Bindings m = Bindings
, bindingsNextRef :: Int
} deriving (Generic)
-instance Semigroup (Bindings m) where
- a <> b = Bindings
- { bindingsEnv = bindingsEnv a <> bindingsEnv b
- , bindingsPrimops = bindingsPrimops a <> bindingsPrimops b
- , bindingsRefs = bindingsRefs a
- <> IntMap.mapKeys (+ bindingsNextRef a) (bindingsRefs b)
- , bindingsNextRef = bindingsNextRef a + bindingsNextRef b
- }
-
-instance Monoid (Bindings m) where
- mempty = Bindings
- { bindingsEnv = mempty
- , bindingsPrimops = mempty
- , bindingsRefs = mempty
- , bindingsNextRef = 0
- }
- mappend = (<>)
-
-- | The environment in which expressions are evaluated.
newtype LangT r m a = LangT
{ fromLangT :: ExceptT (LangError Value) (StateT r m) a }
@@ -170,13 +152,15 @@ runLang
-> m (Either (LangError Value) a, Bindings m)
runLang e l = runStateT (runExceptT $ fromLangT l) e
--- | Like 'local' or 'withState'
-withEnv :: Monad m => (Bindings m -> Bindings m) -> Lang m a -> Lang m a
+-- | Like 'local' or 'withState'. Will run an action with a modified environment
+-- and then restore the original environment. Other bindings (i.e. primops and
+-- refs) are not affected.
+withEnv :: Monad m => (Env Value -> Env Value) -> Lang m a -> Lang m a
withEnv modifier action = do
- oldEnv <- get
- modify modifier
+ oldEnv <- gets bindingsEnv
+ modify $ \s -> s { bindingsEnv = modifier oldEnv }
res <- action
- put oldEnv
+ modify $ \s -> s { bindingsEnv = oldEnv }
pure res
-- * Functions
@@ -220,7 +204,7 @@ eval val = do
Lambda [bnd] body (Just closure) -> do
let mappings = GhcExts.fromList [(bnd, val)]
modEnv = mappings <> closure
- NonEmpty.last <$> withEnv (\e' -> e' { bindingsEnv = modEnv})
+ NonEmpty.last <$> withEnv (const modEnv)
(traverse eval body)
_ -> throwError $ TypeError "Trying to apply a non-function"
@@ -269,7 +253,7 @@ mfn $$ vs = do
vs' <- traverse baseEval vs
let mappings = GhcExts.fromList (zip bnds vs')
NonEmpty.last <$> withEnv
- (\e -> e { bindingsEnv = mappings <> bindingsEnv e })
+ (mappings <>)
(traverse baseEval body)
Lambda bnds body (Just closure) ->
if length bnds /= length vs
@@ -279,7 +263,7 @@ mfn $$ vs = do
vs' <- traverse baseEval vs
let mappings = GhcExts.fromList (zip bnds vs')
modEnv = mappings <> closure
- NonEmpty.last <$> withEnv (\e -> e { bindingsEnv = modEnv })
+ NonEmpty.last <$> withEnv (const modEnv)
(traverse baseEval body)
_ -> throwError $ TypeError "Trying to apply a non-function"
diff --git a/src/Radicle/Internal/Subscriber.hs b/src/Radicle/Internal/Subscriber.hs
index ac749a4..388d3e4 100644
--- a/src/Radicle/Internal/Subscriber.hs
+++ b/src/Radicle/Internal/Subscriber.hs
@@ -86,7 +86,7 @@ replPrimops = Map.fromList $ first toIdent <$>
[x, v] -> do
x' <- eval x
v' <- eval v
- s <- get
+ e <- gets bindingsEnv
case (x', v') of
(Dict m, fn) -> case Map.lookup (Atom $ toIdent "getter") m of
Nothing -> throwError
@@ -102,7 +102,7 @@ replPrimops = Map.fromList $ first toIdent <$>
-- Similarly, the application of the subscriber
-- function is evaluated in the original
-- environment.
- void $ withEnv (const s) (fn $$ [quote line])
+ void $ withEnv (const e) (fn $$ [quote line])
_ -> throwError $ TypeError "subscribe-to!: Expected dict"
xs -> throwError $ WrongNumberOfArgs "subscribe-to!" 2 (length xs))
]
diff --git a/test/spec/Radicle/Tests.hs b/test/spec/Radicle/Tests.hs
index 8974a8e..9892d26 100644
--- a/test/spec/Radicle/Tests.hs
+++ b/test/spec/Radicle/Tests.hs
@@ -280,6 +280,50 @@ test_eval =
res = runTest' prog
res @?= Right (Number 6)
+ , testCase "mutations to refs are persisted beyond a lambda's scope" $ do
+ let prog = [s|
+ (define inc-ref
+ (lambda (r)
+ (define temp (read-ref r))
+ (write-ref r (+ temp 1))
+ ))
+ (define a (ref 0))
+ (inc-ref a)
+ (read-ref a)
+ |]
+ let prog2 = [s|
+ (define ref-mapper
+ (lambda (f)
+ (lambda (r)
+ (define temp (read-ref r))
+ (write-ref r (f temp)))))
+ (define ref-incer (ref-mapper (lambda (x) (+ x 1))))
+ (define foo (ref 0))
+ (ref-incer foo)
+ (read-ref foo)
+ |]
+ runTest' prog @?= Right (Number 1)
+ runTest' prog2 @?= Right (Number 1)
+
+ , testCase "muliple refs mutated in multiple lambdas" $ do
+ let prog = [s|
+ (define make-counter
+ (lambda ()
+ (define current (ref 0))
+ (lambda ()
+ (define temp (read-ref current))
+ (write-ref current (+ temp 1))
+ temp)))
+ (define c1 (make-counter))
+ (c1)
+ (c1)
+ (define c2 (make-counter))
+ (c2)
+ (c1)
+ (list (c1) (c2))
+ |]
+ runTest' prog @?= Right (List [Number 3, Number 1])
+
, testProperty "read-ref . ref == id" $ \(v :: Value) -> do
let derefed = runTest' $ toS [i|(read-ref (ref #{renderPrettyDef v}))|]
orig = runTest' $ toS [i|#{renderPrettyDef v}|]