Download The JoCaml language Release 3.11 - The JoCaml system
Transcript
14 # # # # # # # # def or or or i1([]) & i2([]) = 0 i1(x::xs) & i2([]) = print_int x ; i1(xs) & i2([]) i1([]) & i2(y::ys) = print_int y ; i1([]) & i2(ys) i1(x::xs) & i2(y::ys) = if x < y then begin print_int x ; i1(xs) & i2(y::ys) end else if y < x then begin print_int y ; i1(x::xs) & i2(ys) end else begin print_int x ; i1(xs) & i2(ys) end ;; val i1 : int list Join.chan = <abstr> val i2 : int list Join.chan = <abstr> # spawn i1([1;3;4]) & i2([2;3]) # ;; - : unit = () 1234 It is important to notice that, by contrast with Objective Caml pattern matching, ambiguous matching are indeed ambiguous: as soon as a message matches a pattern it may be consumed, regardless of other receivers on the same channel. # def c([]) = echo_string "Nil" # or c(_) = echo_string "Anything" # ;; val c : ’a list Join.chan = <abstr> # spawn c([]) # ;; - : unit = () Anything In the example above, you can see either “Anything” or “Nil” depending upon unspecified implementation details. To get the textual priority rule of Objective Caml matching semantics, use the match construct. # def c(x) = # match x with # | [] -> echo_string "Nil" # | _ -> echo_string "Anything" # ;; val c : ’a list Join.chan = <abstr> # spawn c([]) # ;; - : unit = () Nil 1.3.3 Mixing asynchronous and synchronous channel definitions Join patterns are the programming paradigm for concurrency in JoCaml. They allow the encoding of many concurrent data structures. For instance, the following code defines a counter: