A peer-to-peer stack for code collaboration
Clojure like syntax for defines and lambdas (#123)
James Haydona494f2af31cf
diff --git a/rad/chain.rad b/rad/chain.radindex ef4f77c..7a1f1b5 100644--- a/rad/chain.rad+++ b/rad/chain.rad@@ -5,14 +5,14 @@;; == base-eval');; - A sequence of inputs.-(define empty-chain+(def empty-chain(dict :state (pure-env):inputs (list):index 0))-(define eval-in-chain- (lambda (expr chain)- (define x (eval-with-env expr (lookup :state chain)))+(def eval-in-chain+ (fn [expr chain]+ (def x (eval-with-env expr (lookup :state chain)))(dict :chain (dict :state (head (tail x)):input (cons expr (lookup :logs chain)):index (+ 1 (lookup :index chain)))@@ -22,13 +22,13 @@'(("expr" any) ("chain" chain))"Evaluates 'expr' in the 'chain' and returns a dict with the ':result' and the resulting ':chain'.")-(define update-chain-with- (lambda (chain remote-chain-id cb-expr cb-res)- (define new-inputs+(def update-chain-with+ (fn [chain remote-chain-id cb-expr cb-res]+ (def new-inputs(receive! remote-chain-id (lookup :index chain)))- (define upd-ch- (lambda (ch expr)- (define x (eval-in-chain expr ch))+ (def upd-ch+ (fn [ch expr]+ (def x (eval-in-chain expr ch))(cb-expr expr)(cb-res (view (@ :result) x))(view (@ :chain) x)))@@ -38,16 +38,16 @@'(("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.")-(define enter-chain- (lambda (eval-definer chain)- (define old-eval eval)- (define ch (ref chain))- (define ev- (lambda (e)+(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 (define x (eval-in-chain e (read-ref ch)))+ (do (def x (eval-in-chain e (read-ref ch)))(write-ref ch (view (@ :chain) x))(view (@ :result) x)))))(eval-definer ev)))@@ -56,25 +56,25 @@'(("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.")-(define enter-remote-chain- (lambda (eval-definer chain-id)- (define old-eval eval)- (define chain (update-chain-with empty-chain chain-id (lambda (e) '()) (lambda (e) '())))+(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))))- (define ch (ref chain))- (define inputs (ref (list)))+ (def ch (ref chain))+ (def inputs (ref (list)))(eval-definer- (lambda (e)+ (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 (lambda (i) (send! chain-id i)) (reverse (read-ref inputs)))+ (map (fn [i] (send! chain-id i)) (reverse (read-ref inputs)))(print! "Done! Leaving remote-chain.")(eval-definer old-eval))- :else (do (define x (eval-in-chain e (read-ref ch)))+ :else (do (def x (eval-in-chain e (read-ref ch)))(write-ref ch (view (@ :chain) x))- (modify-ref inputs (lambda (is) (cons e is)))+ (modify-ref inputs (fn [is] (cons e is)))(view (@ :result) x)))))))(document 'enter-remote-chain@@ -82,8 +82,8 @@"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-(define boom+(def boom(quote- (do (define eval-ref (ref eval))- (define eval-definer (lambda (f) (write-ref eval-ref f)))- (define eval (lambda (e) ((read-ref eval-ref) e))))))+ (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))))))diff --git a/rad/crypto.rad b/rad/crypto.radindex 025cbed..27a8728 100644--- a/rad/crypto.rad+++ b/rad/crypto.rad@@ -1,7 +1,7 @@-(define sign-from-file!- (lambda (keys-file msg)- (define ks (read (read-file! keys-file)))- (define sig (gen-signature! (lookup :private-key ks) msg))+(def sign-from-file!+ (fn [keys-file msg]+ (def ks (read (read-file! keys-file)))+ (def sig (gen-signature! (lookup :private-key ks) msg)){:message msg:signature sig:key (lookup :public-key ks)}))@@ -11,8 +11,8 @@("msg" string))"Create a signed message using a key-pair stored in a file. The file should contain output as generated by 'gen-key-pair!'.")-(define verify-signed-message- (lambda (sig-msg)+(def verify-signed-message+ (fn [sig-msg](verify-signature (lookup :key sig-msg)(lookup :signature sig-msg)(lookup :message sig-msg))))diff --git a/rad/currency.rad b/rad/currency.radindex d682a4c..c89e54f 100644--- a/rad/currency.rad+++ b/rad/currency.rad@@ -1,31 +1,31 @@;; Account stuff. Should be moved elsewhere when we have an import mechanism-(define create-currency (lambda ()+(def create-currency (fn [];; The map of all accounts- (define accounts (ref (dict)))+ (def accounts (ref (dict)));; Create an account with 10 coins.- (define new-account (lambda (name)- (modify-ref accounts (lambda (acc)+ (def new-account (fn [name]+ (modify-ref accounts (fn [acc](insert name 10 acc)))));; Get an account's balance- (define account-balance (lambda (name)+ (def account-balance (fn [name](lookup name (read-ref accounts))));; Transfer money- (define transfer (lambda (from to amount)- (define acc (read-ref accounts))+ (def transfer (fn [from to amount]+ (def acc (read-ref accounts))(write-ref accounts(if (> amount (lookup from acc))(print! "not enough balance")(modify-mapto- (lambda (x) (+ x amount))- (modify-map from (lambda (x) (- x amount)) acc))))))+ (fn [x] (+ x amount))+ (modify-map from (fn [x] (- x amount)) acc))))))- (define dispatch (lambda (m)+ (def dispatch (fn [m](cond(eq? m 'new-account) new-account(eq? m 'account-balance) account-balance@@ -33,10 +33,11 @@:else (print! "error: currency- unknown message"))))dispatch))-(define currency (create-currency))+(def currency (create-currency))-(define new-eval (lambda (expr)++(def new-eval (fn [expr](cond(eq? (head expr) 'new-account) ((currency 'new-account) (nth 1 expr))(eq? (head expr) 'account-balance) ((currency 'account-balance) (nth 2 expr))diff --git a/rad/issues.rad b/rad/issues.radindex d7b93ed..4cc98f5 100644--- a/rad/issues.rad+++ b/rad/issues.rad@@ -1,22 +1,22 @@;; Issues for rad prelude-(define create-rad-issue-chain (lambda ()- (define issues (ref nil))- (define should-be (lambda (name x y) (eq? x y)))- (define current-prelude (ref (dict :env (dict 'should-be should-be 'eval eval) :refs nil)))+(def create-rad-issue-chain (fn []+ (def issues (ref nil))+ (def should-be (fn [name x y] (eq? x y)))+ (def current-prelude (ref (dict :env (dict 'should-be should-be 'eval eval) :refs nil)))- (define failing? (lambda (expr)- (define res (catch 'any+ (def failing? (fn [expr]+ (def res (catch 'any(head (eval-with-env expr (read-ref current-prelude)))- (lambda (x) #f)))+ (fn [x] #f)))(eq? res #f)))- (define new-issue (lambda (creator title desc failing-test)+ (def new-issue (fn [creator title desc failing-test](if (failing? failing-test)(modify-refissues- (lambda (x)+ (fn [x](cons (dict:creator creator:title title@@ -24,9 +24,9 @@:failing-test failing-test) x)))"Test is not failing")))- (define list-issues (lambda () (read-ref issues)))+ (def list-issues (fn [] (read-ref issues)))- (define dispatch (lambda (m)+ (def dispatch (fn [m](cond(eq? m 'new-issue) new-issue(eq? m 'list-issues) list-issues@@ -34,9 +34,9 @@dispatch))-(define rad-issue-chain (create-rad-issue-chain))+(def rad-issue-chain (create-rad-issue-chain))-(define rad-issue-chain-eval (lambda (expr)+(def rad-issue-chain-eval (fn [expr](if (eq? (head expr) 'new-issue)(apply (rad-issue-chain 'new-issue) (tail expr))(if (eq? (head expr) 'list-issues)diff --git a/rad/prelude.rad b/rad/prelude.radindex 5ea5b2e..082daa8 100644--- a/rad/prelude.rad+++ b/rad/prelude.rad@@ -10,7 +10,7 @@(load! "rad/chain.rad")(load! "rad/state-machine.rad")-(define pure-prelude-files+(def pure-prelude-files(list"rad/prelude/doc.rad""rad/prelude/pure-should-be.rad"@@ -27,22 +27,22 @@'()"List of files which together define the pure prelude.")-(define pure-prelude-code+(def pure-prelude-code(read(string-append"(do "(apply string-append- (map (lambda (f) (read-file! f)) pure-prelude-files))+ (map (fn [f] (read-file! f)) pure-prelude-files))")")))(document 'pure-prelude-code'()"The pure prelude.")-(define send-prelude!- (lambda (chain-id)+(def send-prelude!+ (fn [chain-id](send! chain-id pure-prelude-code)))(document 'send-prelude!'(("chain-id" string))-"Send the pure prelude to a chain.")+"Send the pure prelude to a chain.")\ No newline at end of filediff --git a/rad/prelude/bool.rad b/rad/prelude/bool.radindex 4933a2c..68868da 100644--- a/rad/prelude/bool.rad+++ b/rad/prelude/bool.rad@@ -2,7 +2,7 @@;; not-(define not (lambda (x) (if x #f #t)))+(def not (fn [x] (if x #f #t)))(document 'not'(("arg" any))@@ -10,8 +10,8 @@;; and-(define and- (lambda (x y)+(def and+ (fn [x y](if x y x)))(document 'and@@ -25,8 +25,8 @@;; or-(define or- (lambda (x y)+(def or+ (fn [x y](if x x y)))(document 'or@@ -40,8 +40,8 @@;; all-(define all- (lambda (xs) (foldr and #t xs)))+(def all+ (fn [xs] (foldr and #t xs)))(should-be "all" (all '()) #t)(should-be "all" (all '(#t)) #t)@@ -51,8 +51,8 @@;; some-(define some- (lambda (xs) (foldr or #f xs)))+(def some+ (fn [xs] (foldr or #f xs)))(should-be "some" (some '()) #f)(should-be "some" (some '(#t)) #t)diff --git a/rad/prelude/dict.rad b/rad/prelude/dict.radindex 7483544..d5173e2 100644--- a/rad/prelude/dict.rad+++ b/rad/prelude/dict.rad@@ -2,9 +2,9 @@;; dict-from-list-(define dict-from-list- (lambda (xs)- (foldr (lambda (kv acc) (insert (head kv) (head (tail kv)) acc)) (dict) xs)))+(def dict-from-list+ (fn [xs]+ (foldr (fn [kv acc] (insert (head kv) (head (tail kv)) acc)) (dict) xs)))(document 'dict-from-list'(("list" list))@@ -16,8 +16,8 @@;; keys-(define keys- (lambda (d)+(def keys+ (fn [d](map head (seq d))))(document 'keys@@ -31,9 +31,9 @@;; modify-map-(define modify-map (lambda (key fn mp)- (insert key (fn (lookup key mp)) mp)))+(def modify-map (fn [key f mp]+ (insert key (f (lookup key mp)) mp)))(should-be "modify-map"- (modify-map 'a (lambda (x) (+ x 1)) (dict 'a 5))+ (modify-map 'a (fn [x] (+ x 1)) (dict 'a 5))(dict 'a 6))diff --git a/rad/prelude/doc.rad b/rad/prelude/doc.radindex 3327f67..6bb114c 100644--- a/rad/prelude/doc.rad+++ b/rad/prelude/doc.rad@@ -1,8 +1,8 @@;; doc-related functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;-(define document- (lambda (name args description)- (define old (read-ref _doc-ref))+(def document+ (fn [name args description]+ (def old (read-ref _doc-ref))(write-ref_doc-ref(insert name (dict :args args :desc description) old))))@@ -13,9 +13,9 @@;; doc-(define doc- (lambda (name)- (define the-doc (lookup name (read-ref _doc-ref)))+(def doc+ (fn [name]+ (def the-doc (lookup name (read-ref _doc-ref)))(string-append(lookup :desc the-doc)"\nArguments:\n\t"diff --git a/rad/prelude/io.rad b/rad/prelude/io.radindex 0b25ece..9491fc5 100644--- a/rad/prelude/io.rad+++ b/rad/prelude/io.rad@@ -1,17 +1,17 @@;; read-line-(define read-line!- (lambda () (read (get-line!))))+(def read-line!+ (fn [] (read (get-line!))))-(define read-code!- (lambda (filename)+(def read-code!+ (fn [filename](read (string-append "(do " (read-file! filename) ")"))))(document 'read-code!'("filename" string)"Read code (as data) from a file.")-(define send-code!- (lambda (chain-id filename)+(def send-code!+ (fn [chain-id filename](send! chain-id (read-code! filename))))(document 'send-code!diff --git a/rad/prelude/lens.rad b/rad/prelude/lens.radindex 6918d49..7d0d2e8 100644--- a/rad/prelude/lens.rad+++ b/rad/prelude/lens.rad@@ -2,8 +2,8 @@;; make-lens-(define make-lens- (lambda (g s)+(def make-lens+ (fn [g s](dict 'getter g 'setter s)))(document 'make-lens@@ -12,8 +12,8 @@;; view-(define view- (lambda (lens target)+(def view+ (fn [lens target]((lookup 'getter lens) target)))(document 'view@@ -22,8 +22,8 @@;; set-(define set- (lambda (lens new-view target)+(def set+ (fn [lens new-view target]((lookup 'setter lens) new-view target)))(document 'set@@ -32,10 +32,10 @@;; id-lens-(define id-lens+(def id-lens(make-lens- (lambda (target) target)- (lambda (new-view target) new-view)))+ (fn [target] target)+ (fn [new-view target] new-view)))(document 'id-lens'()@@ -43,11 +43,11 @@;; ..-(define ..- (lambda (lens1 lens2)+(def ..+ (fn [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)))))+ (fn [target] (view lens2 (view lens1 target)))+ (fn [new-view target] (set lens1 (set lens2 new-view (view lens1 target)) target)))))(document '..'(("lens1" dict) ("lens2" dict))@@ -55,15 +55,15 @@;; ...-(define ... (lambda (lenses) (foldr .. id-lens lenses)))+(def ... (fn [lenses] (foldr .. id-lens lenses)))(document '...'(("lenses" list))"Compose multiple lenses.");; over-(define over- (lambda (lens f target)+(def over+ (fn [lens f target](set lens (f (view lens target)) target)))(document 'over@@ -72,10 +72,10 @@;; @-(define @- (lambda (k)- (make-lens (lambda (mp) (lookup k mp))- (lambda (v mp) (insert k v mp)))))+(def @+ (fn [k]+ (make-lens (fn [mp] (lookup k mp))+ (fn [v mp] (insert k v mp)))))(document '@'(("key" any))@@ -90,28 +90,28 @@(dict :one 1 :two 42))(should-be "over lens"- (over (@ :one) (lambda (x) (+ x 1)) (dict :one 1 :two 2))+ (over (@ :one) (fn [x] (+ x 1)) (dict :one 1 :two 2))(dict :one 2 :two 2))(should-be "lens composition"(over (.. (@ :a) (@ :b))- (lambda (x) (+ x 1))+ (fn [x] (+ x 1))(dict :a (dict :b 2)))(dict :a (dict :b 3)))-(do (define d- (lambda (x) (dict :a (dict :b (dict :c x)))))+(do (def d+ (fn [x] (dict :a (dict :b (dict :c x)))))(should-be "deep lenses"(over (... (list (@ :a) (@ :b) (@ :c)))- (lambda (x) (+ x 1))+ (fn [x] (+ x 1))(d 0))(d 1)));; Lens access to refs;; view-ref-(define view-ref- (lambda (r lens)+(def view-ref+ (fn [r lens](view lens (read-ref r))))(document 'view-ref@@ -124,35 +124,35 @@;; set-ref-(define set-ref- (lambda (r lens v)- (modify-ref r (lambda (x) (set lens v x)))))+(def set-ref+ (fn [r lens v]+ (modify-ref r (fn [x] (set lens v x)))))(document 'set-ref'(("ref" ref) ("lens" dict) ("new-value" any))"Like 'set', but for refs.")(should-be "set-ref"- ((lambda (r)- (define r (ref (dict :a 1)))+ ((fn [r]+ (def r (ref (dict :a 1)))(set-ref r (@ :a) 2)(view-ref r (@ :a)))(ref (dict :a 1)))2);; set-ref-(define over-ref- (lambda (r lens f)- (modify-ref r (lambda (x) (over lens f x)))))+(def over-ref+ (fn [r lens f]+ (modify-ref r (fn [x] (over lens f x)))))(document 'over-ref'(("ref" ref) ("lens" dict) ("fn" function))"Like 'over', but for refs.")(should-be "over-ref"- ((lambda (r)- (define r (ref (dict :a 2)))- (over-ref r (@ :a) (lambda (x) (* x 2)))+ ((fn [r]+ (def r (ref (dict :a 2)))+ (over-ref r (@ :a) (fn [x] (* x 2)))(view-ref r (@ :a)))(ref (dict :a 2)))4)diff --git a/rad/prelude/list.rad b/rad/prelude/list.radindex e40d18a..b6e2f2f 100644--- a/rad/prelude/list.rad+++ b/rad/prelude/list.rad@@ -2,11 +2,11 @@;; nil-(define nil (list))+(def nil (list));; empty?-(define empty? (lambda (ls) (eq? ls nil)))+(def empty? (fn [ls] (eq? ls nil)))(document 'empty?'(("list" list))@@ -17,9 +17,9 @@;; length-(define length- (lambda (xs)- (foldr (lambda (x acc) (+ acc 1)) 0 xs)))+(def length+ (fn [xs]+ (foldr (fn [x acc] (+ acc 1)) 0 xs)))(document 'length'(("list" list))@@ -30,9 +30,9 @@;; reverse-(define reverse (lambda (ls)- (define go (Y2 (lambda (go)- (lambda (acc new)+(def reverse (fn [ls]+ (def go (Y2 (fn [go]+ (fn [acc new](if (empty? new)acc(go (cons (head new) acc) (tail new)))))))@@ -46,7 +46,7 @@;; range-(define range (Y2 (lambda (range) (lambda (from to)+(def range (Y2 (fn [range] (fn [from to](if (eq? from to)(list to)(cons from (range (+ 1 from) to)))))))@@ -59,8 +59,8 @@;; concat-(define concat (lambda (list1 list2)- (foldr (lambda (a b) (cons a b)) list2 list1)))+(def concat (fn [list1 list2]+ (foldr (fn [a b] (cons a b)) list2 list1)))(document 'concat'(("list1" list) ("list2" list))@@ -71,7 +71,7 @@;; filter-(define filter (Y2 (lambda (filter) (lambda (pred ls)+(def filter (Y2 (fn [filter] (fn [pred ls](if (empty? ls)ls(if (pred (head ls))@@ -83,5 +83,5 @@"Returns 'list' with only the elements that satisfy 'filter-cond'.")(should-be "filter"- (filter (lambda (x) (< x 10)) (list 3 10 11))+ (filter (fn [x] (< x 10)) (list 3 10 11))(list 3))diff --git a/rad/prelude/pure-should-be.rad b/rad/prelude/pure-should-be.radindex eee1454..f5ef529 100644--- a/rad/prelude/pure-should-be.rad+++ b/rad/prelude/pure-should-be.rad@@ -1,7 +1,7 @@;; A version of should-be for pure environments.-(define should-be- (lambda (name x y)+(def should-be+ (fn [name x y](if (eq? x y)(string-append "Test '" name "' succeeded")(string-append "Test '" name "' failed"))))diff --git a/rad/prelude/recursion.rad b/rad/prelude/recursion.radindex 042250c..d1af30e 100644--- a/rad/prelude/recursion.rad+++ b/rad/prelude/recursion.rad@@ -1,16 +1,16 @@;; y-combinators ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;-(define Y- (lambda (h)- ((lambda (x) (x x))- (lambda (g)- (h (lambda (arg) ((g g) arg)))))))-(define Y2- (lambda (h)- ((lambda (x) (x x))- (lambda (g)- (h (lambda (arg1 arg2) ((g g) arg1 arg2)))))))+(def Y+ (fn [h]+ ((fn [x] (x x))+ (fn [g]+ (h (fn [arg] ((g g) arg)))))))+(def Y2+ (fn [h]+ ((fn [x] (x x))+ (fn [g]+ (h (fn [arg1 arg2] ((g g) arg1 arg2)))))))-((lambda ()- (define fac (Y (lambda (f) (lambda (x) (if (< x 2) 1 (* x (f (- x 1))))))))+((fn []+ (def fac (Y (fn [f] (fn [x] (if (< x 2) 1 (* x (f (- x 1))))))))(should-be "fac" (fac 10) 3628800)))diff --git a/rad/prelude/ref.rad b/rad/prelude/ref.radindex a8ffd9f..b145b7c 100644--- a/rad/prelude/ref.rad+++ b/rad/prelude/ref.rad@@ -1,9 +1,9 @@;; ref functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; modify-ref-(define modify-ref- (lambda (r fn)- (write-ref r (fn (read-ref r)))))+(def modify-ref+ (fn [r f]+ (write-ref r (f (read-ref r)))))(document 'modify-ref'(("ref" ref) ("fn" function))diff --git a/rad/prelude/should-be.rad b/rad/prelude/should-be.radindex e41d4a8..921bf2e 100644--- a/rad/prelude/should-be.rad+++ b/rad/prelude/should-be.rad@@ -1,7 +1,7 @@;; should-be - testing functions-(define should-be- (lambda (name x y)+(def should-be+ (fn [name x y](if (eq? x y)(print! (string-append "Test '" name "' succeeded"))(print! (string-append "Test '" name "' failed")))))diff --git a/rad/repl.rad b/rad/repl.radindex 40974b9..14ff9f2 100644--- a/rad/repl.rad+++ b/rad/repl.rad@@ -3,16 +3,16 @@;; The Repl-(define _initial_prompt-text+(def _initial_prompt-text"Welcome to radicle. Type (help) for help.")-(define help (lambda ()+(def help (fn []"radicle is a LISP-like language intendended for programming chains, and interacting with those chains.Type (doc '<name>) for further documentation of <name>."))-(define repl+(def repl(dict'name "repl";; Parse errors are thrown herediff --git a/rad/self-amending-store.rad b/rad/self-amending-store.radindex 97fde4c..7c9e470 100644--- a/rad/self-amending-store.rad+++ b/rad/self-amending-store.rad@@ -1,14 +1,14 @@-(define sas-password "very secret")-(define sas-store (ref (dict)))-(define sas-eval-ref (ref eval))+(def sas-password "very secret")+(def sas-store (ref (dict)))+(def sas-eval-ref (ref eval));; self-amending key-val store-(define sas-starting-eval- (lambda (expr)- (define command (head expr))+(def sas-starting-eval+ (fn [expr]+ (def command (head expr))(cond(eq? command 'get) (lookup (nth 1 expr) (read-ref sas-store))- (eq? command 'set) (modify-ref sas-store (lambda (s) (insert (nth 1 expr) (nth 2 expr) s)))+ (eq? command 'set) (modify-ref sas-store (fn [s] (insert (nth 1 expr) (nth 2 expr) s)))(and (eq? command 'update)(eq? (nth 1 expr)sas-password))diff --git a/rad/state-machine.rad b/rad/state-machine.radindex b5b5e57..622281b 100644--- a/rad/state-machine.rad+++ b/rad/state-machine.rad@@ -1,57 +1,57 @@-(define head-shots+(def head-shots(dict "onur" "https://res.cloudinary.com/juliendonck/image/upload/v1536080565/avatars/1712926.png""ele" "https://res.cloudinary.com/juliendonck/image/upload/v1536080565/avatars/853825.png""alexis" "https://res.cloudinary.com/juliendonck/image/upload/v1536080565/avatars/40774.png""kim" "https://res.cloudinary.com/juliendonck/image/upload/v1536080565/avatars/6163.png""julien" "https://res.cloudinary.com/juliendonck/image/upload/v1536080565/avatars/2326909.jpg"))-(define get-head-shot- (lambda (x)- (define y (lookup x head-shots))+(def get-head-shot+ (fn [x]+ (def y (lookup x head-shots))(if (eq? y '())"https://res.cloudinary.com/juliendonck/image/upload/v1536080565/avatars/2326909.jpg"y)))-(define pr-trans- (lambda (state inp)- (define c (dict "key" (show (view (@ :next-id) state))+(def pr-trans+ (fn [state inp]+ (def c (dict "key" (show (view (@ :next-id) state))"username" (nth 0 inp)"comment" (nth 1 inp)"profile_pic_url" (get-head-shot (nth 0 inp))))(dict :state (over (@ :comments)- (lambda (cs) (cons c cs))+ (fn [cs] (cons c cs))(over (@ :next-id)- (lambda (x) (+ x 1))+ (fn [x] (+ x 1))state)):output c)))-(define kim-trans- (lambda (state i)+(def kim-trans+ (fn [state i](if (eq? (nth 0 i) "kim")(pr-trans state (list "kim" "LGTM"))(pr-trans state i))));; Handle an input in the morphing state-machine.-(define state-machine-input- (lambda (state i)- (define trans-fn (view-ref state (@ :transition-fn)))- (define current-state (view-ref state (@ :machine-state)))- (define next (trans-fn current-state i))+(def state-machine-input+ (fn [state i]+ (def trans-fn (view-ref state (@ :transition-fn)))+ (def current-state (view-ref state (@ :machine-state)))+ (def next (trans-fn current-state i))(set-ref state (@ :machine-state) (view (@ :state) next))(view (@ :output) next)));; Trigger a new vote-(define state-machine-new-trans- (lambda (state func)+(def state-machine-new-trans+ (fn [state func](if (eq? (view-ref state (@ :voting)) :nothing)(do (set-ref state (@ :voting) (dict :votes (dict):proposed-function func))(string-append "A new transition function has been proposed! Please either agree or disagree with:\n" (show func)))(throw 'invalid-input "Can't propose new transition function while vote is ongoing."))))-(define state-machine-agree- (lambda (state voters userid)+(def state-machine-agree+ (fn [state voters userid](if (member? userid voters)(do (set-ref state (... (list (@ :voting) (@ :votes) (@ userid))) #t)(if (eq? (length (seq (view-ref state (.. (@ :voting) (@ :votes))))) (length voters))@@ -61,8 +61,8 @@'()))(throw 'invalid-input "Not allowed to vote."))))-(define state-machine-disagree- (lambda (state voters userid)+(def state-machine-disagree+ (fn [state voters userid](if (member? userid voters)(do (set-ref state (@ :voting) :nothing)"Voting has ended; someone disagreed.")@@ -70,14 +70,14 @@;; Returns an eval which operates a state machine whose transition function may;; be updated. To update the transition function all voters must agree on it.-(define state-machine-eval- (lambda (voters init-state init-transition)- (define state (ref (dict :machine-state init-state+(def state-machine-eval+ (fn [voters init-state init-transition]+ (def state (ref (dict :machine-state init-state:transition-fn init-transition:voting :nothing)))- (lambda (expr)- (define verb (nth 0 expr))- (define input (nth 1 expr))+ (fn [expr]+ (def verb (nth 0 expr))+ (def input (nth 1 expr))(cond(eq? verb 'input) (state-machine-input state input)(eq? verb 'new-trans-func) (state-machine-new-trans state input)@@ -85,14 +85,14 @@(eq? verb 'disagree) (state-machine-disagree state voters input):else (throw 'invalid-input (string-append "The only valid commands are 'input', 'new-trans-func', 'agree' and 'disagree': " (show expr)))))))-(define simple-trans- (lambda (f)- (lambda (s i)- (define y (f s i))+(def simple-trans+ (fn [f]+ (fn [s i]+ (def y (f s i))(dict :state y:output y))))-(define pr-thread+(def pr-thread(state-machine-eval(list "alice" "bob")(dict :next-id 0 :comments (list))diff --git a/src/Radicle/Internal/Interpret.hs b/src/Radicle/Internal/Interpret.hsindex 5a209a5..d8bd7d2 100644--- a/src/Radicle/Internal/Interpret.hs+++ b/src/Radicle/Internal/Interpret.hs@@ -15,7 +15,7 @@ import Radicle.Internal.Parse---- >>> import Control.Monad.Identity-- >>> import Radicle.Internal.Primops--- >>> fmap Ann.untag . runIdentity $ interpret "test" "((lambda (x) x) #t)" pureEnv+-- >>> fmap Ann.untag . runIdentity $ interpret "test" "((fn [x] x) #t)" pureEnv-- Right (Annotated (Identity (BooleanF True)))---- >>> import Control.Monad.Identity@@ -42,7 +42,7 @@ interpret sourceName expr bnds = do-- Examples:---- >>> import Radicle.Internal.Primops--- >>> fmap (fmap Ann.untag . fst) <$> runLang pureEnv $ interpretMany "test" "(define id (lambda (x) x))\n(id #t)"+-- >>> fmap (fmap Ann.untag . fst) <$> runLang pureEnv $ interpretMany "test" "(def id (fn [x] x))\n(id #t)"-- Right (Annotated (Identity (BooleanF True)))interpretMany:: Monad mdiff --git a/src/Radicle/Internal/Pretty.hs b/src/Radicle/Internal/Pretty.hsindex 2927708..c1ba638 100644--- a/src/Radicle/Internal/Pretty.hs+++ b/src/Radicle/Internal/Pretty.hs@@ -45,10 +45,10 @@ instance (Copointed t, Ann.Annotation t) => Pretty (Ann.Annotated t ValueF) whersep [ pretty k <+> pretty val| (k, val) <- Map.toList mp ]Lambda ids vals _ -> parens $- "lambda" <+> align (sep- [ parens . sep $ pretty <$> ids- , sep $ pretty <$> toList vals- ])+ "fn" <+> align (sep+ [ brackets . sep $ pretty <$> ids+ , sep $ pretty <$> toList vals+ ])whereescapeStr = T.replace "\"" "\\\"" . T.replace "\\" "\\\\"diff --git a/src/Radicle/Internal/Primops.hs b/src/Radicle/Internal/Primops.hsindex 8dfa8a0..7560820 100644--- a/src/Radicle/Internal/Primops.hs+++ b/src/Radicle/Internal/Primops.hs@@ -6,7 +6,7 @@ module Radicle.Internal.Primops, readValue) where-import Protolude hiding (TypeError)+import Protolude hiding (TypeError, toList)import qualified Data.Aeson as Aesonimport qualified Data.IntMap as IntMap@@ -32,13 +32,16 @@ pureEnv = Bindings e purePrimops r 1-- not shadowable via 'define'.purePrimops :: forall m. (Monad m) => Primops mpurePrimops = Primops $ fromList $ first Ident <$>- [ ( "lambda"+ [ ( "fn", \case- List atoms_ : b : bs -> do- atoms <- traverse isAtom atoms_ ?? toLangError (TypeError "lambda: expecting a list of symbols")- e <- gets bindingsEnv- pure (Lambda atoms (b :| bs) e)- xs -> throwErrorHere $ WrongNumberOfArgs "lambda" 2 (length xs) -- TODO: technically "at least 2"+ args : b : bs ->+ case args of+ Vec atoms_ -> do+ atoms <- traverse isAtom (toList atoms_) ?? toLangError (TypeError "fn: expecting a list of symbols")+ e <- gets bindingsEnv+ pure (Lambda atoms (b :| bs) e)+ _ -> throwErrorHere $ OtherError "fn: first argument must be a vector of argument symbols, and then at least one form for the body"+ xs -> throwErrorHere $ WrongNumberOfArgs "fn" 2 (length xs) -- TODO: technically "at least 2"), ( "base-eval", evalArgs $ \case@@ -75,14 +78,14 @@ purePrimops = Primops $ fromList $ first Ident <$>, ("quote", \args -> case args of[v] -> pure vxs -> throwErrorHere $ WrongNumberOfArgs "quote" 1 (length xs))- , ("define", \args -> case args of+ , ("def", \args -> case args of[Atom name, val] -> doval' <- baseEval valdefineAtom name val'pure nil- [_, _] -> throwErrorHere $ OtherError "define expects atom for first arg"- xs -> throwErrorHere $ WrongNumberOfArgs "define" 2 (length xs))- , ( "define-rec"+ [_, _] -> throwErrorHere $ OtherError "def expects atom for first arg"+ xs -> throwErrorHere $ WrongNumberOfArgs "def" 2 (length xs))+ , ( "def-rec", \case[Atom name, val] -> doval' <- baseEval val@@ -91,9 +94,9 @@ purePrimops = Primops $ fromList $ first Ident <$>let v = Lambda is b (Env . Map.insert name v . fromEnv $ e)defineAtom name vpure nil- _ -> throwErrorHere $ OtherError "define-rec can only be used to define functions"- [_, _] -> throwErrorHere $ OtherError "define-rec expects atom for first arg"- xs -> throwErrorHere $ WrongNumberOfArgs "define-rec" 2 (length xs)+ _ -> throwErrorHere $ OtherError "def-rec can only be used to define functions"+ [_, _] -> throwErrorHere $ OtherError "def-rec expects atom for first arg"+ xs -> throwErrorHere $ WrongNumberOfArgs "def-rec" 2 (length xs)), ("do", evalArgs $ pure . lastDef nil), ("catch", \args -> case args of@@ -216,7 +219,7 @@ purePrimops = Primops $ fromList $ first Ident <$>Primop _ -> kw "primop"Dict _ -> kw "dict"Ref _ -> kw "ref"- Lambda{} -> kw "lambda"+ Lambda{} -> kw "function"), ("string?", evalOneArg "string?" $ \caseString _ -> pure ttdiff --git a/test/spec/Radicle/Tests.hs b/test/spec/Radicle/Tests.hsindex 755643e..6aa55db 100644--- a/test/spec/Radicle/Tests.hs+++ b/test/spec/Radicle/Tests.hs@@ -35,7 +35,7 @@ test_eval =, testCase "Succeeds for defined atoms" $ dolet prog = [s|- (define rocky-clark "Steve Wozniak")+ (def rocky-clark "Steve Wozniak")rocky-clark|]prog `succeedsWith` String "Steve Wozniak"@@ -137,19 +137,19 @@ test_eval =counterexample (toS info) $ res == expected, testCase "'foldl' foldls the list" $ do- let prog = [s|(foldl (lambda (x y) (- x y)) 0 (list 1 2 3))|]+ let prog = [s|(foldl (fn [x y] (- x y)) 0 (list 1 2 3))|]prog `succeedsWith` Number (-6), testCase "'foldr' foldrs the list" $ do- let prog = [s|(foldr (lambda (x y) (- x y)) 0 (list 1 2 3))|]+ let prog = [s|(foldr (fn [x y] (- x y)) 0 (list 1 2 3))|]prog `succeedsWith` Number 2, testCase "'map' maps over the list" $ do- let prog = [s|(map (lambda (x) (+ x 1)) (list 1 2))|]+ let prog = [s|(map (fn [x] (+ x 1)) (list 1 2))|]prog `succeedsWith` List [Number 2, Number 3], testCase "'map' (and co.) don't over-eval elements of argument list" $ do- let prog = [s|(map (lambda (x) (cons 1 x)) (list (list 1)))+ let prog = [s|(map (fn [x] (cons 1 x)) (list (list 1)))|]prog `succeedsWith` List [List [Number 1, Number 1]]@@ -165,8 +165,8 @@ test_eval =res1 @?= res2, testProperty "'eval' does not alter functions" $ \(_v :: Value) -> do- let prog1 = [i| (head (eval (lambda () #{renderPrettyDef _v}) (get-current-env))) |]- prog2 = [i| (lambda () #{renderPrettyDef _v}) |]+ let prog1 = [i| (head (eval (fn [] #{renderPrettyDef _v}) (get-current-env))) |]+ prog2 = [i| (fn [] #{renderPrettyDef _v}) |]res1 = runTest' $ toS prog1res2 = runTest' $ toS prog2info = "Expected:\n" <> prettyEither res2@@ -174,33 +174,33 @@ test_eval =counterexample (toS info) $ res1 == res2, testCase "lambdas work" $ do- let prog = [s|((lambda (x) x) #t)|]+ let prog = [s|((fn [x] x) #t)|]prog `succeedsWith` Boolean True, testCase "lambda does not have access to future definitions (hyper static)" $ dolet prog = [s|- (define y "a")- (define lam (lambda () y))- (define y "b")+ (def y "a")+ (def lam (fn [] y))+ (def y "b")(lam)|]prog `succeedsWith` String "a", testCase "lambdas allow local definitions" $ dolet prog = [s|- ((lambda ()- (define y 3)+ ((fn []+ (def y 3)y))|]prog `succeedsWith` Number 3, testCase "lambdas throw WrongNumberOfArgs if given wrong number of args" $ do- let prog = [s| ((lambda (x) 3)) |]+ let prog = [s| ((fn [x] 3)) |]prog `failsWith` WrongNumberOfArgs "lambda" 1 0, testCase "'quote' gets evaluated the right number of times" $ dolet prog = [s|- (define test (lambda (x) (eq? x 'hi)))+ (def test (fn [x] (eq? x 'hi)))(test 'hi)|]prog `succeedsWith` Boolean True@@ -232,7 +232,7 @@ test_eval =, testCase "'do' runs effects in order" $ dolet prog = [s|- (define r (ref 0))+ (def r (ref 0))(do (write-ref r 1)(write-ref r 2))(read-ref r)@@ -284,7 +284,7 @@ test_eval ="(type (list 1 2 3))" `hasTy` "list""(type (dict 1 2))" `hasTy` "dict""(type (ref 0))" `hasTy` "ref"- "(type (lambda (x) x))" `hasTy` "lambda"+ "(type (fn [x] x))" `hasTy` "function", testCase "'+' sums the list of numbers" $ dolet prog1 = [s|(+ 2 (+ 2 3))|]@@ -312,48 +312,48 @@ test_eval =res = runTest' $ toS progcounterexample prog $ res == Right (Boolean (x < y))- , testCase "'define' fails when first arg is not an atom" $ do- let prog = [s|(define "hi" "there")|]- prog `failsWith` OtherError "define expects atom for first arg"+ , testCase "'def' fails when first arg is not an atom" $ do+ let prog = [s|(def "hi" "there")|]+ prog `failsWith` OtherError "def expects atom for first arg", testCase "'catch' catches thrown exceptions" $ dolet prog = [s|- (catch (quote exc) (throw (quote exc) #t) (lambda (y) y))+ (catch (quote exc) (throw (quote exc) #t) (fn [y] y))|]prog `succeedsWith` Boolean True, testCase "'catch' has the correct environment" $ dolet prog = [s|- (define t #t)- (define f #f)- (catch (quote exc) (throw (quote exc) f) (lambda (y) t))+ (def t #t)+ (def f #f)+ (catch (quote exc) (throw (quote exc) f) (fn [y] t))|]prog `succeedsWith` Boolean True, testCase "'catch 'any' catches any exception" $ dolet prog = [s|- (catch 'any (throw (quote exc) #f) (lambda (y) #t))+ (catch 'any (throw (quote exc) #f) (fn [y] #t))|]prog `succeedsWith` Boolean True, testCase "evaluation can be redefined" $ dolet prog = [s|- (define eval (lambda (expr env) (list #f env)))+ (def eval (fn [expr env] (list #f env)))#t|]prog `succeedsWith` Boolean False, testCase "redefining eval keeps access to future definitions" $ dolet prog = [s|- (define eval (lambda (expr env) (eval expr env)))- (define t #t)+ (def eval (fn [expr env] (eval expr env)))+ (def t #t)t|]prog `succeedsWith` Boolean True, testCase "'read-ref' returns the most recent value" $ dolet prog = [s|- (define x (ref 5))+ (def x (ref 5))(write-ref x 6)(read-ref x)|]@@ -362,23 +362,23 @@ test_eval =, testCase "mutations to refs are persisted beyond a lambda's scope" $ dolet prog = [s|- (define inc-ref- (lambda (r)- (define temp (read-ref r))+ (def inc-ref+ (fn [r]+ (def temp (read-ref r))(write-ref r (+ temp 1))))- (define a (ref 0))+ (def a (ref 0))(inc-ref a)(read-ref a)|]let prog2 = [s|- (define ref-mapper- (lambda (f)- (lambda (r)- (define temp (read-ref r))+ (def ref-mapper+ (fn [f]+ (fn [r]+ (def temp (read-ref r))(write-ref r (f temp)))))- (define ref-incer (ref-mapper (lambda (x) (+ x 1))))- (define foo (ref 0))+ (def ref-incer (ref-mapper (fn [x] (+ x 1))))+ (def foo (ref 0))(ref-incer foo)(read-ref foo)|]@@ -387,17 +387,17 @@ test_eval =, testCase "muliple refs mutated in multiple lambdas" $ dolet prog = [s|- (define make-counter- (lambda ()- (define current (ref 0))- (lambda ()- (define temp (read-ref current))+ (def make-counter+ (fn []+ (def current (ref 0))+ (fn []+ (def temp (read-ref current))(write-ref current (+ temp 1))temp)))- (define c1 (make-counter))+ (def c1 (make-counter))(c1)(c1)- (define c2 (make-counter))+ (def c2 (make-counter))(c2)(c1)(list (c1) (c2))@@ -421,7 +421,7 @@ test_eval =runTest' "(show (list 'a 1 \"foo\" (list 'b ''x 2 \"bar\")))" @?= Right (String "(a 1.0 \"foo\" (b (quote x) 2.0 \"bar\"))")runTest' "eval" @?= Right (Primop [ident|base-eval|])runTest' "(show (dict 'a 1))" @?= Right (String "{a 1.0}")- runTest' "(show (lambda (x) x))" @?= Right (String "(lambda (x) x)")+ runTest' "(show (fn [x] x))" @?= Right (String "(fn [x] x)"), testCase "'read' works" $runTest' "(read \"(:hello 42)\")" @?= Right (List [Keyword [ident|hello|], Number 42])@@ -431,10 +431,10 @@ test_eval =runTest' "(to-json (dict \"key\" (list 1 \"value\")))" @?= Right (String "{\"key\":[1,\"value\"]}")noStack (runTest' "(to-json (dict 1 2))") @?= Left (OtherError "Could not serialise value to JSON")- , testCase "define-rec can define recursive functions" $ do+ , testCase "def-rec can define recursive functions" $ dolet prog = [s|- (define-rec triangular- (lambda (n)+ (def-rec triangular+ (fn [n](if (eq? n 0)0(+ n (triangular (- n 1))))))@@ -442,13 +442,13 @@ test_eval =|]runTest' prog @?= Right (Number 55)- , testCase "define-rec errors when defining a non-function" $- failsWith "(define-rec x 42)" (OtherError "define-rec can only be used to define functions")+ , testCase "def-rec errors when defining a non-function" $+ failsWith "(def-rec x 42)" (OtherError "def-rec can only be used to define functions"), testCase "stack traces work" $ dolet prog = [s|- (define inner (lambda () (notdefined)))- (define outer (lambda () (inner)))+ (def inner (fn [] (notdefined)))+ (def outer (fn [] (inner)))(outer)|]case runTest' prog of@@ -458,8 +458,8 @@ test_eval =, testCase "stack traces work with HOFs" $ dolet prog = [s|- (define callit (lambda (f) (f)))- (define inner (lambda () (notdefined)))+ (def callit (fn [f] (f)))+ (def inner (fn [] (notdefined)))(callit inner)|]case runTest' prog of@@ -531,7 +531,7 @@ test_parser =test_binding :: [TestTree]test_binding =[ testCase "handles shadowing correctly" $ do- [s|(((lambda (x) (lambda (x) x)) "inner") "outer")|] ~~> String "outer"+ [s|(((fn [x] (fn [x] x)) "inner") "outer")|] ~~> String "outer"]wherex ~~> y = runIdentity (interpret "test" x pureEnv) @?= Right y@@ -576,7 +576,7 @@ test_repl_primops =, testCase "catch catches read-line errors" $ dolet prog = [s|- (catch 'any (read-line!) (lambda (x) "caught"))+ (catch 'any (read-line!) (fn [x] "caught"))|]input = ["\"blah"]res = run input prog@@ -590,15 +590,15 @@ test_repl_primops =(load! "foo.rad")(+ foo bar)|]- files = Map.singleton "foo.rad" "(define foo 42) (define bar 8)"+ files = Map.singleton "foo.rad" "(def foo 42) (def bar 8)"runFiles files prog @?= Right (Number 50), testCase "generating and verifying cryptographic signatures works" $let prog = [s|- (define my-keys (gen-key-pair! (default-ecc-curve)))- (define not-my-keys (gen-key-pair! (default-ecc-curve)))- (define sig (gen-signature! (lookup :private-key my-keys) "hello"))- (define tt (verify-signature (lookup :public-key my-keys) sig "hello"))- (define ff (verify-signature (lookup :public-key not-my-keys) sig "hello"))+ (def my-keys (gen-key-pair! (default-ecc-curve)))+ (def not-my-keys (gen-key-pair! (default-ecc-curve)))+ (def sig (gen-signature! (lookup :private-key my-keys) "hello"))+ (def tt (verify-signature (lookup :public-key my-keys) sig "hello"))+ (def ff (verify-signature (lookup :public-key not-my-keys) sig "hello"))(list tt ff)|]in run [] prog @?= Right (List [Boolean True, Boolean False])@@ -611,13 +611,13 @@ test_repl_primops =test_repl :: [TestTree]test_repl =[ testCase "evaluates correctly" $ do- let input = [ "((lambda (x) x) #t)" ]+ let input = [ "((fn [x] x) #t)" ]output = [ "#t" ](_, result) <- runInRepl inputresult @==> output, testCase "handles env modifications" $ do- let input = [ "(define id (lambda (x) x))"+ let input = [ "(def id (fn [x] x))", "(id #t)"]output = [ "()"@@ -627,7 +627,7 @@ test_repl =result @==> output, testCase "handles 'eval' redefinition" $ do- let input = [ "(define eval (lambda (expr env) (list #t env)))"+ let input = [ "(def eval (fn [expr env] (list #t env)))", "#f"]output = [ "()"@@ -636,9 +636,9 @@ test_repl =(_, result) <- runInRepl inputresult @==> output- , testCase "(define eval (quote base-eval)) doesn't change things" $ do- let input = [ "(define eval (quote base-eval))"- , "(define id (lambda (x) x))"+ , testCase "(def eval (quote base-eval)) doesn't change things" $ do+ let input = [ "(def eval (quote base-eval))"+ , "(def id (fn [x] x))", "(id #t)"]output = [ "()"