# tricky cases involving input from cap, esp. class merging

resource r ()

    # in input stmt below, a1 and a2 must be in same class
    # even though use of a2's cap comes lexically after the input stmt
    op setcap ()
    op a1 (int); op a2 (int)
    cap (int) a3
    setcap ()
    send a1(11)
    send a2(12)
    send a1(13)
    send a2(14)
    while (true) {
	in a1(n) -> write("a1:", n)
	[] a3(n) -> write("a2:", n)
	[] else -> exit
	ni
    }

    proc setcap ()  {
	a3 = a2
    }

    # b1 and b2 must be merged although they're assignment-incompatible
    op b1 (char) {send}
    op b2 (char)
    send b1('A')
    send b2('B')
    cap (char){send} b3 = b1
    cap (char)       b4 = b2
    send b1('C')
    send b2('D')
    while (true) {
	in b3(c) -> write("b3:", c)
	[] b4(c) -> write("b4:", c)
	[] else -> exit
	ni
    }

    #  forwards inside nested inputs from opcaps
    optype iop (int)
    cap iop chico = new (iop)
    cap (int) harpo = new (op (int))
    op fini ()

    process listener  {
	while (true) {
	    in chico (i) ->
		write ("chico", i)
		forward harpo (i + 2)
		in harpo (j) ->
		    write ("   harpo", i, j)
		    if (i % 2 == 1) {
			write ("   fwd")
			forward chico (j + 3)
		    }
		ni
	    [] harpo (j) ->
		write ("harpo", j)
	    [] fini () ->
		write ("fini")
		exit
	    ni
	}
    }

    harpo (3)
    chico (5)
    fini ()
    write ("done")

end
