Zulip Chat Archive

Stream: lean4

Topic: What's @& ?


Daniel Weber (Jul 01 2024 at 17:49):

I see in some places in the code @& in a type annotation, for instance in docs#Nat.log2 and docs#Nat.gcd. What does it mean?

Henrik Böving (Jul 01 2024 at 17:50):

They are ref count borrowing annotations, usually they are inferred by the compiler but for external functions the tags need to be provided explicitly. The tags instruct the compiler to not increment the ref count before sending a value to the function but instead assume that the function will not free the value.

Kyle Miller (Jul 01 2024 at 17:55):

I think that description might be misunderstood. Normally, the semantics are that ownership of a value is transferred in a function call, but @& indicates that the ownership is borrowed instead.

Normally, if a value is used after the function returns, then the compiler inserts a ref count increase before the call, but if it is not used, it's not inserted. (The function is responsible for a ref count decrement for that argument.)

When @& is used, then instead it's sort of dualized. If a value is not used after the function returns, the compiler inserts a ref count decrement after the call, but if it is used it's not inserted. (The function is not responsible for a ref count decrement for that argument.)

(I think I got this right, correct me if I'm wrong.)

Eric Wieser (Jul 01 2024 at 20:43):

Any change you could take over lean4#4305 for me, Kyle?


Last updated: May 02 2025 at 03:31 UTC