dol 0.8.1

DOL (Design Ontology Language) - A declarative specification language for ontology-first development
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
# DOL Examples Gallery

> **Real-World Examples of Ontology-First Development**

All examples in this gallery are verified from the DOL compiler test suite.

---

## Quick Examples

### Hello World Gene
**Source**: `examples/genes/hello.world.dol`

```dol
gene hello.world {
  message has content
  message has sender
  message has timestamp
}

exegesis {
  The hello.world gene is the simplest possible DOL example. It defines
  a message entity with three essential properties: content (what the
  message says), sender (who sent it), and timestamp (when it was sent).
}
```

### Counter State Gene
**Source**: `examples/genes/counter.dol`

```dol
gene counter.state {
  counter has value
  counter has minimum
  counter has maximum
  counter derives from initialization
}

exegesis {
  The counter.state gene models a bounded counter. A counter has a current
  value, and bounds (minimum and maximum). The counter derives from an
  initialization value when created.
}
```

---

## Functions

### Simple Function
**Source**: `tests/codegen/golden/input/function.dol`

```dol
fun add(a: Int64, b: Int64) -> Int64 {
    return a + b
}
```

### Gene with Methods
**Source**: `tests/corpus/traits/trait_relationships.dol`

```dol
module tests.trait_relationships @ 1.0.0

pub gene SimpleValue {
    has value: String
    has count: Int64 = 0

    fun get_value() -> String {
        return this.value
    }
}

pub gene StringWrapper {
    has value: String

    fun to_string() -> String {
        return this.value
    }
}
```

### Pipe Operators
**Source**: `tests/codegen/golden/input/pipe_operators.dol`

```dol
fun process(x: Int64) -> Int64 {
    return x |> double |> increment
}
```

---

## Generic Types
**Source**: `tests/corpus/genes/nested_generics.dol`

```dol
module tests.nested_generics @ 1.0.0

// Simple generic
pub gene Container<T> {
    has item: T
}

// Nested generic
pub gene Nested<T> {
    has items: List<T>
    has mapping: Map<String, T>
    has optional: Option<T>
    has result: Result<T, String>
}

// Deeply nested
pub gene DeepNest<T> {
    has deep: Map<String, List<Option<T>>>
    has matrix: List<List<T>>
    has complex: Result<Map<String, List<T>>, String>
}

// Multiple type params
pub gene Multi<K, V> {
    has key: K
    has value: V
    has pairs: List<Tuple<K, V>>
}

// Bounded generics
pub gene Bounded<T: Comparable> {
    has items: List<T>

    fun max() -> Option<T> {
        if this.items.is_empty() {
            return None
        }
        return Some(this.items.reduce(|a, b| if a > b { a } else { b }))
    }
}

// Generic with default
pub gene WithDefault<T = Int64> {
    has value: T
}

exegesis {
    Tests for nested and complex generic type parameters.
}
```

---

## Control Flow

### If Expressions
**Source**: `tests/dol2_tests.rs`

```dol
if x { result }
```

```dol
if condition { positive } else { negative }
```

```dol
if x { a } else if y { b } else if z { c } else { d }
```

### For Loops
**Source**: `tests/parser_tests.rs`

```dol
for outer in outers {
    for inner in inners {
        break;
    }
}
```

### For Loop with Range
**Source**: `tests/corpus/sex/nested_sex.dol`

```dol
for i in 0..n {
    sex {
        COUNTER += 1
        LOG.push("iteration " + i.to_string())
    }
}
```

### Pattern Matching
**Source**: `tests/dol2_tests.rs`

```dol
match value {
    Some(x) => x,
    None => default,
    _ => fallback
}
```

```dol
match x {
    value if condition => positive,
    _ => zero
}
```

```dol
match pair {
    (Some(x), Some(y)) => result,
    _ => default
}
```

---

## Traits

### Metal DOL Trait
**Source**: `examples/traits/greetable.dol`

```dol
trait entity.greetable {
  uses entity.identity
  greetable can greet
  greetable can receive.greeting
  greeting is polite
}

exegesis {
  The entity.greetable trait defines behavior for entities that can
  participate in greetings.
}
```

### DOL 2.0 Trait with Laws
**Source**: `tests/corpus/genes/complex_constraints.dol`

```dol
pub trait Ordered {
    is compare(other: Self) -> Int64

    law reflexive {
        forall x: Self. x.compare(x) == 0
    }

    law antisymmetric {
        forall x: Self. forall y: Self.
            x.compare(y) <= 0 && y.compare(x) <= 0 implies
                x.compare(y) == 0
    }

    law transitive {
        forall x: Self. forall y: Self. forall z: Self.
            x.compare(y) <= 0 && y.compare(z) <= 0 implies
                x.compare(z) <= 0
    }
}
```

---

## Constraints

### Metal DOL Constraint
**Source**: `examples/constraints/counter_bounds.dol`

```dol
constraint counter.bounds_valid {
  value never overflows
  value never underflows
  bounds never inverted
}

exegesis {
  The counter.bounds_valid constraint ensures the counter state is
  always valid.
}
```

### DOL 2.0 Constraints with Quantifiers
**Source**: `tests/corpus/genes/complex_constraints.dol`

```dol
pub gene OrderedList {
    has items: List<Int64>

    // Simple constraint
    constraint non_empty {
        this.items.length() > 0
    }

    // Forall constraint
    constraint sorted {
        forall i: UInt64.
            i < this.items.length() - 1 implies
                this.items[i] <= this.items[i + 1]
    }

    // Exists constraint
    constraint has_positive {
        exists x: Int64. x in this.items && x > 0
    }
}
```

---

## SEX (Side Effect System)
**Source**: `tests/corpus/sex/nested_sex.dol`

