3  The DSL and its executor

You will learn:

The model never does arithmetic itself. It writes a program, and the executor computes the number. That makes each answer checkable, and it keeps exact arithmetic out of a model that reads numbers one digit at a time.

3.1 The grammar

program   = number | step { "," step }
step      = operation "(" argument "," argument ")"
operation = "add" | "subtract" | "multiply" | "divide" | "exp" | "greater"
argument  = number | percent | "#" index | constant
number    = digits, optionally with commas and a decimal point, e.g. 206588 or 1,234.5
percent   = number "%"            # evaluated as number / 100
index     = digit { digit }       # the result of an earlier step
constant  = "const_" ( digits | "m1" )   # a fixed list of 18, see the chapter on what ConvFinQA looks like

A program with no parentheses is a number-selection turn: its value is just that number. Every other program is a list of steps. Step k may refer to the result of any earlier step as #j with j < k. The last step’s result is the answer.

3.2 Running a program

Code
import pandas as pd

from convfinqa import data
from convfinqa.dsl import executor

program = "subtract(206588, 181001), divide(#0, 181001)"
steps = executor.parse_program(program)
results = executor.trace(program)
pd.DataFrame(
    {
        "step": [f"#{i}" for i in range(len(steps))],
        "operation": [s.op for s in steps],
        "arguments": [", ".join(s.args) for s in steps],
        "result": [
            r if isinstance(r, str)
            else f"{round(r, executor.ROUND_DIGITS):g}" for r in results
        ],
    }
)
step operation arguments result
0 #0 subtract 206588, 181001 25587
1 #1 divide #0, 181001 0.14136

The first step is 206588 - 181001 = 25587, the second divides it by 181001 to get the percentage change, 0.14136.

3.3 Conventions inherited from the official code

The executor follows eval_program and str_to_num in the authors’ code/utils/general_utils.py.

Code
cases = {
    "number with commas": "1,234.5",
    "percent literal": "multiply(900, 0.25%)",
    "constant": "divide(300, const_3)",
    "negative constant": "multiply(5, const_m1)",
    "rounded to 5 places": "divide(2, 3)",
    "a tiny result rounds to 0": "divide(1, const_1000000)",
    "comparison": "greater(3, 2)",
}
pd.DataFrame({
    "program": cases.values(),
    "value": [executor.execute(p) for p in cases.values()]
}, index=cases.keys())
program value
number with commas 1,234.5 1234.5
percent literal multiply(900, 0.25%) 2.25
constant divide(300, const_3) 100.0
negative constant multiply(5, const_m1) -5.0
rounded to 5 places divide(2, 3) 0.66667
a tiny result rounds to 0 divide(1, const_1000000) 0.0
comparison greater(3, 2) yes
  • % divides by 100, so 0.25% is 0.0025.
  • Every result is rounded to 5 decimal places, which is also how the gold answers are stored.
  • greater returns the string "yes" or "no". Such a result cannot be used as a number in a later step.
  • Two answers match only when they are exactly equal after rounding: the official script has no numerical tolerance beyond that rounding.

3.4 When a program is wrong

A model’s output can fail in several ways. The executor reports the kind of failure instead of raising an anonymous error, so the evaluation can count them separately.

Code
bad = {
    "empty output": "",
    "unbalanced parenthesis": "add(1, 2",
    "one argument": "add(1)",
    "nested call": "add(1, add(2, 3))",
    "unknown operator": "sqrt(4, 2)",
    "not a number": "add(one, 2)",
    "reference to a later step": "add(1, 2), add(#1, 2)",
    "division by zero": "divide(1, 0)",
    "complex result": "exp(-8, 0.5)",
}
rows = []
for label, p in bad.items():
    value, kind = executor.try_execute(p)
    rows.append({"failure": label, "program": p, "error kind": kind})
pd.DataFrame(rows)
failure program error kind
0 empty output empty
1 unbalanced parenthesis add(1, 2 syntax
2 one argument add(1) syntax
3 nested call add(1, add(2, 3)) syntax
4 unknown operator sqrt(4, 2) unknown_operator
5 not a number add(one, 2) bad_number
6 reference to a later step add(1, 2), add(#1, 2) bad_reference
7 division by zero divide(1, 0) division_by_zero
8 complex result exp(-8, 0.5) math_error

try_execute returns (value, None) on success and (None, kind) on failure and never raises for a bad program. An invalid output counts as a wrong answer in our headline metric.

3.5 Do the gold programs reproduce the gold answers?

The strongest test of the executor is the data: every turn’s turn_program must execute to its exe_ans_list entry.

Code
rows = []
for split in ["train", "dev"]:
    n = mismatches = 0
    for conv in data.load_split(split):
        a = conv["annotation"]
        for prog, gold in zip(a["turn_program"], a["exe_ans_list"]):
            n += 1
            if not executor.answers_match(executor.try_execute(prog)[0], gold):
                mismatches += 1
    rows.append({
        "split": split,
        "turns": n,
        "mismatches": mismatches,
        "match rate": 1 - mismatches / n
    })
result = pd.DataFrame(rows)
assert (result.mismatches == 0).all()
result
split turns mismatches match rate
0 train 11104 0 1.0
1 dev 1490 0 1.0

There are no mismatches, so there is nothing to explain. The same check runs in pytest (tests/test_executor.py), along with unit tests for each operation and error kind.

3.6 Where our executor differs from the official one

  • table_* operations are rejected as unknown operators. The authors’ code supports table_sum, table_average, table_max and table_min, but no ConvFinQA gold program uses them.
  • Errors are reported by kind. The official function returns a single invalid flag.
  • Syntax is checked with a fixed pattern. The official code checks structure by token position. Ours parses each step as operation(argument, argument) and rejects, for example, nested calls and missing commas. All gold programs pass, so this does not change a gold score. We have not compared the two parsers on malformed model output.
  • #k must refer to an earlier step. A reference to itself or a later step is invalid. The official code reaches the same result through a lookup error.
NoteUnder the hood

The executor never looks at the page, so it cannot tell whether a number in a program was copied from the text or invented. The paper’s own model could only copy numbers from its input. Our fine-tuned model writes freely, and only the constrained-decoding chapter tests whether restricting it helps.

TipFurther reading

The official execution and evaluation code is code/utils/general_utils.py in the ConvFinQA repository (https://github.com/czyssrs/ConvFinQA). The paper (Chen et al. 2022) defines the operations and the metrics.