sim-kernel 0.1.0-rc.1

SIM workspace package for sim kernel.
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
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
//! The [`Sequence`] contract: pull-based iteration over runtime values.
//!
//! The kernel defines the sequence protocol and its next/close/peek surface,
//! including bridging events into sequence items; libraries supply the concrete
//! sequence sources.

use std::{
    collections::VecDeque,
    sync::{Arc, Mutex},
};

use crate::{
    env::Cx,
    error::{Error, Result},
    event::{Event, EventKind, EventSource, Tick, validate_ticks},
    expr::Expr,
    id::{CORE_SEQUENCE_CLASS_ID, Symbol},
    list::ListValue,
    object::{ClassRef, Object},
    ref_id::Ref,
    ref_resolver::value_from_ref,
    stream::Stream,
    term::Term,
    value::Value,
};

/// One item produced by a sequence: a value plus its clock ticks.
#[derive(Clone, Debug)]
pub struct SequenceItem {
    value: Value,
    ticks: Vec<Tick>,
}

impl SequenceItem {
    /// Builds an item carrying a value with no ticks.
    pub fn new(value: Value) -> Self {
        Self {
            value,
            ticks: Vec::new(),
        }
    }

    /// Builds an item with ticks, validating their clock ordering.
    pub fn with_ticks(value: Value, ticks: Vec<Tick>) -> Result<Self> {
        validate_ticks(&ticks)?;
        Ok(Self { value, ticks })
    }

    /// Borrows the carried value.
    pub fn value(&self) -> &Value {
        &self.value
    }

    /// Borrows the carried clock ticks.
    pub fn ticks(&self) -> &[Tick] {
        &self.ticks
    }

    /// Projects the item to a runtime value, folding in any ticks.
    pub fn into_value(self, cx: &mut Cx) -> Result<Value> {
        sequence_item_value(cx, self)
    }
}

/// Pull-based iteration protocol over runtime values.
pub trait Sequence: Send + Sync {
    /// Pulls the next item, or `Ok(None)` once exhausted.
    fn next_item(&self, cx: &mut Cx) -> Result<Option<SequenceItem>>;

    /// Releases resources backing the sequence; idempotent by default.
    fn close(&self, _cx: &mut Cx) -> Result<()> {
        Ok(())
    }

    /// Peeks the next item without consuming it; unsupported by default.
    fn peek_item(&self, _cx: &mut Cx) -> Result<Option<SequenceItem>> {
        Err(Error::Eval("sequence does not support peek".to_owned()))
    }

    /// Whether the sequence is known to be exhausted; `false` by default.
    fn is_done(&self, _cx: &mut Cx) -> Result<bool> {
        Ok(false)
    }
}

/// Pulls the next item from a sequence.
pub fn seq_next(cx: &mut Cx, sequence: &dyn Sequence) -> Result<Option<SequenceItem>> {
    sequence.next_item(cx)
}

/// Closes a sequence.
pub fn seq_close(cx: &mut Cx, sequence: &dyn Sequence) -> Result<()> {
    sequence.close(cx)
}

/// Peeks the next item of a sequence without consuming it.
pub fn seq_peek(cx: &mut Cx, sequence: &dyn Sequence) -> Result<Option<SequenceItem>> {
    sequence.peek_item(cx)
}

/// Reports whether a sequence is exhausted.
pub fn seq_is_done(cx: &mut Cx, sequence: &dyn Sequence) -> Result<bool> {
    sequence.is_done(cx)
}

/// Pulls the next item from a value that is a sequence or a stream.
pub fn seq_next_value(cx: &mut Cx, target: &Value) -> Result<Option<SequenceItem>> {
    if let Some(sequence) = target.object().as_sequence() {
        return seq_next(cx, sequence);
    }
    if let Some(stream) = target.object().as_stream() {
        return stream.next(cx).map(|item| item.map(SequenceItem::new));
    }
    Err(Error::TypeMismatch {
        expected: "sequence",
        found: "non-sequence",
    })
}

/// Closes a value that is a sequence or a stream.
pub fn seq_close_value(cx: &mut Cx, target: &Value) -> Result<()> {
    if let Some(sequence) = target.object().as_sequence() {
        return seq_close(cx, sequence);
    }
    if let Some(stream) = target.object().as_stream() {
        return stream.close(cx);
    }
    Err(Error::TypeMismatch {
        expected: "sequence",
        found: "non-sequence",
    })
}

/// Projects a sequence item to a value, wrapping it in a payload/ticks table
/// when ticks are present.
pub fn sequence_item_value(cx: &mut Cx, item: SequenceItem) -> Result<Value> {
    if item.ticks.is_empty() {
        return Ok(item.value);
    }

    let ticks = item
        .ticks
        .into_iter()
        .map(|tick| tick_value(cx, tick))
        .collect::<Result<Vec<_>>>()?;
    let ticks = cx.factory().list(ticks)?;
    cx.factory().table(vec![
        (Symbol::new("payload"), item.value),
        (Symbol::new("ticks"), ticks),
    ])
}

