unison/unison-src/tests/console.u

53 lines
1.4 KiB
Plaintext
Raw Normal View History

2021-08-24 21:33:27 +03:00
structural ability State s where
2018-08-16 19:42:38 +03:00
get : {State s} s
set : s -> {State s} ()
2021-08-24 21:33:27 +03:00
structural ability Console where
2018-08-16 19:42:38 +03:00
read : {Console} (Optional Text)
write : Text -> {Console} ()
fst = cases Tuple.Cons a _ -> a
2018-08-16 19:42:38 +03:00
--TODO type is wrongly being inferred (or at least displayed) as `Tuple a (Tuple a b) ->{} a`
snd = cases Tuple.Cons _ (Tuple.Cons b _) -> b
2018-08-16 19:42:38 +03:00
state : s -> Request (State s) a -> a
state s = cases
{State.get -> k} -> handle k s with state s
{State.set s' -> k} -> handle k () with state s'
{a} -> a
simulate : Request Console d -> {State ([Text], [Text])} d
simulate = cases
{Console.read -> k} ->
io = State.get
ins = fst io
outs = snd io
State.set (drop 1 ins, outs)
-- this really should typecheck but doesn't for some reason
-- error is that `simulate` doesn't check against `Request Console c -> r`,
-- but seems like that `r` should get instantiated as `{State (..)} c`.
handle k (at 0 ins) with simulate
{Console.write t -> k} ->
io = State.get
ins = fst io
outs = snd io
-- same deal here
handle k (State.set (ins, cons t outs)) with simulate
{a} -> a
2018-08-16 19:42:38 +03:00
2018-09-24 21:38:03 +03:00
(++) = (Text.++)
2018-08-16 19:42:38 +03:00
x = handle
handle
2019-02-12 22:10:33 +03:00
use Console read write
use Optional Some None
write "What's your name?"
match read with
2019-02-12 22:10:33 +03:00
Some name -> write ("Hello" ++ name)
None -> write "Fine, be that way."
with simulate
with state ([],[])
2019-02-12 22:10:33 +03:00
> x