unison/unison-src/tests/console.u

53 lines
1.5 KiB
Plaintext
Raw Normal View History

ability State s where
2018-08-16 19:42:38 +03:00
get : {State s} s
set : s -> {State s} ()
ability Console where
2018-08-16 19:42:38 +03:00
read : {Console} (Optional Text)
write : Text -> {Console} ()
fst x = case x of 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 x = case x of Tuple.Cons _ (Tuple.Cons b _) -> b
2018-08-16 19:42:38 +03:00
namespace Console where
state : s -> Request (State s) a -> a
2018-08-23 21:56:50 +03:00
state s c = case c of
{State.get -> k} -> handle state s in k s
{State.set s' -> k} -> handle state s' in k ()
{a} -> a
simulate : Request Console d -> {State ([Text], [Text])} d
2018-08-16 19:42:38 +03:00
simulate c = case c of
{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`,
2018-09-24 21:38:03 +03:00
-- but seems like that `r` should get instantiated as `{State (..)} c`.
2018-08-16 19:42:38 +03:00
handle simulate in k (at 0 ins)
{Console.write t -> k} ->
io = State.get
ins = fst io
outs = snd io
2018-09-24 21:38:03 +03:00
-- same deal here
2018-08-16 19:42:38 +03:00
handle simulate in k (State.set (ins, cons t outs))
2018-08-23 21:56:50 +03:00
{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
2019-02-12 22:10:33 +03:00
x = handle Console.state ([],[]) in
handle Console.simulate in
use Console read write
use Optional Some None
write "What's your name?"
case read of
Some name -> write ("Hello" ++ name)
None -> write "Fine, be that way."
> x