/// Maps an event into a sequence item, or `Ok(None)` for non-payload events;
/// failure events are surfaced as errors.
pub fn sequence_item_from_event(cx: &mut Cx, event: Event) -> Result<Option<SequenceItem>> {
    match event.kind {
        EventKind::Chunk { payload } | EventKind::Final(payload) => {
            SequenceItem::with_ticks(value_from_ref(cx, &payload)?, event.ticks).map(Some)
        }
        EventKind::Failed(error) => Err(error_from_failed_ref(cx, &error)),
        EventKind::Started { .. }
        | EventKind::Claim { .. }
        | EventKind::Diagnostic(_)
        | EventKind::Trace(_)
        | EventKind::EffectRequested { .. }
        | EventKind::EffectResolved { .. }
        | EventKind::Capture { .. }
        | EventKind::Card { .. }
        | EventKind::Done => Ok(None),
    }
}

// sim-non-citizen(reason = "live event-source sequence adapter; descriptor data is the underlying event source", kind = "handle", descriptor = "core/EventSource")
/// Sequence adapter that drains an [`EventSource`] into sequence items.
pub struct EventSourceSequence {
    source: Arc<dyn EventSource>,
    state: Mutex<EventSourceSequenceState>,
}

#[derive(Debug, Default)]
struct EventSourceSequenceState {
    pending: VecDeque<Event>,
    done: bool,
}

impl EventSourceSequence {
    /// Wraps an event source as a sequence.
    pub fn new(source: Arc<dyn EventSource>) -> Self {
        Self {
            source,
            state: Mutex::new(EventSourceSequenceState::default()),
        }
    }
}

