unison/unison-src/tests/handler-stacking.u

35 lines
784 B
Plaintext
Raw Normal View History

2019-04-11 23:22:11 +03:00
use State get put
use Writer tell
> handle
handle replicate 5 main
with writerHandler []
with stateHandler "hello"
2019-04-11 23:22:11 +03:00
main = '(tell get)
replicate : Nat -> '{e} () -> {e} ()
replicate n x =
if Nat.eq n 0 then () else
2019-04-11 23:22:11 +03:00
!x
replicate (drop n 1) x
2019-04-11 23:22:11 +03:00
2021-08-24 21:33:27 +03:00
structural ability State a where
2019-04-11 23:22:11 +03:00
get : {State a} a
put : a -> {State a} ()
2021-08-24 21:33:27 +03:00
structural ability Writer w where
2019-04-11 23:22:11 +03:00
tell : w -> {Writer w} ()
stateHandler : s -> Request {State s} a -> (s, a)
stateHandler s = cases
{ State.get -> k } -> handle k s with stateHandler s
{ State.put s -> k } -> handle k () with stateHandler s
2019-04-11 23:22:11 +03:00
{ a } -> (s, a)
writerHandler : [w] -> Request {Writer w} a -> ([w], a)
writerHandler ww = cases
{ Writer.tell w -> k } -> handle k () with writerHandler (snoc ww w)
2019-04-11 23:22:11 +03:00
{ a } -> (ww, a)