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} ()
|
|
|
|
|
2020-02-22 02:48:12 +03:00
|
|
|
fst = cases Tuple.Cons a _ -> a
|
2018-08-16 19:42:38 +03:00
|
|
|
|
rename Builtins in initial namespace construction, and update tests
- () -> Unit
- ().() -> Unit.Unit
- Pair -> Tuple
- Pair.Pair -> Tuple.Cons
- Sequence -> List
- Effect -> Request
- {Int,Nat,Float,Text}.{==,<,<=,>,>=} -> {Int,Nat,Float,Text}.{eq,lt,lteq,gt,gteq}
- mark Text.!= as a deprecated builtin
2019-08-22 23:34:12 +03:00
|
|
|
--TODO type is wrongly being inferred (or at least displayed) as `Tuple a (Tuple a b) ->{} a`
|
2020-02-22 02:48:12 +03:00
|
|
|
snd = cases Tuple.Cons _ (Tuple.Cons b _) -> b
|
2018-08-16 19:42:38 +03:00
|
|
|
|
2020-09-28 03:39:26 +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
|
|
|
|
2020-01-18 11:49:15 +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?"
|
2020-02-07 05:29:43 +03:00
|
|
|
match read with
|
2019-02-12 22:10:33 +03:00
|
|
|
Some name -> write ("Hello" ++ name)
|
|
|
|
None -> write "Fine, be that way."
|
2020-09-28 03:39:26 +03:00
|
|
|
with simulate
|
|
|
|
with state ([],[])
|
2019-02-12 22:10:33 +03:00
|
|
|
|
|
|
|
> x
|