Incremental computation through constrained memoization (MoonBit port of comemo).
moon add starAndHonor/comemoon#comemotrack
pub struct Files {
map : Map[String, String]
}
///|
pub fn Files::read(self : Files, path : String) -> String {
self.map.get(path).unwrap_or("")
}
///|
pub fn Files::write(self : Files, path : String, text : String) -> Unit {
self.map[path] = text
}fn eval(script : String, files : FilesTracked) -> Int {
let cache : Cache[FilesCall, Int] = Cache::new()
memoize(cache, input, true, fn() {
// the computation; reads through `files` are tracked
})
}moon run cmd/main| API | Purpose |
|---|---|
| memoize … memoize6 | Memoize a function with 0–6 tracked parameters |
| Cache::new / CacheEntry | Per-function cache holding output + recorded mutable calls + age |
| Tracked[T] / TrackedMut[T] | Read-only / mutable tracked wrappers |
| evict(max_age) | Age-based eviction; resets validation accelerators |
| Input::new | Combine plain-hash args and tracked args into a memoization key |
| last_was_hit() | Test oracle: did the last memoized call hit the cache? |
memoized fn call
│
▼
per-fn Cache (CallTree) key = murmur3-128(receiver + plain args)
│
▼
CallTree lookup: walk trie; at each inner node replay the recorded call on the
live tracked value; continue only if the replayed return-hash == edge label
│
├─ leaf → HIT: replay recorded mutable calls (side effects),
│ clone output, reset age
└─ miss → attach a Constraint sink to tracked args, run fn,
insert (immutable calls → trie path, mutable calls → entry)moon test # wasm
moon test --target native # nativeIncremental computation through constrained memoization (MoonBit port of comemo).