A peer-to-peer stack for code collaboration
{:module 'prelude/state-machine
:doc "An eval for running a state-machine with an updatable transition function."
:exports []}
(def head-shots
"Avatars for some of the monadic team members."
(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"))
(def get-head-shot
"Get a monadic team-member head-shot or return Julien's."
(fn [x]
(def y (lookup x head-shots))
(if (eq? y '())
"https://res.cloudinary.com/juliendonck/image/upload/v1536080565/avatars/2326909.jpg"
y)))
(def pr-trans
"PR state transition function."
(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)
(fn [cs] (cons c cs))
(over (@ :next-id)
(fn [x] (+ x 1))
state))
:output c)))
(def kim-trans
"PR state transition function tailored to comments by Kim."
(fn [state i]
(if (eq? (nth 0 i) "kim")
(pr-trans state (list "kim" "LGTM"))
(pr-trans state i))))
(def state-machine-input
"Handle an input in the morphing state-machine."
(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)))
(def state-machine-new-trans
"Trigger a new vote."
(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."))))
(def state-machine-agree
"Vote to agree on a new transition function."
(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))
(do (set-ref state (@ :transition-fn) (eval (view-ref state (.. (@ :voting) (@ :proposed-function)))))
(set-ref state (@ :voting) :nothing)
"Voting has finished; a new transition function is in effect.")
'()))
(throw 'invalid-input "Not allowed to vote."))))
(def state-machine-disagree
"Vote to disagree on a new transition function."
(fn [state voters userid]
(if (member? userid voters)
(do (set-ref state (@ :voting) :nothing)
"Voting has ended; someone disagreed.")
(throw 'invalid-input "You are not allowed to vote."))))
(def state-machine-eval
"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."
(fn [voters init-state init-transition]
(def state (ref (dict :machine-state init-state
:transition-fn init-transition
:voting :nothing)))
(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)
(eq? verb 'agree) (state-machine-agree state voters input)
(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)))))))
(def simple-trans
"Given a function `f`, makes a transition function who's output is also the next state."
(fn [f]
(fn [s i]
(def y (f s i))
(dict :state y
:output y))))
(def pr-thread
"A PR thread with two voters: `\"alice\"` and `\"bob\"`."
(state-machine-eval
(list "alice" "bob")
(dict :next-id 0 :comments (list))
pr-trans
))