sim-lib-standard-core 0.1.0

SIM workspace package for sim lib standard core.
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
//! The `LanguageProfile` model: organ uses, badges, and profile metadata.

use sim_kernel::{CapabilityName, Expr, Ref, Symbol};

use crate::fidelity::{FidelityBadge, expr_kind, symbol_from_expr};

/// One organ a profile uses, with its configuration options.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct OrganUse {
    /// Symbol of the organ being used.
    pub organ: Symbol,
    /// Key/value options configuring the organ.
    pub options: Vec<(Symbol, Expr)>,
}

impl OrganUse {
    /// Use `organ` with no options.
    pub fn new(organ: Symbol) -> Self {
        Self {
            organ,
            options: Vec::new(),
        }
    }

    /// Add an option `key`/`value` pair.
    pub fn with_option(mut self, key: Symbol, value: Expr) -> Self {
        self.options.push((key, value));
        self
    }

    /// Encode this organ use as an expression (organ symbol plus option map).
    pub fn to_expr(&self) -> Expr {
        Expr::List(vec![
            Expr::Symbol(self.organ.clone()),
            Expr::Map(
                self.options
                    .iter()
                    .map(|(key, value)| (Expr::Symbol(key.clone()), value.clone()))
                    .collect(),
            ),
        ])
    }

    /// Decode an organ use from its [`OrganUse::to_expr`] encoding.
    pub fn from_expr(expr: &Expr) -> sim_kernel::Result<Self> {
        let Expr::List(items) = expr else {
            return Err(sim_kernel::Error::TypeMismatch {
                expected: "organ-use list",
                found: expr_kind(expr),
            });
        };
        let [organ, options] = items.as_slice() else {
            return Err(sim_kernel::Error::Eval(
                "organ use expects organ symbol and option map".to_owned(),
            ));
        };
        let Expr::Map(entries) = options else {
            return Err(sim_kernel::Error::TypeMismatch {
                expected: "organ-use option map",
                found: expr_kind(options),
            });
        };
        Ok(Self {
            organ: symbol_from_expr(organ, "organ symbol")?,
            options: entries
                .iter()
                .map(|(key, value)| Ok((symbol_from_expr(key, "option symbol")?, value.clone())))
                .collect::<sim_kernel::Result<Vec<_>>>()?,
        })
    }
}

/// A language profile: the reader, lowering, eval-policy, organs, and metadata
/// that present one surface language over the shared `Expr` graph.
///
/// Profiles are the unit the standard distribution installs, diffs, and tests;
/// the per-language `sim-lib-lang-*` crates build one each.
///
/// # Examples
///
/// ```
/// use sim_kernel::Symbol;
/// use sim_lib_standard_core::{LanguageProfile, OrganUse, standard_control_organ_symbol};
///
/// let profile = LanguageProfile::new(Symbol::qualified("lang", "demo/v1"))
///     .with_reader(Symbol::qualified("codec", "lisp"))
///     .with_eval_policy(Symbol::qualified("eval", "default"))
///     .with_organ(OrganUse::new(standard_control_organ_symbol()));
///
/// assert_eq!(profile.reader, Symbol::qualified("codec", "lisp"));
/// assert_eq!(profile.organs.len(), 1);
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct LanguageProfile {
    /// Symbol naming the profile.
    pub symbol: Symbol,
    /// Reader (codec) symbol the profile parses with.
    pub reader: Symbol,
    /// Lowering symbol mapping surface forms onto the shared graph.
    pub lowering: Symbol,
    /// Eval-policy symbol the profile evaluates under.
    pub eval_policy: Symbol,
    /// Organs the profile uses.
    pub organs: Vec<OrganUse>,
    /// Optional numeric tower symbol.
    pub numeric_tower: Option<Symbol>,
    /// Capabilities the profile requires.
    pub capabilities: Vec<CapabilityName>,
    /// Surface forms the profile does not support.
    pub unsupported_forms: Vec<Symbol>,
    /// Conformance tests covering the profile.
    pub conformance_tests: Vec<Symbol>,
    /// Fidelity badges declared for the profile.
    pub fidelity_badges: Vec<FidelityBadge>,
}

impl LanguageProfile {
    /// Start a profile named `symbol` with unspecified reader/lowering/eval-policy
    /// and no organs.
    pub fn new(symbol: Symbol) -> Self {
        Self {
            symbol,
            reader: unspecified_symbol("reader"),
            lowering: unspecified_symbol("lowering"),
            eval_policy: unspecified_symbol("eval-policy"),
            organs: Vec::new(),
            numeric_tower: None,
            capabilities: Vec::new(),
            unsupported_forms: Vec::new(),
            conformance_tests: Vec::new(),
            fidelity_badges: Vec::new(),
        }
    }

