A peer-to-peer stack for code collaboration
Review fixes.
Julian K. Arnic8391a33076b
diff --git a/src/Radicle/Internal/Core.hs b/src/Radicle/Internal/Core.hsindex d10a00e..86399eb 100644--- a/src/Radicle/Internal/Core.hs+++ b/src/Radicle/Internal/Core.hs@@ -62,8 +62,8 @@ newtype Ident = Ident { fromIdent :: Text }-- Unsafe! Only use this if you know the string at compile-time and know it's a-- valid identifier-identFromString :: String -> Ident-identFromString = Ident . fromString+toIdent :: String -> Ident+toIdent = Ident . fromString-- | The environment, which keeps all known bindings.newtype Env = Env { fromEnv :: Map Ident Value }@@ -111,8 +111,8 @@ withEnv modifier action = do-- | A Bindings with an Env containing only 'eval' and only pure primops.pureEnv :: Monad m => Bindings mpureEnv = Bindings e purePrimops- where- e = fromList [(identFromString "eval", Primop $ identFromString "core-eval")]+ where+ e = fromList [(toIdent "eval", Primop $ toIdent "base-eval")]addBinding :: Monad m => Ident -> Value -> Bindings m -> Bindings maddBinding i v b = b@@ -136,10 +136,10 @@ defineAtom i v = modify $ addBinding i v-- | The universal primops. These are available in chain evaluation, and are-- not shadowable via 'define'.purePrimops :: Monad m => Primops m-purePrimops = Map.fromList $ first identFromString <$>- [ ("core-eval", \args -> case args of- [x] -> coreEval x- xs -> throwError $ WrongNumberOfArgs "core-eval" 1 (length xs))+purePrimops = Map.fromList $ first toIdent <$>+ [ ("base-eval", \args -> case args of+ [x] -> baseEval x+ xs -> throwError $ WrongNumberOfArgs "base-eval" 1 (length xs)), ("quote", \args -> case args of[v] -> pure vxs -> throwError $ WrongNumberOfArgs "quote" 1 (length xs))@@ -165,10 +165,6 @@ purePrimops = Map.fromList $ first identFromString <$>-- in Lisps a lot of things that one might object to are True...if b == Boolean False then eval f else eval txs -> throwError $ WrongNumberOfArgs "if" 3 (length xs))- , ("ref", \args -> case args of- [Atom var] -> pure $ Ref var- [_] -> throwError $ TypeError "get: expecting atom"- xs -> throwError $ WrongNumberOfArgs "get" 1 (length xs)), ("deref-all", \args -> dolet derefOne :: forall m b. (Monad m, Data b) => (b -> Lang m b)derefOne x = case (cast x, eqT :: Maybe (b :~: Value)) of@@ -181,10 +177,10 @@ purePrimops = Map.fromList $ first identFromString <$>-- * Eval--- Let val be finale of eval+-- | The buck-passing eval. Uses whatever 'eval' is in scope.eval :: Monad m => Value -> Lang m Valueeval val = do- e <- lookupAtom (identFromString "eval")+ e <- lookupAtom (toIdent "eval")case e ofPrimop i -> dofn <- lookupPrimop i@@ -200,15 +196,15 @@ eval val = do_ -> throwError $ TypeError "Trying to apply a non-function"-- | The built-in, original, eval.-coreEval :: Monad m => Value -> Lang m Value-coreEval val = case val of+baseEval :: Monad m => Value -> Lang m Value+baseEval val = case val ofAtom i -> lookupAtom iRef i -> pure $ Ref i- List vals -> List <$> traverse coreEval vals+ List vals -> List <$> traverse baseEval valsString s -> pure $ String sBoolean b -> pure $ Boolean bApply mfn vs -> do- mfn' <- coreEval mfn+ mfn' <- baseEval mfncase mfn' ofPrimop i -> dofn <- lookupPrimop i@@ -218,16 +214,16 @@ coreEval val = case val ofLambda _ _ Nothing -> throwError $ Impossible"lambda should already have an env"Lambda bnds body (Just closure) -> do- vs' <- traverse coreEval vs+ vs' <- traverse baseEval vslet mappings = fromList (zip bnds vs')modEnv = mappings <> closure- withEnv (\e -> e { bindingsEnv = modEnv }) (coreEval body)+ withEnv (\e -> e { bindingsEnv = modEnv }) (baseEval body)_ -> throwError $ TypeError "Trying to apply a non-function"Primop i -> pure $ Primop ie@(Lambda _ _ (Just _)) -> pure eLambda args body Nothing -> gets $ Lambda args body . Just . bindingsEnvSortedMap mp -> do- let evalSnd (a,b) = (a ,) <$> coreEval b+ let evalSnd (a,b) = (a ,) <$> baseEval bSortedMap . Map.fromList <$> traverse evalSnd (Map.toList mp)diff --git a/src/Radicle/Internal/Parse.hs b/src/Radicle/Internal/Parse.hsindex b40e205..5673c7b 100644--- a/src/Radicle/Internal/Parse.hs+++ b/src/Radicle/Internal/Parse.hs@@ -169,7 +169,7 @@ parseValues sourceName srcCode prims = go $ initial-- >>> parse "test" "#t" [] :: Either String Value-- Right (Boolean True)----- >>> parse "test" "hi" [identFromString "hi"] :: Either String Value+-- >>> parse "test" "hi" [toIdent "hi"] :: Either String Value-- Right (Primop (Ident {fromIdent = "hi"}))---- >>> parse "test" "hi" [] :: Either String Valuediff --git a/src/Radicle/Internal/Subscriber.hs b/src/Radicle/Internal/Subscriber.hsindex b2a6dd5..d1939d1 100644--- a/src/Radicle/Internal/Subscriber.hs+++ b/src/Radicle/Internal/Subscriber.hs@@ -32,7 +32,7 @@ replBindings = e { bindingsPrimops = bindingsPrimops e <> replPrimops }e = pureEnvreplPrimops :: forall m. ReplM m => Primops m-replPrimops = Map.fromList $ first identFromString <$>+replPrimops = Map.fromList $ first toIdent <$>[ ("print!", \args -> case args of[x] -> dov <- eval x@@ -60,17 +60,17 @@ replPrimops = Map.fromList $ first identFromString <$>, ("subscribe-to!", \args -> case args of[x, v] -> do- x' <- coreEval x- v' <- coreEval v+ x' <- baseEval x+ v' <- baseEval vcase (x', v') of- (SortedMap m, fn) -> case Map.lookup (identFromString "getter") m of+ (SortedMap m, fn) -> case Map.lookup (toIdent "getter") m ofNothing -> throwError$ OtherError "subscribe-to!: Expected 'getter' key"Just g -> gowherego = do- newBlock <- eval =<< coreEval g- _ <- coreEval (fn $$ [newBlock])+ newBlock <- eval =<< baseEval g+ _ <- baseEval (fn $$ [newBlock])go_ -> throwError $ TypeError "subscribe-to!: Expected sorted-map"xs -> throwError $ WrongNumberOfArgs "subscribe-to!" 2 (length xs))diff --git a/test/spec/Radicle/Tests.hs b/test/spec/Radicle/Tests.hsindex 04623f4..061d350 100644--- a/test/spec/Radicle/Tests.hs+++ b/test/spec/Radicle/Tests.hs@@ -8,21 +8,23 @@ import Data.Generics (everywhere)import Data.Semigroup ((<>))import Data.String.QQ (s)import qualified Data.Text as T+import qualified Data.Text.IO as Timport GHC.Exts (fromList, toList)import Test.Tastyimport Test.Tasty.HUnitimport Test.Tasty.QuickCheck (counterexample, testProperty)import Radicle.Internal.Arbitrary ()-import Radicle.Internal.Core (identFromString)+import Radicle.Internal.Core (toIdent)import Radicle.Internal.TestCapabilitiesimport Radicle-import Paths_radicle++import Paths_radicle -- generated by cabal via 'data-files'test_eval :: [TestTree]test_eval =[ testCase "Fails for undefined atoms" $- [s|blah|] `failsWith` UnknownIdentifier (i "blah")+ [s|blah|] `failsWith` UnknownIdentifier (toIdent "blah"), testCase "Succeeds for defined atoms" $ dolet prog = [s|@@ -33,7 +35,7 @@ test_eval =, testCase "'sorted-map' creates a SortedMap with given key/vals" $ dolet prog = [s|(sorted-map why "not")|]- prog `succeedsWith` SortedMap (fromList [(i "why", String "not")])+ prog `succeedsWith` SortedMap (fromList [(toIdent "why", String "not")]), testCase "'eval' evaluates the list" $ dolet prog = [s|(eval (quote #t))|]@@ -111,19 +113,19 @@ test_parser ="#f" ==> Boolean False, testCase "parses primops" $ do- "boolean?" ==> Primop (i "boolean?")- "core-eval" ==> Primop (i "core-eval")+ "boolean?" ==> Primop (toIdent "boolean?")+ "base-eval" ==> Primop (toIdent "base-eval"), testCase "parses identifiers" $ do- "++" ==> Atom (i "++")- "what?crazy!" ==> Atom (i "what?crazy!")+ "++" ==> Atom (toIdent "++")+ "what?crazy!" ==> Atom (toIdent "what?crazy!"), testCase "parses identifiers that have a primop as prefix" $ do- "evaluate" ==> Atom (i "evaluate")+ "evaluate" ==> Atom (toIdent "evaluate"), testCase "parses function application" $ do- "(++)" ==> (Atom (i "++") $$ [])- "(++ \"merge\" \"d\")" ==> (Atom (i "++") $$ [String "merge", String "d"])+ "(++)" ==> (Atom (toIdent "++") $$ [])+ "(++ \"merge\" \"d\")" ==> (Atom (toIdent "++") $$ [String "merge", String "d"])]wherex ==> y = parseTest x @?= Right y@@ -177,7 +179,7 @@ test_repl_primops =res @?= Right (Boolean True), testProperty "deref-all . ref == id" $ \(i', v) -> do- let derefed = Primop (i "deref-all") $$ [Ref i']+ let derefed = Primop (toIdent "deref-all") $$ [Ref i']atom = Atom i'run' p = runTestWith (addBinding i' v pureEnv) [] (eval p)run' derefed == run' atom@@ -214,8 +216,8 @@ test_repl =(_, result) <- runInRepl inputresult @?= output- , testCase "(define eval (quote core-eval)) doesn't change things" $ do- let input = [ "(define eval (quote core-eval))"+ , testCase "(define eval (quote base-eval)) doesn't change things" $ do+ let input = [ "(define eval (quote base-eval))", "(define id (lambda (x) x))", "(id #t)"]@@ -227,15 +229,12 @@ test_repl =result @?= output]where- getCfg = getDataFileName "repl/config.rad" >>= fmap T.pack . readFile+ getCfg = getDataFileName "repl/config.rad" >>= T.readFilerunInRepl inp = runTestWith replBindings inp. interpretMany "(test)" <$> getCfg-- * Utils-i :: String -> Ident-i = identFromString--- | Environments are neither printed nor parsed, but are generated by the-- arbitrary instance.removeEnv :: Value -> Value