A peer-to-peer stack for code collaboration
Monadic issues chain.
James Henri Haydon8517523e1d57
diff --git a/.gitignore b/.gitignoreindex 87dfab5..11252a3 100644--- a/.gitignore+++ b/.gitignore@@ -17,3 +17,4 @@ docs/build/*.swpdocs/build/TAGS+rad/my-keys.raddiff --git a/docs/source/reference.rst b/docs/source/reference.rstindex 128262e..4a46fb9 100644--- a/docs/source/reference.rst+++ b/docs/source/reference.rst@@ -279,7 +279,8 @@ Functions for manipulating vectors.``<>``~~~~~~-Concatenates two vectors.+Merges two structures together. On vectors this performs concatenations.+On dicts this performs the right-biased merge.``add-left``~~~~~~~~~~~~@@ -353,6 +354,12 @@ short, in which case an exception is thrown.Returns all but the first ``n`` items of a sequence, unless the sequenceis empty, in which case an exception is thrown.+``sort-by``+~~~~~~~~~~~++Given a sequence ``xs`` and a function ``f``, returns a sequence with+the same elements ``x`` of ``xs`` but sorted according to ``(f x)``.+Dicts-----@@ -420,6 +427,51 @@ keys as ``d`` but ``f`` applied to all the associated values.Given a key, a function and a dict, applies the function to the valueassociated to that key.+``delete-many``+~~~~~~~~~~~~~~~++Delete several keys from a dict.++Sets+----++Functions for manipulating sets.++``set/empty``+~~~~~~~~~~~~~++An empty set.++``set/insert``+~~~~~~~~~~~~~~++Insert a value into a set.++``set/delete``+~~~~~~~~~~~~~~++Delete a value from a set.++``set/member?``+~~~~~~~~~~~~~~~++Query if an value is an element of a set.++``set/delete``+~~~~~~~~~~~~~~++Delete a value from a set.++``set/from-seq``+~~~~~~~~~~~~~~~~++Create a set from a sequence.++``set/to-vec``+~~~~~~~~~~~~~~++Convert a set to a vector.+Structures----------@@ -626,6 +678,12 @@ Read a single line of input and interpret it as radicle data.Exit the interpreter immediately.+``now!``+~~~~~~~~++Returns a timestamp for the current Coordinated Universal Time (UTC),+right now, formatted according to ISO 8601.+Lenses------@@ -753,6 +811,19 @@ Given a dict associating keys to validators, returns a validator whichchecks a dict for the existence of those keys, and that they conform tothe associated validators.+``validator/uuid``+~~~~~~~~~~~~~~~~~~++Validates UUIDs.++``validator/signed``+~~~~~~~~~~~~~~~~~~~~++Checks that a value is a dict with ``:signature`` and ``:author`` keys,+and that the signature is valid for the rest of the dict for that+author. The rest of the dict is turned into a string according to+``show``.+Cryptography------------@@ -896,28 +967,4 @@ the next state.~~~~~~~~~~~~~~~~~~~~Update a ref containing a chain with the new expressions from the remote-chain--Issue chain--------------These functions allow creating and interacting with the default issues-chain.--``create-issues-chain!``-~~~~~~~~~~~~~~~~~~~~~~~~--Create a remote issue chain with the given url. Returns a ref with the-chain.--``list-issues``-~~~~~~~~~~~~~~~--Given an issues ref ``x``, returns its issues. Does not itself update-the chain.--``new-issue!``-~~~~~~~~~~~~~~--Create a new remote issue. Takes a chain ref, a keypair, an issue title-and an issue body.\ No newline at end of file+chain\ No newline at end of filediff --git a/exe/GithubIssues.hs b/exe/GithubIssues.hsindex 8a62cd2..c49ace0 100644--- a/exe/GithubIssues.hs+++ b/exe/GithubIssues.hs@@ -39,7 +39,8 @@ getIssues auth owner repo = dois <- ExceptT $ GH.issuesForRepo' auth (N owner) (N repo) GH.stateAlllet issues = filter notPR (toList is)css <- traverse (getComments auth owner repo) issues- pure $ zip issues css+ -- We reverse to produce a list in chronological order+ pure $ reverse $ zip issues csswherenotPR Issue{ issuePullRequest = Nothing} = TruenotPR _ = False@@ -54,10 +55,10 @@ getComments auth owner repo Issue{..} = doradIssue :: Issue -> [IssueComment] -> ValueradIssue Issue{..} cs = dict $- [ ( [kword|author-github-username|], author issueUser )+ [ ( [kword|github-username|], author issueUser ), ( [kword|title|], String issueTitle ), ( [kword|body|], mt issueBody )- , ( [kword|github-assignees|], toRad $ author <$> toList issueAssignees)+ , ( [kword|github-assignee-usernames|], toRad $ author <$> toList issueAssignees), ( [kword|state|], state issueState ), ( [kword|labels|], toRad $ GH.untagName . GH.labelName <$> toList issueLabels@@ -74,7 +75,7 @@ radIssue Issue{..} cs = dict $radComment :: IssueComment -> ValueradComment IssueComment{..} = dict[ ( [kword|body|], String issueCommentBody )- , ( [kword|github-author|], author issueCommentUser )+ , ( [kword|github-username|], author issueCommentUser ), ( [kword|created-at|], date issueCommentCreatedAt )]@@ -84,7 +85,7 @@ dict = Dict . Map.fromListdate :: UTCTime -> Valuedate = String . toS . formatTime defaultTimeLocale fmtwhere- fmt = iso8601DateFormat (Just "%H:%M:%S")+ fmt = iso8601DateFormat (Just "%H:%M:%SZ")author :: GH.SimpleUser -> Valueauthor = String . GH.untagName . GH.simpleUserLogindiff --git a/exe/ReferenceDoc.hs b/exe/ReferenceDoc.hsindex 0a1697c..370dbd6 100644--- a/exe/ReferenceDoc.hs+++ b/exe/ReferenceDoc.hs@@ -49,6 +49,7 @@ main = do, sec "Vectors" vecs $ typ "vectors", sec "Sequences" seqs $ typ "boths lists and vectors", sec "Dicts" dicts $ typ "dicts"+ , sec "Sets" sets $ typ "sets", sec "Structures" structs $ typ "lists, vectors and dicts", sec "Refs" refs "Functions for creating, querying and modifying refs.", sec "Evaluation functions" evalFns "Utilities for creating and extending evaluation functions."@@ -69,8 +70,6 @@ main = do"These functions can be used to simulate remote chains in the local REPL.\\ This is useful for experimenting with inputs or even new evaluation functions\\ before sending these to a remote chain."- , sec "Issue chain" issueChain- "These functions allow creating and interacting with the default issues chain."]basics =@@ -81,25 +80,27 @@ main = domaths = ["+", "*", "-", "/", "<", ">", "integral?"]evalFns = ["base-eval", "eval", "updatable-eval"]envStuff = ["pure-env", "get-current-env", "set-current-env", "set-env!"]- seqs = ["nth", "foldl", "foldr", "map", "seq", "take", "drop"]+ seqs = ["nth", "foldl", "foldr", "map", "seq", "take", "drop", "sort-by"]lists =[ "list", "nil", "head", "tail", "empty?", "cons", "reverse", "length", "concat", "filter", "range", "list-with-head", "for-each" ]vecs = ["<>", "add-left", "add-right", "for-each-vec" ]dicts =["dict", "lookup", "insert", "delete", "dict-from-list", "keys", "values", "rekey"- , "map-values", "modify-map"]+ , "map-values", "modify-map", "delete-many"]+ sets = ["set/empty", "set/insert", "set/delete", "set/member?", "set/delete", "set/from-seq", "set/to-vec"]structs = ["member?"]refs = ["ref", "read-ref", "write-ref", "modify-ref"]docs = ["help", "doc", "doc!", "apropos!", "document", "is-test-env"]io =- ["put-str!", "print!", "get-line!", "get-expression!"+ [ "print!", "get-line!", "load!", "read-file!", "read-code!", "send-code!"- , "send-prelude!", "subscribe-to!", "uuid!", "read-line!", "exit!"]+ , "send-prelude!", "subscribe-to!", "uuid!", "read-line!", "exit!"+ , "now!"]lens = ["@", "@nth", "make-lens", "view", "view-ref", "set", "set-ref", "over", "over-ref", "id-lens", "..", "..."]validation =[ "validator/=", "validator/member", "validator/type", "validator/pred", "validator/every"- , "validator/and", "validator/or", "validator/key", "validator/keys"]+ , "validator/and", "validator/or", "validator/key", "validator/keys", "validator/uuid", "validator/signed"]crypto = ["verify-signature", "default-ecc-curve", "gen-key-pair!", "gen-signature!"]chainTools =[ "new-chain", "@var", "eval-in-chain", "enter-remote-chain", "update-chain", "add-quit", "add-send"@@ -107,7 +108,7 @@ main = do, "state-machine-eval", "state-machine-input", "state-machine-new-trans", "state-machine-agree", "state-machine-disagree", "simple-trans", "update-chain-ref"]- issueChain = ["create-issues-chain!", "list-issues", "new-issue!"]+ -- issueChain = ["create-issues-chain!", "list-issues", "new-issue!"]typ s = "Functions for manipulating " <> s <> "."@@ -116,8 +117,8 @@ main = doallFns =basics ++ maths ++ evalFns ++ envStuff ++ seqs ++ lists ++ vecs- ++ dicts ++ structs ++ refs ++ docs ++ io ++ lens ++ validation ++ crypto ++ chainTools- ++ issueChain ++ doNotInclude+ ++ dicts ++ sets ++ structs ++ refs ++ docs ++ io ++ lens ++ validation ++ crypto ++ chainTools+ ++ doNotInclude-- Function symbol followed by a hard line break, followed by it's doc.funs e fns = mconcatdiff --git a/package.yaml b/package.yamlindex 1911afd..0c28568 100644--- a/package.yaml+++ b/package.yaml@@ -30,6 +30,7 @@ dependencies:- serialise- template-haskell- text+- time- unordered-containers- uuidwhen:diff --git a/rad/chains/issue-remote.rad b/rad/chains/issue-remote.raddeleted file mode 100644index 25d9559..0000000--- a/rad/chains/issue-remote.rad+++ /dev/null@@ -1,102 +0,0 @@-;; Issue chain-;; Meant to be sent to a chain with send-code!--(def issues (ref {}))--(def mod-issues- "A helper to apply a function to `issues`"- (fn [f] (modify-ref issues f)))--(def id-valid-and-free?- "Checks that its argument is a `uuid` that hasn't yet been used."- (fn [id]- (and (uuid? id)- (not (member? id (read-ref issues))))))--(def issue-valid?- "Verifies the signature of an issue."- (fn [i]- (verify-signature (lookup :author i)- (lookup :signature i)- (string-append chain-id- (lookup :id i)- (lookup :title i)- (lookup :body i)))))--(def new-issue- "Create an issue from a dictionary, storing it in `issues`."- (fn [i]- (def id (lookup :id i))- (if (id-valid-and-free? id)- (mod-issues- (fn [is] (insert id i is)))- (throw 'bad-command "Issue ID was not free or was invalid."))))--(def close-issue- "Remove an issue from `issues`."- (fn [id]- (mod-issues (fn [is] (delete id is)))))--(def add-comment- "Add a comment to an issue."- (fn [issue-id comment]- (mod-issues- (fn [is]- (modify-map issue-id- (fn [i]- (modify-map :comments- (fn [cs] (add-right comment cs))- i))- is)))))--;; These are intended for clients to run locally with eval-in-chain--(def list-issues- "List existing issues."- (fn [] (read-ref issues)))--;; General logic--(def commands- "The set of allowed chain commands."- (ref- {'create-issue new-issue- 'close-issue close-issue- 'list-issues list-issues}))--(def process-command- "Function that maps post-eval commands to existing functions"- (fn [expr]- (def command (head expr))- (def args (tail expr))- (def do-this (lookup command (read-ref commands)))- (apply do-this args)))--;; Move to eval-fn-app & updatable-eval from chain.rad to a pure file so it may-;; be imported--(def eval-fn-app- "Given a state, a function, an argument and a callback, returns- the result of evaluating the function call on the arg in the given- state, while also calling the callback on the result."- (fn [state f arg cb]- (def exec (base-eval (list f (list 'quote arg)) state))- (cb (head exec))- exec))--(def updatable-eval- "Given an evaluation function `f`, returns a new one which augments `f` with a new- command `(update expr)` which evaluates arbitrary expression using `base-eval`."- (fn [sub-eval]- (fn [expr state]- (list-with-head- expr- (fn [cmd]- (if (eq? cmd 'update)- (base-eval (nth 1 expr) state)- (sub-eval expr state)))- (fn [] (sub-eval expr state))))))--(def eval (updatable-eval- (fn [expr state]- (eval-fn-app state 'process-command expr (fn [x] x)))))diff --git a/rad/chains/issues.rad b/rad/chains/issues.raddeleted file mode 100644index d7bcfca..0000000--- a/rad/chains/issues.rad+++ /dev/null@@ -1,52 +0,0 @@-;; Helpers for setting up and interacting with issues chain (as defined in-;; rad/issues-chain.rad)--;; Create a remote issue chain. Returns a ref for the chain that you-;; can assign to a variable.--(def create-issues-chain!- "Create a remote issue chain with the given url. Returns a ref with the chain."- (fn [chain-name]- (send-prelude! chain-name)- (send-code! chain-name "rad/chains/issue-remote.rad")- (ref (new-chain chain-name))))--(def new-issue!- "Create a new remote issue. Takes a chain ref, a keypair, an issue title and an issue body."- (fn [chain-ref keys title body]- (def chain (read-ref chain-ref))- (def id (uuid!))- (def msg- (string-append (lookup :url chain)- id- title- body))- (def arg- {:id id- :author (lookup :public-key keys)- :title title- :body body- :signature (gen-signature! (lookup :private-key keys) msg)})- (send! (lookup :url chain)- [(list 'create-issue arg)])))--(def list-issues- "Given an issues ref `x`, returns its issues. Does not itself update the chain."- (fn [chain-ref]- (lookup :result (eval-in-chain '(list-issues) (read-ref chain-ref)))))--(:test- "creating, manipulating and querying issue chains"- [:setup- (do- (def pk (gen-key-pair! (default-ecc-curve)))- (def chain-name (uuid!))- (def chain-ref (create-issues-chain! chain-name))- (new-issue! chain-ref pk "Ah!" "if thou issueless shall hap to die")- (update-chain-ref chain-ref)- (def issues (values (list-issues chain-ref))))- ]- [ (length issues) ==> 1 ]- [ (lookup :title (nth 0 issues)) ==> "Ah!" ]- [ (lookup :body (nth 0 issues)) ==> "if thou issueless shall hap to die" ]-)diff --git a/rad/monadic/issue-remote.rad b/rad/monadic/issue-remote.radnew file mode 100644index 0000000..da7954b--- /dev/null+++ b/rad/monadic/issue-remote.rad@@ -0,0 +1,183 @@+;; Issue chain+;; Meant to be sent to a chain with send-code!++(def chain-id+ "The name of this chain."+ "monadic/radicle/issues")++(def issues+ "Issues are stored in a dict in a ref, keyed by issue-number."+ (ref {}))++(def issue-counter+ "We use a stateful counter for giving issues numbers."+ (make-counter))++(def used-nonces+ "We keep track of the UUIDs which have been used as nonces in inputs."+ (ref set/empty))++(def unused-nonce?+ "Check that a nonce has not been used already."+ (fn [nonce]+ (not (set/member? nonce (read-ref used-nonces)))))++(def mark-used-nonce+ "Mark a UUID as used."+ (fn [nonce]+ (modify-ref used-nonces (fn [s] (set/insert nonce s)))))++(def set-issues+ "A helper to set stuff in the issues ref."+ (fn [l v]+ (set-ref issues l v)))++(def over-issues+ "A helper to modify stuff in the issues ref."+ (fn [l f]+ (over-ref issues l f)))++(def validator/nonce+ "Validator for unused nonces."+ (validator/and+ [validator/uuid+ (validator/pred "Nonce not already used" unused-nonce?)]))++(def validator/text+ "A validator for text."+ (fn [max-len]+ (validator/and+ [(validator/type :string)+ (validator/pred (string-append "< " (show max-len) " chars in length")+ (fn [s] (< (string-length s) max-len)))])))++;; TODO correctly formatted date string+(def validator/timestamp+ (validator/type :string))++(def validator/input+ "Checks that the input as a valid new *chain input*, that is, it has an unused+ nonce, is marked with this chain name, and is signed."+ (validator/and+ [(validator/keys+ {:nonce validator/nonce+ :chain-id (validator/= chain-id)+ ;; NOTE that the timestamp is claimed by the author, is not verified, and+ ;; may be incorrect.+ :created-at validator/timestamp})+ validator/signed]))++(def validator/issue-number+ "A validator for numbers which refer to existing issues."+ (validator/pred "Existing issue number" (fn [n] (member? n (read-ref issues)))))++(def validator/comment+ "A comment should have a body."+ (validator/keys+ {:body (validator/text 4000)}))++(def validator/comment-input+ "A comment input should refer to an existing issue."+ (validator/and+ [validator/input+ validator/comment+ (validator/keys {:issue-number validator/issue-number})]))++(def validator/issue-input+ "An issue should have all the required fields, be a signed input, etc."+ (validator/and+ [validator/input+ (validator/keys+ {:title (validator/text 100)+ :body (validator/text 4000)+ :labels (validator/every (validator/text 80))+ :state (validator/member [:open :closed])+ :comments (validator/every validator/comment)})]))++(def strip-input+ "After an entity input has been processed/verified, we no longer need some of+ the fields."+ (fn [e]+ (delete-many [:chain-id :signature :nonce :issue-number] e)))++(def create-issue+ "Create an issue from a dict, checking that it is valid and storing it in `issues`."+ (fn [i]+ (validator/issue-input i)+ (def n (issue-counter :next))+ (def i_+ (strip-input+ (<> i+ {:number n+ :comments (map strip-input (lookup :comments i))})))+ (set-issues (@ n) i_)+ (mark-used-nonce (lookup :nonce i))))++(def close-issue+ "Given an issue number, sets the state of the issue to `:closed`."+ (fn [n]+ (validator/issue-number n)+ (set-issues (.. (@ n) (@ :state)) :closed)))++(def add-comment+ "Add a comment to an issue."+ (fn [c]+ (validator/comment-input c)+ (def c_ (strip-input c))+ (over-issues+ (.. (@ (lookup :issue-number c)) (@ :comments))+ (fn [cs] (add-right c_ cs)))+ (mark-used-nonce (lookup :nonce c))))++;; These are intended for clients to run locally with eval-in-chain++(def list-issues+ "List existing issues."+ (fn [] (read-ref issues)))++;; General logic++(def commands+ "The set of allowed chain commands."+ (ref+ {'create-issue create-issue+ 'close-issue close-issue+ 'add-comment add-comment+ 'list-issues list-issues}))++(def process-command+ "Function that maps post-eval commands to existing functions"+ (fn [expr]+ (def command (head expr))+ (def args (tail expr))+ (def do-this (lookup command (read-ref commands)))+ (apply do-this args)))++;; Move to eval-fn-app & updatable-eval from chain.rad to a pure file so it may+;; be imported++(def eval-fn-app+ "Given a state, a function, an argument and a callback, returns+ the result of evaluating the function call on the arg in the given+ state, while also calling the callback on the result."+ (fn [state f arg cb]+ (def exec (base-eval (list f (list 'quote arg)) state))+ (cb (head exec))+ exec))++(def updatable-eval+ "Given an evaluation function `f`, returns a new one which augments `f` with a new+ command `(update expr)` which evaluates arbitrary expression using `base-eval`."+ (fn [sub-eval]+ (fn [expr state]+ (list-with-head+ expr+ (fn [cmd]+ (if (eq? cmd 'update)+ (base-eval (nth 1 expr) state)+ (sub-eval expr state)))+ (fn [] (sub-eval expr state))))))++(def eval (updatable-eval+ (fn [expr state]+ (eval-fn-app state 'process-command expr (fn [x] x)))))diff --git a/rad/monadic/issues.rad b/rad/monadic/issues.radnew file mode 100644index 0000000..0733f0f--- /dev/null+++ b/rad/monadic/issues.rad@@ -0,0 +1,146 @@+;; Helpers for setting up and interacting with issues chain (as defined in+;; rad/monadic/issues-remote.rad)++;; Create a remote issue chain. Returns a ref for the chain that you+;; can assign to a variable.++(load! "rad/prelude.rad")++(def radicle-issues-chain-id+ "The name of this chain."+ "monadic/radicle/issues")++(def issues-chain+ (ref (load-chain "http://radicle.xyz/chains/radicle-issues")))++(def create-issues-chain!+ "Create a remote issue chain with the given url."+ (fn []+ (def url (lookup :url (read-ref issues-chain)))+ (send-prelude! url)+ (send-code! url "rad/monadic/issue-remote.rad")))++(def sign-entity!+ "Assumes a key pair is stored at `my-keys.rad`. Using that key pair, will sign a+ dict by adding `:author` and `:signature` fields, so that it is valid+ according to `validator/signed`, while also adding a `:nonce`."+ (fn [e]+ (def nonce (uuid!))+ (def e_ (<> e {:chain-id radicle-issues-chain-id+ :nonce nonce}))+ (def ks (read (read-file! "my-keys.rad")))+ (def sig+ (gen-signature! (lookup :private-key ks)+ (show e_)))+ (<> e_+ {:author (lookup :public-key ks)+ :signature sig})))++(def simple-issue+ "Create an open issue with no comments or labels."+ (fn [tit bod]+ {:title tit+ :body bod+ :labels []+ :state :open+ :comments []+ :created-at (now!)}))++(def simple-comment+ "Create a comment for an issue."+ (fn [issue-number bod]+ {:body bod+ :issue-number issue-number+ :created-at (now!)}))++(def create-issue!+ "Create a new remote issue with the keys in `my-keys.rad`."+ (fn [i]+ (send! (lookup :url (read-ref issues-chain))+ [(list 'create-issue (sign-entity! i))])))++(def add-comment!+ "Create a remote comment with the keys in `my-keys.rad`."+ (fn [c]+ (send! (lookup :url (read-ref issues-chain))+ [(list 'add-comment (sign-entity! c))])))++(def simple-create-issue!+ "Create a remote issue with sensible defaults."+ (fn [tit bod]+ (create-issue! (simple-issue tit bod))))++(def simple-add-comment!+ "Create a remote comment."+ (fn [in b]+ (add-comment! (simple-comment in b))))++(def list-issues+ "Return the full map of issues."+ (fn []+ (lookup :result (eval-in-chain '(list-issues) (read-ref issues-chain)))))++(def fetch+ "Update the locally cached state of the chain."+ (fn []+ (update-chain-ref issues-chain)))++(def import-github+ "Imports the github issues stored in file `githubissues.rad`. To create this+ file use the `radicle-github-issues` executable. Will add a note to all+ `:body`s that this is an imported entity."+ (fn []+ (def note-imported+ (fn [x]+ (insert+ :body+ (string-append+ (lookup :body x)+ "\n\n*Imported from GitHub.* Original author: `"+ (lookup :github-username x)+ "`.")+ x)))+ (def note-comments+ (fn [i] (over (@ :comments) (fn [cs] (map note-imported cs)) i)))+ (def issues (read (read-file! "githubissues.rad")))+ (def imported+ (map+ (fn [i] (note-imported (note-comments i)))+ issues))+ (map create-issue! imported)))++(def short+ "Short render of an issue."+ (fn [i]+ (string-append+ (show (lookup :number i))+ ": "+ (lookup :title i))))++(def list-last+ "Get the last `n` issues."+ (fn [n]+ (def is (sort-by (fn [i] (- 0 (lookup :number i))) (values (list-issues))))+ (take n is)))++(def get-by-id+ (fn [id]+ (lookup id (list-issues))))++(:test+ "The monadic issues chain works."+ [:setup+ (do (create-issues-chain!)+ (simple-create-issue! "title0" "body0")+ (simple-create-issue! "title1" "body1")+ (simple-add-comment! 0 "comment0")+ (simple-add-comment! 1 "comment1")+ (fetch)+ (def is (list-issues)))]+ [ (length (seq is)) ==> 2 ]+ [ (view (.. (@ 0) (@ :title)) is) ==> "title0" ]+ [ (view (.. (@ 1) (@ :title)) is) ==> "title1" ]+ [ (view (... [(@ 1) (@ :comments) (@nth 0) (@ :body)]) is) ==> "comment1" ]+ [ (short (head (values is))) ==> "0: title0" ]+ [ (short (head (list-last 1))) ==> "1: title1" ]+)diff --git a/rad/prelude.rad b/rad/prelude.radindex 1ebc8e0..428f43f 100644--- a/rad/prelude.rad+++ b/rad/prelude.rad@@ -1,14 +1,17 @@(load! "rad/prelude/io.rad")(load! "rad/prelude/test.rad")(load! "rad/prelude/bool.rad")+(load! "rad/prelude/exception.rad")(load! "rad/prelude/recursion.rad")(load! "rad/prelude/list.rad")(load! "rad/prelude/dict.rad")+(load! "rad/prelude/set.rad")(load! "rad/prelude/ref.rad")(load! "rad/prelude/lens.rad")(load! "rad/chain.rad")(load! "rad/state-machine.rad")(load! "rad/prelude/validation.rad")+(load! "rad/prelude/util.rad")(def pure-prelude-files(list@@ -18,10 +21,12 @@"rad/prelude/bool.rad""rad/prelude/list.rad""rad/prelude/dict.rad"+ "rad/prelude/set.rad""rad/prelude/ref.rad""rad/prelude/lens.rad""rad/state-machine.rad""rad/prelude/validation.rad"+ "rad/prelude/util.rad"))(document 'pure-prelude-files@@ -45,25 +50,13 @@'(("chain-id" string))"Send the pure prelude to a chain.")-(def make-counter- "Creates a stateful counter. Returns a dict with two keys: the function at `:next-will-be`- will return the next number (without incrementing it), while the function at `:next`- increments the number and returns it."- (fn []- (def i (ref -1))- {:next-will-be (fn [] (+ 1 (read-ref i)))- :next (fn [] (modify-ref i (fn [x] (+ x 1))))}))-(def _initial-prompt-text"Text used for greeting in the repl.""Welcome to radicle. Type (help) for help.")-(def help+(def help"Default help text."(fn []"radicle is a LISP-like language intended for programming chains, and interacting with those chains.Type (doc '<name>) for further documentation of <name>."))---(load! "rad/chains/issues.rad") ;; send-prelude! must be defined alreadydiff --git a/rad/prelude/dict.rad b/rad/prelude/dict.radindex 3d03b47..b07f845 100644--- a/rad/prelude/dict.rad+++ b/rad/prelude/dict.rad@@ -57,3 +57,8 @@(:test "modify-map"[ (modify-map :a (fn [x] (+ x 1)) {:a 5})==> {:a 6} ])++(def delete-many+ "Delete several keys from a dict."+ (fn [ks d]+ (foldr (fn [k d] (delete k d)) d ks)))diff --git a/rad/prelude/exception.rad b/rad/prelude/exception.radindex 4ae9089..458309c 100644--- a/rad/prelude/exception.rad+++ b/rad/prelude/exception.rad@@ -1,5 +1,5 @@-(should-be "catch handles scope correctly"+(:test "catch handles scope correctly";; Issue #191, catch used to accidentally use the lexical environment of;; the `throw` that generated the exception.- ((fn [body handler] (catch 'any (body) handler)) (fn [] (throw 'foo "foo")) (fn [e stack] #t))- #t)+ [ ((fn [body handler] (catch 'any (body) handler)) (fn [] (throw 'foo "foo")) (fn [e stack] #t)) ==> #t ]+)diff --git a/rad/prelude/set.rad b/rad/prelude/set.radnew file mode 100644index 0000000..28fa39b--- /dev/null+++ b/rad/prelude/set.rad@@ -0,0 +1,26 @@+(def set/empty+ "An empty set."+ {})++(def set/insert+ "Insert a value into a set."+ (fn [x s]+ (insert x #t s)))++(def set/delete+ "Delete a value from a set."+ (fn [x s]+ (delete x s)))++(def set/member?+ "Query if an value is an element of a set."+ (fn [x s]+ (member? x s)))++(def set/to-vec+ "Convert a set to a vector."+ (fn [s] (keys s)))++(def set/from-seq+ "Create a set from a sequence."+ (fn [xs] (foldr set/insert {} xs)))diff --git a/rad/prelude/test.rad b/rad/prelude/test.radindex 4b9bc30..07d9ce4 100644--- a/rad/prelude/test.rad+++ b/rad/prelude/test.rad@@ -34,7 +34,8 @@(def maybe-setup (nth 0 test-contents));; If first block starts with :setup, run tests in that env(if (eq? (nth 0 maybe-setup) :setup)- (foldl (fn [ix x] (check-single x ix (nth 1 (eval (nth 1 maybe-setup) env)))) 1 (tail test-contents))+ (do (def setup-env (nth 1 (eval (nth 1 maybe-setup) env)))+ (foldl (fn [ix x] (check-single x ix setup-env)) 1 (tail test-contents)))(foldl (fn [ix x] (check-single x ix env)) 1 test-contents))(list :ok env))(list :ok env))@@ -58,3 +59,14 @@[(read-ref y) ==> 1][received ==> (0)])++(:test "'test' handles exceptions properly"+ [:setup+ (do+ (def x (catch 'any (throw 'blah) (fn [x] #t)))+ (def y (catch 'any (throw 'blah) (fn [x] #f)))+ )+ ]+ [x ==> #t]+ [y ==> #f]+)diff --git a/rad/prelude/util.rad b/rad/prelude/util.radnew file mode 100644index 0000000..2d3e84a--- /dev/null+++ b/rad/prelude/util.rad@@ -0,0 +1,27 @@+(def make-counter+ "Creates a stateful counter. Returns a dict with two keys: the function at `:next-will-be`+ will return the next number (without incrementing it), while the function at `:next`+ increments the number and returns it."+ (fn []+ (def i (ref -1))+ (fn [command]+ (cond+ (eq? command :current) (+ 1 (read-ref i))+ (eq? command :next) (modify-ref i (fn [x] (+ x 1)))+ :else (throw 'invalid-counter-command "Counters only accept `:current` and `:next` commands.")))))++(:test "make-counter"+ [:setup+ (do (def c1 (make-counter))+ (def c2 (make-counter))+ (def x0 (c1 :current))+ (def x1 (c1 :next))+ (def x2 (c2 :current))+ (def x3 (c2 :next))+ (def x4 (c1 :next))+ (def x5 (c1 :next))+ (def x6 (c1 :next))+ (def x7 (c2 :current)))+ ]+ [ [x0 x1 x2 x3 x4 x5 x6 x7] ==> [0 0 0 0 1 2 3 1] ]+)diff --git a/rad/prelude/validation.rad b/rad/prelude/validation.radindex 8d9e61c..75468e6 100644--- a/rad/prelude/validation.rad+++ b/rad/prelude/validation.rad@@ -79,3 +79,45 @@(fn [v](fn [xs](map v xs))))++(def validator/uuid+ "Validates UUIDs."+ (validator/and+ [(validator/type :string)+ (validator/pred "valid UUID" uuid?)]))++(def validator/signed+ "Checks that a value is a dict with `:signature` and `:author` keys, and that+ the signature is valid for the rest of the dict for that author. The rest of+ the dict is turned into a string according to `show`."+ (validator/and+ [(validator/keys+ {:author (validator/pred "valid public key" public-key?)})+ (validator/pred+ "Valid signature"+ (fn [d]+ (verify-signature+ (lookup :author d)+ (lookup :signature d)+ (show (delete-many [:author :signature] d)))))]))++(:test "validator/signed"+ [:setup+ (do (def keys (gen-key-pair! (default-ecc-curve)))+ (def sk (lookup :private-key keys))+ (def pk (lookup :public-key keys))+ (def payload+ {:nonce (uuid!)+ :some "data"})+ (def payload_ (insert :some "datadata" payload))+ (def sig (gen-signature! sk (show payload)))+ (def seal {:author (lookup :public-key keys)+ :signature sig})+ (def full (<> payload seal))+ (def full_ (<> payload_ seal))+ (def ok+ (catch 'any (do (validator/signed full) :ok) (fn [x] :not-ok)))+ )+ ]+ [ ok ==> :ok ]+)diff --git a/src/Radicle/Internal/Effects.hs b/src/Radicle/Internal/Effects.hsindex 8258244..57933c6 100644--- a/src/Radicle/Internal/Effects.hs+++ b/src/Radicle/Internal/Effects.hs@@ -8,6 +8,7 @@ import qualified Data.Map as Mapimport qualified Data.Text as Timport Data.Text.Prettyprint.Doc (pretty)import Data.Text.Prettyprint.Doc.Render.Terminal (putDoc)+import qualified Data.Time as Timeimport GHC.Exts (IsList(..))import Radicle.Internal.Core@@ -19,10 +20,10 @@ import Radicle.Internal.Prettyimport Radicle.Internal.PrimFnsimport qualified Radicle.Internal.UUID as UUID-type ReplM m =( Monad m, Stdout (Lang m), Stdin (Lang m), MonadRandom m, UUID.MonadUUID m+ , CurrentTime m, ReadFile (Lang m), GetEnv (Lang m) Value, SetEnv (Lang m) Value )@@ -198,4 +199,11 @@ replPrimFns = fromList $ allDocs $[] -> throwErrorHere Exitxs -> throwErrorHere $ WrongNumberOfArgs "exit!" 0 (length xs))+ , ( "now!"+ , "Returns a timestamp for the current Coordinated Universal Time (UTC), right now, formatted according to ISO 8601."+ , \case+ [] -> do t <- currentTime+ pure . String . toS . Time.formatTime Time.defaultTimeLocale (Time.iso8601DateFormat (Just "%H:%M:%SZ")) $ t+ xs -> throwErrorHere $ WrongNumberOfArgs "exit!" 0 (length xs)+ )]diff --git a/src/Radicle/Internal/Effects/Capabilities.hs b/src/Radicle/Internal/Effects/Capabilities.hsindex 264b915..4f2b12e 100644--- a/src/Radicle/Internal/Effects/Capabilities.hs+++ b/src/Radicle/Internal/Effects/Capabilities.hs@@ -9,6 +9,8 @@ import Protoludeimport qualified Data.Map as Mapimport Data.Text.Prettyprint.Doc (PageWidth)+import Data.Time+import System.Console.Haskeline hiding (catch)import System.IO (isEOF)#ifdef ghcjs_HOST_OSimport GHCJS.DOM.XMLHttpRequest@@ -55,6 +57,14 @@ class (Monad m) => Exit m whereinstance Exit m => Exit (Lang m) whereexitS = lift exitS+class Monad m => CurrentTime m where+ currentTime :: m UTCTime++instance CurrentTime (InputT IO) where+ currentTime = liftIO getCurrentTime+instance CurrentTime m => CurrentTime (Lang m) where+ currentTime = lift currentTime+class (Monad m) => GetEnv m r | m -> r wheregetEnvS :: m (Env r)instance Monad m => GetEnv (Lang m) Value wherediff --git a/src/Radicle/Internal/PrimFns.hs b/src/Radicle/Internal/PrimFns.hsindex 0cf9ef0..aa8d378 100644--- a/src/Radicle/Internal/PrimFns.hs+++ b/src/Radicle/Internal/PrimFns.hs@@ -8,7 +8,7 @@ import qualified Data.Map as Mapimport Data.Sequence (Seq(..))import qualified Data.Sequence as Seqimport qualified Data.Text as T-import GHC.Exts (IsList(..))+import GHC.Exts (IsList(..), sortWith)import Text.Megaparsec (parseErrorPretty)import qualified Radicle.Internal.Annotation as Ann@@ -130,12 +130,6 @@ purePrimFns = fromList $ allDocs $xs -> throwErrorHere $ WrongNumberOfArgs "eq?" 2 (length xs))-- Vectors- , ( "<>"- , "Concatenates two vectors."- , twoArg "<>" $ \case- (Vec xs, Vec ys) -> pure $ Vec (xs Seq.>< ys)- _ -> throwErrorHere $ TypeError "<>: both arguments must be vectors"- ), ( "add-left", "Adds an element to the left side of a vector.", twoArg "add-left" $ \case@@ -216,18 +210,29 @@ purePrimFns = fromList $ allDocs $[_,_] -> throwErrorHere $ TypeError "nth: expects a integer and a list or vector"xs -> throwErrorHere $ WrongNumberOfArgs "nth" 2 (length xs))+ , ( "sort-by"+ , "Given a sequence `xs` and a function `f`, returns a sequence with the same\+ \ elements `x` of `xs` but sorted according to `(f x)`."+ , twoArg "sort-by" $ \case+ (f, List xs) -> do ys <- traverse (\x -> (x,) <$> callFn f [x]) xs+ let ys' = sortWith snd ys+ pure (List (fst <$> ys'))+ (f, Vec xs) -> do ys <- traverse (\x -> (x,) <$> callFn f [x]) xs+ let ys' = Seq.sortOn snd ys+ pure (Vec (fst <$> ys'))+ _ -> throwErrorHere $ TypeError "sort-by: second argument must be a sequence"+ )+ -- Dicts, ("lookup", "Given a value `k` (the 'key') and a dict `d`, returns the value associated\\ with `k` in `d`. If the key does not exist in `d` then `()` is returned\\ instead. If `d` is not a dict then an exception is thrown.", \case- [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- [_, _] -> throwErrorHere $ TypeError $ "lookup: second argument must be a dict"+ [a, Dict m] -> case Map.lookup a m of+ Just v -> pure v+ Nothing -> throwErrorHere $ OtherError $ "lookup: key did not exist: " <> renderCompactPretty a+ [_, d] -> throwErrorHere $ TypeError $ "lookup: second argument must be a dict: " <> renderCompactPretty dxs -> throwErrorHere $ WrongNumberOfArgs "lookup" 2 (length xs)), ( "map-values", "Given a function `f` and a dict `d`, returns a dict with the same keys as `d`\@@ -239,6 +244,17 @@ purePrimFns = fromList $ allDocs $pure . Dict . Map.fromList $ zip (fst <$> kvs) vs_ -> throwErrorHere $ TypeError $ "map-values: second argument must be a dict")++ -- Structures+ , ( "<>"+ , "Merges two structures together. On vectors this performs concatenations. On dicts this performs the right-biased merge."+ , twoArg "<>" $ \case+ (List xs, List ys) -> pure $ List (xs ++ ys)+ (Vec xs, Vec ys) -> pure $ Vec (xs Seq.>< ys)+ (Dict m, Dict n) -> pure $ Dict (n <> m)+ _ -> throwErrorHere $ TypeError "<>: only works on vectors of dicts"+ )+, ( "string-length", "Returns the length of a string.", oneArg "string-length" $ \casediff --git a/test/spec/Radicle/Internal/TestCapabilities.hs b/test/spec/Radicle/Internal/TestCapabilities.hsindex ce9494e..6c80b8d 100644--- a/test/spec/Radicle/Internal/TestCapabilities.hs+++ b/test/spec/Radicle/Internal/TestCapabilities.hs@@ -7,6 +7,7 @@ import Protolude hiding (TypeError)import qualified Crypto.Random as CryptoRandimport qualified Data.Map.Strict as Mapimport qualified Data.Sequence as Seq+import qualified Data.Time as Timeimport GHC.Exts (fromList)import qualified System.FilePath.Find as FP@@ -29,6 +30,7 @@ data WorldState = WorldState, worldStateDRG :: CryptoRand.ChaChaDRG, worldStateUUID :: Int, worldStateRemoteChains :: Map Text (Seq.Seq Value)+ , worldStateCurrentTime :: Time.UTCTime}@@ -50,6 +52,7 @@ runTestWithFiles bindings inputs files action =, worldStateDRG = CryptoRand.drgNewSeed (CryptoRand.seedFromInteger 4) -- chosen by fair dice roll, worldStateUUID = 0, worldStateRemoteChains = mempty+ , worldStateCurrentTime = Time.UTCTime (Time.ModifiedJulianDay 0) 0}in case runState (fmap fst $ runLang bindings $ interpretMany "[test]" action) ws of(val, st) -> (val, worldStateStdout st)@@ -163,3 +166,6 @@ instance UUID.MonadUUID (State WorldState) wherewhereprefix = "00000000-0000-0000-0000-"pad i = toS $ prefix <> replicate (12 - length i) '0' <> i++instance CurrentTime (State WorldState) where+ currentTime = gets worldStateCurrentTimediff --git a/test/spec/Radicle/Tests.hs b/test/spec/Radicle/Tests.hsindex eeff75e..c25c482 100644--- a/test/spec/Radicle/Tests.hs+++ b/test/spec/Radicle/Tests.hs@@ -771,8 +771,9 @@ test_cbor =-- Note that loaded files will also be tested, so there's no need to test files-- loaded by the prelude individually.test_source_files :: IO TestTree-test_source_files = testGroup "Radicle source file tests" <$>- testOne "rad/prelude.rad"+test_source_files = do+ tests <- join <$> traverse testOne ["rad/prelude.rad", "rad/monadic/issues.rad"]+ pure $ testGroup "Radicle source file tests" testswheretestOne :: FilePath -> IO [TestTree]testOne file = do@@ -781,7 +782,10 @@ test_source_files = testGroup "Radicle source file tests" <$>contents <- readFile (dir <> f)pure (toS f, contents)contents <- readFile (dir <> file)- let (r, out) = runTestWithFiles testBindings [] (fromList allFiles) contents+ let keyPair :: Text = case runTest testBindings "(gen-key-pair! (default-ecc-curve))" of+ Right kp -> renderCompactPretty kp+ Left _ -> panic "Couldn't generate keypair file."+ let (r, out) = runTestWithFiles testBindings [] (Map.insert "my-keys.rad" keyPair (fromList allFiles)) contentslet makeTest line =let name = T.reverse $ T.drop 1 $ T.dropWhile (/= '\'')$ T.reverse $ T.drop 1 $ T.dropWhile (/= '\'') line