    /// Set the reader symbol.
    pub fn with_reader(mut self, reader: Symbol) -> Self {
        self.reader = reader;
        self
    }

    /// Set the lowering symbol.
    pub fn with_lowering(mut self, lowering: Symbol) -> Self {
        self.lowering = lowering;
        self
    }

    /// Set the eval-policy symbol.
    pub fn with_eval_policy(mut self, eval_policy: Symbol) -> Self {
        self.eval_policy = eval_policy;
        self
    }

    /// Add an organ use.
    pub fn with_organ(mut self, organ: OrganUse) -> Self {
        self.organs.push(organ);
        self
    }

    /// Set the numeric tower symbol.
    pub fn with_numeric_tower(mut self, numeric_tower: Symbol) -> Self {
        self.numeric_tower = Some(numeric_tower);
        self
    }

    /// Add a required capability.
    pub fn requiring(mut self, capability: CapabilityName) -> Self {
        self.capabilities.push(capability);
        self
    }

    /// Add an unsupported surface form.
    pub fn with_unsupported_form(mut self, form: Symbol) -> Self {
        self.unsupported_forms.push(form);
        self
    }

    /// Add a conformance test.
    pub fn with_conformance_test(mut self, test: Symbol) -> Self {
        self.conformance_tests.push(test);
        self
    }

    /// Add a fidelity badge.
    pub fn with_fidelity_badge(mut self, badge: FidelityBadge) -> Self {
        self.fidelity_badges.push(badge);
        self
    }

    /// Encode this profile as constructor arguments for the `standard/Profile` class.
    pub fn to_constructor_args(&self) -> Vec<Expr> {
        vec![
            Expr::Symbol(self.symbol.clone()),
            Expr::Symbol(self.reader.clone()),
            Expr::Symbol(self.lowering.clone()),
            Expr::Symbol(self.eval_policy.clone()),
            Expr::List(self.organs.iter().map(OrganUse::to_expr).collect()),
            self.numeric_tower
                .clone()
                .map(Expr::Symbol)
                .unwrap_or(Expr::Nil),
            Expr::List(
                self.capabilities
                    .iter()
                    .map(|capability| Expr::String(capability.as_str().to_owned()))
                    .collect(),
            ),
            Expr::List(
                self.unsupported_forms
                    .iter()
                    .cloned()
                    .map(Expr::Symbol)
                    .collect(),
            ),
            Expr::List(
                self.conformance_tests
                    .iter()
                    .cloned()
                    .map(Expr::Symbol)
                    .collect(),
            ),
            Expr::List(
                self.fidelity_badges
                    .iter()
                    .map(|badge| Expr::Call {
                        operator: Box::new(Expr::Symbol(crate::fidelity_badge_class_symbol())),
                        args: badge.to_constructor_args(),
                    })
                    .collect(),
            ),
        ]
    }

    /// Decode a profile from `standard/Profile` constructor arguments.
    pub fn from_constructor_args(args: Vec<Expr>) -> sim_kernel::Result<Self> {
        let [
            symbol,
            reader,
            lowering,
            eval_policy,
            organs,
            numeric_tower,
            capabilities,
            unsupported_forms,
            conformance_tests,
            fidelity_badges,
        ] = args.as_slice()
        else {
            return Err(sim_kernel::Error::Eval(
                "standard/Profile expects ten constructor arguments".to_owned(),
            ));
        };

        Ok(Self {
            symbol: symbol_from_expr(symbol, "profile symbol")?,
            reader: symbol_from_expr(reader, "reader symbol")?,
            lowering: symbol_from_expr(lowering, "lowering symbol")?,
            eval_policy: symbol_from_expr(eval_policy, "eval policy symbol")?,
            organs: organ_uses_from_expr(organs)?,
            numeric_tower: optional_symbol(numeric_tower)?,
            capabilities: capabilities_from_expr(capabilities)?,
            unsupported_forms: symbols_from_expr(unsupported_forms, "unsupported form")?,
            conformance_tests: symbols_from_expr(conformance_tests, "conformance test")?,
            fidelity_badges: badges_from_expr(fidelity_badges)?,
        })
    }
}

/// Class symbol for the `standard/Profile` runtime object.
pub fn language_profile_class_symbol() -> Symbol {
    Symbol::qualified("standard", "Profile")
}

/// Symbol naming the built-in sim-expression profile.
pub fn sim_expression_profile_symbol() -> Symbol {
    Symbol::qualified("lang", "sim-expression/v1")
}

