generated from koka-community/template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
adb4a91
commit 851e687
Showing
8 changed files
with
301 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,69 @@ | ||
pub import std/data/rb-map | ||
import std/fixpoint/lattice | ||
|
||
effect cache<s,c> | ||
fun add-result(s: s, r: c): () | ||
fun add-state(s: s): () | ||
fun is-cached(s: s): bool | ||
// k = key, r = result | ||
effect cache<k,r> | ||
fun add-result(key: k, result: r): () | ||
fun is-cached(key: k): bool | ||
ctl do-each(ss: list<a>): a | ||
final ctl none(): b | ||
ctl depend(s: s): c | ||
ctl depend(key: k): r | ||
|
||
fun cache(f: () -> <pure,cache<s,c>|e> b, ?order2: (s, s) -> pure order2<s>, .?change-lattice:change-lattice<r,c>, ?s/show: s -> string, ?c/show: c -> string): <pure|e> rbmap<s,r> | ||
var m : some<s,r> rbmap<s,r> := empty() | ||
var deps : some<s,c,e> rbmap<s, list<(c -> <pure|e> ())>> := empty() | ||
fun update(s, c) | ||
match deps.lookup(s) | ||
// A fixpoint cache handler | ||
fun cache(comp: () -> <pure,cache<k,r>|e> d, ?order2: (k, k) -> pure order2<k>, | ||
.?change-lattice:change-lattice<b,r>, ?k/show: k -> string, ?r/show: r -> string): <pure|e> rbmap<k,b> | ||
var cache : some<k,r> rbmap<k,r> := empty() | ||
var deps : some<k,c,e> rbmap<k, list<(c -> <pure|e> ())>> := empty() | ||
fun update(key, change) | ||
match deps.lookup(key) | ||
Just(resumes) -> | ||
// trace("Updating " ++ resumes.length.show ++ " deps for " ++ s.show ++ " with " ++ c.c/show) | ||
resumes.list/foreach(fn(res) {res(c); ()}) | ||
resumes.list/foreach(fn(resumption) {resumption(change); ()}) | ||
Nothing -> () | ||
val do = | ||
with handler | ||
fun add-state(s) | ||
m := m.set(s, bottom) | ||
fun add-result(s, c) | ||
fun add-result(key, change) | ||
// trace("Adding result for " ++ s.s/show ++ " " ++ c.c/show) | ||
match m.lookup(s) | ||
match cache.lookup(key) | ||
Just(r') -> | ||
val (changed, r'') = join(r', c) | ||
val (changed, r'') = join(r', change) | ||
if changed then | ||
m := m.set(s, r'') | ||
update(s, c) | ||
cache := cache.set(key, r'') | ||
update(key, change) | ||
else () | ||
Nothing -> | ||
m := m.set(s, bottom.join(c).snd) | ||
update(s, c) | ||
fun is-cached(s) | ||
m.contains(s) | ||
ctl depend(s) | ||
cache := cache.set(key, bottom.join(change).snd) | ||
update(key, change) | ||
fun is-cached(key) | ||
if cache.contains(key) || deps.contains(key) then True | ||
else | ||
cache := cache.set(key, bottom) | ||
deps := deps.set(key, Nil) | ||
False | ||
ctl depend(key) | ||
// trace("Adding dep for " ++ s.s/show) | ||
match deps.lookup(s) | ||
match deps.lookup(key) | ||
Just(resumes) -> | ||
val ress = Cons(fn(r) resume(r), resumes) | ||
deps := deps.set(s, ress) | ||
Nothing -> | ||
deps := deps.set(s, [fn(r) resume(r)]) | ||
match m.lookup(s) | ||
Just(r) -> | ||
r.changes.foreach(fn(c) update(s, c)) | ||
Nothing -> () | ||
deps := deps.set(key, ress) | ||
match cache.lookup(key) | ||
Just(r) -> r.changes.foreach(fn(change) resume(change)) | ||
ctl do-each(ss) | ||
ss.foreach(fn(s) resume(s)) | ||
final ctl none() | ||
() | ||
return(x) () | ||
f() | ||
m | ||
comp() | ||
cache | ||
|
||
fun memo(s, f) | ||
match is-cached(s) | ||
True -> depend(s) | ||
False -> | ||
add-state(s) | ||
f(fn(ls) fix/each(s, ls)) | ||
// Inserts a memoization point at a recursive invocation | ||
fun memo(key : k, func : ((list<() -> <cache<k,r>|e> r>) -> <cache<k,r>|e> r) -> <cache<k,r>|e> r): <cache<k,r>|e> r | ||
match is-cached(key) | ||
True -> depend(key) | ||
False -> func(fn(ls) fix/each(key, ls)) | ||
|
||
fun fix/each(s: s, ls: list<(() -> <cache<s,c>|e> c)>): <cache<s,c>|e> c | ||
val f = do-each(ls) | ||
val r = f() | ||
add-result(s,r) | ||
r | ||
fun fix/each(key: k, ls: list<(() -> <cache<k,r>|e> r)>): <cache<k,r>|e> r | ||
val func = do-each(ls) | ||
val result = func() | ||
add-result(key, result) | ||
result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.