> ## Documentation Index
> Fetch the complete documentation index at: https://docs.canary.effectiveai.app/llms.txt
> Use this file to discover all available pages before exploring further.

# Author a rater in either edition

> Write the same small rater in RSL 0.4.2 and the RSL 0.5 authoring preview, then check and export both.

These examples require `rslc` 0.8.0, included with the matching [Relativities release](https://docs.canary.effectiveai.app/relativities/setup.md). Use RSL 0.4.2 when your plan needs verification or underwriting. Use the RSL 0.5 authoring preview to try module and function syntax for supported raters.

The first two examples return the entered amount as written premium, up to \$100. Each example uses a separate folder for checking and packaging.

## Write the rating rule

Save this file as `legacy/rater.rsl`. The `rsl` field in `spec` selects the edition. `RatingOutcomes` names the result.

```rsl legacy/rater.rsl theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
spec {
    rsl: "0.4.2",
    carrier: "Acme",
    state: CA,
    product: "Homeowners",
    version: "1.2.0",
    effective_date: 2026-08-28
}

Sources = {
    manual: Source = {
        artifact: artifact("019f819b-1674-7046-bc54-6f18cb149d6d"),
        kind: pdf
    }
}

Records = {
    Policy = {
        inputs {
            amount: Num {precision: 0.01} where self in [0, 100]
        },
        step rating {
            premium: Premium [cite: manual:p1] = round(self.amount, 2, half_up)
        }
    }
}

RatingOutcomes = {
    written_premium: Policy.premium
}
```

Save this file as `preview/rater.rsl`. The first line selects RSL 0.5. The module names the entry function, and `show` names the result. The sample uses the same cent grid and input range as the first file.

```rsl preview/rater.rsl theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
rsl 0.5

module acme.homeowners version "1.2.0" {
    entry rate
    state CA
    effective 2026-08-28
    meta carrier "Acme"
    meta product "Homeowners"
}

function rate(policy: Policy) {
    let written_premium: Cents = policy.amount
    show written_premium
}

record Policy {
    amount: Money @ 0.01 in [0, 100]
}
```

In RSL 0.5, put a business refusal in the entry function or on the input record.
A helper called from a calculation cannot declare a check. If a check applies
only when the entry calls a helper, include that condition in the entry check.
Inputs that skip the call can then proceed.

## Check and export both files

```sh theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
rslc check legacy/rater.rsl --json
rslc check preview/rater.rsl --json
```

Check the `profile` in each report. RSL 0.4 uses `verify`. RSL 0.5 uses `preview`, which checks the supported authoring forms and reports errors in the source. If you change `Money` to an undefined type in the preview file, the check fails with `E-UNRESOLVED` and points at the type name. Fix the error before exporting.

```sh theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
rslc export legacy/rater.rsl --format bundle --out legacy.zip
rslc export preview/rater.rsl --format bundle --out preview.zip
```

Each command writes a bundle with its own identity. Keep the ZIP for the edition you intend to run and the corresponding source files for your next edit. Both files accept `rslc fmt FILE` for canonical formatting.

## Group related inputs in a record

A required field can hold another record. Declare `coverage: Coverage` in `Policy`, declare the scalar rating fields in `Coverage`, and read them through that field, for example `policy.coverage.amount`. The exported input schema describes a nested JSON object. If the amount is out of range, the refusal names `Policy.coverage.amount`.

Declare `coverage: Coverage?` if the request can omit the object. The schema
allows either the object or `null`. Omitting the field or sending `null` selects
the `none` arm. When a request supplies an object, its fields follow the
`Coverage` declaration, and the `some` arm can read them.

```rsl optional-record/rater.rsl theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
rsl 0.5

module acme.optional_record {
    entry rate
}

function rate(policy: Policy) {
    let written_premium: Cents = match policy.coverage {
        none => 0,
        some coverage => coverage.amount
    }
    show written_premium
}

record Policy {
    coverage: Coverage?
}

record Coverage {
    amount: Money @ 0.01 in [0, 100]
}
```

### Calculate a value on an optional child

A `for` block over an optional child declares a calculation on the
child. Read the calculation in the `some` arm of a match. The `none` arm
handles a missing or null child.

Save the example as `optional-child-calculation/rater.rsl`.

```rsl optional-child-calculation/rater.rsl theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
rsl 0.5

module acme.optional_child_calculation {
    entry rate
}

function rate(policy: Policy) {
    for coverage in policy.coverage {
        let doubled: Cents = coverage.amount * 2
    }
    let written_premium: Cents = match policy.coverage {
        none => 0,
        some selected => selected.doubled
    }
    show written_premium
}

record Policy {
    coverage: Coverage?
}

record Coverage {
    amount: Money @ 0.01 in [0, 100]
}
```

### Accept several records in one field

A plain `List(Exposure)` keeps the same input shape in both editions. Save this RSL 0.4.2 example as `legacy-list/rater.rsl`. The record and its inputs are declared under `Records`.

```rsl legacy-list/rater.rsl theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
spec {
    rsl: "0.4.2",
    carrier: "Acme",
    state: CA,
    product: "Homeowners",
    version: "1.2.0",
    effective_date: 2026-08-28
}

Sources = {
    manual: Source = {
        artifact: artifact("019f819b-1674-7046-bc54-6f18cb149d6d"),
        kind: pdf
    }
}

Records = {
    Exposure = {
        inputs {
            amount: Num {precision: 0.01} where self in [0, 100]
        }
    },
    Policy = {
        inputs {
            amount: Num {precision: 0.01} where self in [0, 100],
            exposures: List(Exposure)
        },
        step rating {
            premium: Premium [cite: manual:p1] = round(self.amount, 2, half_up)
        }
    } where all(self.exposures as exposure, exposure.amount <= 100)
}

RatingOutcomes = {
    written_premium: Policy.premium
}
```

Save the RSL 0.5 version as `preview-list/rater.rsl`. The item type and the containing record are declared at module scope.

```rsl preview-list/rater.rsl theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
rsl 0.5

module acme.exposures {
    entry rate
}

function rate(policy: Policy) {
    let written_premium: Cents = policy.amount
    show written_premium
}

record Policy {
    amount: Money @ 0.01 in [0, 100]
    exposures: List(Exposure)
}

record Exposure {
    amount: Money @ 0.01 in [0, 100]
}
```

Send `{"Policy":{"amount":10,"exposures":[{"amount":5.25},{"amount":12}]}}` to supply two items. An empty array, a missing `exposures` field, and `"exposures": null` all supply an empty list. Each item is validated independently. For example, an amount over 100 in the second item is refused at `Policy.exposures[1].amount`.

### Require a bounded list

Put `count in [1, 3]` inside the list declaration to require at least one item and admit up to three. Save this example as `bounded-list/rater.rsl` and use an RSL 0.5 preview build that supports list counts.

```rsl bounded-list/rater.rsl theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
rsl 0.5

module acme.bounded_exposures {
    entry rate
}

function rate(policy: Policy) {
    let written_premium: Cents = policy.amount
    show written_premium
}

record Policy {
    amount: Money @ 0.01 in [0, 100]
    exposures: List(Exposure) {
        count in [1, 3]
    }
}

record Exposure {
    amount: Money @ 0.01 in [0, 100]
}
```

A missing or null `exposures` field supplies zero items, as does an empty array. Each fails the minimum with `INPUT-LEN` at `Policy.exposures`. Four items fail the maximum at the same path. The published input schema marks `exposures` required and lists the limits as `minItems` and `maxItems`.

### Rate a keyed list and break a tie

Use `key number` to require a unique number for each exposure in the list. Calculate each exposure's charge in a `for` block, add the rounded charges with `sum`, and use `argmax` to select the exposure with the highest charge. The `tie key` clause selects the lowest number when charges are equal.

Save this example as `keyed-list/rater.rsl` and use an RSL 0.5 preview build that supports keyed records and selectors.

```rsl keyed-list/rater.rsl theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
rsl 0.5

module example.list_rating {
    entry rate
}

function rate(policy: Policy) {
    for exposure in policy.exposures {
        let charge: Cents = exposure.amount * policy.factor
    }
    let premium: Cents = sum(exposure.charge for exposure in policy.exposures)
    let lead_number = match argmax(exposure.charge for exposure in policy.exposures, tie key) {
        some exposure => exposure.number,
        none => 0
    }
    show premium
    show lead_number
}

record Policy {
    factor: Factor
    exposures: List(Exposure)
}

record Exposure key number {
    number: Num @ 1 in [1, 9999]
    amount: Money @ 0.01
}
```

```sh theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
rslc check keyed-list/rater.rsl --json
rslc export keyed-list/rater.rsl --format bundle --out keyed-list.zip
```

Send this JSON input to the rater.

```json theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
{
  "Policy": {
    "factor": 0.5,
    "exposures": [
      { "number": 20, "amount": 1.01 },
      { "number": 10, "amount": 1.01 }
    ]
  }
}
```

The rater rounds each charge to 0.51 and returns a premium of 1.02. Both charges tie, so `lead_number` is 10 even though that exposure comes second. When the list is empty, `sum` returns zero and `argmax` returns `none`. The example returns `lead_number` as 0 in that case. Reusing number 20 for the second item returns `INPUT-KEY` at `Policy.exposures[1].number`.

### Select a matching record from a keyed list

Use `find(item.eligible for item in policy.items)` to get the first eligible item by ascending declared key. A match returns `some item`. A list with no match returns `none`. The query stops at the first match, so later rows do not have to calculate the predicate. Use `record Item key number` when the input uses the submitted item number as its key.

## Group arithmetic with a fallback

RSL 0.5 requires parentheses when `otherwise` and arithmetic meet at the same level. For `amount otherwise base + fee`, `rslc` reports `E-FALLBACK-GROUPING` and suggests both ways to group it.

With `amount otherwise (base + fee)`, the fee applies only when `amount` is absent. With `(amount otherwise base) + fee`, the fee applies to either amount. Save this example as `optional/rater.rsl`.

```rsl optional/rater.rsl theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
rsl 0.5

module acme.optional {
    entry rate
}

function rate(policy: Policy) {
    let amount: Money = policy.amount otherwise 7
    let written_premium: Cents = amount + policy.fee
    show written_premium
}

record Policy {
    amount: Money? @ 0.01 in [0, 100]
    fee: Money default 1
}
```

Check the example before exporting it.

```sh theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
rslc check optional/rater.rsl --json
```

An omitted or null `amount` takes the fallback of 7. An explicit zero stays zero. The nonoptional fee defaults to 1 when omitted or null. An explicit zero overrides the default.

## Read a list of enum values

A field declared as `choices: List(Choice)` takes a JSON array such as
`["alpha", "beta"]` when `Choice` declares those enum members. An empty array
is valid. The field is required, and each invalid item receives its own input
path, such as `Policy.choices[1]`. A comprehension can read a table cell for
each member in the submitted order, for example
`sum(Charges[choice].charge for choice in policy.choices)`. Repeated members
contribute once per appearance.

Use a `count in [1, 3]` check on the field to require one to three entries.
The input schema exposes the same minimum and maximum, and the runtime
reports a length violation at the field path.

## Narrow an enum input

A named subset such as `type Polygon = Piece in {.rectangle, .triangle}` retains
the member wire values from `Piece`. Use `piece: Polygon` to require an input
from that subset. An unknown wire value receives `INPUT-ENUM`. A wire value
that belongs to `Piece` but falls outside `Polygon` receives `INPUT-REFINEMENT`
at the input's path.

## Read a tagged input

A required tagged input takes a JSON object with a `tag` and the fields for
that arm. For `type Origin = { measured "Measured" { amount: Num }, estimated "Estimated" }`,
send `{"tag":"Measured","amount":12}` or `{"tag":"Estimated"}`. Match on the
source name, as in `.measured m => m.amount`. The request uses the declared
wire spelling, such as `"Measured"`.

Use `type Status = tagged { absent "Absent", recorded "Recorded" }` when
every arm is empty. Send `{"tag":"Recorded"}` and match with `.recorded`.
An ordinary enum input takes the string `"Recorded"`.

A scalar payload field can be optional or require a filed table key. Admission
checks the selected arm and reports an invalid value at its nested field path.
The input schema lists the closed alternatives in `oneOf`.

## Look up a text code

A `Text` input receives a JSON string. Use it for codes whose spelling matters.
For example, `"0012"` and `"12"` can name different rows, and sending the number
`12` gives an input type refusal.

```rsl text-keys/rater.rsl theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
rsl 0.5

module example.text_keys {
    entry rate
}

function rate(policy: Policy) {
    table Classes[class_code: Text] -> (factor: Factor, label: Text) {
        "0012"  1.25  "Preferred"
        "12"    0.75  "Standard"
        otherwise factor 1, label "Unlisted"
    }
    let factor = Classes[policy.class_code].factor
    show factor
}

record Policy {
    class_code: Text
}
```

To read these rows from a CSV, add `from "classes.csv"` to the table declaration,
move the two filed rows to that file, and use `class_code,factor,label` as its header.
Exact key lookup preserves the text in each cell. A `Text` field can receive strings outside the table, so a
lookup using that field needs an `otherwise` row.

## Require a filed table key

Write the table and key column, such as `Classes.class_code`, to admit only
values that name a filed row. This corresponds to `Key(Classes)` in a legacy
rater with one key column. The input keeps that column's type and wire
representation. A table's `otherwise` row does not make a value a filed key.

```rsl filed-keys/rater.rsl theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
rsl 0.5

module example.filed_keys {
    entry rate
}

function rate(policy: Policy) {
    let factor = Classes[policy.class_code].factor
    show factor
}

record Policy {
    class_code: Classes.class_code or refuse CLASS_NOT_RATED "The class is not rated."
}

table Classes[class_code: ClassCode] -> factor: Factor {
    "0012"  1.25
    "0120"  0.75
}

type ClassCode = code "[0-9]{4}"
```

A string that fails the Code pattern is an input error. A well-formed string
that has no row gets the authored refusal. Omit `or refuse` to use the
standard `INPUT-KEY-UNFILED` response.

Both editions reserve the wire value `unfiled` for a blank cell in a table
output enum. Use another member value for a filed row. A Text output can
contain the literal word `unfiled`.

For a band lookup without a fallback, the compiler must prove that a computed
key stays inside the filed bands. A bounded input can still yield an
out-of-range key after subtraction. Check the bounds of the calculation,
including the first and last cut and the grid that the filing specifies.

## Keep child records in the result

Add `layout nested` to an RSL 0.5 rating entry when callers read child records. Use `entry rate detail demand layout nested` to combine nested results with demand-only detail.

The public and detailed results include the declared input record and list hierarchy. A present child produces an object with its published fields and nested child structure. An omitted or null optional child produces `null`. A list keeps request order and produces `[]` when empty. The layout does not echo scalar inputs.

Use the same `show` lines to publish calculation results. With `detail demand`, detailed results also include the calculations the rating evaluated. The input contract stays the same. Native and Worker readers need to support the layout before loading a bundle that uses it.

### Publish calculations on an optional child

With `layout nested`, a `for` over an owned record field writes calculations
on that record. When the field is optional, the block has one instance if the
request supplies the child and no instance if the field is omitted or null.
Publish the child's result inside the block, as you do for list items.

```rsl nested-child/rater.rsl theme={"languages":{"custom":["languages/rsl.tmLanguage.json"]}}
rsl 0.5

module example.nested_child {
    entry rate detail demand layout nested
}

function rate(policy: Policy) {
    for coverage in policy.coverage {
        let risk_charge: Cents = coverage.amount
        show coverage.risk_charge
    }
}

record Policy {
    coverage: Coverage?
}

record Coverage {
    amount: Money
}
```

A request with a coverage amount of 100 produces
`{"Policy":{"coverage":{"risk_charge":100}}}` in the outputs. An omitted or
null coverage produces `{"Policy":{"coverage":null}}`. The entry's
`detail demand` setting also keeps demanded calculations on the child.

## Refer to a sibling record by key

A required `Ref(Driver)` field can identify an item in a sibling `List(Driver)` on the immediate owner. Declare a key on `Driver`, then send the key as the reference field's value. A calculation can read through the reference with a path such as `vehicle.primary_driver.amount`. Admission rejects duplicate list keys and references that have no matching item.

## Use enum-indexed maps

Declare a required input such as `limit: Num[Coverage] @ 1 in [0, 100]`. Send a JSON object with a value for every wire key in `Coverage`. The interval and grid apply to each cell. Read a cell with `policy.limit[policy.coverage]`.

Define a calculated map with a declaration such as `let charge[c: Coverage]: Num = policy.limit[c] * 2`. Use `show charge` to publish the map as an object keyed by the enum's wire values.

## Choose the edition for your plan

The RSL 0.5 preview covers a subset of rating authoring with scalar and record inputs. Use RSL 0.4 for scenario evaluation with `rslc eval`, proof checks, underwriting and submission logs, or any rating form the preview refuses. Keep an existing rater in the edition it declares until you have compared the outcome and input contract of a rewritten version.

See [Versions](https://docs.canary.effectiveai.app/rsl/concepts/versions.md) for the language, compiler, and bundle versions. See [Programs on Effective](https://docs.canary.effectiveai.app/rsl/programs-on-effective.md) for working with a completed bundle.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.