From 86256cd47e5b8c6a1059e03d9fd179455f954e0d Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Sun, 1 Mar 2020 19:47:38 +0100 Subject: [PATCH] rs: Add union-products. --- rs-tests.rkt | 3 ++- rs.rkt | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/rs-tests.rkt b/rs-tests.rkt index a8231ae..d7347e9 100644 --- a/rs-tests.rkt +++ b/rs-tests.rkt @@ -13,4 +13,5 @@ (check-true (enabled? r1 s1)) (check-false (enabled? r1 s2)) (check-equal? (list-enabled rs s1) '(a b)) - (check-equal? (list-enabled rs s2) '(b)))) + (check-equal? (list-enabled rs s2) '(b)) + (check-equal? (union-products rs '(a b)) (set 'y 'z)))) diff --git a/rs.rkt b/rs.rkt index 6bf4e32..8bb6c39 100644 --- a/rs.rkt +++ b/rs.rkt @@ -10,7 +10,7 @@ ;; Type names Species ReactionSystem ;; Functions - enabled? list-enabled) + enabled? list-enabled union-products) ;;; ================= ;;; Basic definitions @@ -42,3 +42,16 @@ (for/list ([(name reaction) (in-hash rs)] #:when (enabled? reaction s)) name)) + +;;; Returns the union of the product sets of the given reactions in a +;;; reaction system. +;;; +;;; This function can be seen as producing the result of the +;;; application of the given reactions to a set. Clearly, it does not +;;; check whether the reactions are actually enabled. +(: union-products (-> ReactionSystem (Listof Symbol) (Setof Species))) +(define (union-products rs as) + (apply set-union + ((inst set Species)) ; set-union requires at least one argument + (for/list : (Listof (Setof Species)) + ([a as]) (reaction-products (hash-ref rs a)))))