<div dir="ltr">Hi Jimmy,<div><br></div><div>Interesting ideas with !, although I&#39;m worried it would clash with other uses of the operator. For example: optional return types. There&#39;s probably more precedent for this syntax: <font face="monospace, monospace">sin!(x)</font>, but it will also clash in some cases (optional function in objc protocol, optional closure).</div><div><br></div><div>I think something much more verbose (a function) would be fine as it&#39;s probably not going to be called often. I expect initially it&#39;s probably sufficient for it to be a Builtin function only used in the standard library. Failing that a global clearly unsafe function: unsafelyPurifyFunction(...).</div><div><br></div><div>With <font face="monospace, monospace">inout</font> I don&#39;t think it is actually weaker, I could be wrong. I see the following as equivalent (from a purity standpoint):</div><div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><font face="monospace, monospace">func test1(inout a: Int) @pure -&gt; Double {}</font></div><div><font face="monospace, monospace">x = test1(&amp;a)</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">func test2(a: Int) @pure -&gt; (Double, Int) {}</font></div><div><font face="monospace, monospace">(x, a) = test2(a)</font></div></blockquote><br><div>If they&#39;re equivalent then this can be pure:</div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><font face="monospace, monospace">extension Int {</font></div></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><font face="monospace, monospace">mutating func addSome(x: Int) @pure {</font></div></blockquote></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><font face="monospace, monospace">self += x</font></div></blockquote></blockquote></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><font face="monospace, monospace">}</font></div></blockquote></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><font face="monospace, monospace">}</font></div></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><font face="monospace, monospace">let x: (inout Int, Int) </font><span style="font-family:monospace,monospace">@pure </span><span style="font-family:monospace,monospace">-&gt; Void = Int.addOne</span></div></blockquote><div><br></div><div>However the curried function signature must be adjusted so it cannot be partially applied, previously it would have been this:<br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><span style="font-family:monospace,monospace">let x: (inout Int) -&gt; (Int) -&gt; Void = Int.addOne</span></div></blockquote><div><br></div><div>If we&#39;re happy with this equivalence, I don&#39;t think it&#39;s a useful to distinguish <font face="monospace, monospace">inout</font> and other pure functions (no need for @pure(unsafe) here). As long as the parameters predictably cover everything affected I am happy.</div><div><br></div><div>Reference types may be an issue here, but no more than elsewhere.</div><div><br></div><div>----</div><div><br></div><div>As far as weak-pure goes: I think references have a lot of issues with @pure. It may be necessary to know if any types a @pure function uses interact at all with a reference type, stores it, uses it to compute, etc.</div><div><br></div><div>A gotcha example:</div><div><br></div><div>AnySequence uses a class internally (dynamic dispatch for type erasure), a pure function may want to return AnySequence, which appears to be a value type. See ExistentialCollection.swift.gyb.</div><div><br></div><div>The compiler may not be able to ensure two sequences with the same input are equivalent because their internal class has a different reference. Equitable could definitely work, but at compile time?</div><div><br></div><div>This is going to be a problem for referential transparency.</div><div><br></div><div>Possible Solutions</div><div> 1) Reference types or types composed of reference types are initially unavailable to @pure functions, unless locally scoped.</div><div> 2) Relax the referential transparency requirement when the output is a reference type or composed of reference types.</div><div> 3) Add a @copy keyword for types, this would mean it can be safely use copy-on-write with no shared references. I think this would be useful in other cases too, but should be avoided if possible as it&#39;s a large change that would require other proposal(s).</div><div><br></div><div>Not entirely a solution, but interesting:</div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div>You cannot return a newly allocated reference type, but you can return a closure: <font face="monospace, monospace">() -&gt; ReferenceType</font></div></blockquote><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px">It can still use memoization to always return the same closure, avoid any computation to derive the closure, but the closure doesn&#39;t necessarily return the same reference.</blockquote><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jan 11, 2016 at 8:27 AM, Jimmy Sambuo <span dir="ltr">&lt;<a href="mailto:jsambuo@gmail.com" target="_blank">jsambuo@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Thanks Andrew and everyone for your ideas and inputs.<div><br></div><div>Andrew, your summary is amazing. It covers so many non-trivial cases of this feature.</div><div>I believe this is the archive of the other thread: <a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160104/006005.html" target="_blank">https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160104/006005.html</a><br></div><div><br></div><div>I was hesitant to add more to this discussion because I was only able to think of the trivial case, and I know this can have lots of hidden complexities and implications. Looking at Andrew&#39;s summary gave me some ideas for talking points. Specifically, the section with inout parameters and pure mutating functions seems strange to me as I wasn&#39;t expecting mutation to occur, but this may be a valid scenario for &quot;weakly pure&quot; functions, where parameters can be modified.</div><div><br></div><div>Chris had mentioned that there probably should be some way to <span style="font-size:12.8px">to unsafely &quot;force a call to a non-pure function to be allowed in a pure one,&quot; and that </span><span style="font-size:12.8px">it would probably make sense as an attribute @pure instead of a declmodifier. Perhaps this is another form of &quot;weakly pure&quot; functions. I&#39;m thinking in this case, pure is acting as a marker, the type system does not have to know about actual purity and an effects system would not have to be implemented in this case. This should make implementation simpler and less invasive to the existing type system.</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">Imagine if ! was used to call an impure function within a pure function:</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">```swift</span></div><div><span style="font-size:12.8px">/// Wrapper around C sin()</span></div><div><span style="font-size:12.8px">func sin(x: Double) @pure -&gt; Double {</span></div><div><span style="font-size:12.8px">    return sin(x)!</span></div><div><span style="font-size:12.8px">}</span></div><div><span style="font-size:12.8px">```</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">In my mind, seeing ! tells me something could be wrong there so pay closer attention. I would think `sin` is a C function so Swift doesn&#39;t know it should be pure, but sine is a math function so it should be safe. What about for actually impure functions? Optionals and try! crashes when expectations are not met. Should `potentiallyImpureFunc()!` or `mostlyPureFunc()!` crash if an &quot;impure&quot; value or side effect occurs? How would the runtime know this if purity is defined by annotations? Maybe it shouldn&#39;t crash, but now ! is slightly different from optionals and try!. Perhaps there should be a different symbol for &quot;unwrapping&quot; impure functions. Then there would be two different symbols for unwrapping.</span></div><div><br></div><div><span style="font-size:12.8px">My major concern is I&#39;d want to avoid a situation where developers are adding @pure and unwrapping impure functions just to make their app compile, making @pure meaningless since most of their pure functions will actually be impure. I&#39;m not sure of a good way to prevent this.</span></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="h5">On Sun, Jan 10, 2016 at 12:41 AM, Andrew Bennett via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5"><div dir="ltr">Hi,<div><br></div><div>I started another thread on @pure in swift, luckily Chris Lattner reminded me about this one. I&#39;m going to continue any discussion here so we don&#39;t fragment the conversation.</div><div><br></div><div>The other thread was a pre-proposal discussion called &quot;Proposal proposal: @pure keyword&quot;, it isn&#39;t archived so I cannot link it.</div><div><br></div><div>I&#39;ve summarised the progress so far here (it&#39;s in the proposals directory for convenience):</div><div><br></div><div><a href="https://github.com/therealbnut/swift/blob/therealbnut-pure-preproprosal/docs/proposals/PureKeyword.rst" target="_blank">https://github.com/therealbnut/swift/blob/therealbnut-pure-preproprosal/docs/proposals/PureKeyword.rst</a></div><div><br></div><div>If I&#39;ve missed anything or you want to update, clarify, fix typos, etc. please submit a PR :) I&#39;m trying to keep it focused on things that have little contention.</div><div><br></div><div>I&#39;ve tried to unify the ideas from both the other thread and this one into that summary. As it&#39;s not really a proposal I haven&#39;t included the excellent justifications that Jimmy initially stated, they can be added if it becomes a proposal. Please add a PR if you would like them there.</div><div><br></div></div><div><div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Dec 22, 2015 at 9:08 AM, Joe Groff via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span><br>
&gt; On Dec 21, 2015, at 2:04 PM, Alex Popov via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; wrote:<br>
&gt;<br>
&gt; Slight tangent, would a guarantee of purity also allow for more Tail-Call Optimizations? A cursory glance at SO seems to point to TCO not always being applied, especially when ARC is involved.<br>
<br>
<br>
</span>I don&#39;t think any reasonable meaning for `pure` in Swift would affect the possibility of TCO. There was another thread about TCO here you might read back on; as I explained there, ARC is not a barrier to TCO, our ownership and machine-level calling conventions are. We would need to be able to use a specific calling convention for guaranteed-TCOable entry points.<br>
<br>
-Joe<br>
<div><div><br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</div></div></blockquote></div><br></div>
</div></div></div></div><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=nCigty3f1lpVtTdtXLf27rKWzt-2FQNpAdFrHTP2CgQfcEXdP1XrcbLV8nj8B-2FtLoIhR2Jtyr6QNrodSpraBgJZ7MBAlMc2kGuCmLmvPFm2Xmd6H17KiUQULnWU1aFaDthPdvZvXimQ9ragliau4vlAW33QSstCQxuDdj1ucR5eIc3YeJ0JtBHIW6xs2uCVi-2FgbY6Gj4uCCNXDlyOv5ciYFfWJCcRWRc7-2Ba1bMdFoSnbo-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0!important;margin-top:0!important;margin-bottom:0!important;margin-right:0!important;margin-left:0!important;padding-top:0!important;padding-bottom:0!important;padding-right:0!important;padding-left:0!important">
<br><span class="">_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></span></blockquote></div><span class="HOEnZb"><font color="#888888"><br><br clear="all"><div><br></div>-- <br><div>Jimmy Sambuo<br><a href="http://www.sambuo.com" target="_blank">www.sambuo.com</a></div>
</font></span></div>
</blockquote></div><br></div>