-
Notifications
You must be signed in to change notification settings - Fork 5
/
ex_single2.ml
41 lines (32 loc) · 957 Bytes
/
ex_single2.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
(* Single-session example with branching and selection on arbitrary labels. *)
(* This will be rewritten with macros ```match%branch0``` and ```[%select]```. See README.md *)
open Session
open Session0
type binop = Add | Sub | Mul | Div
let eval_binop = function
| Add -> (+) | Sub -> (-)
| Mul -> ( * ) | Div -> (/)
let rec arith_server () =
match%branch0 () with
| `neg -> Ex_single1.neg_server ()
| `bin -> binop_server ()
| `fin -> close ()
and binop_server () =
let%s op = recv () in
let%s (x,y) = recv () in
send (eval_binop op x y) >>
arith_server ()
let arith_client () =
[%select0 `bin] >>
send Add >>
send (150, 250) >>
let%s ans = recv () in
Printf.printf "Answer: %d\n" ans;
[%select0 `fin] >>
close () >>
return ()
let arith_ch = new_channel ()
let _ =
ignore @@ Thread.create
(accept_ arith_ch arith_server) ();
connect_ arith_ch arith_client ()