### Global Mutable State

```dol
// Global mutable state
sex var COUNTER: Int64 = 0
sex var LOG: List<String> = []
sex var CACHE: Map<String, Int64> = Map.new()
```

### SEX Functions

```dol
// Simple sex function
sex fun increment() -> Int64 {
    COUNTER += 1
    return COUNTER
}

// Sex function with sex block
sex fun logged_increment(label: String) -> Int64 {
    sex {
        LOG.push(label + ": incrementing")
    }

    result = COUNTER + 1
    COUNTER = result

    sex {
        LOG.push(label + ": now " + result.to_string())
    }

    return result
}

// Pure function with sex block
fun compute_with_logging(x: Int64) -> Int64 {
    result = x * 2 + 1

    sex {
        LOG.push("computed: " + result.to_string())
    }

    return result
}
```

### Conditional Effects

```dol
sex fun conditional_effects(cond: Bool) -> Int64 {
    if cond {
        sex {
            COUNTER += 10
        }
    } else {
        sex {
            COUNTER -= 10
        }
    }
    return COUNTER
}
```

---

## Systems
**Source**: `examples/systems/greeting.service.dol`

```dol
system greeting.service @0.1.0 {
  requires entity.greetable >= 0.0.1
  requires greeting.protocol >= 0.0.1

  uses hello.world
  service has greeting.templates
  service has response.timeout
}

exegesis {
  The greeting.service system composes genes, traits, and constraints
  into a complete, versioned component.
}
```

### Bounded Counter System
**Source**: `examples/systems/bounded.counter.dol`

```dol
system bounded.counter @0.1.0 {
  requires counter.state >= 0.0.1
  requires counter.countable >= 0.0.1
  requires counter.bounds_valid >= 0.0.1

  counter has persistence.strategy
  counter has overflow.policy
}

exegesis {
  The bounded.counter system composes genes, traits, and constraints
  into a complete, versioned component.
}
```

---

## Evolution and Versioning
**Source**: `tests/corpus/genes/evolution_chain.dol`

```dol
module tests.evolution_chain @ 1.0.0

// Base type
pub gene EntityV1 {
    has id: UInt32
    has name: String
}

// First evolution - add fields
evolves EntityV1 > EntityV2 @ 2.0.0 {
    added created_at: Int64 = 0
    added updated_at: Int64 = 0

    migrate from EntityV1 {
        return EntityV2 {
            ...old,
            created_at: 0,
            updated_at: 0
        }
    }
}

// Second evolution - change types
evolves EntityV2 > EntityV3 @ 3.0.0 {
    changed id: UInt32 -> UInt64
    added metadata: Map<String, String>
    removed updated_at

    migrate from EntityV2 {
        return EntityV3 {
            id: old.id as UInt64,
            name: old.name,
            created_at: old.created_at,
            metadata: Map.new()
        }
    }
}

// Third evolution - rename fields
evolves EntityV3 > EntityV4 @ 4.0.0 {
    renamed name -> display_name
    added tags: List<String> = []

    migrate from EntityV3 {
        return EntityV4 {
            id: old.id,
            display_name: old.name,
            created_at: old.created_at,
            metadata: old.metadata,
            tags: []
        }
    }
}
```

---

## Lambda Expressions
**Source**: `tests/dol2_tests.rs`

### Basic Lambdas

```dol
map(|x| x, list)
```

```dol
|x: Int32, y: Int32, z: Int32| -> Int32 { x }
```

### Curried Lambda

```dol
|x| |y| x
```

### Lambda in Pipeline

```dol
data |> (|x| x) |> result
```

### Lambda with Reduce
**Source**: `tests/corpus/genes/nested_generics.dol`

```dol
this.items.reduce(|a, b| if a > b { a } else { b })
```

---

## Pipes and Function Composition
**Source**: `tests/dol2_tests.rs`

### Forward Pipe

```dol
data |> validate |> transform |> store
```

### Function Composition

```dol
trim >> lowercase >> validate >> normalize
```

### Mixed Pipe and Composition

```dol
data |> (trim >> validate) |> process
```

---

## Example Index by Concept

| Concept | Example Count | Sources |
|---------|---------------|---------|
| Gene Declaration | 8 | examples/genes/, tests/corpus/ |
| Functions | 10 | tests/codegen/, tests/corpus/ |
| Control Flow | 8 | tests/dol2_tests.rs, tests/parser_tests.rs |
| Types & Generics | 6 | tests/corpus/genes/nested_generics.dol |
| Pattern Matching | 5 | tests/dol2_tests.rs |
| Traits | 5 | examples/traits/, tests/corpus/ |
| Constraints | 4 | examples/constraints/, tests/corpus/ |
| Systems | 2 | examples/systems/ |
| Evolution | 3 | tests/corpus/genes/evolution_chain.dol |
| Lambdas | 5 | tests/dol2_tests.rs |
| SEX (Side Effects) | 6 | tests/corpus/sex/nested_sex.dol |

---

## Running the Examples

```bash
# Parse and validate
dol check examples/

# Generate Rust code
dol compile examples/container.dol --output generated/

# Run tests
dol test examples/
```

---

## More Examples

Find more examples in the repository:

- `examples/genes/` - Gene definitions
- `examples/traits/` - Trait definitions
- `examples/constraints/` - Constraint examples
- `examples/systems/` - System definitions
- `tests/corpus/` - Comprehensive test corpus
- `dol/` - Self-hosted compiler (DOL in DOL!)

**Official Resources:**
- [GitHub]https://github.com/univrs/dol/releases/tag/v0.3.0
- [Crates.io]https://crates.io/crates/dol/0.3.0

---

*"Programs must be written for people to read, and only incidentally for machines to execute."* — Harold Abelson