[swift-evolution] Revisiting SE-0110

Djura Retired Hunter retired.hunter.djura at gmail.com
Sun Jun 25 03:04:55 CDT 2017


The “inout” modifier is probably one of the main culprits here. While it is certainly very convenient to use (and I personally use it a lot), its very existence seems to have some downsides that restrict Swift design decisions.

Maybe there is a solution though: make “inout” distribute over elements of a tuple, something that actually already happens. The following code compiles and works in Xcode 9 beta:

———

protocol Incrementable {
    mutating func addOne()
}

func incrementFirstAndPrintSecond1<A: Incrementable,B>(tuple: inout (A, B)) {
    tuple.0.addOne()
    print(tuple.1)
}

extension Int: Incrementable {
    mutating func addOne() {
        self += 1
    }
}

var m1 = (42,"this will be printed")
incrementFirstAndPrintSecond1(tuple: &m1) /// prints "this will be printed"
print(m1.0) /// prints "43"

func incrementFirstAndPrintSecond2<A: Incrementable,B>(first: inout A, second: inout B) {
    first.addOne()
    print(second)
}

var m21 = 42
var m22 = "this will be printed"
incrementFirstAndPrintSecond2(first: &m21, second: &m22) /// prints "this will be printed"
print(m21) /// prints "43"

func applying1<A>(value: A, f: (inout A) -> ()) -> A {
    var m = value
    f(&m)
    return m
}

let x1 = applying1(value: 42) { (a: inout Int) in
    a.addOne()
}
print(x1) /// prints "43"

func applying2<A,B>(value: (A,B), f: (inout (A,B)) -> ()) -> (A,B) {
    var m = value
    f(&m)
    return m
}

let x2 = applying2(value: (42,"OK")) { (tuple: inout (Int,String)) in
    tuple.0.addOne()
}
print(x2) /// prints "(43, OK)"

func applying3<A,B>(first: A, second: B, f: (inout A, inout B) -> ()) -> (A,B) {
    var m1 = first
    var m2 = second
    f(&m1,&m2)
    return (m1,m2)
}

let x3 = applying3(first: 42, second: "OK") { (a: inout Int, b: inout String) in
    a.addOne()
}
print(x3) /// prints "(43, OK)"

———

As you can see, a function with a single inout tuple corresponds to a function with two inout arguments. This means that in a hypothetic model with isomorphic tuples and arguments, a tuple could be used in place of a subset of the arguments only if:

- none of the arguments in the subset is inout;
- all the arguments in the subset are inout, thus the tuple is to be considered wholly inout;

Obviously the subset could include all arguments. This way we could take 100% advantage of tuple isomorphism for functions with no inout arguments (thus, most of them), and still gain something where inout is there.

———

 About your second point, I’m definitely not an expert in type checkers, but if every composition of tuples was always reduced to simplest flattened form, maybe the type checker could have an even easier time. What I mean is that, because something like ((A,B),(C,(D,E))) could be considered equivalent to (A,B,C,D,E), the compiler could reduce the first tuple to the second, and then check the latter against a potential consumer. It seems to me that structuring-destructuring exists as a problem only when we need to differentiate isomorphic tuples just by their structure: without this need, the problems goes away. 

Still, I don't have a deep enough understanding of how the compiler works to analyze this second problem in full. And as I said in my first post, if the distinctions are truly important at the compiler level, and there are impossible challenges to overcome in finite space and time to acquire a feature like tuple isomorphism, then there's not much to do about it. My suggestion is simply that, if we need to define some workarounds, we should take into account the fact that at the usage point tuples with same content are equivalent, and a potential new model for the SE-110 aftermath should at least consider this idea.

Thanks


Elviro


> Il giorno 25 giu 2017, alle ore 06:28, David Hart <david at hartbit.com> ha scritto:
> 
> I think the Core Team initially (pre-Swift 1) wanted to make tuples and arguments isomorphic. But that model has many downsides which forced us to backtrack from that model:
> 
> • Modifiers on arguments (like inout) would force us to introduce them as part of the type-system on tuples, which makes for a messy model,
> • Tuple structuring and de-structuring greatly complicates the work of the type-checker.
> 
> The decision was made early in the Swift-evolution timeframe to strip what was left of the old model. It's much too late to backtrack now. Even if one wanted to, you'd have to address the original concerns that drove us to the current model.
> 
> David.
> 
> On 24 Jun 2017, at 21:12, Djura Retired Hunter via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
> 
>>>>> 
>>>>> 
>>>>>> On Jun 23, 2017, at 7:46 AM, Elviro Rocca via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>>>>>> 
>>>>>> It's probably late to just casually add a couple of cents to a discussion that has been going for so long, but it seems to me that from a user standpoint, that uses types to structure their programs and define logic and relationships, isomorphic types should be considered the same by the compiler. The added burden of distinguishing between, to say, a function that takes 2 arguments and one that takes a single tuple of two arguments doesn't seem useful at all, at least from the standpoint of the types involves. All the rest, like named parameters or tuple labels, are just really about style and convenience, but isomorphic types, while not strictly equal (the very concept of "equal" is in fact a huge deal in abstract mathematics) are for all means "equivalent" for the world-modeler.
>>>>> 
>>>>> Doesn’t seem useful?…
>>>>> 
>>>>> let myFunc: (MyTypeAlias) -> Int = /* … */
>>>>> 
>>>>> Does the function pointer have a single parameter? Or does it trigger Super-Secret Tuple-Destructing mode and actually indicate two parameters? My secret unknown single type should always be a single type, no matter what kind of type it is.
>>> 
>>> (A, B, C)  ((A, B), C)  (A, (B, C))
>>> 
>>> You’re saying partitions aren’t important. I’m saying that they are. Even though the second two tuples above are implemented like the first, I wouldn’t want them to be indistinguishable from an user’s standpoint. I wouldn’t want my two-argument functions magically become a three-argument one due to implementation details.
>> 
>> Why are partitions, just partitions, of tuples important? And why should anybody even consider writing a function that takes a tuple as "single argument" instead of just taking two arguments?
>> 
>> The difference between the following two functions is completely meaningless from a user standpoint:
>> 
>> func x<A,B> (tuple: (first: A, second: B))
>> func y<A,B> (first: A, second: B)
>> 
>>> 
>>> My previous example will stay an one-argument function for any non-tuple type behind the alias. But if it’s a tuple type, my assumption breaks because your rules would ban tuples from being first-class types.
>>> 
>> 
>> 
>> What? Why my rules say that tuples are not first class types? I'm just saying that equivalent tuples should be allowed as arguments for functions that take equivalent tuples as arguments.
>> 
>> You examples shows a function that takes a "MyTypeAlias" as input:
>> 
>> - if MyTypeAlias is an alias for, say, a "Person", that function can be called with an instance of "Person"
>> - if MyTypeAlias is an alias for (Int,Int), that function can be called with a couple of Int
>> 
>> What's the matter?
>> 
>> 
>> Elviro
>> 
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org <mailto:swift-evolution at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-evolution <https://lists.swift.org/mailman/listinfo/swift-evolution>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170625/e98880a8/attachment.html>


More information about the swift-evolution mailing list