/// The built-in sim-expression profile: the standard distribution's own surface
/// over the shared `Expr` graph (lisp reader, default eval policy, core organs).
pub fn sim_expression_profile() -> LanguageProfile {
    let profile = sim_expression_profile_symbol();
    let test = Symbol::qualified("test", "sim-expression-core");
    LanguageProfile::new(profile.clone())
        .with_reader(Symbol::qualified("codec", "lisp"))
        .with_lowering(Symbol::qualified("standard", "identity-lowering"))
        .with_eval_policy(Symbol::qualified("eval", "default"))
        .with_organ(OrganUse::new(standard_control_organ_symbol()))
        .with_organ(OrganUse::new(standard_binding_organ_symbol()))
        .with_organ(OrganUse::new(standard_sequence_organ_symbol()))
        .with_organ(OrganUse::new(standard_pattern_organ_symbol()))
        .with_numeric_tower(Symbol::qualified("numbers", "sim-expression"))
        .with_conformance_test(test.clone())
        .with_fidelity_badge(FidelityBadge::new(
            Ref::Symbol(profile),
            Symbol::qualified("standard", "host-native"),
            1,
            Ref::Symbol(test),
        ))
}

/// Symbol for the standard control organ.
pub fn standard_control_organ_symbol() -> Symbol {
    Symbol::qualified("organ", "control")
}

/// Symbol for the standard binding organ.
pub fn standard_binding_organ_symbol() -> Symbol {
    Symbol::qualified("organ", "binding")
}

/// Symbol for the standard sequence organ.
pub fn standard_sequence_organ_symbol() -> Symbol {
    Symbol::qualified("organ", "sequence")
}

/// Symbol for the standard pattern organ.
pub fn standard_pattern_organ_symbol() -> Symbol {
    Symbol::qualified("organ", "pattern")
}

fn unspecified_symbol(name: &str) -> Symbol {
    Symbol::qualified("standard/unspecified", name.to_owned())
}

fn optional_symbol(expr: &Expr) -> sim_kernel::Result<Option<Symbol>> {
    match expr {
        Expr::Nil => Ok(None),
        Expr::Symbol(symbol) => Ok(Some(symbol.clone())),
        _ => Err(sim_kernel::Error::TypeMismatch {
            expected: "optional symbol",
            found: expr_kind(expr),
        }),
    }
}

fn organ_uses_from_expr(expr: &Expr) -> sim_kernel::Result<Vec<OrganUse>> {
    let Expr::List(items) = expr else {
        return Err(sim_kernel::Error::TypeMismatch {
            expected: "organ-use list",
            found: expr_kind(expr),
        });
    };
    items.iter().map(OrganUse::from_expr).collect()
}

fn capabilities_from_expr(expr: &Expr) -> sim_kernel::Result<Vec<CapabilityName>> {
    let Expr::List(items) = expr else {
        return Err(sim_kernel::Error::TypeMismatch {
            expected: "capability list",
            found: expr_kind(expr),
        });
    };
    items
        .iter()
        .map(|item| match item {
            Expr::String(name) => Ok(CapabilityName::new(name.clone())),
            _ => Err(sim_kernel::Error::TypeMismatch {
                expected: "capability string",
                found: expr_kind(item),
            }),
        })
        .collect()
}

fn symbols_from_expr(expr: &Expr, expected: &'static str) -> sim_kernel::Result<Vec<Symbol>> {
    let Expr::List(items) = expr else {
        return Err(sim_kernel::Error::TypeMismatch {
            expected: "symbol list",
            found: expr_kind(expr),
        });
    };
    items
        .iter()
        .map(|item| symbol_from_expr(item, expected))
        .collect()
}

fn badges_from_expr(expr: &Expr) -> sim_kernel::Result<Vec<FidelityBadge>> {
    let Expr::List(items) = expr else {
        return Err(sim_kernel::Error::TypeMismatch {
            expected: "fidelity badge list",
            found: expr_kind(expr),
        });
    };
    items
        .iter()
        .map(|item| match item {
            Expr::Call { operator, args } => {
                let class = symbol_from_expr(operator, "badge class")?;
                if class != crate::fidelity_badge_class_symbol() {
                    return Err(sim_kernel::Error::Eval(format!(
                        "expected standard/FidelityBadge, found {class}"
                    )));
                }
                FidelityBadge::from_constructor_args(args.clone())
            }
            _ => Err(sim_kernel::Error::TypeMismatch {
                expected: "fidelity badge constructor call",
                found: expr_kind(item),
            }),
        })
        .collect()
}