unison/unison-src/tests/console.u

53 lines
1.4 KiB
Plaintext
Raw Normal View History

2018-08-16 19:42:38 +03:00
effect State s where
get : {State s} s
set : s -> {State s} ()
effect Console where
read : {Console} (Optional Text)
write : Text -> {Console} ()
fst x = case x of Pair.Pair a _ -> a
--TODO type is wrongly being inferred (or at least displayed) as `Pair a (Pair a b) ->{} a`
2018-08-16 19:42:38 +03:00
snd x = case x of Pair.Pair _ (Pair.Pair b _) -> b
namespace Console where
2018-08-23 21:56:50 +03:00
state : s -> Effect (State s) a -> a
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 : Effect 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 `Effect 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
2018-08-23 21:56:50 +03:00
handle Console.state ([],[]) in
2018-09-24 21:38:03 +03:00
handle Console.simulate in
2018-08-23 21:56:50 +03:00
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."
2018-08-16 19:42:38 +03:00
2019-02-12 19:15:46 +03:00