impl Object for EventSourceSequence {
    fn display(&self, _cx: &mut Cx) -> Result<String> {
        Ok("#<event-source-sequence>".to_owned())
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

impl crate::ObjectCompat for EventSourceSequence {
    fn class(&self, cx: &mut Cx) -> Result<ClassRef> {
        cx.factory().class_stub(
            CORE_SEQUENCE_CLASS_ID,
            Symbol::qualified("core", "EventSourceSequence"),
        )
    }
    fn as_sequence(&self) -> Option<&dyn Sequence> {
        Some(self)
    }
}

impl Sequence for EventSourceSequence {
    fn next_item(&self, cx: &mut Cx) -> Result<Option<SequenceItem>> {
        while let Some(event) = self.next_event(cx)? {
            let done = matches!(event.kind, EventKind::Done);
            if let Some(item) = sequence_item_from_event(cx, event)? {
                return Ok(Some(item));
            }
            if done {
                self.mark_done()?;
                return Ok(None);
            }
        }
        Ok(None)
    }

    fn close(&self, cx: &mut Cx) -> Result<()> {
        {
            let mut state = self
                .state
                .lock()
                .map_err(|_| Error::PoisonedLock("event source sequence"))?;
            state.pending.clear();
            state.done = true;
        }
        self.source.close(cx)
    }

    fn peek_item(&self, cx: &mut Cx) -> Result<Option<SequenceItem>> {
        loop {
            if let Some(item) = self.pending_item(cx)? {
                return Ok(Some(item));
            }
            if self.is_done(cx)? {
                return Ok(None);
            }
            let Some(event) = self.source.next(cx)? else {
                self.mark_done()?;
                return Ok(None);
            };
            let done = matches!(event.kind, EventKind::Done);
            self.push_pending(event)?;
            if done {
                self.mark_done()?;
            }
        }
    }

    fn is_done(&self, _cx: &mut Cx) -> Result<bool> {
        let state = self
            .state
            .lock()
            .map_err(|_| Error::PoisonedLock("event source sequence"))?;
        Ok(state.done && state.pending.is_empty())
    }
}

impl EventSourceSequence {
    fn next_event(&self, cx: &mut Cx) -> Result<Option<Event>> {
        {
            let mut state = self
                .state
                .lock()
                .map_err(|_| Error::PoisonedLock("event source sequence"))?;
            if let Some(event) = state.pending.pop_front() {
                return Ok(Some(event));
            }
            if state.done {
                return Ok(None);
            }
        }

        match self.source.next(cx)? {
            Some(event) => Ok(Some(event)),
            None => {
                self.mark_done()?;
                Ok(None)
            }
        }
    }

    fn pending_item(&self, cx: &mut Cx) -> Result<Option<SequenceItem>> {
        let events = self
            .state
            .lock()
            .map_err(|_| Error::PoisonedLock("event source sequence"))?
            .pending
            .iter()
            .cloned()
            .collect::<Vec<_>>();
        for event in events {
            if let Some(item) = sequence_item_from_event(cx, event)? {
                return Ok(Some(item));
            }
        }
        Ok(None)
    }

    fn push_pending(&self, event: Event) -> Result<()> {
        let mut state = self
            .state
            .lock()
            .map_err(|_| Error::PoisonedLock("event source sequence"))?;
        state.pending.push_back(event);
        Ok(())
    }

    fn mark_done(&self) -> Result<()> {
        let mut state = self
            .state
            .lock()
            .map_err(|_| Error::PoisonedLock("event source sequence"))?;
        state.done = true;
        Ok(())
    }
}

// sim-non-citizen(reason = "live list sequence cursor; canonical list data remains the source list value", kind = "handle", descriptor = "core/List")
/// Sequence cursor that walks the spine of a kernel list value.
#[derive(Debug)]
pub struct ListSequence {
    state: Mutex<ListSequenceState>,
}

#[derive(Debug)]
struct ListSequenceState {
    next: Option<Value>,
    closed: bool,
}

impl ListSequence {
    /// Builds a sequence that iterates the given list value.
    pub fn new(list: Value) -> Self {
        Self {
            state: Mutex::new(ListSequenceState {
                next: Some(list),
                closed: false,
            }),
        }
    }
}

impl Object for ListSequence {
    fn display(&self, _cx: &mut Cx) -> Result<String> {
        Ok("#<list-sequence>".to_owned())
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

impl crate::ObjectCompat for ListSequence {
    fn class(&self, cx: &mut Cx) -> Result<ClassRef> {
        cx.factory().class_stub(
            CORE_SEQUENCE_CLASS_ID,
            Symbol::qualified("core", "Sequence"),
        )
    }
    fn as_sequence(&self) -> Option<&dyn Sequence> {
        Some(self)
    }
}

impl Sequence for ListSequence {
    fn next_item(&self, cx: &mut Cx) -> Result<Option<SequenceItem>> {
        let mut state = self
            .state
            .lock()
            .map_err(|_| Error::PoisonedLock("list sequence"))?;
        if state.closed {
            return Ok(None);
        }

        let Some(node) = state.next.clone() else {
            state.closed = true;
            return Ok(None);
        };
        let list = list_from_value(&node)?;
        if list.is_empty(cx)? {
            state.next = None;
            state.closed = true;
            return Ok(None);
        }

        let value = list
            .car(cx)?
            .ok_or_else(|| Error::Eval("list car missing for non-empty list".to_owned()))?;
        state.next = list.cdr(cx)?;
        Ok(Some(SequenceItem::new(value)))
    }

    fn close(&self, _cx: &mut Cx) -> Result<()> {
        let mut state = self
            .state
            .lock()
            .map_err(|_| Error::PoisonedLock("list sequence"))?;
        state.next = None;
        state.closed = true;
        Ok(())
    }

    fn peek_item(&self, cx: &mut Cx) -> Result<Option<SequenceItem>> {
        let mut state = self
            .state
            .lock()
            .map_err(|_| Error::PoisonedLock("list sequence"))?;
        if state.closed {
            return Ok(None);
        }

        let Some(node) = state.next.as_ref() else {
            state.closed = true;
            return Ok(None);
        };
        let list = list_from_value(node)?;
        if list.is_empty(cx)? {
            state.next = None;
            state.closed = true;
            return Ok(None);
        }
        list.car(cx).map(|item| item.map(SequenceItem::new))
    }

    fn is_done(&self, cx: &mut Cx) -> Result<bool> {
        let mut state = self
            .state
            .lock()
            .map_err(|_| Error::PoisonedLock("list sequence"))?;
        if state.closed {
            return Ok(true);
        }

        let Some(node) = state.next.as_ref() else {
            state.closed = true;
            return Ok(true);
        };
        let done = list_from_value(node)?.is_empty(cx)?;
        if done {
            state.next = None;
            state.closed = true;
        }
        Ok(done)
    }
}

impl<T> Sequence for T
where
    T: Stream + ?Sized,
{
    fn next_item(&self, cx: &mut Cx) -> Result<Option<SequenceItem>> {
        self.next(cx).map(|item| item.map(SequenceItem::new))
    }

    fn close(&self, cx: &mut Cx) -> Result<()> {
        Stream::close(self, cx)
    }
}

fn list_from_value(value: &Value) -> Result<&dyn ListValue> {
    value.object().as_list().ok_or(Error::TypeMismatch {
        expected: "list",
        found: "non-list",
    })
}

fn tick_value(cx: &mut Cx, tick: Tick) -> Result<Value> {
    let clock = cx.factory().symbol(tick.clock)?;
    let index = ref_value(cx, tick.index)?;
    cx.factory().table(vec![
        (Symbol::new("clock"), clock),
        (Symbol::new("index"), index),
    ])
}

fn ref_value(cx: &mut Cx, reference: Ref) -> Result<Value> {
    cx.factory().expr(Expr::from(Term::Ref(reference)))
}

fn error_from_failed_ref(cx: &mut Cx, reference: &Ref) -> Error {
    match value_from_ref(cx, reference) {
        Ok(value) => match value.object().display(cx) {
            Ok(message) => Error::Eval(message),
            Err(err) => err,
        },
        Err(err) => err,
    }
}