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
//! Effect tracking and propagation
//!
//! This module tracks side effects through the DOL AST, allowing the linter
//! to detect purity violations and enforce sex context rules.

use crate::ast::{Block, Declaration, Expr, Gen, Purity, Span, Statement, Stmt, Trait};
use std::collections::{HashMap, HashSet};

/// The kind of side effect being tracked.
///
/// Different kinds of effects may have different severity levels
/// and linting rules.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum EffectKind {
    /// I/O operations (file, network, console)
    Io,
    /// Foreign Function Interface calls
    Ffi,
    /// Mutable global state access
    MutableGlobal,
    /// Non-deterministic operations (random, time)
    NonDeterministic,
    /// Unsafe operations
    Unsafe,
    /// General side effect (marked with 'sex' keyword)
    General,
}

impl EffectKind {
    /// Returns a human-readable description of this effect kind.
    pub fn description(&self) -> &'static str {
        match self {
            EffectKind::Io => "I/O operation",
            EffectKind::Ffi => "FFI call",
            EffectKind::MutableGlobal => "mutable global state",
            EffectKind::NonDeterministic => "non-deterministic operation",
            EffectKind::Unsafe => "unsafe operation",
            EffectKind::General => "side effect",
        }
    }
}

impl std::fmt::Display for EffectKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.description())
    }
}

/// A tracked side effect occurrence.
///
/// Records where an effect occurs in the source code and what kind of effect it is.
#[derive(Debug, Clone, PartialEq)]
pub struct Effect {
    /// The kind of effect
    pub kind: EffectKind,
    /// Location in source code
    pub span: Span,
    /// Optional description or context
    pub context: Option<String>,
}

impl Effect {
    /// Create a new effect.
    ///
    /// # Arguments
    ///
    /// * `kind` - The kind of effect
    /// * `span` - Source location
    ///
    /// # Example
    ///
    /// ```rust
    /// use metadol::sex::tracking::{Effect, EffectKind};
    /// use metadol::ast::Span;
    ///
    /// let effect = Effect::new(EffectKind::Io, Span::default());
    /// assert_eq!(effect.kind, EffectKind::Io);
    /// ```
    pub fn new(kind: EffectKind, span: Span) -> Self {
        Self {
            kind,
            span,
            context: None,
        }
    }

    /// Create a new effect with context.
    ///
    /// # Arguments
    ///
    /// * `kind` - The kind of effect
    /// * `span` - Source location
    /// * `context` - Additional context or description
    pub fn with_context(kind: EffectKind, span: Span, context: String) -> Self {
        Self {
            kind,
            span,
            context: Some(context),
        }
    }
}

/// Tracks side effects in DOL code.
///
/// The effect tracker analyzes DOL declarations and expressions to identify
/// all side effects, building a map of which declarations and functions
/// perform which effects.
///
/// # Example
///
/// ```rust
/// use metadol::sex::tracking::EffectTracker;
/// use metadol::ast::{Declaration, Gen, Span, Visibility};
///
/// let mut tracker = EffectTracker::new();
///
/// let gene = Gen {
///     name: "test.gene".to_string(),
///     extends: None,
///     statements: vec![],
///     exegesis: "Test".to_string(),
///     span: Span::default(),
///     visibility: Visibility::default(),
/// };
///
/// tracker.track_declaration(&Declaration::Gene(gene));
/// ```
#[derive(Debug, Default)]
pub struct EffectTracker {
    /// Effects found in each declaration (by name)
    declaration_effects: HashMap<String, Vec<Effect>>,
    /// Set of declarations that perform effects
    effectful_declarations: HashSet<String>,
    /// Purity annotations for declarations
    purity_map: HashMap<String, Purity>,
}

impl EffectTracker {
    /// Create a new effect tracker.
    pub fn new() -> Self {
        Self::default()
    }

    /// Track effects in a declaration.
    ///
    /// Analyzes the declaration and records any side effects found.
    ///
    /// # Arguments
    ///
    /// * `decl` - The declaration to analyze
    pub fn track_declaration(&mut self, decl: &Declaration) {
        let name = decl.name().to_string();
        let mut effects = Vec::new();

        match decl {
            Declaration::Gene(gene) => {
                self.track_gene(gene, &mut effects);
            }
            Declaration::Trait(trait_decl) => {
                self.track_trait(trait_decl, &mut effects);
            }
            _ => {}
        }

        if !effects.is_empty() {
            self.effectful_declarations.insert(name.clone());
            self.declaration_effects.insert(name, effects);
        }
    }

    /// Track effects in a gene.
    fn track_gene(&mut self, gene: &Gen, effects: &mut Vec<Effect>) {
        for statement in &gene.statements {
            self.track_statement(statement, effects);
        }
    }

    /// Track effects in a trait.
    fn track_trait(&mut self, trait_decl: &Trait, effects: &mut Vec<Effect>) {
        for statement in &trait_decl.statements {
            self.track_statement(statement, effects);
        }
    }

    /// Track effects in a statement.
    ///
    /// Identifies common patterns that indicate side effects.
    fn track_statement(&mut self, statement: &Statement, effects: &mut Vec<Effect>) {
        if let Statement::Has { property, span, .. } = statement {
            // Check for I/O-related properties
            if self.is_io_property(property) {
                effects.push(Effect::with_context(
                    EffectKind::Io,
                    *span,
                    format!("property '{}'", property),
                ));
            }
            // Check for FFI-related properties
            if self.is_ffi_property(property) {
                effects.push(Effect::with_context(
                    EffectKind::Ffi,
                    *span,
                    format!("property '{}'", property),
                ));
            }
            // Check for mutable global properties
            if self.is_global_property(property) {
                effects.push(Effect::with_context(
                    EffectKind::MutableGlobal,
                    *span,
                    format!("property '{}'", property),
                ));
            }
        }
    }

    /// Check if a property name suggests I/O operations.
    fn is_io_property(&self, property: &str) -> bool {
        let io_keywords = [
            "input", "output", "read", "write", "file", "network", "socket", "stream", "console",
            "stdout", "stderr", "stdin",
        ];
        io_keywords
            .iter()
            .any(|keyword| property.to_lowercase().contains(keyword))
    }

    /// Check if a property name suggests FFI operations.
    fn is_ffi_property(&self, property: &str) -> bool {
        let ffi_keywords = ["ffi", "extern", "native", "syscall", "foreign"];
        ffi_keywords
            .iter()
            .any(|keyword| property.to_lowercase().contains(keyword))
    }

    /// Check if a property name suggests global state.
    fn is_global_property(&self, property: &str) -> bool {
        let global_keywords = ["global", "static", "singleton", "shared"];
        global_keywords
            .iter()
            .any(|keyword| property.to_lowercase().contains(keyword))
    }

    /// Track effects in an expression.
    ///
    /// # Arguments
    ///
    /// * `expr` - The expression to analyze
    /// * `effects` - Vector to accumulate effects into
    pub fn track_expr(&mut self, expr: &Expr, effects: &mut Vec<Effect>) {
        match expr {
            Expr::Binary { left, right, .. } => {
                self.track_expr(left, effects);
                self.track_expr(right, effects);
            }
            Expr::Unary { operand, .. } => {
                self.track_expr(operand, effects);
            }
            Expr::Call { callee, args, .. } => {
                self.track_expr(callee, effects);
                for arg in args {
                    self.track_expr(arg, effects);
                }
            }
            Expr::Lambda { body, .. } => {
                self.track_expr(body, effects);
            }
            Expr::If {
                condition,
                then_branch,
                else_branch,
                ..
            } => {
                self.track_expr(condition, effects);
                self.track_expr(then_branch, effects);
                if let Some(else_expr) = else_branch {
                    self.track_expr(else_expr, effects);
                }
            }
            Expr::Block(Block {
                statements,
                final_expr,
                ..
            }) => {
                for stmt in statements {
                    self.track_stmt(stmt, effects);
                }
                if let Some(expr) = final_expr {
                    self.track_expr(expr, effects);
                }
            }
            _ => {}
        }
    }

