CSCI 1730 · Fall 2026
8 DSL: Pricing Rules
8.1 Business Rules
A shop’s discount policy is mostly not written by programmers. It changes week to week, it is argued over by people who do not read code, and afterwards it has to be explicable to an auditor, or to an annoyed customer who did not get the lower price they expected. Therefore, it tends not to live in the shop’s ordinary source code but rather in a language of its own. That language has to be rich enough to express common policies but would ideally be narrow enough that someone who is not a programmer can read a policy and correctly infer what they believe it says.
Such languages are everywhere. IBM’s decision manager has a rule language whose documentation opens with an example giving gold customers 10% off; DMN is an OMG standard for the same purpose; Drools, at Apache KIE, is the best known open-source engine. Outside pricing, XACML does it for access control.
All of them have to confront a question: when several rules match at once, what happens? They answer differently. XACML names two of the answers first-applicable and only-one-applicable; Shopify applies the single best discount to a line; Magento can be told to discard every rule after the first that fires.
However, the policies are like programs: they cause certain behavior. That may not be the desired behavior. Therefore, in this assigment we will play with a custom verification engine for this custom policy language that lets us check certain semantic properties of policies.
8.2 The Setting
Consider a bookstore.A “book” is an ancient technology that loosely resembles the 📖 emoji. At Brown, if you are determined, you can find a handful in select locations, some of which even enclose interesting artwork. It sells a paid membership at two tiers, gold and silver; everyone else is a regular customer. (So, a customer’s tier is one of gold, silver, and regular.)
The shop offers these discounts: a gold member buying a book gets 15% off that line; a silver member buying a book gets 10% off; anyone ordering five or more copies of the same item (it need not be a book) gets 12% off; and a gold member ordering five or more copies of a book gets 20% off. When a line qualifies for more than one offer, an offer for members takes priority over one that is open to everyone, and the gold member’s 20% offer takes priority over the 15% one. On a single line of the cart, at most one offer applies.
On the cart as a whole, there are three discounts: staff get 25% off; a regular customer gets $5 off an order of $30 or more; and a member (gold or silver) gets $3 off. These are meant for different people, so no two of them should ever apply to the same cart. The shop also never wants a discount to be bigger than the price it is applied to (e.g., $3 off a $2 order).
8.3 The Policy Language
#lang pricing is a small language for writing policies like the one above.
A program is some rule definitions followed by a rule set naming the ones it uses.
program ::= rule* rule-set |
Here is a rule on one line of the cart, with a compound condition and a percentage off. It is the rule IBM’s documentation opens with, plus the condition that it be a book:
rule gold_member_book: |
scope: line |
when: CondAnd(CondCmp("==", CondLineField("product"), CondLiteral("book")), |
CondCmp("==", CondCustomerField("tier"), CondLiteral("gold"))) |
effect: Percent(15) |
And here is one on the cart total, taking off a flat amount rather than a percentage:
rule big_basket: |
scope: cart |
when: CondCmp(">", CondCartField("subtotal"), CondLiteral(50)) |
effect: Amount(5) |
A line-scope effect applies to what the line costs at list price—
quantity times unit price— so Amount(5) on a line takes $5 off the line, not off each item in it. A line, like the cart total, is clamped at $0: prices cannot be negative. (That is, you can’t cleverly combine discounts to get the store to pay you money.)
A cart-scope rule refers to a cart whose lines have already been discounted, so "subtotal" there is the running total rather than the list price. For instance: suppose a $40 book is sold at half off. Say the shop takes $5 off orders of $30 or more. An order consisting of just that book costs $20, because by the time the cart rule can be applied, the subtotal is $20.
The price of each line, and the cart total, is rounded to the nearest cent (half a cent rounds up). Nothing else is rounded.
CondLineField "product" "qty" "unit_price" |
CondCustomerField "tier" "is_employee" |
CondCartField "subtotal" "item_count" |
A product or a tier is a string; the other fields are numbers, except for "is_employee", which is true or false. Therefore, "is_employee" is a condition by itself: write CondCustomerField("is_employee"), and wrap it in CondNot for the opposite. Every other field has to appear inside a comparison.
An effect is Percent(n), where n is a whole number from 0 to 100, or Amount(m), where m is an amount of money that is not negative. An effect can only ever reduce a price.
The language rejects nonsense when a file is loaded, with an error that names the rule. E.g., you cannot compare things of different kinds (a quantity against a subtotal, or a product against a number), and a cart-scope rule cannot use a line field such as "qty" (use "item_count" or "subtotal").
The condition portion cannot do any arithmetic. Rather, the arithmetic is in the the discount that an effect computes. There are no functions, no recursion, and no way to reach anything outside the cart.
When there is only one rule, there is no ambiguity about how it applies. When there is more than one rule, however, we have to declare how they combine. A rule set lists its rules and two combiners, one for each scope; e.g.:
rule_set the_rule_set: |
rules: [gold_member_book, big_basket] |
line_combiner: highest_wins |
cart_combiner: only_one_applicable |
only_one_applicable: At most one rule is allowed to match. If more than one does, that is an error.
first_applicable: The first rule that matches is used, and the rest are ignored. Therefore, the textual order of the rules matters, so that order is part of your discount policy design.
highest_wins: Of the rules that match, the one that gives the biggest discount is used.
all_apply_against_base: Every rule that matches is used. Each discount is computed on the price before any of the discounts (not on what is left after the others), and the discounts are added up.
Call your rule set the_rule_set, as above: the other files refer to it by that name.
8.4 The Property Language
There is no separate DSL for writing properties. Instead, you write them by making function calls in Rhombus. The language comes with three fixed properties:
No two rules of a scope ever apply together. Declare the scope only_one_applicable and call verify_mutual_exclusion. It checks whether, for every pair of rules in that scope, no cart makes both match.
Every rule can apply. Call check_reachability, whatever the combiners. It checks whether, for every rule, there is some cart on which that rule is the one that is used. What that means depends on the combiner: e.g., under first_applicable, there must be a cart for which the rule matches and no earlier rule does.
The rules alone decide every price. Call check_clamp_activation, whatever the combiners. The language already has a safety guard: that no price goes below $0. This property checks whether, when a line (or the cart total) has $0 price, that is set by the rule, not by the clamp. Counter-examples would be rules that the user might not have wanted.
The first property only makes sense if some scope is only_one_applicable. If no scope is, there is nothing to check, so checking that property results in an error.
Each analysis takes a rule set and returns a list of results. The property holds exactly when that list is [Pass()]. Thus, a verify.rhm might look like:
def results = verify_mutual_exclusion(shop.the_rule_set) |
for (r in results): println(describe(r)) |
check: results ~is [Pass()] |
When a property fails, describe prints the cart that shows it. (A product or tier called "other" stands for any name the rules do not mention.)
The set of results is:
Pass: all queries succeeded. It only ever appears alone, as [Pass()].
Conflict, from verify_mutual_exclusion: two rules of a scope both match. It names the two rules and shows a cart on which they do.
Unreachable, from check_reachability: no cart reaches a rule. It names the rule and says why: either its condition can never hold, or the other rules always get in its way (e.g., under first_applicable, every cart that satisfies its condition is matched by an earlier rule first). It does not show a cart, because the whole point is that no such cart exists.
ClampActivation, from check_clamp_activation: a rule would take a price below $0. It names the rule and shows a cart that demonstrates this. (Under all_apply_against_base, it may take several rules together to do this. Each of them is reported.)
As noted, both Conflict and ClampActivation present a concrete cart. Before the verifier presents one, it runs it through the actual policy and confirms that it sees the problem happen.
In addition to these outcomes, you may also get the following two outcomes, which are inconclusive about the truth or falsity of the properties. (Both are kinds of Undetermined.) Each of the three functions takes two optional arguments that may help: e.g., verify_mutual_exclusion(shop.the_rule_set, ~max_lines: 50, ~timeout: 30) looks at carts of up to 50 lines and gives the solver 30 seconds per question.
BoundReached, from any of the three properties, about cart-scope rules. This means that the verifier could not prove or disprove the property. The verifier constructs an over-approximation of the carts, in which a cart’s subtotal may be any amount. The problem occurred in that over-approximation. The verifier tries to find a concrete cart, of up to 25 lines, that will demonstrate the error, but it might fail. This could be because a longer cart was needed, or because this is an artifact of the over-approximation. This output provides information about the potential problem: the customer, subtotal, and item count for which it would occur. You have to determine whether a real cart can have this form.
SolverGaveUp, from any of the three properties. It names the property and means that the solver ran out of time or answered unknown. This should not happen on typical policies, but can happen in general. It sometimes carries the same information about a potential problem that BoundReached does.
You may (but should not!) also get an internal error. If this happens, please carefully record all the files and report on EdStem!
8.5 The Starter Kit
Get the starter kit and unzip it. In its folder (dsl1-starter), run these two commands, once:
racket install.rkt |
racket check-setup.rkt |
The first installs the languages (it takes about a minute and prints a lot). The second checks that everything you need is in place, including Z3, which the verifier uses. If something is missing, it tells you what to do.
The kit has been tested only on macOS, not on Windows or Linux. If you run into a problem on either, please report it.
The folder example has a complete, small answer to this assignment for a different shop: a farm stand. The files have the same names that yours will have:
rules.rhm: the policy, in #lang pricing.
tests.rhm: sample carts and the prices they should have.
verify.rhm: the properties, checked by the verifier.
broken_1.rhm and verify_broken_1.rhm: a copy of the policy with one rule changed so that a property fails, and a file that checks that the verifier notices.
Read them, and then run each one from inside example; e.g.:
racket tests.rhm |
rules.rhm prints nothing if the policy is well-formed. Each of the others prints what it found, and ends with a line like 3 tests passed. If a check fails, you instead see what it expected and what it got, and a line like 1/3 test failures.
The files other than the policies are in #lang pricing/check. This is Rhombus with everything you need to price carts and to call the verifier already imported.
Your own files should start as copies of these, with the bookstore in place of the farm stand.
8.6 The Assignment
Use the DSL to describe the policy; describe what properties must hold of it; and then check that it passes those properties.
Imagine that you are working at a company that sells this system to individual shop-owners. You should assume that your customer needs code in formal languages to run on the computer, but would also like prose versions that they can read and audit. Therefore, you need to produce English equivalents for some of the formal material below.
8.6.1 Code and Tests
Write a program that implements the aforementioned store policy.
In prose, write a justification for why you chose those particular combiners.
Manually enumerate all the interesting conditions that you would want to
test. For each
of these, manually create a handful of sample carts and their expected
prices. Confirm
that these have the price you expected; if not, revise. Document, in prose, what
conditions you chose, and give a sample for each condition. If you had to change
your expected price, explain why—
To price a cart, use price_cart, which takes a rule set and a cart, as in example/tests.rhm. A cart is a list of lines and a customer. A line is a product, a quantity, and a unit price. A customer is a tier and whether they are staff. E.g.:
Cart([Line("book", 2, 7.50)], Customer("regular", #false)) |
Write an expected price as text, with a dollar sign and two decimal places (e.g., "$7.25"). The function dollars turns a price into that form.
8.6.2 Properties
Manually enumerate, in prose, all the interesting properties that the store’s policy must have. For each one, state it to the verifier, run the verifier, and confirm that it holds.
The verifier may find that the policy, as the shop stated it, does not have a property. If so, change your rules until it does. Record what the verifier reported, what you changed, and how your rules now differ from what the shop asked for.
Some of the properties you enumerate may not be any of the three that the language provides: e.g., “no cart is discounted by more than half”. The verifier cannot check such a property. You should still write it down, and say that it has not been verified.
8.6.3 Break
When a verifier reports nothing, we can’t actually tell whether it ran or not! In particular, we may sometimes have written a vacuously true property that always holds, and may be congratulating ourselves over something that should not have given us any confidence (because the property is buggy).
The way we check this is through the verification equivalent of mutation testing. For each property that you verified, make a copy of your rules and change the copy so that the rules violate the property. Each copy needs its own verify file, which checks that the verifier does report the problem (as in example/verify_broken_1.rhm). Record the change, why (in prose) it should violate the property, and what the verifier printed. In this way, you can confirm that all your properties are actually non-trivial.
8.7 What To Hand In
Your design document.
The files rules.rhm, tests.rhm, and verify.rhm.
The broken copies with their verify files. Name them broken_1.rhm and verify_broken_1.rhm, and so on. In your design, say in a sentence what each edit was and which property it breaks.