unison/unison-src/tests/console1.u
2023-04-13 21:11:57 -04:00

43 lines
989 B
Plaintext

-- This confusingly gives an error that
-- it doesn't know what `Console.simulate` is.
structural ability State s where
get : {State s} s
set : s -> {State s} ()
structural ability Console where
read : {Console} (Optional Text)
write : Text -> {Console} ()
fst = cases Tuple.Cons a _ -> a
snd = cases Tuple.Cons _ (Tuple.Cons b _) -> b
simulate : Request Console a -> {State ([Text], [Text])} a
simulate = cases
{ pure } -> pure
{Console.read -> k} -> handle
io = State.get
ins = fst io
outs = snd io
State.set (drop 1 ins, outs)
k (at 0 ins)
with simulate
{Console.write t -> k} -> handle
io = State.get
ins = fst io
outs = snd io
State.set (ins, outs ++ [t])
k ()
with simulate
e = 'let handle
use Console read write
use Optional Some None
write "What's your name?"
match read with
Some name -> write ("Hello" ++ name)
None -> write "Fine, be that way."
with simulate