Guest User

Untitled

a guest
Jul 1st, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 1.35 KB | None | 0 0
  1. type buyer =
  2.     { id: string;
  3.       accept: unit Join.chan;
  4.       refuse: int Join.chan;
  5.       win: unit Join.chan }
  6.  
  7. def seller(Some buyer, _) & bid_timeout() = buyer.win()
  8. or  seller(win, curr_bid) & bid(buyer, amount) =
  9.   print_endline (buyer.id ^ " bet " ^ (string_of_int amount));
  10.   if curr_bid < amount then
  11.     match win with
  12.     Some a -> seller(Some buyer, amount) & buyer.accept() & a.refuse(amount)
  13.       | _ -> seller(Some buyer, amount) & buyer.accept()
  14.   else seller(win, curr_bid) & buyer.refuse(curr_bid)
  15.  
  16. let mk_buyer id init_amount =
  17.   def buyer(money) & refuse(curr_bid) =
  18.     print_endline (id ^ " was refused; money: " ^ (string_of_int money) ^ "; current bid: " ^ (string_of_int curr_bid));
  19.     if money > 0 && money > curr_bid then begin
  20.       print_endline (id ^ " will bid " ^ (string_of_int (curr_bid + 1)));
  21.       bid(self(), curr_bid + 1) & buyer(money - curr_bid - 1)
  22.     end
  23.   or  buyer(money) & accept() =
  24.     print_endline (id ^ " was accepted");
  25.     buyer(money)
  26.   or  buyer(_) & win() =
  27.     print_endline (id ^ " won!"); 0
  28.   and self() = reply { id = id;
  29.                accept = accept;
  30.                refuse = refuse;
  31.                win = win } to self in
  32.   spawn buyer(init_amount) & bid(self(), 1)
  33.  
  34. def timer(t) =
  35.   Unix.sleep t;
  36.   spawn bid_timeout(); 0
  37.  
  38. let start () =
  39.   spawn timer(10) & seller(None, 0);
  40.   mk_buyer "a" 10;
  41.   mk_buyer "b" 2;
  42.   mk_buyer "c" 3
Add Comment
Please, Sign In to add comment