Skip to content

refactor: De-couple Chips from a specific ExecutionRecord, part II#37

Closed
huitseeker wants to merge 3 commits into
devfrom
output_events
Closed

refactor: De-couple Chips from a specific ExecutionRecord, part II#37
huitseeker wants to merge 3 commits into
devfrom
output_events

Conversation

@huitseeker
Copy link
Copy Markdown
Contributor

Previously: #9

This applies the same logic to output events in generate_trace.

General approach

// implemented on the chip
trait WithEvents<'a> {
  type InputEvents : 'a;
  type OutputEvents: 'a;
}

// implemented on the record
trait EventLens<T: for <'a> WithEvent<'a>> {
  fn events<'a>(&'a self) -> <T as WithEvents<'a>>::InputEvents;
}
// implemented on the record
trait EventMutLens<T: for <'a> WithEvent<'a>> {
  fn events<'a>(&'a mut self, events: <T as WithEvents<'a>>::OutputEvents);
}

Now, one would wish that this would be a bit more ergonomic in Rust, because we do have anonymous cartesian products of arbitrary arity (tuples), but we do not have anonymous coproducts - either crate notwithstanding.

DivRemChip Example

pub enum DivRemEvent<'a> {
    ByteLookupEvent(&'a ByteLookupEvent),
    MulEvent(&'a AluEvent),
    LtEvent(&'a AluEvent),
}

impl<'a> WithEvents<'a> for DivRemChip {
    type InputEvents = &'a [AluEvent];
    type OutputEvents =  [DivRemEvent<'a>];
}

impl EventMutLens<DivRemChip> for ExecutionRecord {
    fn add_events(&mut self, events: <DivRemChip as crate::air::WithEvents<'_>>::OutputEvents) {
        for event in events {
            match event {
                DivRemEvent::ByteLookupEvent(e) => self.add_byte_lookup_event(*e),
                DivRemEvent::MulEvent(e) => self.add_mul_event(*e),
                DivRemEvent::LtEvent(e) => self.add_lt_event(*e),
            }
        }
    }
}

Previously: #9

This applies the same logic to output events in `generate_trace`.

```rust
// implemented on the chip
trait WithEvents<'a> {
  type InputEvents : 'a;
  type OutputEvents: 'a;
}

// implemented on the record
trait EventLens<T: for <'a> WithEvent<'a>> {
  fn events<'a>(&'a self) -> <T as WithEvents<'a>>::InputEvents;
}
// implemented on the record
trait EventMutLens<T: for <'a> WithEvent<'a>> {
  fn events<'a>(&'a mut self, events: <T as WithEvents<'a>>::OutputEvents);
}
```

Now, one would wish that this would be a bit more ergonomic in Rust, because we do have anonymous cartesian products of arbitrary arity (tuples), but we do not have anonymous coproducts - either crate notwithstanding.

```rust
pub enum DivRemEvent<'a> {
    ByteLookupEvent(&'a ByteLookupEvent),
    MulEvent(&'a AluEvent),
    LtEvent(&'a AluEvent),
}

impl<'a> WithEvents<'a> for DivRemChip {
    type InputEvents = &'a [AluEvent];
    type OutputEvents =  [DivRemEvent<'a>];
}

impl EventMutLens<DivRemChip> for ExecutionRecord {
    fn add_events(&mut self, events: <DivRemChip as crate::air::WithEvents<'_>>::OutputEvents) {
        for event in events {
            match event {
                DivRemEvent::ByteLookupEvent(e) => self.add_byte_lookup_event(*e),
                DivRemEvent::MulEvent(e) => self.add_mul_event(*e),
                DivRemEvent::LtEvent(e) => self.add_lt_event(*e),
            }
        }
    }
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant