From b29f98105fa46923e4bf72be9a00e3d684c87b60 Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Thu, 10 Aug 2023 01:07:41 +0200 Subject: [PATCH] Type union-products. --- rs.rkt | 22 +++++++++++++++++++++- scribblings/rs.scrbl | 12 ++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/rs.rkt b/rs.rkt index 9d85ed1..70200c3 100644 --- a/rs.rkt +++ b/rs.rkt @@ -5,7 +5,7 @@ (provide Species (struct-out reaction) Reaction ReactionName ReactionSystem - make-reaction enabled? list-enabled) + make-reaction enabled? list-enabled union-products) (module+ test (require typed/rackunit)) @@ -57,6 +57,26 @@ 'b (make-reaction '(x y) '() '(z)))) (check-equal? (list-enabled rs (set 'x 'y)) '(b)) (check-equal? (list-enabled rs (set 'x)) '(a)))) + + (: union-products (-> ReactionSystem (Listof ReactionName) (Setof Species))) + (define (union-products rs as) + (cond + [(empty? as) (set)] + [else (define products (for/list : (Listof (Setof Species)) + ([a as]) + (reaction-products (hash-ref rs a)))) + (apply set-union (assert-type products (NonemptyListof (Setof Species))))])) + + (module+ test + (test-case "union-products" + (define rs (hash 'a (make-reaction '(x) '(y) '(z)) + 'b (make-reaction '(x y) '() '(t)))) + (check-equal? (union-products rs '(a b)) + (set 't 'z)) + (check-equal? (union-products rs '(a)) + (set 'z)) + (check-equal? (union-products rs '()) + (set)))) ) (require graph "utils.rkt" "generic.rkt") diff --git a/scribblings/rs.scrbl b/scribblings/rs.scrbl index ad60f8a..6f733a5 100644 --- a/scribblings/rs.scrbl +++ b/scribblings/rs.scrbl @@ -99,6 +99,18 @@ Returns the list of the names of reactions of @racket[rs] enabled on (list-enabled rs (set 'x)))) ]} +@defproc[(union-products [rs ReactionSystem] [as (Listof ReactionName)]) + (Setof Species)]{ + +Returns the union of the product sets of the given reactions listed in +@racket[as] in @racket[rs]. If no reactions are supplied, returns the +empty set. + +@ex[ +(union-products (hash 'a (make-reaction '(x) '(y) '(z)) + 'b (make-reaction '(x y) '() '(z))) + '(a b)) +]} @section{Org-mode interaction}