Skip to content

[API] Derive conformances for AdditiveArithmetic and VectorNumeric. #21568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 30, 2018

Conversation

dan-zheng
Copy link
Contributor

Implement AdditiveArithmetic and VectorNumeric derived conformances for
struct types.

  • AdditiveArithmetic conformances can be derived for struct types whose
    members all conform to AdditiveArithmetic.
  • VectorNumeric conformances can be derived for struct types whose members
    all conform to VectorNumeric and all share the same Scalar associated
    type.

Derived conformances improve the usability of these math protocols for
user-defined types.
Derived conformances can be extended for enum types in the future.

Examples:

struct IntFloat: AdditiveArithmetic {
  // Synthesis requirement: all members must conform to `AdditiveArithmetic`.
  var int: Int
  var float: Float

  // Compiler synthesizes:
  // static var zero: IntFloat {
  //   return IntFloat(int: Int.zero, float: Float.zero)
  // }
  // static func + (lhs: IntFloat, rhs: IntFloat) -> IntFloat {
  //   return IntFloat(int: lhs.int + rhs.int, float: lhs.float + rhs.float)
  // }
  // static func - (lhs: IntFloat, rhs: IntFloat) -> IntFloat {
  //   return IntFloat(int: lhs.int - rhs.int, float: lhs.float - rhs.float)
  // }
}

// Generic type.
struct Vector2<T : VectorNumeric>: VectorNumeric {
  // Synthesis requirement: all members must conform to `VectorNumeric`
  // and share the same `Scalar` type.
  var x: T
  var y: T

  // Compiler synthesizes:
  // <all three `AdditiveArithmetic` requirements from above>
  // typealias Scalar = T.Scalar
  // static func * (lhs: Scalar, rhs: Vector2) -> Vector2 {
  //   return Vector2(x: lhs * rhs.x, y: lhs * rhs.y)
  // }
}

// Nested generic type.
struct NestedGeneric: VectorNumeric {
  var vec2: Vector2<Float>
  var float: Float

  // Compiler synthesizes:
  // typealias Scalar = Float
  // <all other `VectorNumeric` requirements from above>
}

@dan-zheng dan-zheng added the tensorflow This is for "tensorflow" branch PRs. label Dec 30, 2018
@dan-zheng dan-zheng requested review from rxwei and marcrasi December 30, 2018 06:37
Implement `AdditiveArithmetic` and `VectorNumeric` derived conformances for
struct types.

* `AdditiveArithmetic` conformances can be derived for struct types whose
  members all conform to `AdditiveArithmetic`.
* `VectorNumeric` conformances can be derived for struct types whose members
  all conform to `VectorNumeric` and all share the same `Scalar` associated
  type.

Derived conformances improve the usability of these math protocols for
user-defined types.
Derived conformances can be extended for enum types in the future.
@dan-zheng dan-zheng force-pushed the derive-math-protocols branch from ea4b1f8 to b38b56b Compare December 30, 2018 06:41
Copy link
Contributor

@rxwei rxwei left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent!

@dan-zheng
Copy link
Contributor Author

@swift-ci Please test tensorflow

@rxwei
Copy link
Contributor

rxwei commented Dec 30, 2018

One request I have is that you make the derived methods and properties be @inlinable, as this will be very important for GPE in TensorFlow use cases. You could do that in a separate patch in case of any hurdles in re-triggering the CI.

@dan-zheng
Copy link
Contributor Author

One request I have is that you make the derived methods and properties be @inlinable, as this will be very important for GPE in TensorFlow use cases. You could do that in a separate patch in case of any hurdles in re-triggering the CI.

Good point! I'd prefer to add @inlinable in a follow-up patch as I've started working on another PR.

@rxwei
Copy link
Contributor

rxwei commented Dec 30, 2018

I suspect something's wrong with our GPU test machines. Please disregard GPU test failures for now since they are not connected to AD.

@rxwei rxwei merged commit be8131b into swiftlang:tensorflow Dec 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tensorflow This is for "tensorflow" branch PRs.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants