Skip to content

A peer-to-peer stack for code collaboration

Better names for primops.

Julian K. Arni3da5d199e496
diff --git a/repl/config.rad b/repl/config.rad
index 2bfc69e..3a0768f 100644
--- a/repl/config.rad
+++ b/repl/config.rad
@@ -1,5 +1,5 @@
(define repl
- (sorted-map
+ (dict
name "repl"
;; Parse errors are thrown here
getter (lambda () (catch 'parse-error (get-line!) print!))))
diff --git a/repl/prelude.rad b/repl/prelude.rad
index f93439c..827a2a7 100644
--- a/repl/prelude.rad
+++ b/repl/prelude.rad
@@ -30,7 +30,7 @@
(lambda (acc new)
(if (empty? new)
acc
- (go (cons (car new) acc) (cdr new)))))))
+ (go (cons (head new) acc) (tail new)))))))
(go (list) ls)))
(should-be "reverse" (reverse (list 1 2 3)) (list 3 2 1))
@@ -47,9 +47,9 @@
(define filter (Y2 (lambda (filter) (lambda (cond ls)
(if (empty? ls)
ls
- (if (cond (car ls))
- (cons (car ls) (filter cond (cdr ls)))
- (filter cond (cdr ls))))))))
+ (if (cond (head ls))
+ (cons (head ls) (filter cond (tail ls)))
+ (filter cond (tail ls))))))))
(should-be "filter"
(filter (lambda (x) (< x 10)) (list 3 10 11))
@@ -57,22 +57,22 @@
;; modify-ref
(define modify-ref (lambda (ref fn)
- (write-ref ref (fn (deref ref)))))
+ (write-ref ref (fn (read-ref ref)))))
;; modify-map
(define modify-map (lambda (key fn mp)
(insert key (fn (lookup key mp)) mp)))
(should-be "modify-map"
- (modify-map 'a (lambda (x) (+ x 1)) (sorted-map a 5))
- (sorted-map a 6))
+ (modify-map 'a (lambda (x) (+ x 1)) (dict a 5))
+ (dict a 6))
;; Account stuff. Should be moved elsewhere when we have an import mechanism
(define create-currency (lambda ()
;; The map of all accounts
- (define accounts (ref (sorted-map)))
+ (define accounts (ref (dict)))
;; Create an account with 10 coins.
(define new-account (lambda (name)
@@ -81,11 +81,11 @@
;; Get an account's balance
(define account-balance (lambda (name)
- (lookup name (deref accounts))))
+ (lookup name (read-ref accounts))))
;; Transfer money
(define transfer (lambda (from to amount)
- (define acc (deref accounts))
+ (define acc (read-ref accounts))
(write-ref accounts (if (> amount (lookup from acc))
(print! "not enough balance")
(modify-map
@@ -104,25 +104,25 @@
(define new-eval (lambda (expr)
- (if (eq? (car expr) 'new-account)
- ((currency 'new-account) (car (cdr expr)))
- (if (eq? (car expr) 'account-balance)
- ((currency 'account-balance) (car (cdr expr)))
- (if (eq? (car expr) 'transfer)
- ((currency 'transfer) (car (cdr expr)) (car (cdr (cdr expr))) (car (cdr
- (cdr (cdr expr)))))
+ (if (eq? (head expr) 'new-account)
+ ((currency 'new-account) (head (tail expr)))
+ (if (eq? (head expr) 'account-balance)
+ ((currency 'account-balance) (head (tail expr)))
+ (if (eq? (head expr) 'transfer)
+ ((currency 'transfer) (head (tail expr)) (head (tail (tail expr))) (head
+ (tail
+ (tail (tail expr)))))
(print! "transaction must be new-account, transfer, or account-balance"))))))
;; The Repl
(define repl
- (sorted-map
+ (dict
name "repl"
;; Parse errors are thrown here
getter (lambda () (catch 'parse-error (get-line!) print!))))
-;; Evaluation errors are thrown here
(catch 'any
(subscribe-to! repl print!)
(lambda (x) (print! x) (subscribe-to! repl print!)))
diff --git a/src/Radicle/Internal/Core.hs b/src/Radicle/Internal/Core.hs
index 2a554b6..c1db038 100644
--- a/src/Radicle/Internal/Core.hs
+++ b/src/Radicle/Internal/Core.hs
@@ -76,7 +76,7 @@ errorToValue e = case e of
Exit -> makeVal ("exit", [])
where
makeA = quote . Atom
- makeVal (t,v) = pure (Ident t, SortedMap $ Map.mapKeys Ident . fromList $ v)
+ makeVal (t,v) = pure (Ident t, Dict $ Map.mapKeys Ident . fromList $ v)
newtype Reference s = Reference { getReference :: STRef s (Value (Reference s)) }
deriving (Eq)
@@ -86,8 +86,8 @@ newRef :: Monad m => Value (Reference s) -> Lang s m (Value (Reference s))
newRef v = LangT $ lift $ lift $ Ref . Reference <$> newSTRef v
-- | Read the value of a reference.
-deref :: Monad m => Reference s -> Lang s m (Value (Reference s))
-deref (Reference r) = LangT . lift . lift $ readSTRef r
+readRef :: Monad m => Reference s -> Lang s m (Value (Reference s))
+readRef (Reference r) = LangT . lift . lift $ readSTRef r
-- | An expression or value in the language.
--
@@ -105,7 +105,7 @@ data Value r =
| Boolean Bool
| List [Value r]
| Primop Ident
- | SortedMap (Map.Map Ident (Value r))
+ | Dict (Map.Map Ident (Value r))
| Ref r
-- | Takes the arguments/parameters, a body, and possibly a closure.
--
@@ -127,7 +127,7 @@ makeRefs v = cata go (Fix v)
Boolean i -> pure $ Boolean i
Number i -> pure $ Number i
Primop i -> pure $ Primop i
- SortedMap m -> SortedMap <$> sequence (go <$> m)
+ Dict m -> Dict <$> sequence (go <$> m)
List vs -> List <$> sequence (go <$> vs)
Ref i -> i >>= newRef
Lambda is bd e -> Lambda is <$> sequence (go <$> bd)
@@ -274,25 +274,25 @@ purePrimops = Map.fromList $ first Ident <$>
[x, List xs] -> pure $ List (x:xs)
[_, _] -> throwError $ TypeError "cons: second argument must be list"
xs -> throwError $ WrongNumberOfArgs "cons" 2 (length xs))
- , ("car", evalArgs $ \args -> case args of
+ , ("head", evalArgs $ \args -> case args of
[List (x:_)] -> pure x
- [List []] -> throwError $ OtherError "car: empty list"
- [_] -> throwError $ TypeError "car: expects list argument"
- xs -> throwError $ WrongNumberOfArgs "car" 1 (length xs))
- , ("cdr", evalArgs $ \args -> case args of
+ [List []] -> throwError $ OtherError "head: empty list"
+ [_] -> throwError $ TypeError "head: expects list argument"
+ xs -> throwError $ WrongNumberOfArgs "head" 1 (length xs))
+ , ("tail", evalArgs $ \args -> case args of
[List (_:xs)] -> pure $ List xs
- [List []] -> throwError $ OtherError "cdr: empty list"
- [_] -> throwError $ TypeError "cdr: expects list argument"
- xs -> throwError $ WrongNumberOfArgs "cdr" 1 (length xs))
+ [List []] -> throwError $ OtherError "tail: empty list"
+ [_] -> throwError $ TypeError "tail: expects list argument"
+ xs -> throwError $ WrongNumberOfArgs "tail" 1 (length xs))
, ("lookup", evalArgs $ \args -> case args of
- [Atom a, SortedMap m] -> pure $ case Map.lookup a m of
+ [Atom a, Dict m] -> pure $ case Map.lookup a m of
Just v -> v
-- Probably an exception is better, but that seems cruel
-- when you have no exception handling facilities.
Nothing -> nil
[Atom _, _] -> throwError
$ TypeError "lookup: second argument must be map"
- [_, SortedMap _] -> throwError
+ [_, Dict _] -> throwError
$ TypeError "lookup: first argument must be atom"
xs -> throwError $ WrongNumberOfArgs "lookup" 2 (length xs))
, ("string-append", evalArgs $ \args ->
@@ -303,7 +303,7 @@ purePrimops = Map.fromList $ first Ident <$>
then pure . String . mconcat $ catMaybes ss
else throwError $ TypeError "string-append: non-string argument")
, ("insert", evalArgs $ \args -> case args of
- [Atom k, v, SortedMap m] -> pure . SortedMap $ Map.insert k v m
+ [Atom k, v, Dict m] -> pure . Dict $ Map.insert k v m
[Atom _, _, _] -> throwError
$ TypeError "insert: third argument must be map"
[_, _, _] -> throwError
@@ -366,10 +366,10 @@ purePrimops = Map.fromList $ first Ident <$>
-- in Lisps a lot of things that one might object to are True...
if b == Boolean False then baseEval f else baseEval t
xs -> throwError $ WrongNumberOfArgs "if" 3 (length xs))
- , ("deref", evalArgs $ \args -> case args of
- [Ref x] -> eval =<< deref x
- [_] -> throwError $ TypeError "deref: argument must be a ref"
- xs -> throwError $ WrongNumberOfArgs "deref" 1 (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))
, ("write-ref", evalArgs $ \args -> case args of
[Ref (Reference x), v] -> LangT (lift $ lift $ writeSTRef x v) >> pure nil
[_, _] -> throwError
@@ -431,9 +431,9 @@ baseEval val = case val of
Primop i -> pure $ Primop i
e@(Lambda _ _ (Just _)) -> pure e
Lambda args body Nothing -> gets $ Lambda args body . Just . bindingsEnv
- SortedMap mp -> do
+ Dict mp -> do
let evalSnd (a,b) = (a ,) <$> baseEval b
- SortedMap . Map.fromList <$> traverse evalSnd (Map.toList mp)
+ Dict . Map.fromList <$> traverse evalSnd (Map.toList mp)
diff --git a/src/Radicle/Internal/Parse.hs b/src/Radicle/Internal/Parse.hs
index 37c61ff..f62844c 100644
--- a/src/Radicle/Internal/Parse.hs
+++ b/src/Radicle/Internal/Parse.hs
@@ -81,10 +81,10 @@ applyP = List <$> valueP `sepBy` spaceConsumer
quoteP :: VParser
quoteP = List . ((Primop $ toIdent "quote") :) . pure <$> (char '\'' >> valueP)
-sortedMapP :: VParser
-sortedMapP = do
- void $ symbol "sorted-map"
- SortedMap . Map.fromList <$> pairP `sepBy` spaceConsumer
+dictP :: VParser
+dictP = do
+ void $ symbol "dict"
+ Dict . Map.fromList <$> pairP `sepBy` spaceConsumer
where
pairP = (,) <$> identP <*> valueP
@@ -115,7 +115,7 @@ valueP = do
where
appLike = choice
[ lambdaP <?> "lambda"
- , sortedMapP <?> "sorted-map"
+ , dictP <?> "dict"
, refP <?> "ref"
, applyP <?> "application"
]
diff --git a/src/Radicle/Internal/Pretty.hs b/src/Radicle/Internal/Pretty.hs
index fd1d066..d0bf141 100644
--- a/src/Radicle/Internal/Pretty.hs
+++ b/src/Radicle/Internal/Pretty.hs
@@ -31,9 +31,9 @@ instance Pretty r => Pretty (Value r) where
Boolean False -> "#f"
List vs -> parens $ sep (pretty <$> vs)
Primop i -> pretty i
- SortedMap mp -> parens $
- "sorted-map" <+> sep [ pretty k <+> pretty val
- | (k, val) <- Map.toList mp ]
+ Dict mp -> parens $
+ "dict" <+> sep [ pretty k <+> pretty val
+ | (k, val) <- Map.toList mp ]
Lambda ids vals _ -> parens $
"lambda" <+> parens (sep $ pretty <$> ids)
<+> sep (pretty <$> toList vals)
diff --git a/src/Radicle/Internal/Subscriber.hs b/src/Radicle/Internal/Subscriber.hs
index 987a1a8..2adead8 100644
--- a/src/Radicle/Internal/Subscriber.hs
+++ b/src/Radicle/Internal/Subscriber.hs
@@ -93,7 +93,7 @@ replPrimops = Map.fromList $ first toIdent <$>
v' <- eval v
s <- get
case (x', v') of
- (SortedMap m, fn) -> case Map.lookup (toIdent "getter") m of
+ (Dict m, fn) -> case Map.lookup (toIdent "getter") m of
Nothing -> throwError
$ OtherError "subscribe-to!: Expected 'getter' key"
Just g -> forever go
@@ -108,6 +108,6 @@ replPrimops = Map.fromList $ first toIdent <$>
-- function is evaluated in the original
-- environment.
void $ withEnv (const s) (fn $$ [quote line])
- _ -> throwError $ TypeError "subscribe-to!: Expected sorted-map"
+ _ -> throwError $ TypeError "subscribe-to!: Expected dict"
xs -> throwError $ WrongNumberOfArgs "subscribe-to!" 2 (length xs))
]
diff --git a/test/spec/Radicle/Internal/Arbitrary.hs b/test/spec/Radicle/Internal/Arbitrary.hs
index 2da346a..eb5ed99 100644
--- a/test/spec/Radicle/Internal/Arbitrary.hs
+++ b/test/spec/Radicle/Internal/Arbitrary.hs
@@ -29,7 +29,7 @@ instance {-# OVERLAPPING #-} Arbitrary (Value Void) where
, (3, Number <$> arbitrary)
, (1, List <$> sizedList)
, (3, Primop <$> elements (Map.keys prims))
- , (3, SortedMap . Map.fromList <$> sizedList)
+ , (3, Dict . Map.fromList <$> sizedList)
, (1, Lambda <$> sizedList
<*> scale (`div` 3) arbitrary
<*> scale (`div` 3) arbitrary)
diff --git a/test/spec/Radicle/Tests.hs b/test/spec/Radicle/Tests.hs
index c4237ae..a3d5b7c 100644
--- a/test/spec/Radicle/Tests.hs
+++ b/test/spec/Radicle/Tests.hs
@@ -37,20 +37,20 @@ test_eval =
|]
prog `succeedsWith` String "Steve Wozniak"
- , testCase "'sorted-map' creates a SortedMap with given key/vals" $ do
- let prog = [s|(sorted-map why "not")|]
- prog `succeedsWith` SortedMap (fromList [(toIdent "why", String "not")])
+ , testCase "'dict' creates a Dict with given key/vals" $ do
+ let prog = [s|(dict why "not")|]
+ prog `succeedsWith` Dict (fromList [(toIdent "why", String "not")])
, testCase "'cons' conses an element" $ do
let prog = [s|(cons #t (list #f))|]
prog `succeedsWith` List [Boolean True, Boolean False]
- , testCase "'car' returns the first element of a list" $ do
- let prog = [s|(car (list #t #f))|]
+ , testCase "'head' returns the first element of a list" $ do
+ let prog = [s|(head (list #t #f))|]
prog `succeedsWith` Boolean True
- , testCase "'cdr' returns the tail of a list" $ do
- let prog = [s|(cdr (list #t #f #t))|]
+ , testCase "'tail' returns the tail of a list" $ do
+ let prog = [s|(tail (list #t #f #t))|]
prog `succeedsWith` List [Boolean False, Boolean True]
, testProperty "'eq?' considers equal values equal" $ \(val :: Value Void) -> do
@@ -81,15 +81,15 @@ test_eval =
prog `succeedsWith` Boolean False
, testCase "'lookup' returns value of key in map" $ do
- let prog = [s|(lookup 'key1 (sorted-map key1 "a" key2 "b"))|]
+ let prog = [s|(lookup 'key1 (dict key1 "a" key2 "b"))|]
prog `succeedsWith` String "a"
, testCase "'insert' updates the value of key in map" $ do
- let prog = [s|(lookup 'key1 (insert 'key1 "b" (sorted-map key1 "a" key2 "b")))|]
+ let prog = [s|(lookup 'key1 (insert 'key1 "b" (dict key1 "a" key2 "b")))|]
prog `succeedsWith` String "b"
, testCase "'insert' insert the value of key in map" $ do
- let prog = [s|(lookup 'key1 (insert 'key1 "b" (sorted-map)))|]
+ let prog = [s|(lookup 'key1 (insert 'key1 "b" (dict)))|]
prog `succeedsWith` String "b"
, testProperty "'string-append' concatenates string" $ \ss -> do
@@ -262,17 +262,17 @@ test_eval =
|]
prog `succeedsWith` Boolean False
- , testCase "'deref' returns the most recent value" $ do
+ , testCase "'read-ref' returns the most recent value" $ do
let prog = [s|
(define x (ref 5))
(write-ref x 6)
- (deref x)
+ (read-ref x)
|]
res = runTest' prog
res @?= Right (Number 6)
- , testProperty "deref . ref == id" $ \v -> do
- let derefed = runTest' $ T.pack [i|(deref (ref #{renderPrettyFix v}))|]
+ , testProperty "read-ref . ref == id" $ \v -> do
+ let derefed = runTest' $ T.pack [i|(read-ref (ref #{renderPrettyFix v}))|]
orig = runTest' $ T.pack [i|#{renderPrettyFix v}|]
info = "Expected:\n" <> T.unpack (prettyEither orig)
<> "\nGot:\n" <> T.unpack (prettyEither derefed)
@@ -359,7 +359,7 @@ test_repl_primops =
, testCase "catch catches get-line errors" $ do
let prog = [s|
- (define repl (sorted-map name "repl" getter get-line!))
+ (define repl (dict name "repl" getter get-line!))
(catch 'any
(subscribe-to! repl (lambda (x) (print! x)))
(lambda (x) "caught"))
@@ -444,7 +444,7 @@ test_source_files = testGroup "Radicle source file tests" <$> do
removeEnv :: Value s -> Value s
removeEnv v = case v of
List xs -> List $ removeEnv <$> xs
- SortedMap m -> SortedMap $ removeEnv <$> m
+ Dict m -> Dict $ removeEnv <$> m
Lambda ids bd _ -> Lambda ids (removeEnv <$> bd) Nothing
x -> x