Skip to content

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.hs
index 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 m
pureEnv = 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 m
addBinding 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 v
xs -> 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 t
xs -> 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 -> do
let 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 Value
eval val = do
- e <- lookupAtom (identFromString "eval")
+ e <- lookupAtom (toIdent "eval")
case e of
Primop i -> do
fn <- 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 of
Atom i -> lookupAtom i
Ref i -> pure $ Ref i
- List vals -> List <$> traverse coreEval vals
+ List vals -> List <$> traverse baseEval vals
String s -> pure $ String s
Boolean b -> pure $ Boolean b
Apply mfn vs -> do
- mfn' <- coreEval mfn
+ mfn' <- baseEval mfn
case mfn' of
Primop i -> do
fn <- lookupPrimop i
@@ -218,16 +214,16 @@ coreEval val = case val of
Lambda _ _ Nothing -> throwError $ Impossible
"lambda should already have an env"
Lambda bnds body (Just closure) -> do
- vs' <- traverse coreEval vs
+ vs' <- traverse baseEval vs
let 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 i
e@(Lambda _ _ (Just _)) -> pure e
Lambda args body Nothing -> gets $ Lambda args body . Just . bindingsEnv
SortedMap mp -> do
- let evalSnd (a,b) = (a ,) <$> coreEval b
+ let evalSnd (a,b) = (a ,) <$> baseEval b
SortedMap . Map.fromList <$> traverse evalSnd (Map.toList mp)
diff --git a/src/Radicle/Internal/Parse.hs b/src/Radicle/Internal/Parse.hs
index 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 Value
diff --git a/src/Radicle/Internal/Subscriber.hs b/src/Radicle/Internal/Subscriber.hs
index 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 = pureEnv
replPrimops :: forall m. ReplM m => Primops m
-replPrimops = Map.fromList $ first identFromString <$>
+replPrimops = Map.fromList $ first toIdent <$>
[ ("print!", \args -> case args of
[x] -> do
v <- 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 v
case (x', v') of
- (SortedMap m, fn) -> case Map.lookup (identFromString "getter") m of
+ (SortedMap m, fn) -> case Map.lookup (toIdent "getter") m of
Nothing -> throwError
$ OtherError "subscribe-to!: Expected 'getter' key"
Just g -> go
where
go = 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.hs
index 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 T
import GHC.Exts (fromList, toList)
import Test.Tasty
import Test.Tasty.HUnit
import Test.Tasty.QuickCheck (counterexample, testProperty)
import Radicle.Internal.Arbitrary ()
-import Radicle.Internal.Core (identFromString)
+import Radicle.Internal.Core (toIdent)
import Radicle.Internal.TestCapabilities
import 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" $ do
let prog = [s|
@@ -33,7 +35,7 @@ test_eval =
, testCase "'sorted-map' creates a SortedMap with given key/vals" $ do
let 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" $ do
let 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"])
]
where
x ==> 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 input
result @?= 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.readFile
runInRepl 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