[swift-evolution] [Proposal idea] Support for pure functions

Jimmy Sambuo jsambuo at gmail.com
Sun Jan 10 15:27:32 CST 2016


Thanks Andrew and everyone for your ideas and inputs.

Andrew, your summary is amazing. It covers so many non-trivial cases of
this feature.
I believe this is the archive of the other thread:
https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160104/006005.html

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'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't expecting
mutation to occur, but this may be a valid scenario for "weakly pure"
functions, where parameters can be modified.

Chris had mentioned that there probably should be some way to to unsafely
"force a call to a non-pure function to be allowed in a pure one," and that it
would probably make sense as an attribute @pure instead of a declmodifier.
Perhaps this is another form of "weakly pure" functions. I'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.

Imagine if ! was used to call an impure function within a pure function:

```swift
/// Wrapper around C sin()
func sin(x: Double) @pure -> Double {
    return sin(x)!
}
```

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'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 "impure" value or side effect occurs? How
would the runtime know this if purity is defined by annotations? Maybe it
shouldn't crash, but now ! is slightly different from optionals and try!.
Perhaps there should be a different symbol for "unwrapping" impure
functions. Then there would be two different symbols for unwrapping.

My major concern is I'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'm not sure of a good way to prevent this.


On Sun, Jan 10, 2016 at 12:41 AM, Andrew Bennett via swift-evolution <
swift-evolution at swift.org> wrote:

> Hi,
>
> I started another thread on @pure in swift, luckily Chris Lattner reminded
> me about this one. I'm going to continue any discussion here so we don't
> fragment the conversation.
>
> The other thread was a pre-proposal discussion called "Proposal proposal:
> @pure keyword", it isn't archived so I cannot link it.
>
> I've summarised the progress so far here (it's in the proposals directory
> for convenience):
>
>
> https://github.com/therealbnut/swift/blob/therealbnut-pure-preproprosal/docs/proposals/PureKeyword.rst
>
> If I've missed anything or you want to update, clarify, fix typos, etc.
> please submit a PR :) I'm trying to keep it focused on things that have
> little contention.
>
> I've tried to unify the ideas from both the other thread and this one into
> that summary. As it's not really a proposal I haven'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.
>
>
> On Tue, Dec 22, 2015 at 9:08 AM, Joe Groff via swift-evolution <
> swift-evolution at swift.org> wrote:
>
>>
>> > On Dec 21, 2015, at 2:04 PM, Alex Popov via swift-evolution <
>> swift-evolution at swift.org> wrote:
>> >
>> > 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.
>>
>>
>> I don'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.
>>
>> -Joe
>>
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>
>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>


-- 
Jimmy Sambuo
www.sambuo.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160110/39e194c1/attachment.html>


More information about the swift-evolution mailing list