Skip to content

A peer-to-peer stack for code collaboration

Update radicle files to reflect new eval

Julian Arni4fc9701d5090
diff --git a/docs/source/guide/Chains.lrad b/docs/source/guide/Chains.lrad
index 22a644d..b9ff1dc 100644
--- a/docs/source/guide/Chains.lrad
+++ b/docs/source/guide/Chains.lrad
@@ -1,5 +1,15 @@
# Chains
+(N.B.: for this section, you should use the `radicle-client` executable rather
+than the `radicle` one. The simplest way to run it is to pass `radicle.xyz` as
+the `url` argument:
+
+```
+radicle-client --config rad/repl.rad --url radicle.xyz
+```
+
+In the future `radicle-client` and `radicle` will be folded into one.)
+
By now you've seen some of `radicle`, but we haven't touched upon what makes it
different from other languages.
@@ -10,11 +20,62 @@ particular machine. *Which* machine that is often matters to understanding how
the program behaves; a program might after all read from a file, for example,
and behave differently based on whether that file exists, and what it's
contents are. But other machines may not have that file, or have one that
-differs in some way.
+differs in some way.
This aspect of program execution is often a source of headache for developers,
as it leads to hard-to-reproduce or -predict behavior. But--and more
importantly for our purposes--it also means we can't think of programs in the
abstract
-In other words, we would like for programs to be *deterministic*.
+In other words, we would like for programs to be *deterministic*.
+
+TODO: more.
+
+## Chains
+
+### Remote chains
+
+The basic building blocks of interacting with remote chains are the `send!` and
+`receive!` operations. These send expressions to a remote chain, or receive new
+ones since a given index, respectively.
+
+These functions are, however, relatively low-level. Higher-level ones exist
+that do the grunt-work of only requesting data you don't already have, and of
+allowing interactive explorations of existing chains before submitting
+expressions. These are described in the next section.
+
+### Exploring chains
+
+Chains are a lot like shared environments. You can enter an environment that
+corresponds to a chain and explore it:
+
+```
+(def x 0)
+x ;; ==> 0
+(:enter-chain empty-chain) ;; ==> :ok
+(def x 1)
+x ;; ==> 1
+:quit ;; ==> :ok
+x ;; ==> 0
+```
+
+As you can see, between entering a chain (with `:enter-chain`) and leaving it
+(with `:quit`), all inputs are evaluated in the context of that chain.
+
+(You may be wondering how `(:enter-chain empty-chain)` could possibly be valid,
+as it contains a keyword apparently doing the job of a function application.
+The short answer is that we can define special macro-like operations, of which
+we will see more.)
+
+`:enter-chain` allows you to enter a local chain. More often, you'll want to
+explore and interact with chains that exist elsewhere--that are replicated in
+a distributed way, or exist in some centralized server. For that, you can use
+`:enter-remote-chain`.
+
+`:enter-remote-chain` takes an argument just like `:enter-chain`, and indeed
+for the most part both behave identically. The main difference is that with
+`:enter-remote-chain`, you also have access to the special command `:send`,
+which sends all the inputs you have so far to the remote.
+
+In order to receive new inputs and update the chain state accordingly, you
+can use the function `update-chain`.
diff --git a/docs/source/guide/index.rst b/docs/source/guide/index.rst
index 874a99c..9f8fb39 100644
--- a/docs/source/guide/index.rst
+++ b/docs/source/guide/index.rst
@@ -58,3 +58,4 @@ how to allow for future changes to the chain.
GettingStarted.md
Basics.lrad
+ Chains.lrad
diff --git a/docs/source/index.rst b/docs/source/index.rst
index 3d9039e..58cb105 100644
--- a/docs/source/index.rst
+++ b/docs/source/index.rst
@@ -12,6 +12,7 @@ radicle
guide/index.rst
guide/GettingStarted.rst
guide/Basics.lrad
+ guide/Chains.lrad
diff --git a/rad/chain.rad b/rad/chain.rad
index 7a1f1b5..f4010fa 100644
--- a/rad/chain.rad
+++ b/rad/chain.rad
@@ -6,13 +6,21 @@
;; - A sequence of inputs.
(def empty-chain
- (dict :state (pure-env)
- :inputs (list)
- :index 0))
+ {:state (pure-env)
+ :inputs (list)
+ :index 0})
+
+(def test-chain
+ {:state (pure-env)
+ :inputs (list)
+ :name "test"
+ :index 0})
+
+;; eval-in-chain
(def eval-in-chain
(fn [expr chain]
- (def x (eval-with-env expr (lookup :state chain)))
+ (def x (eval expr (lookup :state chain)))
(dict :chain (dict :state (head (tail x))
:input (cons expr (lookup :logs chain))
:index (+ 1 (lookup :index chain)))
@@ -22,6 +30,15 @@
'(("expr" any) ("chain" chain))
"Evaluates 'expr' in the 'chain' and returns a dict with the ':result' and the resulting ':chain'.")
+((fn []
+ (def res (eval-in-chain '(+ 3 2) empty-chain))
+ (def ~~> (fn [comp expected] (should-be "eval-in-chain" (view comp res) expected)))
+ (~~> (@ :result) 5)
+ (~~> (.. (@ :chain) (@ :input)) '((+ 3 2)))
+ (~~> (.. (@ :chain) (@ :index)) 1)))
+
+;; update-chain-with
+
(def update-chain-with
(fn [chain remote-chain-id cb-expr cb-res]
(def new-inputs
@@ -38,52 +55,90 @@
'(("chain" chain) ("chain-id" string) ("cb-expr" function) ("cb-res" function))
"Updates 'chain' according to activity on the remote chain with ID 'chain-id', by pulling down and evaluating new inputs. Accepts two callbacks: 'cb-expr' is applied to the incoming expressions, and 'cb-res' is applied to the resulting values. Usually one uses 'print!' for both of these. If 'chain' is not a past version of the remote chain then the behaviour is unspecified.")
-(def enter-chain
- (fn [eval-definer chain]
- (def old-eval eval)
- (def ch (ref chain))
- (def ev
- (fn [e]
- (if (eq? e :quit)
- (do (print! "leaving chain")
- (eval-definer old-eval))
- (do (def x (eval-in-chain e (read-ref ch)))
- (write-ref ch (view (@ :chain) x))
- (view (@ :result) x)))))
- (eval-definer ev)))
-
-(document 'enter-chain
- '(("eval-definer" function) ("chain" chain))
- "Instantiates an 'eval' which evaluates expressions in 'chain'. The expression ':quit' will return to the previous eval. Uses the parameter 'eval-definer' for eval redefinition, this should be a function which can be used to redefine 'eval': it should set eval to it's input.")
-
-(def enter-remote-chain
- (fn [eval-definer chain-id]
- (def old-eval eval)
- (def chain (update-chain-with empty-chain chain-id (fn [e] '()) (fn [e] '())))
- (print! (string-append "Synced with remote chain, at index: " (show (view (@ :index) chain))))
- (def ch (ref chain))
- (def inputs (ref (list)))
- (eval-definer
- (fn [e]
- (cond
- (eq? e :quit) (do (print! "Leaving remote-chain.")
- (eval-definer old-eval))
- (eq? e :send) (do (print! "Sending inputs to remote chain.")
- (map (fn [i] (send! chain-id i)) (reverse (read-ref inputs)))
- (print! "Done! Leaving remote-chain.")
- (eval-definer old-eval))
- :else (do (def x (eval-in-chain e (read-ref ch)))
- (write-ref ch (view (@ :chain) x))
- (modify-ref inputs (fn [is] (cons e is)))
- (view (@ :result) x)))))))
-
-(document 'enter-remote-chain
- '(("eval-definer" function) ("chain-id" string))
- "Syncs up to a remote chain, and allows experimentation inputs. There are two special inputs: ':quit' will leave the chain, ':send' will send all the accumulated inputs to the remote chain, one by one, and then leave the chain. Uses the parameter 'eval-definer' for eval redefinition, this should be a function which can be used to redefine 'eval': it should set eval to it's input.")
-
-;; Setup an eval definer
-(def boom
- (quote
- (do (def eval-ref (ref eval))
- (def eval-definer (fn [f] (write-ref eval-ref f)))
- (def eval (fn [e] ((read-ref eval-ref) e))))))
+;; update-chain
+
+(def update-chain
+ (fn [chain]
+ (def next (ref chain))
+ (update-chain-with chain (lookup :name chain) (fn [x] (modify-ref next (fn [r] (eval-in-chain x r)))) print!)))
+
+(document 'update-chain
+ '(("chain" chain))
+ "Return a version of 'chain' with new inputs and correspondingly updated state")
+
+;; add-quit
+
+(def add-quit
+ (fn [after-quit-state before-quit-eval]
+ (fn [expr env]
+ (if (eq? expr :quit)
+ (list :ok after-quit-state)
+ (before-quit-eval expr env)))))
+
+(document 'add-quit
+ '(("after-quit-state" dict) ("before-quit-eval" fn))
+ "Adds a ':quit' command to 'before-quit-eval', which switches to 'after-quit-state' (and to the eval in that state)")
+
+;; store-exprs
+
+(def store-exprs
+ (fn [evalfn]
+ (fn [expr env]
+ (def env_ (over (.. (@ :env) (@ '_inputs)) (fn [x] (add-right expr x)) env))
+ (evalfn expr env_ ))))
+
+(document 'store-exprs
+ '(("eval" fn))
+ "Store each new evaluated expression in '_inputs'")
+
+;; add-send
+(def add-send
+ (fn [oeval]
+ (fn [expr env]
+ (if (eq? expr :send)
+ (do (def inpl (.. (@ :env) (@ '_inputs)))
+ (def chainl (.. (@ :env) (@ '_cur-chain)))
+ (map (fn [e] (send! (view chainl env) e)) (view inpl env))
+ (def env_ (set inpl [] env))
+ (list :ok env_))
+ ((store-exprs oeval) expr env)))))
+
+(document 'add-send
+ '(("eval-fn" fn))
+ "Add a :send special form that sends the contents of _input to the chain _cur-chain")
+
+;; enter-chain & enter-remote-chain
+
+(def eval__ eval)
+(def-rec eval
+ (fn [expr env]
+ ;; The horror, the horror
+ (if (list? expr)
+ (if (> (length expr) 0)
+ (cond
+ (eq? (head expr) :enter-chain) (do
+ (def arg_ (nth 1 expr))
+ (def arg (head (eval arg_ env)))
+ (def mod-state
+ (over (.. (@ :env) (@ 'eval))
+ (fn [x] (add-quit env x))
+ (lookup :state arg)))
+ (list :ok mod-state))
+ (eq? (head expr) :enter-remote-chain) (do
+ (def arg_ (nth 1 expr))
+ (def arg (head (eval arg_ env)))
+ (def state (lookup :state arg))
+ (def mod-state1
+ (over (@ :env) (fn [x] (insert '_inputs [] x)) state))
+ (def mod-state2
+ (over (@ :env)
+ (fn [x] (insert '_cur-chain (lookup :name arg) x))
+ mod-state1))
+ (def mod-state3
+ (over (.. (@ :env) (@ 'eval))
+ (fn [x] (add-send (add-quit (get-current-env) x)))
+ mod-state2))
+ (list :ok mod-state3))
+ :else (eval__ expr env))
+ (eval__ expr env))
+ (eval__ expr env))))
diff --git a/rad/prelude/dict.rad b/rad/prelude/dict.rad
index d5173e2..0d39333 100644
--- a/rad/prelude/dict.rad
+++ b/rad/prelude/dict.rad
@@ -30,6 +30,19 @@
;; '(:one :two))
+;; rekey
+(def rekey (fn [old-key new-key mp]
+ (def old-val (lookup old-key mp))
+ (delete old-key (insert new-key old-val mp))))
+
+(document 'rekey
+ '(("old-key" any) ("new-key" any) ("dict" dict))
+ "Change the key from 'old-key' to 'new-key' in 'dict'. If 'new-key' already exists, it is overwritten.")
+
+(should-be "rekey"
+ (rekey :a :b {:a 5})
+ {:b 5})
+
;; modify-map
(def modify-map (fn [key f mp]
(insert key (f (lookup key mp)) mp)))
diff --git a/rad/prelude/should-be.rad b/rad/prelude/should-be.rad
index 921bf2e..5f3f88e 100644
--- a/rad/prelude/should-be.rad
+++ b/rad/prelude/should-be.rad
@@ -4,7 +4,9 @@
(fn [name x y]
(if (eq? x y)
(print! (string-append "Test '" name "' succeeded"))
- (print! (string-append "Test '" name "' failed")))))
+ (do
+ (print! (string-append "Test '" name "' failed"))
+ (print! (string-append "LHS:\n\t" (show x) "\nRHS:\n\t" (show y)))))))
(document 'should-be
'(("test-name" string) ("actual" any) ("expected" any))
diff --git a/src/Radicle/Internal/PrimFns.hs b/src/Radicle/Internal/PrimFns.hs
index 300442b..9e58344 100644
--- a/src/Radicle/Internal/PrimFns.hs
+++ b/src/Radicle/Internal/PrimFns.hs
@@ -89,7 +89,7 @@ purePrimFns = PrimFns $ fromList $ first Ident <$>
, ( "add-right"
, twoArg "add-right" $ \case
(x, Vec xs) -> pure $ Vec (xs Seq.:|> x)
- _ -> throwErrorHere $ TypeError "add-left: second argument must be a vector"
+ _ -> throwErrorHere $ TypeError "add-right: second argument must be a vector"
)
-- Lists
@@ -140,6 +140,10 @@ purePrimFns = PrimFns $ fromList $ first Ident <$>
[_, _, _] -> throwErrorHere
$ TypeError "insert: third argument must be a dict"
xs -> throwErrorHere $ WrongNumberOfArgs "insert" 3 (length xs))
+ , ("delete", \case
+ [k, Dict m] -> pure . Dict $ Map.delete k m
+ [_, _] -> throwErrorHere $ TypeError "delete: second argument must be a dict"
+ xs -> throwErrorHere $ WrongNumberOfArgs "delete" 2 (length xs))
-- The semantics of + and - in Scheme is a little messed up. (+ 3)
-- evaluates to 3, and of (- 3) to -3. That's pretty intuitive.
-- But while (+ 3 2 1) evaluates to 6, (- 3 2 1) evaluates to 0. So with -
@@ -171,7 +175,8 @@ purePrimFns = PrimFns $ fromList $ first Ident <$>
xs -> throwErrorHere $ WrongNumberOfArgs "foldr" 3 (length xs))
, ("map", \case
[fn, List ls] -> List <$> traverse (callFn fn) (pure <$> ls)
- [_, _] -> throwErrorHere $ TypeError "map: second argument should be a list"
+ [fn, Vec ls] -> Vec <$> traverse (callFn fn) (pure <$> ls)
+ [_, _] -> throwErrorHere $ TypeError "map: second argument should be a list or vector"
xs -> throwErrorHere $ WrongNumberOfArgs "map" 3 (length xs))
, ("keyword?", oneArg "keyword?" $ \case
Keyword _ -> pure tt
diff --git a/test/spec/Radicle/Tests.hs b/test/spec/Radicle/Tests.hs
index 23cd9ae..53c5ed0 100644
--- a/test/spec/Radicle/Tests.hs
+++ b/test/spec/Radicle/Tests.hs
@@ -649,6 +649,24 @@ test_repl =
]
(_, result) <- runInRepl input
result @==> output
+
+ , testCase "'enter-chain' changes environment" $ do
+ let input = [ "(def x 0)"
+ , "(:enter-chain empty-chain)"
+ , "(def x 1)"
+ , "x"
+ , ":quit"
+ , "x"
+ ]
+ output = [ "()"
+ , ":ok"
+ , "()"
+ , "1.0"
+ , ":ok"
+ , "0.0"
+ ]
+ (_, result) <- runInRepl input
+ result @==> output
]
where
-- In addition to the output of the lines tested, 'should-be's get