lemma 0.11.4

A pure, declarative language for business rules.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
---
nav_title: Composing specs
nav_order: 60
---

# Composing Specs

Once a Spec can reference its own Data and Rules, it can import another Spec with Uses and reference its members through an alias. This chapter covers composition, temporal versions, planning checks, and (last) repositories and registry imports for advanced usage.

For syntax details see [Using other Specs](../reference/readme.md#using-other-specs).

## Importing another Spec

A Lemma source file defines one or more Specs. Each Spec is a namespace of Data and Rules. With Uses, you import another Spec under an alias and refer to its members through that alias. Read a value with `alias.field`, use a dependency Rule with `alias.rule_name`, and reuse an imported Data declaration as a parent type with the alias: `data country: iso.code`. A bare name that an import declares is a planning error.

```lemma
spec premium_membership

data discount_rate: 10%

rule free_shipping_threshold:
  50

rule monthly_bonus_points:
  100


spec membership_benefits

uses membership: premium_membership

data monthly_spend: 150

rule discount:
  monthly_spend * membership.discount_rate

rule bonus_points:
  membership.monthly_bonus_points
```

- `uses membership: premium_membership` binds alias `membership` to Spec `premium_membership`.
- `uses premium_membership` uses an implicit alias equal to the full target name.
- `membership.discount_rate` reads Data from the imported Spec.
- `membership.monthly_bonus_points` uses a Rule from the imported Spec.

A minimal two-Spec example:

```lemma
spec base_employee

data name:   "John Doe"
data salary: 5_000


spec manager

uses employee: base_employee
  -> with name: "Alice Smith"
  -> with salary: 8_000

rule manager_bonus:
  employee.salary * 0.15
```

`uses alias: target` imports a Spec under an explicit alias; a bare `uses target` defaults the alias to the full target name. Read Data and Rules as `alias.field` or `alias.rule_name`.

## Setting Data on an import (`uses` block)

Every `uses` is its own instance of the imported spec. `-> with path: …` under the `uses` line writes a value on that instance (path relative to the imported spec):

```lemma
spec inner

data x: number
  -> suggest 1


spec outer

uses i: inner
  -> with x: 42

rule r:
  i.x
```

- `-> with x: 42` sets Data `x` on `inner` to `42`.
- `-> with x: i` is an error: `i` is a Spec reference, not a value.
- A rule may return the alias: `rule chosen: i unless flag then other`. `chosen.x` reads `x` on whichever instance was chosen. Both branches must be the same spec.
- Standalone `with copy: i.x` is a parse error; nest `  -> with` under `uses` (see [Reference](../reference/readme.md#setting-data-on-an-imported-spec-uses-block)).

The binding path must be relative to the imported spec (`x`, not `i.x`). Local slots use `data`.

A slot bound under `uses` is sealed. A value (`-> with x: 42`) fixes `i.x`; a data or Rule reference (`-> with x: base`, `-> with x: src.total`) copies from that source, as written: a value in a dynamic unit is its number and unit, sized by `i`'s type. Evaluation data for `i.x` is a request error: only data a `uses` leaves unbound can be supplied at evaluate. A value is filled once, so a spec further out cannot bind `i.x` again; that is a planning error. To let it, bind `x` to data of your own, which can hold a default. The bindings hold for the rows of a list that names the `uses` too (see [What a list dictates to its rows](lists.md#what-a-list-dictates-to-its-rows)).

See [Setting data on an imported spec](../reference/readme.md#setting-data-on-an-imported-spec-uses-block).

### Global instances

Every `uses` line makes its own instance. When several specs each import the same spec for evaluation-wide data, such as exchange rates, write `uses global` instead: every `uses global` of that spec in one repository reads one instance per evaluation.

```lemma
spec rates

data usd_per_eur: number
  -> minimum 0

rule eur_per_usd:
  1 / usd_per_eur


spec purchase

uses global rates

data price_usd: number

rule price_eur:
  price_usd * rates.eur_per_usd


spec shipping

uses global rates

data freight_usd: number

rule freight_eur:
  freight_usd * rates.eur_per_usd


spec order

uses purchase
  -> with price_usd: 1_200

uses shipping
  -> with freight_usd: 150

rule cost_eur:
  purchase.price_eur + shipping.freight_eur


spec order_test

uses global rates
  -> with usd_per_eur: 1.25

uses order

rule cost_is_right:
  order.cost_eur is 1_080
```

Evaluate `order` with `global.rates.usd_per_eur=1.25`: both imported specs read that one rate, and `cost_eur` is 1080. A field no spec writes comes from evaluation data, under `global.<spec>.<field>`; `purchase.rates.usd_per_eur` is a request error that names the global key.

#### Writing a global

`uses global rates -> with usd_per_eur: 1.25` writes `usd_per_eur` on the one instance: every `uses global rates` reads it, inside the writing spec and outside it. A path through a global slot writes on it too (`uses purchase -> with rates.usd_per_eur: 1.25`). Every spec can be the evaluated one, so a test spec pins the globals of everything it uses this way: `order_test` above writes the rate, and evaluating it needs no evaluation data (`cost_is_right` is true).

- A value is written once. Two specs writing one field of a global are a planning error, and so is a spec that writes on a global and has more than one instance (two `uses` of it, or rows of a list of it), since each instance writes again. Different specs may write different fields.
- Evaluation data for a field a spec writes is a request error (`global.rates.usd_per_eur` for `order_test`).
- The global belongs to the repository whose specs write `uses global`. A library that writes `uses global rates` keeps its own instance, with data under `@owner/library global.rates.<field>`.
- All `uses global rates` of one repository name one spec version, so a global has one instance. `uses global rates -> effective` is a planning error.
- A unit sized by the global's data resolves without qualification.

## Temporal versions

The same Spec name may appear several times with different effective datetimes (`spec Name 2025-05-01`). Each declaration is immutable; you add a new row on the timeline instead of editing history in place.

```lemma
spec pricing

data base_price: 20
data quantity:   number

rule total:
  base_price * quantity


spec pricing 2025-01-01

data base_price: 25
data quantity:   number

rule total:
  base_price * quantity
```

Evaluate with different effective dates:

```bash
lemma evaluate pricing --effective 2024-06-01   # uses base_price: 20
lemma evaluate pricing --effective 2025-06-01   # uses base_price: 25
```

Which row applies at evaluation time is determined by the `--effective` instant (CLI) or `Accept-Datetime` (HTTP) for the Spec you evaluate.

### Unpinned vs pinned Uses

| Form | Meaning |
|------|--------|
| `uses dep` | Unpinned. Every temporal version of `dep` whose range intersects this Spec's range can matter. Planning may split this Spec into temporal slices at dependency `effective_from` boundaries. Values resolved through the import can change when `dep` gains a new row. |
| `uses dep 2025-06-01` | Pinned to an instant. One body of `dep` active at that datetime, including its transitive imports. Later rows of `dep` do not affect this edge. |
| `uses dep` with `-> effective <date>` | Dynamic pin. One local date or datetime data or rule chooses the body at evaluation. Dependency version dates do not split the consumer. Option sets may differ across versions; other interface drift is still a planning error. |

An unpinned import (`uses pricing`) follows the dependency's timeline; a pinned import (`uses p: pricing 2025-06-01`) freezes the dependency at that instant. A dynamic pin keeps one consumer plan and selects the body from a date-typed local value:

```lemma
spec alpha2 2020-01-01

data code: text
  -> option "NL"
  -> option "CS"

rule name:
  veto "unknown"
  unless code is "NL" then "Netherlands"
  unless code is "CS" then "Serbia and Montenegro"


spec alpha2 2023-01-01

data code: text
  -> option "NL"
  -> option "RS"

rule name:
  veto "unknown"
  unless code is "NL" then "Netherlands"
  unless code is "RS" then "Serbia"


spec intake

uses iso: alpha2
  -> effective birth_date
  -> with code: birth_country

data birth_date:    date
data birth_country: text

rule birth_country_name:
  iso.name
```

Do not combine a trailing literal pin with `-> effective` on the same `uses`. If no version of the dependency is active at the pin instant, evaluation vetoes.

#### Unpinned: values follow the timeline

```lemma
spec policy

rule discount:
  10


spec policy 2025-05-01

rule discount:
  25


spec shop 2025-01-01

uses p: policy

rule d:
  p.discount
```

Evaluating `shop` in early 2025 yields `d = 10`; from May 2025 onward, `d = 25`. The Uses line is unchanged; the resolved policy body changes at the slice boundary.

A consumer with no effective date on its Spec line (origin) still gets slices when a dependency's `effective_from` falls inside its range.

#### Pinned: freeze a dependency at one instant

```lemma
spec finance

data money: measure
  -> unit eur: 1.00


spec finance 2025-07-01

data money: measure
  -> unit eur: 1.00
  -> unit usd: 0.91


spec shop 2025-01-01

uses f: finance 2025-02-01

data money: f.money
data price: money

rule doubled:
  price * 2
```

`shop` keeps the February 2025 finance shape (EUR only) even when you evaluate in September 2025. Pinning locks a tariff or regulation snapshot into the consumer.

## Planning checks

Before `evaluate`, planning validates the dependency graph. Two temporal failures authors see most often:

### Temporal coverage (gaps)

Unpinned `uses dep` requires `dep` to exist for every instant in the consumer's temporal range. If the consumer starts in January but `dep` only exists from July, planning fails (no active version / not active at that instant).

Fix: add an earlier `spec dep` row, move the consumer's `effective_from` later, or pin `uses dep 2025-08-01` so the consumer no longer requires `dep` to cover its whole range.

### Interface compatibility (contract changes)

When unpinned imports span multiple rows of the same dependency, every name the consumer reads through the import must have the same type in each of those rows. A name is read when the consumer writes it after the alias: in a rule (`f.total`), as a type (`data price: f.money`), or in `-> with` on the `uses` row. Type sameness covers units, constraints such as `maximum`, and `help` text. Names the consumer does not read may change freely, and new names in a later row are fine.

Incompatible example: `money` gains a `usd` unit in a later `finance` row while `shop` still has unpinned `uses f: finance` and `data money: f.money`. Planning names the member and both rows: `'shop' depends on 'finance' without pinning an effective date, but type 'money' changed between finance 2025-01-01 and finance 2025-07-01`. When the base type itself changes, the message shows both types, for example `rule 'fee' has type number in calc and text in calc 2025-06-01`.

Fix: pin `uses f: finance 2025-02-01`, or add a new temporal row of `shop` written for the new finance body.

Coverage is presence on the timeline; interface validation is type sameness, across those rows, of what the consumer reads.

### Same name, different bodies

You may import an earlier temporal row that shares your base name:

```lemma
spec finance 2026-01-01

data rate: 1


spec finance 2027-01-01

uses f26: finance 2026-01-01

rule ok:
  f26.rate
```

Planning rejects a Uses edge that resolves to the same Spec body (self-reference):

```lemma-skip
spec finance

uses finance


spec finance 2026-01-01

uses finance 2026-01-01
```

A later row must not use unpinned `uses finance` when that resolves to itself at that row's instant. Pin an earlier row (`uses f26: finance 2026-01-01`) instead.

Cycles across temporal rows (2026 → 2027 → 2026) are rejected as dependency cycles.

## Repositories

When Specs in the same project share a name, Repo blocks namespace them so they do not collide. Cross-repo targets use a repo qualifier on the Uses line:

```lemma
spec billing

uses inv: accounting invoice

rule out:
  inv.total


repo accounting

spec invoice

data total: 1
```

Cross-repo targets use a repo qualifier on the Uses line (`accounting invoice`). When you `evaluate` a Spec from the default repository, use its unqualified name; the CLI does not pick between two loaded Specs with the same name in different repos.

Most projects never need Repo blocks: files without one belong to the default repository. Repositories installed from LemmaBase live under their own `@owner/name` names (see [LemmaBase](#lemmabase) below).

## LemmaBase

When Specs live outside your project, import them from LemmaBase with `@owner/repo` qualifiers:

```lemma
spec invoicing

uses @iso/countries alpha2

data price: measure
  -> unit eur: 1

data country: alpha2.code

rule tariff:
  0 eur
  unless country is "NL" then price * 5%

rule total:
  price + tariff
```

Install repositories before running:

```bash
lemma install --all           # install all @... repositories into lemma_deps/
lemma install @iso/countries -f   # force re-install if content changed
```

Importing from LemmaBase uses `@owner/name` on the `uses` line (for example `uses iso: @iso/countries alpha2`). `Engine` does not auto-fetch; install with `lemma install` (or your SDK's `install`) first, then load. See [LemmaBase](../reference/registry.md).

## Evaluating a composed Spec

You always `evaluate` (or call the API for) a named root Spec in a repository, with an effective instant:

```bash
lemma evaluate membership_benefits --effective 2025-03-01
```

- The engine selects the consumer's temporal slice that contains that instant.
- Unpinned imports: paths such as `p.discount` use dependency bodies resolved for that slice.
- Pinned imports: the dependency body stays at the pinned instant.

To inspect Types (local data declarations), Data (inputs rules need), and rules for that temporal slice:

```bash
lemma show membership_benefits --effective 2025-03-01
```

Evaluation-data-aware discovery of what a concrete `evaluate` still needs comes from each rule's `missing_data`, not from `show`. Library specs with no rules list their declarations under Types (`x-lemma-types`); Data/`properties` stays empty.
## Quick decision guide

| Goal | Pattern |
|------|---------|
| Track compatible dependency rows across the consumer's lifetime | `uses dep` (unpinned) |
| Lock a regulation / tariff / spec version at a known date | `uses dep 2025-06-01` |
| Import an earlier row of the same Spec name | `uses prev: finance 2026-01-01` |
| Reuse a Data shape from a library | `uses iso: @iso/countries alpha2` and `data x: iso.code` |
| Set Data on an imported Spec | `uses alias: spec` then `  -> with path: value` |
| Read import Data in a Rule | `rule r: alias.field` |

If planning fails, check whether the message is coverage, interface, self-reference, or cycle. Each remedy is different (see above).

## Next up

[Numeric precision](precision.md): exact rational arithmetic, limits, and client parsing.