Created
September 26, 2020 09:24
-
-
Save osfameron/30b821232096e7e013f62441a54235ed to your computer and use it in GitHub Desktop.
Unison tickers sketch
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
type Counter a = { value : a, | |
tick : (a -> a) } | |
tock : (state -> Counter a) | |
-> (Counter a -> state -> state) | |
-> state | |
-> (a, state) | |
tock sc css s = | |
c = sc s | |
v = Counter.value c | |
c' = Counter.value.modify (Counter.tick c) c | |
s' = css c' s | |
(v, s') | |
adder = Counter 1 Nat.increment | |
> tock id const adder | |
ability Ticker where | |
tick : (state -> Counter a) | |
-> (Counter a -> state -> state) | |
-> a | |
Ticker.withCounters.handler state = | |
cases | |
{ Ticker.tick sc css -> k } -> | |
match (tock sc css state) with | |
(v, state') -> | |
handle k v | |
with Ticker.withCounters.handler state' | |
{ v } -> v | |
Ticker.withCounters c x = | |
handle !x with Ticker.withCounters.handler c | |
c = '(Ticker.tick id const) | |
> withCounters adder '[!c, !c, !c] | |
type MyCounter = { c1 : Counter Nat, | |
c2 : Counter Nat } | |
counters = MyCounter (Counter 1 Nat.increment) | |
(Counter 10 (c -> c + 10)) | |
tick1 = '(Ticker.tick c1 c1.set) | |
tick2 = '(Ticker.tick c2 c2.set) | |
> withCounters counters '[!tick1, !tick2, !tick1, !tick2] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment