@@ -1548,22 +1548,26 @@ language, but the developers of the differentiable programming feature will
1548
1548
prototype and pitch this change through Swift Evolution._
1549
1549
1550
1550
``` swift
1551
- func foo <T : Differentiable & AdditiveArithmetic >(_ x : T, _ y : T, _ z : T) -> T { ... }
1551
+ func foo <T : Differentiable & AdditiveArithmetic >(_ x : T, _ y : T, _ z : T) -> T
1552
+ where T == T.TangentVector { ... }
1552
1553
1553
1554
// Transpose with respect to all parameters, making `foo(_:_:_:)` linear with
1554
1555
// with respect to all parameters.
1555
1556
@transpose (of: foo)
1556
- func _ <T : Differentiable & AdditiveArithmetic >(_ v : T) -> (x: T, y: T, z: T) { ... }
1557
+ func _ <T : Differentiable & AdditiveArithmetic >(_ v : T) -> (x: T, y: T, z: T)
1558
+ where T == T.TangentVector { ... }
1557
1559
1558
1560
// Transpose with respect to original parameter `x`, making `foo(_:_:_:)`
1559
1561
// linear with respect to `x`.
1560
1562
@transpose (of: foo, wrt: 0 )
1561
- func _ <T : Differentiable & AdditiveArithmetic >(y : T, z : T, v : T) -> T { ... }
1563
+ func _ <T : Differentiable & AdditiveArithmetic >(y : T, z : T, v : T) -> T
1564
+ where T == T.TangentVector { ... }
1562
1565
1563
1566
// Transpose with respect to original parameters `x` and `z`, making
1564
1567
// `foo(_:_:_:)` linear with respect to `x` and `z`.
1565
1568
@transpose (of: foo, wrt: (0 , 2 ))
1566
- func _ <T : Differentiable & AdditiveArithmetic >(y : T, v : T) -> (x: T, z: T) { ... }
1569
+ func _ <T : Differentiable & AdditiveArithmetic >(y : T, v : T) -> (x: T, z: T)
1570
+ where T == T.TangentVector { ... }
1567
1571
```
1568
1572
1569
1573
###### Static methods
@@ -1575,24 +1579,28 @@ parameter cannot be a linearity parameter, because metatypes cannot conform to
1575
1579
1576
1580
``` swift
1577
1581
extension MyType {
1578
- static func foo <T : Differentiable & AdditiveArithmetic >(_ x : T, _ y : T, _ z : T) -> T { ... }
1582
+ static func foo <T : Differentiable & AdditiveArithmetic >(_ x : T, _ y : T, _ z : T) -> T
1583
+ where T == T.TangentVector { ... }
1579
1584
}
1580
1585
1581
1586
extension MyType {
1582
1587
// Transpose with respect to all parameters, making `foo(_:_:_:)` linear with
1583
1588
// with respect to all parameters.
1584
1589
@transpose (of: foo)
1585
- static func _ <T : Differentiable & AdditiveArithmetic >(_ v : T) -> (x: T, y: T, z: T) { ... }
1590
+ static func _ <T : Differentiable & AdditiveArithmetic >(_ v : T) -> (x: T, y: T, z: T)
1591
+ where T == T.TangentVector { ... }
1586
1592
1587
1593
// Transpose with respect to original parameter `x`, making `foo(_:_:_:)`
1588
1594
// linear with respect to `x`.
1589
1595
@transpose (of: foo, wrt: 0 )
1590
- static func _ <T : Differentiable & AdditiveArithmetic >(y : T, z : T, v : T) -> T { ... }
1596
+ static func _ <T : Differentiable & AdditiveArithmetic >(y : T, z : T, v : T) -> T
1597
+ where T == T.TangentVector { ... }
1591
1598
1592
1599
// Transpose with respect to original parameters `x` and `z`, making
1593
1600
// `foo(_:_:_:)` linear with respect to `x` and `z`.
1594
1601
@transpose (of: foo, wrt: (0 , 2 ))
1595
- static func _ <T : Differentiable & AdditiveArithmetic >(y : T, v : T) -> (x: T, z: T) { ... }
1602
+ static func _ <T : Differentiable & AdditiveArithmetic >(y : T, v : T) -> (x: T, z: T)
1603
+ where T == T.TangentVector { ... }
1596
1604
}
1597
1605
```
1598
1606
@@ -1605,7 +1613,9 @@ respect to each parameter). Here's how they are made differentiable in the
1605
1613
standard library.
1606
1614
1607
1615
``` swift
1608
- extension FloatingPoint where Self : Differentiable & AdditiveArithmetic {
1616
+ extension FloatingPoint
1617
+ where Self : Differentiable & AdditiveArithmetic , Self == TangentVector
1618
+ {
1609
1619
@transpose (of: + )
1610
1620
static func _ (_ v : Self ) -> (Self , Self ) { (v, v) }
1611
1621
@@ -1657,34 +1667,40 @@ A transpose of a static method is exactly like top-level functions except:
1657
1667
1658
1668
``` swift
1659
1669
extension MyType {
1660
- func foo <T : Differentiable & AdditiveArithmetic >(_ x : T, _ y : T, _ z : T) -> T { ... }
1670
+ func foo <T : Differentiable & AdditiveArithmetic >(_ x : T, _ y : T, _ z : T) -> T
1671
+ where T == T.TangentVector { ... }
1661
1672
}
1662
1673
1663
1674
extension MyType {
1664
1675
// Transpose with respect to all parameters, making `foo(_:_:_:)` linear with
1665
1676
// with respect to all parameters.
1666
1677
@transpose (of: foo)
1667
- func _ <T : Differentiable & AdditiveArithmetic >(_ v : T) -> (x: T, y: T, z: T) { ... }
1678
+ func _ <T : Differentiable & AdditiveArithmetic >(_ v : T) -> (x: T, y: T, z: T)
1679
+ where T == T.TangentVector { ... }
1668
1680
1669
1681
// Transpose with respect to original parameter `x`, making `foo(_:_:_:)`
1670
1682
// linear with respect to `x`.
1671
1683
@transpose (of: foo, wrt: 0 )
1672
- func _ <T : Differentiable & AdditiveArithmetic >(y : T, z : T, v : T) -> T { ... }
1684
+ func _ <T : Differentiable & AdditiveArithmetic >(y : T, z : T, v : T) -> T
1685
+ where T == T.TangentVector { ... }
1673
1686
1674
1687
// Transpose with respect to original parameters `x` and `z`, making
1675
1688
// `foo(_:_:_:)` linear with respect to `x` and `z`.
1676
1689
@transpose (of: foo, wrt: (0 , 2 ))
1677
- func _ <T : Differentiable & AdditiveArithmetic >(y : T, v : T) -> (x: T, z: T) { ... }
1690
+ func _ <T : Differentiable & AdditiveArithmetic >(y : T, v : T) -> (x: T, z: T)
1691
+ where T == T.TangentVector { ... }
1678
1692
1679
1693
// Transpose with respect to original parameters `self`, making `foo(_:_:_:)`
1680
1694
// linear with respect to `self`.
1681
1695
@transpose (of: foo, wrt: self )
1682
- static func _ <T : Differentiable & AdditiveArithmetic >(x : T, y : T, z : T, v : T) -> MyType { ... }
1696
+ static func _ <T : Differentiable & AdditiveArithmetic >(x : T, y : T, z : T, v : T) -> MyType
1697
+ where T == T.TangentVector { ... }
1683
1698
1684
1699
// Transpose with respect to original parameters `self`, `x` and `z`, making
1685
1700
// `foo(_:_:_:)` linear with respect to `self`, `x` and `z`.
1686
1701
@transpose (of: foo, wrt: (self , 0 , 2 ))
1687
- static func _ <T : Differentiable & AdditiveArithmetic >(y : T, v : T) -> (self: MyType, x: T, z: T) { ... }
1702
+ static func _ <T : Differentiable & AdditiveArithmetic >(y : T, v : T) -> (self: MyType, x: T, z: T)
1703
+ where T == T.TangentVector { ... }
1688
1704
}
1689
1705
```
1690
1706
@@ -1702,13 +1718,15 @@ in `@differentiable` attributes.
1702
1718
func foo <T , U , V >(_ x : T, _ y : U, _ z : V) -> W { ... }
1703
1719
1704
1720
// Transpose with respect to `x` and `z`, requiring that `T` and `V` to conform
1705
- // to `Differentiable & AdditiveArithmetic`.
1721
+ // to `Differentiable & AdditiveArithmetic` and equal their corresponding
1722
+ `TangentVector` types.
1706
1723
@transpose (of: foo, wrt: (x, z))
1707
1724
func _ <
1708
1725
T : Differentiable & AdditiveArithmetic ,
1709
1726
U ,
1710
1727
V : Differentiable & AdditiveArithmetic
1711
1728
>(_ y : U, _ v : W) -> (x: T, z: V)
1729
+ where T.TangentVector == T, V.TangentVector == V { ... }
1712
1730
```
1713
1731
1714
1732
##### Examples
@@ -1717,7 +1735,7 @@ Many floating-point operations are linear. Addition and subtraction are linear.
1717
1735
Multiplication is bilinear (linear with respect to each argument).
1718
1736
1719
1737
``` swift
1720
- extension FloatingPoint {
1738
+ extension FloatingPoint where Self : Differentiable , Self == TangentVector {
1721
1739
@inlinable
1722
1740
@transpose (of: + )
1723
1741
func _ (_ v : Self ) -> (Self , Self ) {
0 commit comments