A peer-to-peer stack for code collaboration
Additions to primops and prelude (#54)
James Haydona19587ed6882
diff --git a/repl/prelude.rad b/repl/prelude.radindex 4283107..983ff8d 100644--- a/repl/prelude.rad+++ b/repl/prelude.rad@@ -1,3 +1,4 @@+(define should-be(lambda (name x y)(if (eq? x y)@@ -8,7 +9,47 @@(define empty? (lambda (ls) (eq? ls (list))))(should-be "empty" (empty? (list 2)) #f)-;; y-combinator+;; boolean functions++(define and+ (lambda (x y)+ (if x y x)))++(should-be "and" (and #t #t) #t)+(should-be "and" (and #t #f) #f)+(should-be "and" (and #f #t) #f)+(should-be "and" (and #f #f) #f)++(define or+ (lambda (x y)+ (if x x y)))++(should-be "or" (or #t #t) #t)+(should-be "or" (or #t #f) #t)+(should-be "or" (or #f #t) #t)+(should-be "or" (or #f #f) #f)++(define all+ (lambda (xs) (foldr and #t xs)))++(should-be "all" (all '()) #t)+(should-be "all" (all '(#t)) #t)+(should-be "all" (all '(#f)) #f)+(should-be "all" (all '(#t #t #t #t)) #t)+(should-be "all" (all '(#t #t #f #t)) #f)++(define some+ (lambda (xs) (foldr or #f xs)))++(should-be "some" (some '()) #f)+(should-be "some" (some '(#t)) #t)+(should-be "some" (some '(#f)) #f)+(should-be "some" (some '(#f #f #f)) #f)+(should-be "some" (some '(#t #t #t #t)) #t)+(should-be "some" (some '(#t #t #f #t)) #t)++;; y-combinators+(define Y(lambda (h)((lambda (x) (x x))@@ -24,6 +65,12 @@(define fac (Y (lambda (f) (lambda (x) (if (< x 2) 1 (* x (f (- x 1))))))))(should-be "fac" (fac 10) 3628800)))+;; List functions++(define length+ (lambda (xs)+ (foldr (lambda (x acc) (+ acc 1)) 0 xs)))+;; reverse(define reverse (lambda (ls)(define go (Y2 (lambda (go)@@ -55,9 +102,29 @@(filter (lambda (x) (< x 10)) (list 3 10 11))(list 3))+;; dict functions++(define dict-from-list+ (lambda (xs)+ (foldr (lambda (kv acc) (insert (head kv) (head (tail kv)) acc)) (dict) xs)))++(should-be "dict-from-list"+ (dict-from-list '('(:one 1) '(:two 2)))+ (dict :one 1 :two 2))++(define keys+ (lambda (d)+ (map head (seq d))))++;; Uncomment this test once map is fixed.+;; (should-be "keys"+;; (keys (dict :one 1 :two 2))+;; '(:one :two))+;; modify-ref-(define modify-ref (lambda (ref fn)- (write-ref ref (fn (read-ref ref)))))+(define modify-ref+ (lambda (r fn)+ (write-ref r (fn (read-ref r)))));; modify-map(define modify-map (lambda (key fn mp)@@ -67,6 +134,96 @@(modify-map 'a (lambda (x) (+ x 1)) (dict 'a 5))(dict 'a 6))+;; Lenses++;; Makes a lens out of a getter and a setter.+(define make-lens+ (lambda (g s)+ (dict 'getter g 'setter s)))++;; View a value through a lens.+(define view+ (lambda (lens target)+ ((lookup 'getter lens) target)))++;; Set a value though a lens.+(define set+ (lambda (lens new-view target)+ ((lookup 'setter lens) new-view target)))++;; Compose lenses+(define ..+ (lambda (lens1 lens2)+ (make-lens+ (lambda (target) (view lens2 (view lens1 target)))+ (lambda (new-view target) (set lens1 (set lens2 new-view (view lens1 target)) target)))))++;; Modify a value through a lens.+(define over+ (lambda (lens f target)+ (set lens (f (view lens target)) target)))++;; A lens targetting keys of dicts.+(define key+ (lambda (k)+ (make-lens (lambda (mp) (lookup k mp))+ (lambda (v mp) (insert k v mp)))))++(should-be "view lens"+ (view (key :one) (dict :one 1 :two 2))+ 1)++(should-be "set lens"+ (set (key :two) 42 (dict :one 1 :two 2))+ (dict :one 1 :two 42))++(should-be "over lens"+ (over (key :one) (lambda (x) (+ x 1)) (dict :one 1 :two 2))+ (dict :one 2 :two 2))++(should-be "lens composition"+ (over (.. (key :a) (key :b))+ (lambda (x) (+ x 1))+ (dict :a (dict :b 2)))+ (dict :a (dict :b 3)))++;; Voting stuff. Should be moved elsewhere when we have an import mechanism.++;; Initial state of the vote+(define state+ (ref+ (dict+ 'voters '("Alice" "Bob" "Charlie" "Dan")+ 'votes (dict))))++;; Takes a state and a vote and returns a new state, or throws and error if the+;; voting expression is invalid.+(define ev-vote+ (lambda (s expr)+ (if (eq? (head expr) 'vote)+ (if (and (string? (head (tail expr)))+ (boolean? (head (tail (tail expr)))))+ (if (member? (head (tail expr)) (view (key 'voters) (read-ref state)))+ (set (.. (key 'votes) (key (head (tail expr))))+ (head (tail (tail expr)))+ s)+ (throw 'invalid-input "You do not have permission to vote."))+ (throw 'invalid-input "A vote must be a name and a bool."))+ (throw 'invalid-input "All you can do is vote."))+ ))++(define voting-ended?+ (lambda (s)+ (define votes (dict-from-list (map reverse (seq (view (key 'votes) s)))))+ votes+ ))++(define boom+ (quote+ (define eval ev-vote)))+++;; Account stuff. Should be moved elsewhere when we have an import mechanism(define create-currency (lambda ()diff --git a/src/Radicle/Internal/Primops.hs b/src/Radicle/Internal/Primops.hsindex 32f8ab7..1d9ed8a 100644--- a/src/Radicle/Internal/Primops.hs+++ b/src/Radicle/Internal/Primops.hs@@ -23,9 +23,7 @@ pureEnv = Bindings e purePrimops mempty 0-- not shadowable via 'define'.purePrimops :: forall m. (Monad m) => Primops mpurePrimops = fromList $ first Ident <$>- [ ("base-eval", evalArgs $ \args -> case args of- [x] -> baseEval x- xs -> throwError $ WrongNumberOfArgs "base-eval" 1 (length xs))+ [ ("base-eval", evalOneArg "base-eval" baseEval), ("list", evalArgs $ \args -> pure $ List args), ("dict", evalArgs $ \args ->let go (k:v:rest) = Map.insert k v $ go rest@@ -44,6 +42,7 @@ purePrimops = fromList $ first Ident <$>pure nil[_, _] -> throwError $ OtherError "define expects atom for first arg"xs -> throwError $ WrongNumberOfArgs "define" 2 (length xs))+ , ("do", evalArgs $ pure . lastDef nil), ("catch", \args -> case args of[l, form, handler] -> domlabel <- baseEval l@@ -66,16 +65,14 @@ purePrimops = 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))- , ("head", evalArgs $ \args -> case args of- [List (x:_)] -> pure x- [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 "tail: empty list"- [_] -> throwError $ TypeError "tail: expects list argument"- xs -> throwError $ WrongNumberOfArgs "tail" 1 (length xs))+ , ("head", evalOneArg "head" $ \case+ List (x:_) -> pure x+ List [] -> throwError $ OtherError "head: empty list"+ _ -> throwError $ TypeError "head: expects list argument")+ , ("tail", evalOneArg "tail" $ \case+ List (_:xs) -> pure $ List xs+ List [] -> throwError $ OtherError "tail: empty list"+ _ -> throwError $ TypeError "tail: expects list argument"), ("lookup", evalArgs $ \args -> case args of[a, Dict m] -> pure $ case Map.lookup a m ofJust v -> v@@ -129,22 +126,24 @@ purePrimops = fromList $ first Ident <$>[fn, List ls] -> List <$> traverse (fn $$) (pure <$> ls)[_, _] -> throwError $ TypeError "map: second argument should be a list"xs -> throwError $ WrongNumberOfArgs "map" 3 (length xs))- , ("keyword?", evalArgs $ \case- [Keyword _] -> pure $ Boolean True- [_] -> pure $ Boolean False- xs -> throwError $ WrongNumberOfArgs "keyword?" 1 (length xs))- , ("string?", evalArgs $ \args -> case args of- [String _] -> pure $ Boolean True- [_] -> pure $ Boolean False- xs -> throwError $ WrongNumberOfArgs "string?" 1 (length xs))- , ("boolean?", evalArgs $ \args -> case args of- [Boolean _] -> pure $ Boolean True- [_] -> pure $ Boolean False- xs -> throwError $ WrongNumberOfArgs "boolean?" 1 (length xs))- , ("number?", evalArgs $ \args -> case args of- [Number _] -> pure $ Boolean True- [_] -> pure $ Boolean False- xs -> throwError $ WrongNumberOfArgs "number?" 1 (length xs))+ , ("keyword?", evalOneArg "keyword?" $ \case+ Keyword _ -> pure tt+ _ -> pure ff)+ , ("atom?", evalOneArg "atom?" $ \case+ Atom _ -> pure tt+ _ -> pure ff)+ , ("list?", evalOneArg "list?" $ \case+ List _ -> pure tt+ _ -> pure ff)+ , ("string?", evalOneArg "string?" $ \case+ String _ -> pure tt+ _ -> pure ff)+ , ("boolean?", evalOneArg "boolean?" $ \case+ Boolean _ -> pure tt+ _ -> pure ff)+ , ("number?", evalOneArg "number?" $ \case+ Number _ -> pure tt+ _ -> pure ff), ("member?", evalArgs $ \args -> case args of[x, List xs] -> pure . Boolean $ elem x xs[_, _] -> throwError@@ -155,17 +154,14 @@ purePrimops = fromList $ first Ident <$>b <- baseEval cond-- I hate this as much as everyone that might ever read Haskell, but-- in Lisps a lot of things that one might object to are True...- if b == Boolean False then baseEval f else baseEval t+ if b == ff then baseEval f else baseEval txs -> throwError $ WrongNumberOfArgs "if" 3 (length xs))- , ("ref", evalArgs $ \args -> case args of- [x] -> newRef x- xs -> throwError $ WrongNumberOfArgs "ref" 1 (length xs))- , ("read-ref", evalArgs $ \args -> case args of- [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))+ , ("ref", evalOneArg "ref" newRef)+ , ("read-ref", evalOneArg "read-ref" $ \case+ 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"), ("write-ref", evalArgs $ \args -> case args of[Ref (Reference x), v] -> dost <- get@@ -175,16 +171,27 @@ purePrimops = fromList $ first Ident <$>$ TypeError "write-ref: first argument must be a ref"xs -> throwError$ WrongNumberOfArgs "write-ref" 2 (length xs))- , ( "show"- , evalArgs $ \args -> case args of- [x] -> pure (String (renderPrettyDef x))- xs -> throwError $ WrongNumberOfArgs "show" 1 (length xs)+ , ("show", evalOneArg "show" (pure . String . renderPrettyDef))+ , ( "seq"+ , evalOneArg "seq" $+ \case+ x@(List _) -> pure x+ Dict kvs -> pure $ List [List [k, v] | (k,v) <- Map.toList kvs ]+ _ -> throwError $ TypeError "seq: can only create a list from a list of a dict")]where-- Many primops evaluate their arguments just as normal functions do.evalArgs f args = traverse baseEval args >>= f+ -- Many primops evaluate a single argument.+ evalOneArg fname f = evalArgs $ \case+ [x] -> f x+ xs -> throwError $ WrongNumberOfArgs fname 1 (length xs)++ tt = Boolean True+ ff = Boolean False+numBinop :: (Scientific -> Scientific -> Scientific)-> Text-> (Text, [Value] -> Lang m Value)diff --git a/test/spec/Radicle/Tests.hs b/test/spec/Radicle/Tests.hsindex d25cad4..0d5d401 100644--- a/test/spec/Radicle/Tests.hs+++ b/test/spec/Radicle/Tests.hs@@ -194,6 +194,21 @@ test_eval =, testCase "'keyword?' is false for non keywords" $ do"(keyword? #t)" `succeedsWith` Boolean False+ , testCase "'do' returns the empty list if called on nothing" $ do+ "(do)" `succeedsWith` List []++ , testCase "'do' returns the result of the last argument" $ do+ "(do 1)" `succeedsWith` Number 1+ "(do 1 2 3)" `succeedsWith` Number 3++ , testCase "'do' runs effects in order" $ do+ let prog = [s|+ (define r (ref 0))+ (do (write-ref r 1)+ (write-ref r 2))+ (read-ref r)+ |]+ prog `succeedsWith` Number 2, testCase "'string?' is true for strings" $ dolet prog = [s|(string? "hi")|]