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.hsindex bb828b6..bbc4715 100644--- a/src/Radicle.hs+++ b/src/Radicle.hs@@ -20,7 +20,6 @@ module Radicle, Ident, Reference(..), makeRefs- , labelRefs, coerceRefs, fromIdent, mkIdentdiff --git a/src/Radicle/Internal/Core.hs b/src/Radicle/Internal/Core.hsindex c1db038..e94d79a 100644--- a/src/Radicle/Internal/Core.hs+++ b/src/Radicle/Internal/Core.hs@@ -4,8 +4,6 @@ module Radicle.Internal.Core whereimport Control.Monad.Except (ExceptT(..), MonadError, catchError,runExceptT, throwError)-import Control.Monad.ST.Trans (STRef, STT, newSTRef, readSTRef,- runSTT, writeSTRef)import Control.Monad.Stateimport 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 NonEmptyimport Data.Map (Map)+import Data.IntMap (IntMap)import qualified Data.Map as Map+import qualified Data.IntMap as IntMapimport 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 ofUnknownIdentifier i -> makeVal( "unknown-identifier"@@ -78,22 +78,32 @@ errorToValue e = case e ofmakeA = quote . AtommakeVal (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)wherego x = case x of@@ -129,20 +139,10 @@ makeRefs v = cata go (Fix v)Primop i -> pure $ Primop iDict m -> Dict <$> sequence (go <$> m)List vs -> List <$> sequence (go <$> vs)- Ref i -> i >>= newRef+ Ref i -> i >>= eval >>= newRefLambda 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 acoerceRefs = unsafeCoerce@@ -169,34 +169,36 @@ instance IsList (Env s) wheretoList = 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) mrunLang:: 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 awithEnv modifier action = dooldEnv <- getmodify 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 0wheree = 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 maddBinding 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 ofNothing -> throwError $ UnknownIdentifier iJust 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 ofNothing -> 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 vquote :: 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 mpurePrimops = 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 txs -> 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 ofNumber 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 = doe <- 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 ofAtom i -> lookupAtom iRef i -> pure $ Ref iList (f:vs) -> f $$ vsList 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 applicationinfixr 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 = domfn' <- baseEval mfncase mfn' ofdiff --git a/src/Radicle/Internal/Parse.hs b/src/Radicle/Internal/Parse.hsindex 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 = dolet primopNames = Map.keys (bindingsPrimops bnds)parsed = runReader (runParserT (valueP <* eof) sourceName expr) primopNamescase parsed ofLeft 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 = doprimopNames <- gets $ Map.keys . bindingsPrimopslet parsed = parseValues sourceName src primopNames@@ -204,7 +201,7 @@ parse file src ids = doparseTest :: 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 = pureEnvdiff --git a/src/Radicle/Internal/Pretty.hs b/src/Radicle/Internal/Pretty.hsindex 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 wherepretty _ = angles "ref"instance Pretty Ident wherediff --git a/src/Radicle/Internal/Subscriber.hs b/src/Radicle/Internal/Subscriber.hsindex 2adead8..130fe1d 100644--- a/src/Radicle/Internal/Subscriber.hs+++ b/src/Radicle/Internal/Subscriber.hs@@ -23,10 +23,10 @@ import Radicle.Internal.Prettyimport 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 mcompletion = completeWord Nothing ['(', ')', ' ', '\n'] gowhere-- Any type for first param will do- bnds :: Bindings Int (InputT IO)+ bnds :: Bindings (InputT IO)bnds = replBindingsgo 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 mreplBindings = e { bindingsPrimops = bindingsPrimops e <> replPrimops }where- e :: Bindings s m+ e :: Bindings me = pureEnv-replPrimops :: forall s m. ReplM s m => Primops s m+replPrimops :: forall m. ReplM m => Primops mreplPrimops = Map.fromList $ first toIdent <$>[ ("print!", \args -> case args of[x] -> dodiff --git a/src/Radicle/Internal/Subscriber/Capabilities.hs b/src/Radicle/Internal/Subscriber/Capabilities.hsindex 764998e..69f2f40 100644--- a/src/Radicle/Internal/Subscriber/Capabilities.hs+++ b/src/Radicle/Internal/Subscriber/Capabilities.hs@@ -15,7 +15,7 @@ import Radicle.Internal.Coreclass (Monad m) => Stdin m wheregetLineS :: m Text-instance {-# OVERLAPPABLE #-} Stdin m => Stdin (Lang r m) where+instance {-# OVERLAPPABLE #-} Stdin m => Stdin (Lang m) wheregetLineS = lift getLineSinstance (MonadException m, Monad m) => Stdin (InputT m) wheregetLineS = getInputLine "rad> " >>= \x -> case x of@@ -24,24 +24,24 @@ instance (MonadException m, Monad m) => Stdin (InputT m) whereclass (Monad m) => Stdout m whereputStrS :: Text -> m ()-instance {-# OVERLAPPABLE #-} Stdout m => Stdout (Lang r m) where+instance {-# OVERLAPPABLE #-} Stdout m => Stdout (Lang m) whereputStrS = lift . putStrSinstance (MonadException m, Monad m) => Stdout (InputT m) whereputStrS = outputStrLn . T.unpackclass (Monad m) => Exit m whereexitS :: m ()-instance Exit m => Exit (Lang r m) where+instance Exit m => Exit (Lang m) whereexitS = lift exitSclass (Monad m) => GetEnv m r | m -> r wheregetEnvS :: m (Env r)-instance Monad m => GetEnv (Lang r m) (Value (Reference r)) where+instance Monad m => GetEnv (Lang m) (Value Reference) wheregetEnvS = gets bindingsEnvclass (Monad m) => SetEnv m r | m -> r wheresetEnvS :: Env r -> m ()-instance Monad m => SetEnv (Lang r m) (Value (Reference r)) where+instance Monad m => SetEnv (Lang m) (Value Reference) wheresetEnvS e = modify (\bnds -> bnds {bindingsEnv = e})class (Monad m) => GetSourceName m wherediff --git a/test/spec/Radicle/Internal/Arbitrary.hs b/test/spec/Radicle/Internal/Arbitrary.hsindex 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) wheresizedList = sized $ \n -> dok <- 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 = purePrimopsisPrimop x = x `elem` Map.keys primsisNum x = isJust (readMay (T.unpack $ fromIdent x) :: Maybe Scientific)diff --git a/test/spec/Radicle/Internal/TestCapabilities.hs b/test/spec/Radicle/Internal/TestCapabilities.hsindex 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.Capabilitiesdata 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 pureEnvrunTestWith':: [Text]-> Text- -> (Either (LangError (Value Int)) (Value Int), [Text])+ -> (Either (LangError (Value Reference)) (Value Reference), [Text])runTestWith' = runTestWith pureEnv-- | Run a test without stdin/stdoutrunTest- :: (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 wheregetLineS = dows <- lift getcase 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 whereputStrS t = lift $modify (\ws -> ws { worldStateStdout = t:worldStateStdout ws })