Skip to content

Commit

Permalink
Port PastConfigs
Browse files Browse the repository at this point in the history
  • Loading branch information
nickdrozd committed Feb 2, 2024
1 parent 3567c82 commit a77e0a4
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use blocks::{measure_blocks, unroll_tape};
use parse::{
parse as parse_fn, read_slot, read_state, show_instr, show_slot, show_state, tcompile,
};
use prover::PastConfig;
use prover::PastConfigs;
use rules::{InfiniteRule, RuleLimit, UnknownRule};

#[pymodule]
Expand All @@ -39,7 +39,7 @@ fn rust_stuff(py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(show_instr, m)?)?;

// prover
m.add_class::<PastConfig>()?;
m.add_class::<PastConfigs>()?;

// rules
m.add("UnknownRule", py.get_type::<UnknownRule>())?;
Expand Down
33 changes: 29 additions & 4 deletions src/prover.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use std::collections::HashMap;

use pyo3::prelude::*;

use crate::instrs::State;

type Cycle = u32;

#[pyclass]
pub struct PastConfig {
struct PastConfig {
cycles: Vec<Cycle>,
}

#[pymethods]
impl PastConfig {
#[new]
const fn new() -> Self {
Self { cycles: Vec::new() }
}
Expand Down Expand Up @@ -55,3 +56,27 @@ impl PastConfig {
None
}
}

#[pyclass]
pub struct PastConfigs {
configs: HashMap<State, PastConfig>,
}

#[pymethods]
impl PastConfigs {
#[new]
fn new() -> Self {
Self {
configs: HashMap::new(),
}
}

fn next_deltas(&mut self, state: State, cycle: Cycle) -> Option<(Cycle, Cycle, Cycle)> {
let config = self.configs.entry(state).or_insert_with(PastConfig::new);
config.next_deltas(cycle)
}

fn delete_configs(&mut self, state: State) {
self.configs.remove(&state);
}
}
24 changes: 1 addition & 23 deletions tm/prover.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import TYPE_CHECKING

from tm.rules import make_rule
from tm.rust_stuff import PastConfig
from tm.rust_stuff import PastConfigs

if TYPE_CHECKING:
from tm.rules import Rule
Expand All @@ -17,28 +17,6 @@ class ConfigLimit(Exception):
pass


class PastConfigs:
_configs: dict[State, PastConfig]

def __init__(self) -> None:
self._configs = {}

def next_deltas(
self,
state: State,
cycle: int,
) -> tuple[int, int, int] | None:
try:
config = self._configs[state]
except KeyError:
self._configs[state] = (config := PastConfig())

return config.next_deltas(cycle)

def delete_configs(self, state: State) -> None:
del self._configs[state]


class Prover:
prog: GetInstr

Expand Down
12 changes: 9 additions & 3 deletions tm/rust_stuff.pyi
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
## prover ##############################

class PastConfig:
cycles: list[int]
class PastConfigs:
def __init__(self) -> None: ...

def next_deltas(self, cycle: int) -> tuple[int, int, int] | None: ...
def next_deltas(
self,
state: State,
cycle: int,
) -> tuple[int, int, int] | None: ...

def delete_configs(self, state: State) -> None: ...

## parse ###############################

Expand Down

0 comments on commit a77e0a4

Please sign in to comment.