lambdust 0.1.1

A Scheme dialect with gradual typing and effect systems
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
//! SRFI-9 `define-record-type` macro implementation.
//!
//! This module provides the `define-record-type` macro that expands to
//! the appropriate primitive record operations and procedure definitions.

use crate::ast::Literal;
use crate::diagnostics::{Error, Result};
use crate::eval::value::{Value, ThreadSafeEnvironment};
use crate::macro_system::SyntaxRulesTransformer;
use crate::utils::symbol::intern_symbol;
use std::sync::Arc;

/// Installs the `define-record-type` macro into the given environment.
pub fn install_define_record_type_macro(env: &Arc<ThreadSafeEnvironment>) {
    // Create a simple macro transformer that handles define-record-type
    // For now, we'll use a procedural approach since syntax-rules might be complex
    
    // The macro will be installed as a special form that gets handled during evaluation
    // This is a simplified approach - in a full implementation, this would use syntax-rules
    
    // For demonstration, let's create a basic implementation
    env.define("define-record-type".to_string(), Value::Primitive(Arc::new(
        crate::eval::value::PrimitiveProcedure {
            name: "define-record-type".to_string(),
            arity_min: 3,
            arity_max: None,
            implementation: crate::eval::value::PrimitiveImpl::RustFn(expand_define_record_type),
            effects: vec![crate::effects::Effect::State],
        }
    )));
}

/// Expands a define-record-type form.
/// 
/// This is a simplified implementation that handles the basic case:
/// ```scheme
/// (define-record-type <type-name>
///   (constructor field1 field2 ...)
///   predicate
///   (field1 accessor1 [mutator1])
///   (field2 accessor2 [mutator2])
///   ...)
/// ```
fn expand_define_record_type(args: &[Value]) -> Result<Value> {
    if args.len() < 3 {
        return Err(Box::new(Error::runtime_error(
            "define-record-type requires at least 3 arguments".to_string(),
            None,
        )));
    }
    
    // Extract type name
    let type_name = extract_type_name(&args[0])?;
    
    // Extract constructor specification
    let (constructor_name, constructor_fields) = extract_constructor_spec(&args[1])?;
    
    // Extract predicate name
    let predicate_name = extract_predicate_name(&args[2])?;
    
    // Extract field specifications
    let field_specs = extract_field_specs(&args[3..])?;
    
    // Generate the expansion
    generate_record_type_expansion(
        type_name,
        constructor_name,
        constructor_fields,
        predicate_name,
        field_specs,
    )
}

/// Extracts the type name from a define-record-type form.
fn extract_type_name(value: &Value) -> Result<String> {
    match value {
        Value::Symbol(sym_id) => {
            if let Some(name) = crate::utils::symbol_name(*sym_id) {
                Ok(name)
            } else {
                Err(Box::new(Error::runtime_error(
                    "Invalid symbol for record type name".to_string(),
                    None,
                )))
            }
        }
        Value::Literal(Literal::String(name)) => Ok(name.clone()),
        _ => Err(Box::new(Error::runtime_error(
            "Record type name must be a symbol or string".to_string(),
            None,
        )))
    }
}

/// Extracts constructor specification from a define-record-type form.
fn extract_constructor_spec(value: &Value) -> Result<(String, Vec<String>)> {
    let constructor_list = value.as_list().ok_or_else(|| {
        Error::runtime_error(
            "Constructor specification must be a list".to_string(),
            None,
        )
    })?;
    
    if constructor_list.is_empty() {
        return Err(Box::new(Error::runtime_error(
            "Constructor specification cannot be empty".to_string(),
            None,
        )));
    }
    
    // First element is constructor name
    let constructor_name = match &constructor_list[0] {
        Value::Symbol(sym_id) => {
            if let Some(name) = crate::utils::symbol_name(*sym_id) {
                name
            } else {
                return Err(Box::new(Error::runtime_error(
                    "Invalid symbol for constructor name".to_string(),
                    None,
                )));
            }
        }
        _ => return Err(Box::new(Error::runtime_error(
            "Constructor name must be a symbol".to_string(),
            None,
        )))
    };
    
    // Remaining elements are field names
    let mut field_names = Vec::new();
    for field in &constructor_list[1..] {
        match field {
            Value::Symbol(sym_id) => {
                if let Some(name) = crate::utils::symbol_name(*sym_id) {
                    field_names.push(name);
                } else {
                    return Err(Box::new(Error::runtime_error(
                        "Invalid symbol for field name".to_string(),
                        None,
                    )));
                }
            }
            _ => return Err(Box::new(Error::runtime_error(
                "Field names must be symbols".to_string(),
                None,
            )))
        }
    }
    
    Ok((constructor_name, field_names))
}

/// Extracts predicate name from a define-record-type form.
fn extract_predicate_name(value: &Value) -> Result<String> {
    match value {
        Value::Symbol(sym_id) => {
            if let Some(name) = crate::utils::symbol_name(*sym_id) {
                Ok(name)
            } else {
                Err(Box::new(Error::runtime_error(
                    "Invalid symbol for predicate name".to_string(),
                    None,
                )))
            }
        }
        _ => Err(Box::new(Error::runtime_error(
            "Predicate name must be a symbol".to_string(),
            None,
        )))
    }
}

/// Field specification from a define-record-type form.
#[derive(Debug, Clone)]
struct FieldSpec {
    name: String,
    accessor: String,
    mutator: Option<String>,
}

/// Extracts field specifications from a define-record-type form.
fn extract_field_specs(values: &[Value]) -> Result<Vec<FieldSpec>> {
    let mut field_specs = Vec::new();
    
    for value in values {
        let field_list = value.as_list().ok_or_else(|| {
            Error::runtime_error(
                "Field specification must be a list".to_string(),
                None,
            )
        })?;
        
        if field_list.len() < 2 || field_list.len() > 3 {
            return Err(Box::new(Error::runtime_error(
                "Field specification must have 2 or 3 elements: (field accessor [mutator])".to_string(),
                None,
            )));
        }
        
        // Extract field name
        let field_name = match &field_list[0] {
            Value::Symbol(sym_id) => {
                if let Some(name) = crate::utils::symbol_name(*sym_id) {
                    name
                } else {
                    return Err(Box::new(Error::runtime_error(
                        "Invalid symbol for field name".to_string(),
                        None,
                    )));
                }
            }
            _ => return Err(Box::new(Error::runtime_error(
                "Field name must be a symbol".to_string(),
                None,
            )))
        };
        
        // Extract accessor name
        let accessor_name = match &field_list[1] {
            Value::Symbol(sym_id) => {
                if let Some(name) = crate::utils::symbol_name(*sym_id) {
                    name
                } else {
                    return Err(Box::new(Error::runtime_error(
                        "Invalid symbol for accessor name".to_string(),
                        None,
                    )));
                }
            }
            _ => return Err(Box::new(Error::runtime_error(
                "Accessor name must be a symbol".to_string(),
                None,
            )))
        };
        
        // Extract optional mutator name
        let mutator_name = if field_list.len() == 3 {
            match &field_list[2] {
                Value::Symbol(sym_id) => {
                    if let Some(name) = crate::utils::symbol_name(*sym_id) {
                        Some(name)
                    } else {
                        return Err(Box::new(Error::runtime_error(
                            "Invalid symbol for mutator name".to_string(),
                            None,
                        )));
                    }
                }
                _ => return Err(Box::new(Error::runtime_error(
                    "Mutator name must be a symbol".to_string(),
                    None,
                )))
            }
        } else {
            None
        };
        
        field_specs.push(FieldSpec {
            name: field_name,
            accessor: accessor_name,
            mutator: mutator_name,
        });
    }
    
    Ok(field_specs)
}

/// Generates the expanded code for a define-record-type form.
fn generate_record_type_expansion(
    type_name: String,
    constructor_name: String,
    constructor_fields: Vec<String>,
    predicate_name: String,
    field_specs: Vec<FieldSpec>,
) -> Result<Value> {
    // This is a simplified implementation that returns a begin form
    // In a real implementation, this would generate proper Scheme code
    
    // For now, we'll create a simple procedure that sets up the record type
    // This is a placeholder - the actual implementation would need to integrate
    // with the macro system properly
    
    // Create a quoted list representing the expanded form
    let expansion = Value::list(vec![
        Value::symbol(intern_symbol("begin".to_string())),
        
        // Create the record type
        Value::list(vec![
            Value::symbol(intern_symbol("define".to_string())),
            Value::symbol(intern_symbol(format!("%{type_name}-type"))),
            Value::list(vec![
                Value::symbol(intern_symbol("make-record-type".to_string())),
                Value::string(&type_name),
                Value::list(constructor_fields.iter()
                    .map(Value::string)
                    .collect()),
            ]),
        ]),
        
        // Create the constructor
        Value::list(vec![
            Value::symbol(intern_symbol("define".to_string())),
            Value::symbol(intern_symbol(constructor_name)),
            Value::list(vec![
                Value::symbol(intern_symbol("record-constructor".to_string())),
                Value::symbol(intern_symbol(format!("%{type_name}-type"))),
            ]),
        ]),
        
        // Create the predicate
        Value::list(vec![
            Value::symbol(intern_symbol("define".to_string())),
            Value::symbol(intern_symbol(predicate_name)),
            Value::list(vec![
                Value::symbol(intern_symbol("record-predicate".to_string())),
                Value::symbol(intern_symbol(format!("%{type_name}-type"))),
            ]),
        ]),
    ]);
    
    // Add field accessors and mutators
    let mut expanded_forms = expansion.as_list().unwrap_or_default();
    
    for field_spec in field_specs {
        // Add accessor
        expanded_forms.push(Value::list(vec![
            Value::symbol(intern_symbol("define".to_string())),
            Value::symbol(intern_symbol(field_spec.accessor)),
            Value::list(vec![
                Value::symbol(intern_symbol("record-accessor".to_string())),
                Value::symbol(intern_symbol(format!("%{type_name}-type"))),
                Value::string(&field_spec.name),
            ]),
        ]));
        
        // Add mutator if specified
        if let Some(mutator_name) = field_spec.mutator {
            expanded_forms.push(Value::list(vec![
                Value::symbol(intern_symbol("define".to_string())),
                Value::symbol(intern_symbol(mutator_name)),
                Value::list(vec![
                    Value::symbol(intern_symbol("record-mutator".to_string())),
                    Value::symbol(intern_symbol(format!("%{type_name}-type"))),
                    Value::string(&field_spec.name),
                ]),
            ]));
        }
    }
    
    Ok(Value::list(expanded_forms))
}

/// Creates a syntax-rules based implementation of define-record-type.
/// This is the proper way to implement it, but requires more macro system integration.
pub fn create_define_record_type_syntax_rules() -> SyntaxRulesTransformer {
    // This would be a proper syntax-rules implementation
    // For now, we use the procedural approach above
    todo!("Full syntax-rules implementation needs more macro system work")
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::utils::symbol::intern_symbol;

    #[test]
    fn test_type_name_extraction() {
        let symbol_val = Value::symbol(intern_symbol("point".to_string()));
        let result = extract_type_name(&symbol_val).unwrap();
        assert_eq!(result, "point");
        
        let string_val = Value::string("person");
        let result = extract_type_name(&string_val).unwrap();
        assert_eq!(result, "person");
    }
    
    #[test]
    fn test_constructor_spec_extraction() {
        let constructor_spec = Value::list(vec![
            Value::symbol(intern_symbol("make-point".to_string())),
            Value::symbol(intern_symbol("x".to_string())),
            Value::symbol(intern_symbol("y".to_string())),
        ]);
        
        let (name, fields) = extract_constructor_spec(&constructor_spec).unwrap();
        assert_eq!(name, "make-point");
        assert_eq!(fields, vec!["x", "y"]);
    }
    
    #[test]
    fn test_predicate_name_extraction() {
        let predicate_val = Value::symbol(intern_symbol("point?".to_string()));
        let result = extract_predicate_name(&predicate_val).unwrap();
        assert_eq!(result, "point?");
    }
    
    #[test]
    fn test_field_specs_extraction() {
        let field_specs = vec![
            Value::list(vec![
                Value::symbol(intern_symbol("x".to_string())),
                Value::symbol(intern_symbol("point-x".to_string())),
                Value::symbol(intern_symbol("point-x-set!".to_string())),
            ]),
            Value::list(vec![
                Value::symbol(intern_symbol("y".to_string())),
                Value::symbol(intern_symbol("point-y".to_string())),
            ]),
        ];
        
        let specs = extract_field_specs(&field_specs).unwrap();
        assert_eq!(specs.len(), 2);
        
        assert_eq!(specs[0].name, "x");
        assert_eq!(specs[0].accessor, "point-x");
        assert_eq!(specs[0].mutator, Some("point-x-set!".to_string()));
        
        assert_eq!(specs[1].name, "y");
        assert_eq!(specs[1].accessor, "point-y");
        assert_eq!(specs[1].mutator, None);
    }
}