comparison.carbon 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. package Core library "prelude/operators/comparison";
  5. export import library "prelude/types/bool";
  6. import library "prelude/types/int_literal";
  7. // Equality comparison: `a == b` and `a != b`.
  8. interface Eq {
  9. fn Equal[self: Self](other: Self) -> bool;
  10. fn NotEqual[self: Self](other: Self) -> bool;
  11. }
  12. // Relational comparison: `a < b`, `a <= b`, `a > b`, `a >= b`.
  13. interface Ordered {
  14. // TODO: fn Compare
  15. fn Less[self: Self](other: Self) -> bool;
  16. fn LessOrEquivalent[self: Self](other: Self) -> bool;
  17. fn Greater[self: Self](other: Self) -> bool;
  18. fn GreaterOrEquivalent[self: Self](other: Self) -> bool;
  19. }
  20. // Equality comparison for `bool`.
  21. // Note that this must be provided in this library as `bool` doesn't have any
  22. // associated libraries of its own.
  23. impl bool as Eq {
  24. fn Equal[self: Self](other: Self) -> bool = "bool.eq";
  25. fn NotEqual[self: Self](other: Self) -> bool = "bool.neq";
  26. }
  27. // Operations for IntLiteral. These need to be here because IntLiteral has no
  28. // associated library of its own.
  29. impl IntLiteral() as Eq {
  30. fn Equal[self: Self](other: Self) -> bool = "int.eq";
  31. fn NotEqual[self: Self](other: Self) -> bool = "int.neq";
  32. }
  33. impl IntLiteral() as Ordered {
  34. // TODO: fn Compare
  35. fn Less[self: Self](other: Self) -> bool = "int.less";
  36. fn LessOrEquivalent[self: Self](other: Self) -> bool = "int.less_eq";
  37. fn Greater[self: Self](other: Self) -> bool = "int.greater";
  38. fn GreaterOrEquivalent[self: Self](other: Self) -> bool = "int.greater_eq";
  39. }