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

[tlaplus] Formalizing second-order operator parameters to recursive operators



As is popularly known, second-order operator parameters to recursive operators are currently allowed by TLC due to a bug. This lets you write useful operators like map/filter/fold over sets or sequences in the usual way.

I was talking to the GPU demons about Lamport & Gonthier's paper defining semantics for recursive operators because I couldn't understand it very well myself. It pointed out that a commonly-used form of second-order operator parameters to recursive operators could be easily formalized within TLA+, using only syntactic transformation. Specifically, the form where the second-order operator does not change within the body of the recursive operator; so the currently illegal (outside TLC) recursive operator written as:

RECURSIVE Fold(Op(_,_), _, _)
Fold(Op(_, _), acc, s) == 
  IF s = <<>>
  THEN acc
  ELSE Fold(Op, Op(acc, Head(s)), Tail(s))

Can be written as this functionally equivalent totally legal operator:
Fold(Op(_, _), acc, s) ==
    LET RECURSIVE F(_, _)
        F(a, t) ==
            IF t = <<>>
            THEN a
            ELSE F(Op(a, Head(t)), Tail(t))
    IN F(acc, s)
Stephan Merz suggested this method back in 2015. Basically you hold the second-order operator fixed outside the recursive operator definition, and recurse only on the non-operator parameters. This method is generally known as a compiler optimization called static argument transformation which avoids having to repeatedly copy invariant recursive operator arguments to the stack.

Of course, this does not handle the really difficult case, which is what happens when the second-order operator does change within the body of the recursive operator; something like:
RECURSIVE F(Op(_), _)
F(Op(_), n) ==
    IF n = 0
    THEN Op(0)
    ELSE F(LAMBDA x : Op(x) + 1, n - 1)
The meaning of this is not easily-definable within the formal semantics of TLA+. Of course we can all imagine how easy this is to write an interpreter for (even by accident, as TLC does), but here we are caring about the formal meaning such that TLAPS could be extended to write proofs over it.

So in conclusion I think it could be viable to rescue TLC's buggy behavior by formalizing it to only allow invariant second-order operator parameters. They would be defined as a syntactic transformation similar to Stephan's 2015 suggestion. It would require changing TLC to allow second-order operator parameters in any argument position, but also require writing quite a complicated bit of static analysis. It wouldn't be sufficient to just ensure that each usage of the recursive operator in the recursive operator's body supplies the same second-order operator parameter(s) in the same position(s), since you also have to handle mutually-recursive operators and before you know it you're writing a cycle detection algorithm over a call graph. But it's possible! You could even get minor performance improvements for TLC, since it would let TLC implement static argument transformation in the general case!

Of course, this latter piece of work could be obviated by doing the semantic legwork of defining the meaning of second-order operator parameters generally. It seems like a fun project, at least.

Andrew Helwer

--
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 view this discussion visit https://groups.google.com/d/msgid/tlaplus/CABj%3DxUV0BJdDuwmbZ5Q94iNKZd9%3DKqWyHdZkjB0e6TrcE%2BqT3A%40mail.gmail.com.