Skip to content

A peer-to-peer stack for code collaboration

Move from ST to State.

Julian K. Arni8e48441d4e1e
diff --git a/src/Radicle.hs b/src/Radicle.hs
index bb828b6..bbc4715 100644
--- a/src/Radicle.hs
+++ b/src/Radicle.hs
@@ -20,7 +20,6 @@ module Radicle
, Ident
, Reference(..)
, makeRefs
- , labelRefs
, coerceRefs
, fromIdent
, mkIdent
diff --git a/src/Radicle/Internal/Core.hs b/src/Radicle/Internal/Core.hs
index c1db038..e94d79a 100644
--- a/src/Radicle/Internal/Core.hs
+++ b/src/Radicle/Internal/Core.hs
@@ -4,8 +4,6 @@ module Radicle.Internal.Core where
import Control.Monad.Except (ExceptT(..), MonadError, catchError,
runExceptT, throwError)
-import Control.Monad.ST.Trans (STRef, STT, newSTRef, readSTRef,
- runSTT, writeSTRef)
import Control.Monad.State
import Data.Bifunctor (first)
import Data.Data (Data)
@@ -15,7 +13,9 @@ import Data.Functor.Foldable (Fix(..), cata)
import Data.List.NonEmpty (NonEmpty)
import qualified Data.List.NonEmpty as NonEmpty
import Data.Map (Map)
+import Data.IntMap (IntMap)
import qualified Data.Map as Map
+import qualified Data.IntMap as IntMap
import Data.Maybe (catMaybes, isJust)
import Data.Scientific (Scientific)
import Data.Semigroup (Semigroup, (<>))
@@ -48,8 +48,8 @@ data LangError r =
-- catching exceptions.
errorToValue
:: Monad m
- => LangError (Value (Reference r))
- -> Lang r m (Ident, Value (Reference r))
+ => LangError (Value Reference)
+ -> Lang m (Ident, Value Reference)
errorToValue e = case e of
UnknownIdentifier i -> makeVal
( "unknown-identifier"
@@ -78,22 +78,32 @@ errorToValue e = case e of
makeA = quote . Atom
makeVal (t,v) = pure (Ident t, Dict $ Map.mapKeys Ident . fromList $ v)
-newtype Reference s = Reference { getReference :: STRef s (Value (Reference s)) }
- deriving (Eq)
+newtype Reference = Reference { getReference :: Int }
+ deriving (Show, Read, Ord, Eq, Generic)
-- | Create a new ref with the supplied initial value.
-newRef :: Monad m => Value (Reference s) -> Lang s m (Value (Reference s))
-newRef v = LangT $ lift $ lift $ Ref . Reference <$> newSTRef v
+newRef :: Monad m => Value Reference -> Lang m (Value Reference)
+newRef v = do
+ b <- get
+ let ix = bindingsNextRef b
+ put $ b { bindingsNextRef = succ ix
+ , bindingsRefs = IntMap.insert ix v $ bindingsRefs b
+ }
+ pure . Ref $ Reference ix
-- | Read the value of a reference.
-readRef :: Monad m => Reference s -> Lang s m (Value (Reference s))
-readRef (Reference r) = LangT . lift . lift $ readSTRef r
+readRef :: MonadError (LangError (Value Reference)) m => Reference -> Lang m (Value Reference)
+readRef (Reference r) = do
+ refs <- gets bindingsRefs
+ case IntMap.lookup r refs of
+ Nothing -> throwError $ Impossible "undefined reference"
+ Just v -> pure v
-- | An expression or value in the language.
--
--- The parameter is for refs. Usually it is 'Reference s' (with 's' being the
--- thread param of the ST monad). However, we first parse into 'Fix Value'
--- before converting it into 'Reference' (see 'makeRefs').
+-- The parameter is for refs. Usually it is 'Reference'. However, we first
+-- parse into 'Fix Value' before converting it into 'Reference' (see
+-- 'makeRefs').
--
-- A Value that no longer contains any references can have type 'Value Void',
-- indicating it is safe to share between threads or chains.
@@ -118,7 +128,7 @@ data Value r =
-- | Replace all Refs containing 'Fix Value' into ones containing references to
-- those values.
-makeRefs :: Monad m => Value (Fix Value) -> Lang s m (Value (Reference s))
+makeRefs :: Monad m => Value (Fix Value) -> Lang m (Value Reference)
makeRefs v = cata go (Fix v)
where
go x = case x of
@@ -129,20 +139,10 @@ makeRefs v = cata go (Fix v)
Primop i -> pure $ Primop i
Dict m -> Dict <$> sequence (go <$> m)
List vs -> List <$> sequence (go <$> vs)
- Ref i -> i >>= newRef
+ Ref i -> i >>= eval >>= newRef
Lambda is bd e -> Lambda is <$> sequence (go <$> bd)
<*> traverse sequence (fmap go <$> e)
--- | Replace all refs containing a 'Reference' into ones containing a number
--- representing that 'Reference'.
-labelRefs :: Value (Reference s) -> Value Int
-labelRefs v = evalState (traverse go v) (0, [])
- where
- go :: Reference s -> State (Int, [(Reference s, Int)]) Int
- go s = get >>= \(cur, mapping) -> case lookup s mapping of
- Nothing -> put (cur + 1, (s, cur):mapping) >> pure cur
- Just ix -> pure ix
-
-- | Safely coerce a 'Value' containing no refs into one of a different type.
coerceRefs :: Value Void -> Value a
coerceRefs = unsafeCoerce
@@ -169,34 +169,36 @@ instance IsList (Env s) where
toList = GHC.Exts.toList . fromEnv
-- | Primop mappings. The parameter specifies the monad the primops run in.
-type Primops s m = Map Ident ([Value (Reference s)] -> Lang s m (Value (Reference s)))
+type Primops m = Map Ident ([Value Reference] -> Lang m (Value Reference))
-- | Bindings, either from the env or from the primops.
-data Bindings s m = Bindings
- { bindingsEnv :: Env (Value (Reference s))
- , bindingsPrimops :: Primops s m
+data Bindings m = Bindings
+ { bindingsEnv :: Env (Value Reference)
+ , bindingsPrimops :: Primops m
+ , bindingsRefs :: IntMap (Value Reference)
+ , bindingsNextRef :: Int
} deriving (Generic)
-- | The environment in which expressions are evaluated.
-newtype LangT s r m a = LangT
- { fromLangT :: ExceptT (LangError (Value (Reference s))) (StateT r (STT s m)) a }
- deriving (Functor, Applicative, Monad, MonadError (LangError (Value (Reference s))), MonadState r)
+newtype LangT r m a = LangT
+ { fromLangT :: ExceptT (LangError (Value Reference)) (StateT r m) a }
+ deriving (Functor, Applicative, Monad, MonadError (LangError (Value Reference)), MonadState r)
-instance MonadTrans (LangT s r) where lift = LangT . lift . lift . lift
+instance MonadTrans (LangT r) where lift = LangT . lift . lift
-- | A monad for language operations specialized to have as state the Bindings
-- with appropriate underlying monad.
-type Lang s m = LangT s (Bindings s m) m
+type Lang m = LangT (Bindings m) m
runLang
:: Monad m
- => (forall s. Bindings s m)
- -> (forall s. Lang s m a)
- -> m (Either (LangError (Value Int)) a)
-runLang e l = runSTT $ first (fmap labelRefs) <$> evalStateT (runExceptT $ fromLangT l) e
+ => Bindings m
+ -> Lang m a
+ -> m (Either (LangError (Value Reference)) a)
+runLang e l = evalStateT (runExceptT $ fromLangT l) e
-- | Like 'local' or 'withState'
-withEnv :: Monad m => (Bindings s m -> Bindings s m) -> Lang s m a -> Lang s m a
+withEnv :: Monad m => (Bindings m -> Bindings m) -> Lang m a -> Lang m a
withEnv modifier action = do
oldEnv <- get
modify modifier
@@ -207,28 +209,28 @@ withEnv modifier action = do
-- * Functions
-- | A Bindings with an Env containing only 'eval' and only pure primops.
-pureEnv :: (Monad m) => Bindings r m
-pureEnv = Bindings e purePrimops
+pureEnv :: (Monad m) => Bindings m
+pureEnv = Bindings e purePrimops mempty 0
where
e = fromList [(toIdent "eval", Primop $ toIdent "base-eval")]
-addBinding :: Monad m => Ident -> Value (Reference r) -> Bindings r m -> Bindings r m
+addBinding :: Monad m => Ident -> Value Reference -> Bindings m -> Bindings m
addBinding i v b = b
{ bindingsEnv = Env . Map.insert i v . fromEnv $ bindingsEnv b }
-- | Lookup an atom in the environment
-lookupAtom :: Monad m => Ident -> Lang r m (Value (Reference r))
+lookupAtom :: Monad m => Ident -> Lang m (Value Reference)
lookupAtom i = get >>= \e -> case Map.lookup i . fromEnv $ bindingsEnv e of
Nothing -> throwError $ UnknownIdentifier i
Just v -> pure v
-- | Lookup a primop.
-lookupPrimop :: Monad m => Ident -> Lang r m ([Value (Reference r)] -> Lang r m (Value (Reference r)))
+lookupPrimop :: Monad m => Ident -> Lang m ([Value Reference] -> Lang m (Value Reference))
lookupPrimop i = get >>= \e -> case Map.lookup i $ bindingsPrimops e of
Nothing -> throwError $ Impossible "Unknown primop"
Just v -> pure v
-defineAtom :: Monad m => Ident -> Value (Reference r) -> Lang r m ()
+defineAtom :: Monad m => Ident -> Value Reference -> Lang m ()
defineAtom i v = modify $ addBinding i v
quote :: Value r -> Value r
@@ -236,7 +238,7 @@ quote v = List [Primop (Ident "quote"), v]
-- | The universal primops. These are available in chain evaluation, and are
-- not shadowable via 'define'.
-purePrimops :: forall r m. (Monad m) => Primops r m
+purePrimops :: forall m. (Monad m) => Primops m
purePrimops = Map.fromList $ first Ident <$>
[ ("base-eval", evalArgs $ \args -> case args of
[x] -> baseEval x
@@ -367,11 +369,16 @@ purePrimops = Map.fromList $ first Ident <$>
if b == Boolean False then baseEval f else baseEval t
xs -> throwError $ WrongNumberOfArgs "if" 3 (length xs))
, ("read-ref", evalArgs $ \args -> case args of
- [Ref x] -> eval =<< readRef x
- [_] -> throwError $ TypeError "read-ref: argument must be a ref"
- xs -> throwError $ WrongNumberOfArgs "read-ref" 1 (length xs))
+ [Ref (Reference x)] -> gets bindingsRefs >>= \m -> case IntMap.lookup x m of
+ Nothing -> throwError $ Impossible "undefined reference"
+ Just v -> pure v
+ [_] -> throwError $ TypeError "read-ref: argument must be a ref"
+ xs -> throwError $ WrongNumberOfArgs "read-ref" 1 (length xs))
, ("write-ref", evalArgs $ \args -> case args of
- [Ref (Reference x), v] -> LangT (lift $ lift $ writeSTRef x v) >> pure nil
+ [Ref (Reference x), v] -> do
+ st <- get
+ put $ st { bindingsRefs = IntMap.insert x v $ bindingsRefs st }
+ pure nil
[_, _] -> throwError
$ TypeError "write-ref: first argument must be a ref"
xs -> throwError
@@ -383,7 +390,7 @@ purePrimops = Map.fromList $ first Ident <$>
numBinop :: (Scientific -> Scientific -> Scientific)
-> Text
- -> (Text, [Value (Reference r)] -> Lang r m (Value (Reference r)))
+ -> (Text, [Value Reference] -> Lang m (Value Reference))
numBinop fn name = (name, evalArgs $ \args -> case args of
Number x:x':xs -> foldM go (Number x) (x':xs)
where
@@ -397,7 +404,7 @@ purePrimops = Map.fromList $ first Ident <$>
-- * Eval
-- | The buck-passing eval. Uses whatever 'eval' is in scope.
-eval :: Monad m => Value (Reference r) -> Lang r m (Value (Reference r))
+eval :: Monad m => Value Reference -> Lang m (Value Reference)
eval val = do
e <- lookupAtom (toIdent "eval")
case e of
@@ -416,13 +423,13 @@ eval val = do
_ -> throwError $ TypeError "Trying to apply a non-function"
-- | The built-in, original, eval.
-baseEval :: Monad m => Value (Reference r) -> Lang r m (Value (Reference r))
+baseEval :: Monad m => Value Reference -> Lang m (Value Reference)
baseEval val = case val of
Atom i -> lookupAtom i
Ref i -> pure $ Ref i
List (f:vs) -> f $$ vs
List xs -> throwError
- $ WrongNumberOfArgs ("application: " <> T.pack (show $ labelRefs <$> xs))
+ $ WrongNumberOfArgs ("application: " <> T.pack (show xs))
2
(length xs)
String s -> pure $ String s
@@ -441,7 +448,7 @@ baseEval val = case val of
-- | Infix function application
infixr 1 $$
-($$) :: Monad m => Value (Reference r) -> [Value (Reference r)] -> Lang r m (Value (Reference r))
+($$) :: Monad m => Value Reference -> [Value Reference] -> Lang m (Value Reference)
mfn $$ vs = do
mfn' <- baseEval mfn
case mfn' of
diff --git a/src/Radicle/Internal/Parse.hs b/src/Radicle/Internal/Parse.hs
index f62844c..6ce370a 100644
--- a/src/Radicle/Internal/Parse.hs
+++ b/src/Radicle/Internal/Parse.hs
@@ -137,25 +137,22 @@ interpret
:: Monad m
=> String
-> Text
- -> (forall s . Bindings s m)
- -> m (Either (LangError (Value Int)) (Value Int))
+ -> Bindings m
+ -> m (Either (LangError (Value Reference)) (Value Reference))
interpret sourceName expr bnds = do
let primopNames = Map.keys (bindingsPrimops bnds)
parsed = runReader (runParserT (valueP <* eof) sourceName expr) primopNames
case parsed of
Left e -> pure . Left $ ParseError e
- Right v -> runLang bnds (fmap labelRefs $ makeRefs v >>= eval)
+ Right v -> runLang bnds (makeRefs v >>= eval)
-- | Parse and evaluate a Text as multiple expressions.
--
--- Before running the result, you must convert references so they don't escape
--- (or use IO).
---
-- Examples:
--
--- >>> runLang pureEnv $ fmap labelRefs $ interpretMany "test" "(define id (lambda (x) x))\n(id #t)"
+-- >>> runLang pureEnv $ interpretMany "test" "(define id (lambda (x) x))\n(id #t)"
-- Right (Boolean True)
-interpretMany :: Monad m => String -> Text -> Lang r m (Value (Reference r))
+interpretMany :: Monad m => String -> Text -> Lang m (Value Reference)
interpretMany sourceName src = do
primopNames <- gets $ Map.keys . bindingsPrimops
let parsed = parseValues sourceName src primopNames
@@ -204,7 +201,7 @@ parse file src ids = do
parseTest :: MonadError String m => Text -> m (Value (Fix Value))
parseTest t = parse "(test)" t (Map.keys $ bindingsPrimops e)
where
- e :: Bindings (Fix Value) (Lang (Fix Value) Identity)
+ e :: Bindings (Lang Identity)
e = pureEnv
diff --git a/src/Radicle/Internal/Pretty.hs b/src/Radicle/Internal/Pretty.hs
index d0bf141..a6400cd 100644
--- a/src/Radicle/Internal/Pretty.hs
+++ b/src/Radicle/Internal/Pretty.hs
@@ -14,7 +14,7 @@ import Radicle.Internal.Core
-- | We can't just pretty print the pointer id since that would break
-- referential transparency, so instead we just label refs as '<ref>'
-instance Pretty (Reference s) where
+instance Pretty Reference where
pretty _ = angles "ref"
instance Pretty Ident where
diff --git a/src/Radicle/Internal/Subscriber.hs b/src/Radicle/Internal/Subscriber.hs
index 2adead8..130fe1d 100644
--- a/src/Radicle/Internal/Subscriber.hs
+++ b/src/Radicle/Internal/Subscriber.hs
@@ -23,10 +23,10 @@ import Radicle.Internal.Pretty
import Radicle.Internal.Subscriber.Capabilities
-type ReplM s m =
- ( Monad m, Stdout (Lang s m), Stdin (Lang s m)
- , GetEnv (Lang s m) (Value (Reference s))
- , SetEnv (Lang s m) (Value (Reference s)) )
+type ReplM m =
+ ( Monad m, Stdout (Lang m), Stdin (Lang m)
+ , GetEnv (Lang m) (Value Reference)
+ , SetEnv (Lang m) (Value Reference) )
repl :: FilePath -> Text -> IO ()
repl histFile preCode = do
@@ -44,7 +44,7 @@ completion :: Monad m => CompletionFunc m
completion = completeWord Nothing ['(', ')', ' ', '\n'] go
where
-- Any type for first param will do
- bnds :: Bindings Int (InputT IO)
+ bnds :: Bindings (InputT IO)
bnds = replBindings
go s = pure $ fmap simpleCompletion
@@ -53,13 +53,13 @@ completion = completeWord Nothing ['(', ')', ' ', '\n'] go
$ (Map.keys . fromEnv $ bindingsEnv bnds)
<> Map.keys (bindingsPrimops bnds)
-replBindings :: forall s m. ReplM s m => Bindings s m
+replBindings :: forall m. ReplM m => Bindings m
replBindings = e { bindingsPrimops = bindingsPrimops e <> replPrimops }
where
- e :: Bindings s m
+ e :: Bindings m
e = pureEnv
-replPrimops :: forall s m. ReplM s m => Primops s m
+replPrimops :: forall m. ReplM m => Primops m
replPrimops = Map.fromList $ first toIdent <$>
[ ("print!", \args -> case args of
[x] -> do
diff --git a/src/Radicle/Internal/Subscriber/Capabilities.hs b/src/Radicle/Internal/Subscriber/Capabilities.hs
index 764998e..69f2f40 100644
--- a/src/Radicle/Internal/Subscriber/Capabilities.hs
+++ b/src/Radicle/Internal/Subscriber/Capabilities.hs
@@ -15,7 +15,7 @@ import Radicle.Internal.Core
class (Monad m) => Stdin m where
getLineS :: m Text
-instance {-# OVERLAPPABLE #-} Stdin m => Stdin (Lang r m) where
+instance {-# OVERLAPPABLE #-} Stdin m => Stdin (Lang m) where
getLineS = lift getLineS
instance (MonadException m, Monad m) => Stdin (InputT m) where
getLineS = getInputLine "rad> " >>= \x -> case x of
@@ -24,24 +24,24 @@ instance (MonadException m, Monad m) => Stdin (InputT m) where
class (Monad m) => Stdout m where
putStrS :: Text -> m ()
-instance {-# OVERLAPPABLE #-} Stdout m => Stdout (Lang r m) where
+instance {-# OVERLAPPABLE #-} Stdout m => Stdout (Lang m) where
putStrS = lift . putStrS
instance (MonadException m, Monad m) => Stdout (InputT m) where
putStrS = outputStrLn . T.unpack
class (Monad m) => Exit m where
exitS :: m ()
-instance Exit m => Exit (Lang r m) where
+instance Exit m => Exit (Lang m) where
exitS = lift exitS
class (Monad m) => GetEnv m r | m -> r where
getEnvS :: m (Env r)
-instance Monad m => GetEnv (Lang r m) (Value (Reference r)) where
+instance Monad m => GetEnv (Lang m) (Value Reference) where
getEnvS = gets bindingsEnv
class (Monad m) => SetEnv m r | m -> r where
setEnvS :: Env r -> m ()
-instance Monad m => SetEnv (Lang r m) (Value (Reference r)) where
+instance Monad m => SetEnv (Lang m) (Value Reference) where
setEnvS e = modify (\bnds -> bnds {bindingsEnv = e})
class (Monad m) => GetSourceName m where
diff --git a/test/spec/Radicle/Internal/Arbitrary.hs b/test/spec/Radicle/Internal/Arbitrary.hs
index eb5ed99..87a87f7 100644
--- a/test/spec/Radicle/Internal/Arbitrary.hs
+++ b/test/spec/Radicle/Internal/Arbitrary.hs
@@ -40,7 +40,7 @@ instance {-# OVERLAPPING #-} Arbitrary (Value Void) where
sizedList = sized $ \n -> do
k <- choose (0, n)
scale (`div` (k + 1)) $ vectorOf k arbitrary
- prims :: Map.Map Ident ([Value (Reference s)] -> Lang s Identity (Value (Reference s)))
+ prims :: Map.Map Ident ([Value Reference] -> Lang Identity (Value Reference))
prims = purePrimops
isPrimop x = x `elem` Map.keys prims
isNum x = isJust (readMay (T.unpack $ fromIdent x) :: Maybe Scientific)
diff --git a/test/spec/Radicle/Internal/TestCapabilities.hs b/test/spec/Radicle/Internal/TestCapabilities.hs
index 551ddf3..270716c 100644
--- a/test/spec/Radicle/Internal/TestCapabilities.hs
+++ b/test/spec/Radicle/Internal/TestCapabilities.hs
@@ -12,53 +12,53 @@ import Radicle.Internal.Subscriber.Capabilities
data WorldState = WorldState
{ worldStateStdin :: [Text]
, worldStateStdout :: [Text]
- , worldStateEnv :: forall s. Env (Value (Reference s))
+ , worldStateEnv :: Env (Value Reference)
}
-type TestLang s = Lang s (State WorldState )
+type TestLang = Lang (State WorldState)
-- | Run a possibly side-effecting program with the given stdin input lines.
runTestWith
- :: (forall s. Bindings s (State WorldState))
+ :: Bindings (State WorldState)
-> [Text] -- The stdin (errors if it runs out)
-> Text -- The program
- -> (Either (LangError (Value Int)) (Value Int), [Text])
+ -> (Either (LangError (Value Reference)) (Value Reference), [Text])
runTestWith bindings inputs action =
let ws = WorldState
{ worldStateStdin = inputs
, worldStateStdout = []
, worldStateEnv = bindingsEnv bindings
}
- in case runState (runLang bindings $ labelRefs <$> interpretMany "[test]" action) ws of
+ in case runState (runLang bindings $ interpretMany "[test]" action) ws of
(val, st) -> (val, reverse $ worldStateStdout st)
-- | Like `runTestWith`, but uses the pureEnv
runTestWith'
:: [Text]
-> Text
- -> (Either (LangError (Value Int)) (Value Int), [Text])
+ -> (Either (LangError (Value Reference)) (Value Reference), [Text])
runTestWith' = runTestWith pureEnv
-- | Run a test without stdin/stdout
runTest
- :: (forall s. Bindings s (State WorldState))
+ :: Bindings (State WorldState)
-> Text
- -> Either (LangError (Value Int)) (Value Int)
+ -> Either (LangError (Value Reference)) (Value Reference)
runTest bnds prog = fst $ runTestWith bnds [] prog
-- | Like 'runTest', but uses the pureEnv
-runTest' :: Text -> Either (LangError (Value Int)) (Value Int)
+runTest' :: Text -> Either (LangError (Value Reference)) (Value Reference)
runTest' = runTest pureEnv
-instance {-# OVERLAPPING #-} Stdin (TestLang s) where
+instance {-# OVERLAPPING #-} Stdin TestLang where
getLineS = do
ws <- lift get
case worldStateStdin ws of
[] -> throwError $ OtherError "test: out of stdin"
h:hs -> lift (put $ ws { worldStateStdin = hs }) >> pure h
-instance {-# OVERLAPPING #-} Stdout (TestLang s) where
+instance {-# OVERLAPPING #-} Stdout TestLang where
putStrS t = lift $
modify (\ws -> ws { worldStateStdout = t:worldStateStdout ws })