    /// Track effects in a statement.
    fn track_stmt(&mut self, stmt: &Stmt, effects: &mut Vec<Effect>) {
        match stmt {
            Stmt::Let { value, .. } => {
                self.track_expr(value, effects);
            }
            Stmt::Assign { target, value } => {
                self.track_expr(target, effects);
                self.track_expr(value, effects);
            }
            Stmt::For { iterable, body, .. } => {
                self.track_expr(iterable, effects);
                for s in body {
                    self.track_stmt(s, effects);
                }
            }
            Stmt::While { condition, body } => {
                self.track_expr(condition, effects);
                for s in body {
                    self.track_stmt(s, effects);
                }
            }
            Stmt::Loop { body } => {
                for s in body {
                    self.track_stmt(s, effects);
                }
            }
            Stmt::Return(Some(expr)) => {
                self.track_expr(expr, effects);
            }
            Stmt::Expr(expr) => {
                self.track_expr(expr, effects);
            }
            _ => {}
        }
    }

    /// Check if a declaration has effects.
    ///
    /// # Arguments
    ///
    /// * `name` - The declaration name
    ///
    /// # Returns
    ///
    /// `true` if the declaration has recorded effects
    pub fn has_effects(&self, name: &str) -> bool {
        self.effectful_declarations.contains(name)
    }

    /// Get the effects for a declaration.
    ///
    /// # Arguments
    ///
    /// * `name` - The declaration name
    ///
    /// # Returns
    ///
    /// A slice of effects, or an empty slice if none were found
    pub fn get_effects(&self, name: &str) -> &[Effect] {
        self.declaration_effects
            .get(name)
            .map(|v| v.as_slice())
            .unwrap_or(&[])
    }

    /// Set the purity annotation for a declaration.
    ///
    /// # Arguments
    ///
    /// * `name` - The declaration name
    /// * `purity` - The purity level
    pub fn set_purity(&mut self, name: String, purity: Purity) {
        self.purity_map.insert(name, purity);
    }

    /// Get the purity annotation for a declaration.
    ///
    /// # Arguments
    ///
    /// * `name` - The declaration name
    ///
    /// # Returns
    ///
    /// The purity level, or `None` if not annotated
    pub fn get_purity(&self, name: &str) -> Option<Purity> {
        self.purity_map.get(name).copied()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ast::Visibility;

    #[test]
    fn test_effect_kind_display() {
        assert_eq!(EffectKind::Io.to_string(), "I/O operation");
        assert_eq!(EffectKind::Ffi.to_string(), "FFI call");
    }

    #[test]
    fn test_effect_creation() {
        let effect = Effect::new(EffectKind::Io, Span::default());
        assert_eq!(effect.kind, EffectKind::Io);
        assert!(effect.context.is_none());

        let effect_with_ctx =
            Effect::with_context(EffectKind::Ffi, Span::default(), "test".to_string());
        assert_eq!(effect_with_ctx.context, Some("test".to_string()));
    }

    #[test]
    fn test_effect_tracker_io_detection() {
        let tracker = EffectTracker::new();
        assert!(tracker.is_io_property("file_read"));
        assert!(tracker.is_io_property("network_socket"));
        assert!(!tracker.is_io_property("counter"));
    }

    #[test]
    fn test_effect_tracker_ffi_detection() {
        let tracker = EffectTracker::new();
        assert!(tracker.is_ffi_property("ffi_call"));
        assert!(tracker.is_ffi_property("extern_func"));
        assert!(!tracker.is_ffi_property("normal_func"));
    }

    #[test]
    fn test_effect_tracker_global_detection() {
        let tracker = EffectTracker::new();
        assert!(tracker.is_global_property("global_state"));
        assert!(tracker.is_global_property("static_var"));
        assert!(!tracker.is_global_property("local_var"));
    }

    #[test]
    fn test_effect_tracker_gene_tracking() {
        let mut tracker = EffectTracker::new();

        let gene = Gen {
            visibility: Visibility::default(),
            name: "io.gene".to_string(),
            extends: None,
            statements: vec![Statement::Has {
                subject: "io".to_string(),
                property: "file_read".to_string(),
                span: Span::default(),
            }],
            exegesis: "Test".to_string(),
            span: Span::default(),
        };

        tracker.track_declaration(&Declaration::Gene(gene));
        assert!(tracker.has_effects("io.gene"));

        let effects = tracker.get_effects("io.gene");
        assert_eq!(effects.len(), 1);
        assert_eq!(effects[0].kind, EffectKind::Io);
    }
}