prelude.carbon 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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 Carbon api;
  5. // ----------------------
  6. // Conversion interfaces.
  7. // ----------------------
  8. // Explicitly convert `Self` to `T`.
  9. interface As(T:! Type) {
  10. fn Convert[me: Self]() -> T;
  11. }
  12. // Implicitly convert `Self` to `T`.
  13. interface ImplicitAs(T:! Type) {
  14. fn Convert[me: Self]() -> T;
  15. }
  16. // TODO: ImplicitAs(T) should extend As(T).
  17. impl forall [T:! Type, U:! ImplicitAs(T)] U as As(T) {
  18. fn Convert[me: Self]() -> T { return me.Convert(); }
  19. }
  20. // Every type implicitly converts to itself.
  21. impl forall [T:! Type] T as ImplicitAs(T) {
  22. fn Convert[me: Self]() -> T { return me; }
  23. }
  24. // TODO: Simplify this once we have variadics.
  25. // TODO: Should these be final?
  26. impl forall [U1:! Type, T1:! ImplicitAs(U1)]
  27. (T1,) as ImplicitAs((U1,)) {
  28. fn Convert[me: Self]() -> (U1,) {
  29. let (v1: T1,) = me;
  30. return (v1.Convert(),);
  31. }
  32. }
  33. impl forall [U1:! Type, U2:! Type, T1:! ImplicitAs(U1), T2:! ImplicitAs(U2)]
  34. (T1, T2) as ImplicitAs((U1, U2)) {
  35. fn Convert[me: Self]() -> (U1, U2) {
  36. let (v1: T1, v2: T2) = me;
  37. return (v1.Convert(), v2.Convert());
  38. }
  39. }
  40. impl forall [U1:! Type, U2:! Type, U3:! Type,
  41. T1:! ImplicitAs(U1), T2:! ImplicitAs(U2), T3:! ImplicitAs(U3)]
  42. (T1, T2, T3) as ImplicitAs((U1, U2, U3)) {
  43. fn Convert[me: Self]() -> (U1, U2, U3) {
  44. let (v1: T1, v2: T2, v3: T3) = me;
  45. return (v1.Convert(), v2.Convert(), v3.Convert());
  46. }
  47. }
  48. // ----------------------
  49. // Comparison interfaces.
  50. // ----------------------
  51. // ----------------------
  52. // EQUAL
  53. // ----------------------
  54. interface EqWith(U:! Type) {
  55. fn Equal[me: Self](other: U) -> bool;
  56. // TODO: NotEqual with default impl
  57. }
  58. // TODO: constraint Eq { ... }
  59. // TODO: Simplify this once we have variadics
  60. impl forall [T2:! Type, U2:! Type, T1:! EqWith(T2), U1:! EqWith(U2)]
  61. (T1, U1) as EqWith((T2, U2)) {
  62. fn Equal[me: Self](other: (T2, U2)) -> bool {
  63. let (l1: T1, l2: U1) = me;
  64. let (r1: T2, r2: U2) = other;
  65. return l1 == r1 and l2 == r2;
  66. }
  67. }
  68. impl i32 as EqWith(Self) {
  69. fn Equal[me: Self](other: Self) -> bool {
  70. return __intrinsic_int_eq(me, other);
  71. }
  72. }
  73. impl String as EqWith(Self) {
  74. fn Equal[me: Self](other: Self) -> bool {
  75. return __intrinsic_str_eq(me, other);
  76. }
  77. }
  78. // ----------------------
  79. // COMPARE
  80. // ----------------------
  81. choice Ordering {
  82. Less,
  83. Equivalent,
  84. Greater,
  85. Incomparable
  86. }
  87. interface CompareWith(U:! Type) {
  88. fn Compare[me: Self](u: U) -> Ordering;
  89. // TODO: Add `default fn` for Less, LessOrEquivalent, Greater, and GreaterOrEquivalent once it's available.
  90. }
  91. // TODO: constraint Ordered { ... }
  92. impl i32 as CompareWith(Self) {
  93. fn Compare[me: Self](other: Self) -> Ordering {
  94. var comp: i32 = __intrinsic_int_compare(me, other);
  95. if (comp == -1) {
  96. return Ordering.Less();
  97. }
  98. if (comp == 0) {
  99. return Ordering.Equivalent();
  100. }
  101. if (comp == 1) {
  102. return Ordering.Greater();
  103. }
  104. return Ordering.Incomparable();
  105. }
  106. }
  107. impl String as CompareWith(Self) {
  108. fn Compare[me: Self](other: Self) -> Ordering {
  109. var comp: i32 = __intrinsic_str_compare(me, other);
  110. if (comp == -1) {
  111. return Ordering.Less();
  112. }
  113. if (comp == 0) {
  114. return Ordering.Equivalent();
  115. }
  116. if (comp == 1) {
  117. return Ordering.Greater();
  118. }
  119. return Ordering.Incomparable();
  120. }
  121. }
  122. interface LessWith(U:! Type) {
  123. fn Less[me: Self](other: U) -> bool;
  124. }
  125. interface LessEqWith(U:! Type) {
  126. fn LessEq[me: Self](other: U) -> bool;
  127. }
  128. interface GreaterWith(U:! Type) {
  129. fn Greater[me: Self](other: U) -> bool;
  130. }
  131. interface GreaterEqWith(U:! Type) {
  132. fn GreaterEq[me: Self](other: U) -> bool;
  133. }
  134. impl i32 as LessWith(Self) {
  135. fn Less[me: Self](other: Self) -> bool {
  136. var comp: Ordering = me.(CompareWith(i32).Compare)(other);
  137. match (comp) {
  138. case Ordering.Less() => {
  139. return true;
  140. }
  141. }
  142. return false;
  143. }
  144. }
  145. impl String as LessWith(Self) {
  146. fn Less[me: Self](other: Self) -> bool {
  147. var comp: Ordering = me.(CompareWith(String).Compare)(other);
  148. match(comp){
  149. case Ordering.Less() => {
  150. return true;
  151. }
  152. }
  153. return false;
  154. }
  155. }
  156. impl i32 as LessEqWith(Self) {
  157. fn LessEq[me: Self](other: Self) -> bool {
  158. var comp: Ordering = me.(CompareWith(i32).Compare)(other);
  159. match(comp){
  160. case Ordering.Less() => {
  161. return true;
  162. }
  163. case Ordering.Equivalent() => {
  164. return true;
  165. }
  166. }
  167. return false;
  168. }
  169. }
  170. impl String as LessEqWith(Self) {
  171. fn LessEq[me: Self](other: Self) -> bool {
  172. var comp: Ordering = me.(CompareWith(String).Compare)(other);
  173. match(comp){
  174. case Ordering.Less() => {
  175. return true;
  176. }
  177. case Ordering.Equivalent() => {
  178. return true;
  179. }
  180. }
  181. return false;
  182. }
  183. }
  184. impl i32 as GreaterWith(Self) {
  185. fn Greater[me: Self](other: Self) -> bool {
  186. var comp: Ordering = me.(CompareWith(i32).Compare)(other);
  187. match(comp){
  188. case Ordering.Greater() => {
  189. return true;
  190. }
  191. }
  192. return false;
  193. }
  194. }
  195. impl String as GreaterWith(Self) {
  196. fn Greater[me: Self](other: Self) -> bool {
  197. var comp: Ordering = me.(CompareWith(String).Compare)(other);
  198. match(comp){
  199. case Ordering.Greater() => {
  200. return true;
  201. }
  202. }
  203. return false;
  204. }
  205. }
  206. impl i32 as GreaterEqWith(Self) {
  207. fn GreaterEq[me: Self](other: Self) -> bool {
  208. var comp: Ordering = me.(CompareWith(i32).Compare)(other);
  209. match(comp){
  210. case Ordering.Greater() => {
  211. return true;
  212. }
  213. case Ordering.Equivalent() => {
  214. return true;
  215. }
  216. }
  217. return false;
  218. }
  219. }
  220. impl String as GreaterEqWith(Self) {
  221. fn GreaterEq[me: Self](other: Self) -> bool {
  222. var comp: Ordering = me.(CompareWith(String).Compare)(other);
  223. match(comp){
  224. case Ordering.Greater() => {
  225. return true;
  226. }
  227. case Ordering.Equivalent() => {
  228. return true;
  229. }
  230. }
  231. return false;
  232. }
  233. }
  234. // ----------------------
  235. // Arithmetic interfaces.
  236. // ----------------------
  237. interface Negate {
  238. // TODO: = Self
  239. let Result:! Type;
  240. fn Op[me: Self]() -> Result;
  241. }
  242. interface AddWith(U:! Type) {
  243. // TODO: = Self
  244. let Result:! Type;
  245. fn Op[me: Self](other: U) -> Result;
  246. }
  247. // TODO: constraint Add { ... }
  248. interface SubWith(U:! Type) {
  249. // TODO: = Self
  250. let Result:! Type;
  251. fn Op[me: Self](other: U) -> Result;
  252. }
  253. // TODO: constraint Sub { ... }
  254. interface MulWith(U:! Type) {
  255. // TODO: = Self
  256. let Result:! Type;
  257. fn Op[me: Self](other: U) -> Result;
  258. }
  259. // TODO: constraint Mul { ... }
  260. interface ModWith(U:! Type) {
  261. // TODO: = Self
  262. let Result:! Type;
  263. fn Op[me: Self](other: U) -> Result;
  264. }
  265. // TODO: constraint Mod { ... }
  266. // Note, these impls use the builtin addition for i32.
  267. external impl i32 as Negate where .Result == i32 {
  268. fn Op[me: i32]() -> i32 { return -me; }
  269. }
  270. external impl i32 as AddWith(i32) where .Result == i32 {
  271. fn Op[me: i32](other: i32) -> i32 { return me + other; }
  272. }
  273. external impl i32 as SubWith(i32) where .Result == i32 {
  274. fn Op[me: i32](other: i32) -> i32 { return me - other; }
  275. }
  276. external impl i32 as MulWith(i32) where .Result == i32 {
  277. fn Op[me: i32](other: i32) -> i32 { return me * other; }
  278. }
  279. external impl i32 as ModWith(i32) where .Result == i32 {
  280. fn Op[me: i32](other: i32) -> i32 { return me % other; }
  281. }
  282. // ---------------------------------
  283. // Bitwise and bit-shift interfaces.
  284. // ---------------------------------
  285. // Unary `^`.
  286. interface BitComplement {
  287. // TODO: = Self
  288. let Result:! Type;
  289. fn Op[me: Self]() -> Result;
  290. }
  291. // Binary `&`.
  292. interface BitAndWith(U:! Type) {
  293. // TODO: = Self
  294. let Result:! Type;
  295. fn Op[me: Self](other: U) -> Result;
  296. }
  297. // TODO:
  298. // constraint BitAnd {
  299. // extends BitAndWith(Self) where .Result == Self;
  300. // }
  301. // Binary `|`.
  302. interface BitOrWith(U:! Type) {
  303. // TODO: = Self
  304. let Result:! Type;
  305. fn Op[me: Self](other: U) -> Result;
  306. }
  307. // TODO:
  308. // constraint BitOr {
  309. // extends BitOrWith(Self) where .Result == Self;
  310. // }
  311. // Binary `^`.
  312. interface BitXorWith(U:! Type) {
  313. // TODO: = Self
  314. let Result:! Type;
  315. fn Op[me: Self](other: U) -> Result;
  316. }
  317. // TODO:
  318. // constraint BitXor {
  319. // extends BitXorWith(Self) where .Result == Self;
  320. // }
  321. // Binary `<<`.
  322. interface LeftShiftWith(U:! Type) {
  323. // TODO: = Self
  324. let Result:! Type;
  325. fn Op[me: Self](other: U) -> Result;
  326. }
  327. // TODO:
  328. // constraint LeftShift {
  329. // extends LeftShiftWith(Self) where .Result == Self;
  330. // }
  331. // Binary `>>`.
  332. interface RightShiftWith(U:! Type) {
  333. // TODO: = Self
  334. let Result:! Type;
  335. fn Op[me: Self](other: U) -> Result;
  336. }
  337. // TODO:
  338. // constraint RightShift {
  339. // extends RightShiftWith(Self) where .Result == Self;
  340. // }
  341. external impl i32 as BitComplement where .Result == i32 {
  342. fn Op[me: i32]() -> i32 {
  343. return __intrinsic_int_bit_complement(me);
  344. }
  345. }
  346. external impl i32 as BitAndWith(i32) where .Result == i32 {
  347. fn Op[me: i32](other: i32) -> i32 {
  348. return __intrinsic_int_bit_and(me, other);
  349. }
  350. }
  351. external impl i32 as BitOrWith(i32) where .Result == i32 {
  352. fn Op[me: i32](other: i32) -> i32 {
  353. return __intrinsic_int_bit_or(me, other);
  354. }
  355. }
  356. external impl i32 as BitXorWith(i32) where .Result == i32 {
  357. fn Op[me: i32](other: i32) -> i32 {
  358. return __intrinsic_int_bit_xor(me, other);
  359. }
  360. }
  361. external impl i32 as LeftShiftWith(i32) where .Result == i32 {
  362. fn Op[me: i32](other: i32) -> i32 {
  363. return __intrinsic_int_left_shift(me, other);
  364. }
  365. }
  366. external impl i32 as RightShiftWith(i32) where .Result == i32 {
  367. fn Op[me: i32](other: i32) -> i32 {
  368. return __intrinsic_int_right_shift(me, other);
  369. }
  370. }
  371. // ------------------------
  372. // Miscellaneous utilities.
  373. // ------------------------
  374. // Note that Print is experimental, and not part of an accepted proposal, but
  375. // is included here for printing state in tests.
  376. // TODO: Remove Print special casing once we have variadics or overloads.
  377. // fn Print(format_str: String) {
  378. // __intrinsic_print(format_str);
  379. // }
  380. fn Rand(low: i32, high: i32) -> i32{
  381. return __intrinsic_rand(low,high);
  382. }
  383. class Heap {
  384. fn New[T:! Type, me: Self](x : T) -> T* {
  385. return __intrinsic_new(x);
  386. }
  387. fn Delete[T:! Type, me: Self](p : T*) {
  388. __intrinsic_delete(p);
  389. }
  390. }
  391. var heap: Heap = {};