packages feed

alms-0.6.5: examples/ex34-session-types.alms

(* An example with session types, including choice. *)

#load "libthread"
#load "libsessiontype"

open SessionType

type 'a protocol = !'a; !'a; ?'a; 1
                   |+|
                   !'a; ?'a; 1

let server (c : int protocol dual channel) =
    match follow c with
    | Left c ->
        let (x, c) = recv c in
        let (y, c) = recv c in
          send (x + y) c;
          ()
    | Right c ->
        let (x, c) = recv c in
          send (0 - x) c;
          ()

let client c x y =
      let c = sel1 c in
      let c = send x c in
      let c = send y c in
      let (r, _) = recv c in
        r

let main x y =
  let rv = newRendezvous () in
    AThread.fork (fun () -> server (accept rv));
    client (request rv) x y

in print (main 3 4)