aboutsummaryrefslogtreecommitdiff
path: root/Semestr 3/pf/lista3/procc.ml
blob: 07f44046a2aba6a36c7418e283e0816bcbc50c4c (plain)
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
type ('z,'i) in_channel =
  | In of (('z,'i) out_channel -> 'z)

and ('z,'o) out_channel =
  | Out of ('o -> ('z,'o) in_channel -> 'z)

type ('z,'i,'o) ans = ('z,'i) in_channel -> ('z,'o) out_channel -> 'z

type ('a,'z,'i,'o) proc = ('a -> ('z,'i,'o) ans) -> ('z,'i,'o) ans

let send o k in_ch (Out sendd) =
  sendd o (In(fun out_ch -> k () in_ch out_ch))

let recv k (In recv) out_ch =
  recv (Out(fun v in_ch -> k v in_ch out_ch))

let exit_k x _ _ = x

let (<|>>) p1 p2 _ in_ch out_ch =
  p2 exit_k
    (In(fun m_ch -> p1 exit_k in_ch m_ch))
    out_ch

let rec stdin_ch =
  In (fun (Out send) -> send (read_line ()) stdin_ch)

let rec stdout_ch =
  Out (fun str (In recv) -> print_endline str; recv stdout_ch)

let run p = p exit_k stdin_ch stdout_ch