utahplt / chorex

Choreographic programming in Elixir
https://hex.pm/packages/chorex
MIT License
14 stars 0 forks source link

Counter example code #8

Open bennn opened 3 weeks ago

bennn commented 3 weeks ago

Possible code for the counter example: https://doi.org/10.1016/j.jlamp.2023.100891

def loop(dummy):
  if Client.continue()
    Client[L] ~> Server
    Client.incr() ~> Server.incr_amt
    Server.do_incr() # add incr_amt to a running total
    loop(dummy_val)
  else
    Client.[R] ~> Server
    Server.(running_total) ~> Client.(final_amount)
  end
end

Does it work in Chorex?

EDIT: how to functional update the server's counter value?

bennn commented 2 weeks ago

Are recursive functions like loop not allowed? I'm getting an error.

Code:

  defmodule TestCounterChor do
    defchor [Client, Server] do
      def loop(dummy) do
        if Client.continue(dummy) do
          Client[L] ~> Server
          Client.incr(2) ~> Server.incr_amt
          Server.do_incr()
          loop(0)
        else
          Client[R] ~> Server
          Server.(running_total) ~> Client.(final_amount)
          Client.(final_amount)
        end
      end

      loop(4)
    end
  end

Error

** (MatchError) no match of right hand side value: 0
    (chorex 0.1.0) lib/chorex.ex:477: Chorex.project/3
    ....
bennn commented 2 weeks ago

TODO implement as a singleton / genserver proxy once those are in.