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
// ═══════════════════════════════════════════════════════════════════════════════
// Physics Spirit - Particle Physics Module
// DOL v0.9.0
// ═══════════════════════════════════════════════════════════════════════════════

module physics.particles @ 0.9.0

docs {
    Particle physics module providing fundamental particle types and properties.

    This module models:
    - Elementary particles (electrons, protons, neutrons, photons)
    - Particle properties (mass, charge, spin, position, velocity)
    - Particle classifications (fermions, bosons)
    - Conservation laws
    - Kinematic calculations

    All calculations use SI units unless otherwise specified.
}

use local::lib::SPEED_OF_LIGHT
use local::lib::ELECTRON_MASS
use local::lib::PROTON_MASS
use local::lib::NEUTRON_MASS
use local::lib::ELEMENTARY_CHARGE

// ═══════════════════════════════════════════════════════════════════════════════
// CORE TYPES
// ═══════════════════════════════════════════════════════════════════════════════

docs {
    3D position vector in meters.
}

pub gen Position {
    has x: f64
    has y: f64
    has z: f64
}

docs {
    3D velocity vector in meters per second.
}

pub gen Velocity {
    has vx: f64
    has vy: f64
    has vz: f64
}

docs {
    3D momentum vector in kg·m/s.
}

pub gen Momentum {
    has px: f64
    has py: f64
    has pz: f64
}

docs {
    Spin quantum number (can be half-integer for fermions).
    Represented as twice the spin value to avoid floating point.
    e.g., spin-1/2 is represented as spin_twice = 1
}

pub gen Spin {
    has spin_twice: i32
}

docs {
    Base particle type with fundamental properties.

    All particles have:
    - mass: Rest mass in kilograms
    - charge: Electric charge in Coulombs
    - spin: Spin quantum number
    - position: Current position in 3D space
    - velocity: Current velocity in 3D space
}

pub gen Particle {
    has mass: f64
    has charge: f64
    has spin: Spin
    has position: Position
    has velocity: Velocity
}

// ═══════════════════════════════════════════════════════════════════════════════
// SPECIFIC PARTICLES
// ═══════════════════════════════════════════════════════════════════════════════

docs {
    Electron - fundamental lepton with negative charge.

    Properties:
    - Mass: 9.109e-31 kg
    - Charge: -1.602e-19 C
    - Spin: 1/2 (fermion)
}

pub gen Electron is Particle {
    has lepton_number: i8
    has generation: u8
}

docs {
    Proton - composite baryon with positive charge.

    Properties:
    - Mass: 1.673e-27 kg
    - Charge: +1.602e-19 C
    - Spin: 1/2 (fermion)
    - Composition: 2 up quarks, 1 down quark
}

pub gen Proton is Particle {
    has baryon_number: i8
    has up_quarks: u8
    has down_quarks: u8
}

docs {
    Neutron - electrically neutral baryon.

    Properties:
    - Mass: 1.675e-27 kg
    - Charge: 0 C
    - Spin: 1/2 (fermion)
    - Composition: 1 up quark, 2 down quarks
}

pub gen Neutron is Particle {
    has baryon_number: i8
    has up_quarks: u8
    has down_quarks: u8
    has is_stable: bool
}

docs {
    Photon - massless gauge boson carrying electromagnetic force.

    Properties:
    - Mass: 0 kg
    - Charge: 0 C
    - Spin: 1 (boson)
    - Always travels at speed of light
}

pub gen Photon {
    has wavelength: f64
    has frequency: f64
    has energy: f64
    has polarization: f64
    has direction: Velocity
}

docs {
    Neutrino - nearly massless lepton with no charge.
}

pub gen Neutrino is Particle {
    has lepton_number: i8
    has flavor: string
    has is_antineutrino: bool
}

// ═══════════════════════════════════════════════════════════════════════════════
// TRAITS
// ═══════════════════════════════════════════════════════════════════════════════

docs {
    Fermionic particles obey the Pauli exclusion principle.
    They have half-integer spin (1/2, 3/2, 5/2, ...).
}

pub trait Fermionic {
    docs {
        Check if the particle has half-integer spin.
    }

    fun has_half_integer_spin() -> bool

    docs {
        Get the fermion's spin multiplicity (2s + 1).
    }

    fun spin_multiplicity() -> i32
}

docs {
    Bosonic particles do not obey Pauli exclusion.
    They have integer spin (0, 1, 2, ...).
}

pub trait Bosonic {
    docs {
        Check if the particle has integer spin.
    }

    fun has_integer_spin() -> bool

    docs {
        Check if the boson can mediate a fundamental force.
    }

    fun is_gauge_boson() -> bool
}

docs {
    Charged particles interact electromagnetically.
}

pub trait Charged {
    docs {
        Get the particle's electric charge.
    }

    fun get_charge() -> f64

    docs {
        Check if the particle is positively charged.
    }

    fun is_positive() -> bool

    docs {
        Check if the particle is negatively charged.
    }

    fun is_negative() -> bool
}

docs {
    Massive particles have rest mass.
}

pub trait Massive {
    docs {
        Get the particle's rest mass.
    }

    fun get_mass() -> f64

    docs {
        Calculate rest energy (E = mc²).
    }

    fun rest_energy() -> f64
}

// ═══════════════════════════════════════════════════════════════════════════════
// RULES (CONSERVATION LAWS)
// ═══════════════════════════════════════════════════════════════════════════════

docs {
    Conservation of electric charge in all interactions.
    Total charge before = total charge after.
}

pub rule conservation_of_charge {
    each interaction {
        sum(initial.charges) == sum(final.charges)
    }
}

docs {
    Conservation of baryon number in standard model processes.
}

pub rule conservation_of_baryon_number {
    each interaction {
        sum(initial.baryon_numbers) == sum(final.baryon_numbers)
    }
}

docs {
    Conservation of lepton number (per generation in SM).
}

pub rule conservation_of_lepton_number {
    each interaction {
        sum(initial.lepton_numbers) == sum(final.lepton_numbers)
    }
}

docs {
    Conservation of energy-momentum in all interactions.
}

