Guest User

S0B1B2-pastebin.dats

a guest
Mar 15th, 2016
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.80 KB | None | 0 0
  1. (* ****** ****** *)
  2.  
  3. (*
  4. **
  5. ** For testing g-sessions
  6. **
  7. ** This famous example is taken from
  8. ** a paper on multiparty session types
  9. ** by Kohei Honda et al.
  10. **
  11. *)
  12.  
  13. (* ****** ****** *)
  14.  
  15. %{^
  16. //
  17. #include <pthread.h>
  18. //
  19. %} // end of [%{^]
  20.  
  21. (* ****** ****** *)
  22. //
  23. #include
  24. "share/atspre_staload.hats"
  25. //
  26. (* ****** ****** *)
  27.  
  28. staload
  29. UNISTD = "libc/SATS/unistd.sats"
  30.  
  31. (* ****** ****** *)
  32.  
  33. staload
  34. "./../SATS/basis_intset.sats"
  35. staload _ =
  36. "./../DATS/basis_intset.dats"
  37.  
  38. (* ****** ****** *)
  39. //
  40. staload
  41. "./../SATS/basis_ssntype.sats"
  42. staload
  43. "./../SATS/basis_ssntype2r.sats"
  44. //
  45. (* ****** ****** *)
  46. //
  47. staload _ =
  48. "libats/DATS/deqarray.dats"
  49. //
  50. (* ****** ****** *)
  51. //
  52. staload _ =
  53. "libats/DATS/athread.dats"
  54. staload _ =
  55. "libats/DATS/athread_posix.dats"
  56. //
  57. (* ****** ****** *)
  58. //
  59. staload _ =
  60. "./../DATS/basis_uchan.dats"
  61. staload _ =
  62. "./../DATS/basis_channel0.dats"
  63. staload _ =
  64. "./../DATS/basis_channel1.dats"
  65. staload _ =
  66. "./../DATS/basis_ssntype2r.dats"
  67. //
  68. (* ****** ****** *)
  69.  
  70. #define N 3
  71.  
  72. (* ****** ****** *)
  73.  
  74. #define SELLER 0
  75. #define BUYER1 1
  76. #define BUYER2 2
  77.  
  78. (* ****** ****** *)
  79.  
  80. typedef title = string
  81. typedef price = double
  82. typedef proof = string
  83. typedef receipt = string
  84.  
  85. (* ****** ****** *)
  86.  
  87. typedef
  88. ssn_s0b1b2_succ =
  89. msg(BUYER2,SELLER,proof) ::
  90. msg(SELLER,BUYER2,receipt) :: nil
  91.  
  92. typedef
  93. ssn_s0b1b2_fail = nil
  94.  
  95. typedef
  96. ssn_s0b1b2 =
  97. msg(BUYER1,SELLER,title) ::
  98. msg(SELLER,BUYER1,price) ::
  99. msg(SELLER,BUYER2,price) ::
  100. msg(BUYER1,BUYER2,price) ::
  101. choose(BUYER2,ssn_s0b1b2_succ,ssn_s0b1b2_fail)
  102.  
  103. (* ****** ****** *)
  104.  
  105. stadef S0 = iset(SELLER)
  106. stadef B1 = iset(BUYER1)
  107. stadef B2 = iset(BUYER2)
  108.  
  109. (* ****** ****** *)
  110.  
  111. local
  112. //
  113. prval() =
  114. lemma_iset_sing_is_member{SELLER}()
  115. prval() =
  116. lemma_iset_sing_isnot_member{SELLER,BUYER1}()
  117. prval() =
  118. lemma_iset_sing_isnot_member{SELLER,BUYER2}()
  119. //
  120. prval() = lemma_iset_sing_isnot_nil{SELLER}()
  121. //
  122. in (* in-of-local *)
  123.  
  124. fun
  125. fserv_seller
  126. (
  127. chan:
  128. channel1(S0, N, ssn_s0b1b2)
  129. ) : void = let
  130. //
  131. val
  132. title =
  133. channel1_recv_val(chan, BUYER1, SELLER)
  134. val () =
  135. println! ("SELLER: title = ", title)
  136. //
  137. val
  138. price =
  139. (
  140. case+
  141. title of
  142. | "book1" => 100.0
  143. | "book2" => 200.0
  144. | _(*other*) => 500.0
  145. ) : price
  146. //
  147. val () =
  148. channel1_send
  149. (chan, SELLER, BUYER1, price)
  150. val () =
  151. channel1_send
  152. (chan, SELLER, BUYER2, price)
  153. //
  154. val () = channel1_skipex(chan)
  155. //
  156. val tag =
  157. channel1_choose_tag(chan, BUYER2)
  158. //
  159. in
  160. //
  161. case+ tag of
  162. | choosetag_l() => fserv_seller_succ(chan)
  163. | choosetag_r() => fserv_seller_fail(chan)
  164. //
  165. end (* end of [fserv_seller] *)
  166.  
  167. and
  168. fserv_seller_succ
  169. (
  170. chan:
  171. channel1(S0, N, ssn_s0b1b2_succ)
  172. ) : void = let
  173. //
  174. val
  175. proof =
  176. channel1_recv_val(chan, BUYER2, SELLER)
  177. val
  178. receipt = "receipt-for-sale"
  179. val () =
  180. channel1_send(chan, SELLER, BUYER2, receipt)
  181. //
  182. in
  183. channel1_close(chan)
  184. end (* end of [fserv_seller_succ] *)
  185.  
  186. and
  187. fserv_seller_fail
  188. (
  189. chan:
  190. channel1(S0, N, ssn_s0b1b2_fail)
  191. ) : void =
  192. {
  193. val () = channel1_close(chan)
  194. val () = println! ("SELLER: no sale is made!")
  195. } (* end of [fserv_seller_fail] *)
  196.  
  197. end // end of [local]
  198.  
  199. (* ****** ****** *)
  200.  
  201. local
  202.  
  203. val
  204. title_ref = ref<string>("book1")
  205.  
  206. in
  207. //
  208. fun the_title_get() = !title_ref
  209. fun the_title_set(title: string): void = !title_ref := title
  210. //
  211. end // end of [local]
  212.  
  213. (* ****** ****** *)
  214.  
  215. local
  216. //
  217. prval() =
  218. lemma_iset_sing_is_member{BUYER1}()
  219. prval() =
  220. lemma_iset_sing_isnot_member{BUYER1,SELLER}()
  221. prval() =
  222. lemma_iset_sing_isnot_member{BUYER1,BUYER2}()
  223. //
  224. prval() = lemma_iset_sing_isnot_nil{BUYER1}()
  225. //
  226. in (* in-of-local *)
  227.  
  228. fun
  229. fserv_buyer1
  230. (
  231. chan:
  232. channel1(B1, N, ssn_s0b1b2)
  233. ) : void = let
  234. //
  235. val
  236. title = the_title_get()
  237. //
  238. val () =
  239. channel1_send
  240. (chan, BUYER1, SELLER, title)
  241. //
  242. val
  243. price =
  244. channel1_recv_val
  245. (chan, SELLER, BUYER1)
  246. val () =
  247. println!("BUYER1: price = ", price)
  248. //
  249. val () = channel1_skipex(chan)
  250. //
  251. val () =
  252. channel1_send
  253. (chan, BUYER1, BUYER2, price / 2)
  254. //
  255. val tag =
  256. channel1_choose_tag(chan, BUYER2)
  257. //
  258. in
  259. //
  260. case+ tag of
  261. | choosetag_l() => fserv_buyer1_succ(chan)
  262. | choosetag_r() => fserv_buyer1_fail(chan)
  263. //
  264. end // end of [fserv_buyer1]
  265.  
  266. and
  267. fserv_buyer1_succ
  268. (
  269. chan:
  270. channel1(B1, N, ssn_s0b1b2_succ)
  271. ) : void =
  272. {
  273. val () = channel1_skipex(chan)
  274. val () = channel1_skipex(chan)
  275. val ((*closed*)) = channel1_close(chan)
  276. } (* end of [fserv_buyer1_succ] *)
  277.  
  278. and
  279. fserv_buyer1_fail
  280. (
  281. chan:
  282. channel1(B1, N, ssn_s0b1b2_fail)
  283. ) : void =
  284. {
  285. val ((*closed*)) = channel1_close(chan)
  286. } (* end of [fserv_buyer1_fail] *)
  287.  
  288. end // end of [local]
  289.  
  290. (* ****** ****** *)
  291.  
  292. local
  293. //
  294. prval() =
  295. lemma_iset_sing_is_member{BUYER2}()
  296. prval() =
  297. lemma_iset_sing_isnot_member{BUYER2,SELLER}()
  298. prval() =
  299. lemma_iset_sing_isnot_member{BUYER2,BUYER1}()
  300. //
  301. prval() = lemma_iset_sing_isnot_nil{BUYER2}()
  302. //
  303. in (* in-of-local *)
  304.  
  305. fun
  306. fserv_buyer2
  307. (
  308. chan:
  309. channel1(B2, N, ssn_s0b1b2)
  310. ) : void = let
  311. //
  312. val () = channel1_skipex(chan)
  313. val () = channel1_skipex(chan)
  314. //
  315. val
  316. price =
  317. channel1_recv_val
  318. (chan, SELLER, BUYER2)
  319. val
  320. price2 =
  321. channel1_recv_val
  322. (chan, BUYER1, BUYER2)
  323. //
  324. val () =
  325. println!("BUYER2: price = ", price)
  326. val () =
  327. println!("BUYER2: price2 = ", price2)
  328. //
  329. val test = (price - price2 <= 100.0)
  330. //
  331. in
  332. //
  333. if test
  334. then {
  335. val () =
  336. channel1_choose_l(chan, BUYER2)
  337. val () = fserv_buyer2_succ(chan)
  338. } (* end of [then] *)
  339. else {
  340. val () =
  341. channel1_choose_r(chan, BUYER2)
  342. val () = fserv_buyer2_fail(chan)
  343. } (* end of [else] *)
  344. //
  345. end (* end of [fserv_buyer2] *)
  346.  
  347. and
  348. fserv_buyer2_succ
  349. (
  350. chan:
  351. channel1(B2, N, ssn_s0b1b2_succ)
  352. ) : void = let
  353. //
  354. val
  355. proof = "proof-of-payment"
  356. val () =
  357. channel1_send
  358. (chan, BUYER2, SELLER, proof)
  359. //
  360. val
  361. receipt =
  362. channel1_recv_val(chan, SELLER, BUYER2)
  363. val () =
  364. println! ("BUYER2: receipt = ", receipt)
  365. //
  366. in
  367. channel1_close(chan)
  368. end (* end of [fserv_buyer2_succ] *)
  369.  
  370. and
  371. fserv_buyer2_fail
  372. (
  373. chan:
  374. channel1(B2, N, ssn_s0b1b2_fail)
  375. ) : void =
  376. {
  377. val () = channel1_close(chan)
  378. } (* end of [fserv_buyer2_fail] *)
  379.  
  380. end // end of [local]
  381.  
  382. (* ****** ****** *)
  383. //
  384. // Try the following to see difference:
  385. //
  386. // ./S0B1B2 book1
  387. // ./S0B1B2 book2
  388. // ./S0B1B2 book3
  389. //
  390. implement
  391. main0
  392. (
  393. argc, argv
  394. ) = let
  395. //
  396. val () =
  397. if argc >= 2
  398. then the_title_set(argv[1])
  399. // end of [if]
  400. //
  401. val S0 = intset_int{N}(SELLER)
  402. val B1 = intset_int{N}(BUYER1)
  403. val B2 = intset_int{N}(BUYER2)
  404. //
  405. val chn0 =
  406. cchannel1_create_exn
  407. (
  408. N, S0, llam(chp) => fserv_seller(chp)
  409. ) (* end of [val] *)
  410. val chn2 =
  411. cchannel1_create_exn
  412. (
  413. N, B2, llam(chp) => fserv_buyer2(chp)
  414. ) (* end of [val] *)
  415. //
  416. val () = fserv_buyer1(channel1_link(chn0, chn2))
  417. //
  418. in
  419. ignoret($UNISTD.usleep(250000u)) // wait for S0 and B2
  420. end // end of [main0]
  421.  
  422. (* ****** ****** *)
  423.  
  424. (* end of [S0B1B2-pastebin.dats] *)
Advertisement
Add Comment
Please, Sign In to add comment