[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [tlaplus] how to updating state and return in a definition



Thanks for the reply. Is there a way to emulate "returning" a value? My module A is being used as an instance in another module B, but I want A to be as isolated as possible.
I came up with something like below. Is this acceptable?

CONSTANTS Participants
VARIABLE counters

Init == counters = [i \in Participants |-> 0]


UpdateCounter(participant, newCounter) ==

   IF /\ counters[participant] < newCounter THEN

      /\ counters' = [counters EXCEPT ![participant] = newCounter]

      /\ TRUE

   ELSE

      /\ FALSE

      /\ UNCHANGED << counters >>


UpdateAndGet(participant, newCounter) ==

   IF UpdateCounter(participant, newCounter) THEN

      [p |-> participant, c |-> newCounter]

   ELSE

      [p |-> participant, c |-> counters[participant]]





On Wednesday, May 15, 2019 at 4:26:35 AM UTC-4, Stephan Merz wrote:
Hello,

if you remove the empty pair of parentheses, your definition is correct TLA+. It defines an operator whose effect it is to increment the state variable val and in can be used inside an action like so:

MyAction ==
  /\ ...
  /\ Calculate   \* update variable val
  /\ ...

There is no notion of "returning" an update of a variable.

If you'd rather compute a value that can be used within an _expression_ you can define

ValPlusOne == val + 1

and have something like

f' = [f EXCEPT ![p] = ValPlusOne]

for updating an array at a certain position.

Regards,
Stephan

On 15 May 2019, at 02:54, Balaji Arun <ba2...@xxxxxx> wrote:

I am trying to have a specification with a definition for Calculate()

module A:

VARIABLE val

Calculate() ==
   val
' = val + 1
   (*return [v: val] here*)

How do I return that val to whoever is calling it (even another module that instantiates module A)? Assuming multiple processes call Calculate concurrently.

--
You received this message because you are subscribed to the Google Groups "tlaplus" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tla...@googlegroups.com.
To post to this group, send email to tla...@xxxxxxxxxxxxxxxx.
Visit this group at https://groups.google.com/group/tlaplus.
To view this discussion on the web visit https://groups.google.com/d/msgid/tlaplus/d6598cfb-609a-4f6c-98a6-b12d58e6c6b4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "tlaplus" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tlaplus+unsubscribe@xxxxxxxxxxxxxxxx.
To post to this group, send email to tlaplus@xxxxxxxxxxxxxxxx.
Visit this group at https://groups.google.com/group/tlaplus.
To view this discussion on the web visit https://groups.google.com/d/msgid/tlaplus/5430a4d4-784b-4ee4-8eb3-b9061c008fbe%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.