pub rule conservation_of_four_momentum {
    each interaction {
        initial.energy + initial.momentum == final.energy + final.momentum
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// PURE FUNCTIONS
// ═══════════════════════════════════════════════════════════════════════════════

docs {
    Create a new electron at the origin at rest.
}

pub fun new_electron() -> Electron {
    return Electron {
        mass: ELECTRON_MASS,
        charge: -ELEMENTARY_CHARGE,
        spin: Spin { spin_twice: 1 },
        position: Position { x: 0.0, y: 0.0, z: 0.0 },
        velocity: Velocity { vx: 0.0, vy: 0.0, vz: 0.0 },
        lepton_number: 1,
        generation: 1
    }
}

docs {
    Create a new proton at the origin at rest.
}

pub fun new_proton() -> Proton {
    return Proton {
        mass: PROTON_MASS,
        charge: ELEMENTARY_CHARGE,
        spin: Spin { spin_twice: 1 },
        position: Position { x: 0.0, y: 0.0, z: 0.0 },
        velocity: Velocity { vx: 0.0, vy: 0.0, vz: 0.0 },
        baryon_number: 1,
        up_quarks: 2,
        down_quarks: 1
    }
}

docs {
    Create a new neutron at the origin at rest.
}

pub fun new_neutron() -> Neutron {
    return Neutron {
        mass: NEUTRON_MASS,
        charge: 0.0,
        spin: Spin { spin_twice: 1 },
        position: Position { x: 0.0, y: 0.0, z: 0.0 },
        velocity: Velocity { vx: 0.0, vy: 0.0, vz: 0.0 },
        baryon_number: 1,
        up_quarks: 1,
        down_quarks: 2,
        is_stable: false
    }
}

docs {
    Create a photon with specified wavelength.
    Automatically calculates frequency and energy.
}

pub fun new_photon(wavelength: f64) -> Photon {
    let frequency = SPEED_OF_LIGHT / wavelength
    let energy = 6.62607015e-34 * frequency

    return Photon {
        wavelength: wavelength,
        frequency: frequency,
        energy: energy,
        polarization: 0.0,
        direction: Velocity { vx: SPEED_OF_LIGHT, vy: 0.0, vz: 0.0 }
    }
}

docs {
    Calculate the magnitude of velocity (speed).
}

pub fun speed(v: Velocity) -> f64 {
    return (v.vx * v.vx + v.vy * v.vy + v.vz * v.vz)
}

docs {
    Calculate the classical momentum of a particle.
    p = m * v
}

pub fun calculate_momentum(p: Particle) -> Momentum {
    return Momentum {
        px: p.mass * p.velocity.vx,
        py: p.mass * p.velocity.vy,
        pz: p.mass * p.velocity.vz
    }
}

docs {
    Calculate the magnitude of momentum.
}

pub fun momentum_magnitude(m: Momentum) -> f64 {
    return (m.px * m.px + m.py * m.py + m.pz * m.pz)
}

docs {
    Calculate the classical kinetic energy of a particle.
    KE = (1/2) * m * v²
}

pub fun kinetic_energy(p: Particle) -> f64 {
    let v_squared = p.velocity.vx * p.velocity.vx
                  + p.velocity.vy * p.velocity.vy
                  + p.velocity.vz * p.velocity.vz
    return 0.5 * p.mass * v_squared
}

docs {
    Calculate the rest energy of a particle.
    E₀ = m * c²
}

pub fun rest_energy(p: Particle) -> f64 {
    return p.mass * SPEED_OF_LIGHT * SPEED_OF_LIGHT
}

docs {
    Calculate the relativistic total energy.
    E² = (pc)² + (mc²)²
}

pub fun relativistic_energy(p: Particle) -> f64 {
    let rest_e = rest_energy(p)
    let mom = calculate_momentum(p)
    let mom_mag = momentum_magnitude(mom)
    let pc = mom_mag * SPEED_OF_LIGHT

    return (pc * pc + rest_e * rest_e)
}

docs {
    Calculate the Lorentz factor (gamma) for a particle.
    γ = 1 / sqrt(1 - v²/c²)
}

pub fun lorentz_factor(p: Particle) -> f64 {
    let v_squared = p.velocity.vx * p.velocity.vx
                  + p.velocity.vy * p.velocity.vy
                  + p.velocity.vz * p.velocity.vz
    let beta_squared = v_squared / (SPEED_OF_LIGHT * SPEED_OF_LIGHT)

    if beta_squared >= 1.0 {
        return 1.0e308
    }

    return 1.0 / (1.0 - beta_squared)
}

docs {
    Calculate the de Broglie wavelength of a particle.
    λ = h / p
}

pub fun de_broglie_wavelength(p: Particle) -> f64 {
    let mom = calculate_momentum(p)
    let mom_mag = momentum_magnitude(mom)

    if mom_mag == 0.0 {
        return 1.0e308
    }

    return 6.62607015e-34 / mom_mag
}

docs {
    Update particle position based on velocity over time dt.
}

pub fun update_position(p: Particle, dt: f64) -> Particle {
    return Particle {
        mass: p.mass,
        charge: p.charge,
        spin: p.spin,
        position: Position {
            x: p.position.x + p.velocity.vx * dt,
            y: p.position.y + p.velocity.vy * dt,
            z: p.position.z + p.velocity.vz * dt
        },
        velocity: p.velocity
    }
}

docs {
    Calculate the distance between two particles.
}

pub fun distance(p1: Particle, p2: Particle) -> f64 {
    let dx = p2.position.x - p1.position.x
    let dy = p2.position.y - p1.position.y
    let dz = p2.position.z - p1.position.z

    return (dx * dx + dy * dy + dz * dz)
}

docs {
    Calculate the Coulomb force magnitude between two charged particles.
    F = k * |q1 * q2| / r²
}

pub fun coulomb_force(p1: Particle, p2: Particle) -> f64 {
    let k = 8.9875517923e9
    let r = distance(p1, p2)

    if r == 0.0 {
        return 1.0e308
    }

    let q_product = p1.charge * p2.charge
    if q_product < 0.0 {
        return -k * (-q_product) / (r * r)
    } else {
        return k * q_product / (r * r)
    }
}

docs {
    Check if a particle is relativistic (v > 0.1c).
}

pub fun is_relativistic(p: Particle) -> bool {
    let v_squared = p.velocity.vx * p.velocity.vx
                  + p.velocity.vy * p.velocity.vy
                  + p.velocity.vz * p.velocity.vz
    let threshold = 0.01 * SPEED_OF_LIGHT * SPEED_OF_LIGHT

    return v_squared > threshold
}

docs {
    Check if two particles can annihilate (particle-antiparticle pair).
}

pub fun can_annihilate(p1: Particle, p2: Particle) -> bool {
    let same_mass = (p1.mass - p2.mass) < 1.0e-35 && (p1.mass - p2.mass) > -1.0e-35
    let opposite_charge = (p1.charge + p2.charge) < 1.0e-25 && (p1.charge + p2.charge) > -1.0e-25

    return same_mass && opposite_charge && p1.charge != 0.0
}

// ═══════════════════════════════════════════════════════════════════════════════
// EVOLUTION
// ═══════════════════════════════════════════════════════════════════════════════

docs {
    Evolution adding antimatter support to the particle system.
}

evo particle_antimatter @ 0.9.1 > 0.9.0 {
    adds gen Positron is Particle { lepton_number: i8 }
    adds gen Antiproton is Particle { baryon_number: i8 }
    adds fun create_positron() -> Positron
    adds fun annihilation_energy(p1: Particle, p2: Particle) -> f64
    because "antimatter modeling required for high-energy physics"
}