diff --git a/prospect/core.rkt b/prospect/core.rkt index 49ef0b2..ab03fdf 100644 --- a/prospect/core.rkt +++ b/prospect/core.rkt @@ -33,6 +33,7 @@ event? action? + match-event meta-label? @@ -125,6 +126,11 @@ (define (event? x) (or (patch? x) (message? x))) (define (action? x) (or (event? x) (spawn? x) (quit-network? x))) +(define-syntax-rule (match-event e clause ...) + (match e + clause ... + [_ #f])) + (define (prepend-at-meta pattern level) (if (zero? level) pattern @@ -153,19 +159,20 @@ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (define (general-transition? v) - (or (not v) (transition? v) (quit? v))) + (or (not v) (transition? v) (quit? v) (void? v))) (define (ensure-transition v) (if (general-transition? v) v - (raise (exn:fail:contract (format "Expected transition, quit or #f; got ~v" v) + (raise (exn:fail:contract (format "Expected transition, quit, #f or (void); got ~v" v) (current-continuation-marks))))) (define (clean-transition t) (match t [#f #f] [(quit exn actions) (quit exn (clean-actions actions))] - [(transition state actions) (transition state (clean-actions actions))])) + [(transition state actions) (transition state (clean-actions actions))] + [(? void?) #f])) (define (clean-actions actions) (filter (lambda (x) (and (action? x) (not (patch-empty? x)))) (flatten actions))) diff --git a/prospect/examples/bank-account.rkt b/prospect/examples/bank-account.rkt index f5a0f8a..8c55d5a 100644 --- a/prospect/examples/bank-account.rkt +++ b/prospect/examples/bank-account.rkt @@ -5,22 +5,19 @@ (struct deposit (amount) #:prefab) (define (manager e balance) - (match e + (match-event e [(message (deposit amount)) (transition (+ balance amount) (patch-seq (retract (account balance)) - (assert (account (+ balance amount)))))] - [_ #f])) + (assert (account (+ balance amount)))))])) (define (observer e _) (when (patch? e) (for [(balance (project-assertions (patch-added e) (account (?!))))] - (printf "Balance changed to ~a\n" balance))) - #f) + (printf "Balance changed to ~a\n" balance)))) (define (updater e _) - (if (and (patch? e) (trie-non-empty? (patch-added e))) - (quit (list (message (deposit +100)) - (message (deposit -30)))) - #f)) + (when (and (patch? e) (trie-non-empty? (patch-added e))) + (quit (list (message (deposit +100)) + (message (deposit -30)))))) (spawn manager 0 (patch-seq (assert (observe (deposit ?))) (assert (account 0)))) (spawn observer (void) (assert (observe (account ?))))