effect 4.1.2 → 4.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (495) hide show
  1. package/ai-docs/package.json +1 -1
  2. package/ai-docs/tsconfig.json +2 -1
  3. package/dist/DateTime.d.ts +33 -2
  4. package/dist/DateTime.d.ts.map +1 -1
  5. package/dist/DateTime.js +16 -0
  6. package/dist/DateTime.js.map +1 -1
  7. package/dist/Match.d.ts +6 -6
  8. package/dist/Match.d.ts.map +1 -1
  9. package/dist/Match.js.map +1 -1
  10. package/dist/Queue.d.ts +2 -1
  11. package/dist/Queue.d.ts.map +1 -1
  12. package/dist/Queue.js +16 -9
  13. package/dist/Queue.js.map +1 -1
  14. package/dist/Rational.d.ts +1214 -0
  15. package/dist/Rational.d.ts.map +1 -0
  16. package/dist/Rational.js +880 -0
  17. package/dist/Rational.js.map +1 -0
  18. package/dist/RcMap.d.ts +3 -0
  19. package/dist/RcMap.d.ts.map +1 -1
  20. package/dist/RcMap.js +13 -14
  21. package/dist/RcMap.js.map +1 -1
  22. package/dist/Runtime.d.ts +75 -3
  23. package/dist/Runtime.d.ts.map +1 -1
  24. package/dist/Runtime.js +81 -4
  25. package/dist/Runtime.js.map +1 -1
  26. package/dist/Schema.d.ts +131 -29
  27. package/dist/Schema.d.ts.map +1 -1
  28. package/dist/Schema.js +128 -22
  29. package/dist/Schema.js.map +1 -1
  30. package/dist/SchemaAST.d.ts.map +1 -1
  31. package/dist/SchemaAST.js +24 -12
  32. package/dist/SchemaAST.js.map +1 -1
  33. package/dist/SchemaParser.js +2 -2
  34. package/dist/SchemaParser.js.map +1 -1
  35. package/dist/SchemaRepresentation.d.ts +26 -3
  36. package/dist/SchemaRepresentation.d.ts.map +1 -1
  37. package/dist/SchemaRepresentation.js +13 -2
  38. package/dist/SchemaRepresentation.js.map +1 -1
  39. package/dist/ScopedCache.d.ts +7 -0
  40. package/dist/ScopedCache.d.ts.map +1 -1
  41. package/dist/ScopedCache.js +18 -1
  42. package/dist/ScopedCache.js.map +1 -1
  43. package/dist/ai/AgUi.d.ts +109 -0
  44. package/dist/ai/AgUi.d.ts.map +1 -0
  45. package/dist/ai/AgUi.js +543 -0
  46. package/dist/ai/AgUi.js.map +1 -0
  47. package/dist/ai/AgUiSchema.d.ts +6965 -0
  48. package/dist/ai/AgUiSchema.d.ts.map +1 -0
  49. package/dist/ai/AgUiSchema.js +974 -0
  50. package/dist/ai/AgUiSchema.js.map +1 -0
  51. package/dist/ai/AgUiServer.d.ts +6400 -0
  52. package/dist/ai/AgUiServer.d.ts.map +1 -0
  53. package/dist/ai/AgUiServer.js +309 -0
  54. package/dist/ai/AgUiServer.js.map +1 -0
  55. package/dist/ai/Chat.d.ts +31 -5
  56. package/dist/ai/Chat.d.ts.map +1 -1
  57. package/dist/ai/Chat.js +34 -4
  58. package/dist/ai/Chat.js.map +1 -1
  59. package/dist/ai/LanguageModel.d.ts +24 -5
  60. package/dist/ai/LanguageModel.d.ts.map +1 -1
  61. package/dist/ai/LanguageModel.js +26 -12
  62. package/dist/ai/LanguageModel.js.map +1 -1
  63. package/dist/ai/McpSchema.d.ts +110 -110
  64. package/dist/ai/McpSchema.d.ts.map +1 -1
  65. package/dist/ai/McpServer.d.ts +15 -0
  66. package/dist/ai/McpServer.d.ts.map +1 -1
  67. package/dist/ai/McpServer.js +211 -33
  68. package/dist/ai/McpServer.js.map +1 -1
  69. package/dist/ai/Prompt.d.ts +6 -2
  70. package/dist/ai/Prompt.d.ts.map +1 -1
  71. package/dist/ai/Prompt.js.map +1 -1
  72. package/dist/ai/Response.d.ts +5 -2
  73. package/dist/ai/Response.d.ts.map +1 -1
  74. package/dist/ai/Response.js +5 -1
  75. package/dist/ai/Response.js.map +1 -1
  76. package/dist/ai/Tool.d.ts +265 -69
  77. package/dist/ai/Tool.d.ts.map +1 -1
  78. package/dist/ai/Tool.js +19 -2
  79. package/dist/ai/Tool.js.map +1 -1
  80. package/dist/ai/Toolkit.d.ts +32 -2
  81. package/dist/ai/Toolkit.d.ts.map +1 -1
  82. package/dist/ai/Toolkit.js +61 -8
  83. package/dist/ai/Toolkit.js.map +1 -1
  84. package/dist/ai/WorkloadIdentity.d.ts +111 -0
  85. package/dist/ai/WorkloadIdentity.d.ts.map +1 -0
  86. package/dist/ai/WorkloadIdentity.js +227 -0
  87. package/dist/ai/WorkloadIdentity.js.map +1 -0
  88. package/dist/ai/index.d.ts +20 -0
  89. package/dist/ai/index.d.ts.map +1 -1
  90. package/dist/ai/index.js +20 -0
  91. package/dist/ai/index.js.map +1 -1
  92. package/dist/ai/internal/mcpCore.d.ts +0 -2
  93. package/dist/ai/internal/mcpCore.d.ts.map +1 -1
  94. package/dist/ai/internal/mcpCore.js +2 -8
  95. package/dist/ai/internal/mcpCore.js.map +1 -1
  96. package/dist/ai/internal/mcpProtocol/v2024_11_05.js +5 -8
  97. package/dist/ai/internal/mcpProtocol/v2024_11_05.js.map +1 -1
  98. package/dist/ai/internal/mcpProtocol/v2025_03_26.js +5 -8
  99. package/dist/ai/internal/mcpProtocol/v2025_03_26.js.map +1 -1
  100. package/dist/ai/internal/mcpProtocol/v2025_06_18.js +5 -5
  101. package/dist/ai/internal/mcpProtocol/v2025_06_18.js.map +1 -1
  102. package/dist/ai/internal/mcpProtocol/v2025_11_25.js +5 -5
  103. package/dist/ai/internal/mcpProtocol/v2025_11_25.js.map +1 -1
  104. package/dist/ai/internal/mcpProtocol/v2026_07_28.js +60 -51
  105. package/dist/ai/internal/mcpProtocol/v2026_07_28.js.map +1 -1
  106. package/dist/ai/internal/mcpProtocol.js +11 -1
  107. package/dist/ai/internal/mcpProtocol.js.map +1 -1
  108. package/dist/ai/internal/mcpProtocolRegistry.js +4 -0
  109. package/dist/ai/internal/mcpProtocolRegistry.js.map +1 -1
  110. package/dist/ai/internal/mcpRuntime.js +2 -0
  111. package/dist/ai/internal/mcpRuntime.js.map +1 -1
  112. package/dist/ai/internal/mcpSchema/v2024_11_05.js +0 -2
  113. package/dist/ai/internal/mcpSchema/v2024_11_05.js.map +1 -1
  114. package/dist/ai/internal/mcpSchema/v2025_03_26.d.ts.map +1 -1
  115. package/dist/ai/internal/mcpSchema/v2025_03_26.js +0 -2
  116. package/dist/ai/internal/mcpSchema/v2025_03_26.js.map +1 -1
  117. package/dist/ai/internal/mcpSchema/v2025_06_18.d.ts.map +1 -1
  118. package/dist/ai/internal/mcpSchema/v2025_06_18.js +0 -2
  119. package/dist/ai/internal/mcpSchema/v2025_06_18.js.map +1 -1
  120. package/dist/ai/internal/mcpSchema/v2025_11_25.d.ts.map +1 -1
  121. package/dist/ai/internal/mcpSchema/v2025_11_25.js +0 -2
  122. package/dist/ai/internal/mcpSchema/v2025_11_25.js.map +1 -1
  123. package/dist/ai/internal/mcpSchema/v2026_07_28.d.ts.map +1 -1
  124. package/dist/ai/internal/mcpSchema/v2026_07_28.js +0 -2
  125. package/dist/ai/internal/mcpSchema/v2026_07_28.js.map +1 -1
  126. package/dist/ai/internal/mcpSkills.js +5 -17
  127. package/dist/ai/internal/mcpSkills.js.map +1 -1
  128. package/dist/ai/internal/mcpStatefulRuntime.js +0 -2
  129. package/dist/ai/internal/mcpStatefulRuntime.js.map +1 -1
  130. package/dist/ai/internal/structured-output.js +5 -1
  131. package/dist/ai/internal/structured-output.js.map +1 -1
  132. package/dist/cli/Argument.d.ts +81 -0
  133. package/dist/cli/Argument.d.ts.map +1 -1
  134. package/dist/cli/Argument.js +26 -0
  135. package/dist/cli/Argument.js.map +1 -1
  136. package/dist/cli/CliError.d.ts +7 -4
  137. package/dist/cli/CliError.d.ts.map +1 -1
  138. package/dist/cli/CliError.js +7 -4
  139. package/dist/cli/CliError.js.map +1 -1
  140. package/dist/cli/Command.d.ts +241 -22
  141. package/dist/cli/Command.d.ts.map +1 -1
  142. package/dist/cli/Command.js +88 -10
  143. package/dist/cli/Command.js.map +1 -1
  144. package/dist/cli/CommandSpec.d.ts +287 -0
  145. package/dist/cli/CommandSpec.d.ts.map +1 -0
  146. package/dist/cli/CommandSpec.js +277 -0
  147. package/dist/cli/CommandSpec.js.map +1 -0
  148. package/dist/cli/Completions.d.ts +5 -0
  149. package/dist/cli/Completions.d.ts.map +1 -1
  150. package/dist/cli/Completions.js.map +1 -1
  151. package/dist/cli/Flag.d.ts +102 -0
  152. package/dist/cli/Flag.d.ts.map +1 -1
  153. package/dist/cli/Flag.js +47 -0
  154. package/dist/cli/Flag.js.map +1 -1
  155. package/dist/cli/Param.d.ts +154 -1
  156. package/dist/cli/Param.d.ts.map +1 -1
  157. package/dist/cli/Param.js +87 -29
  158. package/dist/cli/Param.js.map +1 -1
  159. package/dist/cli/index.d.ts +5 -0
  160. package/dist/cli/index.d.ts.map +1 -1
  161. package/dist/cli/index.js +5 -0
  162. package/dist/cli/index.js.map +1 -1
  163. package/dist/cli/internal/command.d.ts +2 -2
  164. package/dist/cli/internal/command.d.ts.map +1 -1
  165. package/dist/cli/internal/command.js +1 -1
  166. package/dist/cli/internal/command.js.map +1 -1
  167. package/dist/cli/internal/completions/bash.js +6 -7
  168. package/dist/cli/internal/completions/bash.js.map +1 -1
  169. package/dist/cli/internal/completions/descriptor.js +21 -7
  170. package/dist/cli/internal/completions/descriptor.js.map +1 -1
  171. package/dist/cli/internal/completions/fish.js +3 -3
  172. package/dist/cli/internal/completions/fish.js.map +1 -1
  173. package/dist/cli/internal/completions/powershell.js +2 -2
  174. package/dist/cli/internal/completions/powershell.js.map +1 -1
  175. package/dist/cli/internal/completions/zsh.js +2 -2
  176. package/dist/cli/internal/completions/zsh.js.map +1 -1
  177. package/dist/cli/internal/lexer.js +13 -0
  178. package/dist/cli/internal/lexer.js.map +1 -1
  179. package/dist/cli/internal/parser.js +23 -16
  180. package/dist/cli/internal/parser.js.map +1 -1
  181. package/dist/cli/internal/wizard.d.ts.map +1 -1
  182. package/dist/cli/internal/wizard.js +4 -0
  183. package/dist/cli/internal/wizard.js.map +1 -1
  184. package/dist/cluster/ClusterWorkflowEngine.d.ts.map +1 -1
  185. package/dist/cluster/ClusterWorkflowEngine.js +15 -13
  186. package/dist/cluster/ClusterWorkflowEngine.js.map +1 -1
  187. package/dist/cluster/Entity.d.ts +17 -0
  188. package/dist/cluster/Entity.d.ts.map +1 -1
  189. package/dist/cluster/Entity.js +20 -0
  190. package/dist/cluster/Entity.js.map +1 -1
  191. package/dist/cluster/MessageRetention.d.ts +71 -0
  192. package/dist/cluster/MessageRetention.d.ts.map +1 -0
  193. package/dist/cluster/MessageRetention.js +100 -0
  194. package/dist/cluster/MessageRetention.js.map +1 -0
  195. package/dist/cluster/MessageStorage.d.ts +81 -0
  196. package/dist/cluster/MessageStorage.d.ts.map +1 -1
  197. package/dist/cluster/MessageStorage.js +34 -2
  198. package/dist/cluster/MessageStorage.js.map +1 -1
  199. package/dist/cluster/ShardingConfig.d.ts +24 -2
  200. package/dist/cluster/ShardingConfig.d.ts.map +1 -1
  201. package/dist/cluster/ShardingConfig.js.map +1 -1
  202. package/dist/cluster/SqlMessageStorage.d.ts.map +1 -1
  203. package/dist/cluster/SqlMessageStorage.js +187 -29
  204. package/dist/cluster/SqlMessageStorage.js.map +1 -1
  205. package/dist/cluster/index.d.ts +5 -0
  206. package/dist/cluster/index.d.ts.map +1 -1
  207. package/dist/cluster/index.js +5 -0
  208. package/dist/cluster/index.js.map +1 -1
  209. package/dist/encoding/Base64.d.ts +0 -6
  210. package/dist/encoding/Base64.d.ts.map +1 -1
  211. package/dist/encoding/Base64.js +25 -5
  212. package/dist/encoding/Base64.js.map +1 -1
  213. package/dist/encoding/Base64Url.d.ts +0 -6
  214. package/dist/encoding/Base64Url.d.ts.map +1 -1
  215. package/dist/encoding/Base64Url.js +91 -8
  216. package/dist/encoding/Base64Url.js.map +1 -1
  217. package/dist/encoding/Csv.d.ts.map +1 -1
  218. package/dist/encoding/Csv.js +11 -5
  219. package/dist/encoding/Csv.js.map +1 -1
  220. package/dist/encoding/Hex.d.ts.map +1 -1
  221. package/dist/encoding/Hex.js +51 -34
  222. package/dist/encoding/Hex.js.map +1 -1
  223. package/dist/encoding/SchemaBinary.d.ts +14 -2
  224. package/dist/encoding/SchemaBinary.d.ts.map +1 -1
  225. package/dist/encoding/SchemaBinary.js +78 -5
  226. package/dist/encoding/SchemaBinary.js.map +1 -1
  227. package/dist/encoding/Xml.d.ts +25 -0
  228. package/dist/encoding/Xml.d.ts.map +1 -1
  229. package/dist/encoding/Xml.js +44 -19
  230. package/dist/encoding/Xml.js.map +1 -1
  231. package/dist/graphql/GraphQL.d.ts.map +1 -1
  232. package/dist/graphql/GraphQL.js +4 -1
  233. package/dist/graphql/GraphQL.js.map +1 -1
  234. package/dist/http/Headers.d.ts +5 -1
  235. package/dist/http/Headers.d.ts.map +1 -1
  236. package/dist/http/Headers.js +7 -2
  237. package/dist/http/Headers.js.map +1 -1
  238. package/dist/http/HttpClient.d.ts +57 -12
  239. package/dist/http/HttpClient.d.ts.map +1 -1
  240. package/dist/http/HttpClient.js +92 -54
  241. package/dist/http/HttpClient.js.map +1 -1
  242. package/dist/http/HttpClientError.d.ts +22 -8
  243. package/dist/http/HttpClientError.d.ts.map +1 -1
  244. package/dist/http/HttpClientError.js +68 -14
  245. package/dist/http/HttpClientError.js.map +1 -1
  246. package/dist/http/HttpEffect.d.ts +63 -4
  247. package/dist/http/HttpEffect.d.ts.map +1 -1
  248. package/dist/http/HttpEffect.js +89 -9
  249. package/dist/http/HttpEffect.js.map +1 -1
  250. package/dist/http/HttpMiddleware.d.ts.map +1 -1
  251. package/dist/http/HttpMiddleware.js +5 -3
  252. package/dist/http/HttpMiddleware.js.map +1 -1
  253. package/dist/http/HttpServer.d.ts +80 -0
  254. package/dist/http/HttpServer.d.ts.map +1 -1
  255. package/dist/http/HttpServer.js +16 -0
  256. package/dist/http/HttpServer.js.map +1 -1
  257. package/dist/http/HttpServerRequest.d.ts.map +1 -1
  258. package/dist/http/HttpServerRequest.js +1 -14
  259. package/dist/http/HttpServerRequest.js.map +1 -1
  260. package/dist/http/HttpStaticServer.js +10 -16
  261. package/dist/http/HttpStaticServer.js.map +1 -1
  262. package/dist/http/internal/requestState.js +15 -0
  263. package/dist/http/internal/requestState.js.map +1 -1
  264. package/dist/http-api/HttpApi.d.ts +47 -0
  265. package/dist/http-api/HttpApi.d.ts.map +1 -1
  266. package/dist/http-api/HttpApi.js +66 -0
  267. package/dist/http-api/HttpApi.js.map +1 -1
  268. package/dist/http-api/HttpApiBuilder.d.ts.map +1 -1
  269. package/dist/http-api/HttpApiBuilder.js +18 -2
  270. package/dist/http-api/HttpApiBuilder.js.map +1 -1
  271. package/dist/http-api/HttpApiClient.d.ts +1 -1
  272. package/dist/http-api/HttpApiClient.d.ts.map +1 -1
  273. package/dist/http-api/HttpApiClient.js +5 -1
  274. package/dist/http-api/HttpApiClient.js.map +1 -1
  275. package/dist/http-api/HttpApiSchema.d.ts +85 -0
  276. package/dist/http-api/HttpApiSchema.d.ts.map +1 -1
  277. package/dist/http-api/HttpApiSchema.js +74 -2
  278. package/dist/http-api/HttpApiSchema.js.map +1 -1
  279. package/dist/index.d.ts +5 -0
  280. package/dist/index.d.ts.map +1 -1
  281. package/dist/index.js +5 -0
  282. package/dist/index.js.map +1 -1
  283. package/dist/internal/base64.d.ts +2 -0
  284. package/dist/internal/base64.d.ts.map +1 -0
  285. package/dist/internal/base64.js +30 -0
  286. package/dist/internal/base64.js.map +1 -0
  287. package/dist/internal/core.js +3 -4
  288. package/dist/internal/core.js.map +1 -1
  289. package/dist/internal/dateTime.js +18 -10
  290. package/dist/internal/dateTime.js.map +1 -1
  291. package/dist/internal/effect.js +41 -17
  292. package/dist/internal/effect.js.map +1 -1
  293. package/dist/internal/equal.js +7 -0
  294. package/dist/internal/equal.js.map +1 -1
  295. package/dist/internal/matcher.js +1 -1
  296. package/dist/internal/matcher.js.map +1 -1
  297. package/dist/internal/schema/codegen.js +7 -7
  298. package/dist/internal/schema/codegen.js.map +1 -1
  299. package/dist/internal/schema/fromJsonSchemaDocument.js +161 -36
  300. package/dist/internal/schema/fromJsonSchemaDocument.js.map +1 -1
  301. package/dist/internal/schema/interpreter.js +10 -10
  302. package/dist/internal/schema/interpreter.js.map +1 -1
  303. package/dist/internal/schema/parser.js +9 -4
  304. package/dist/internal/schema/parser.js.map +1 -1
  305. package/dist/internal/schema/toCodeDocument.js +3 -0
  306. package/dist/internal/schema/toCodeDocument.js.map +1 -1
  307. package/dist/internal/schema/toEquivalence.js +6 -5
  308. package/dist/internal/schema/toEquivalence.js.map +1 -1
  309. package/dist/internal/schema/toFormatter.js +3 -2
  310. package/dist/internal/schema/toFormatter.js.map +1 -1
  311. package/dist/internal/version.js +1 -1
  312. package/dist/observability/OtlpMetrics.js +1 -1
  313. package/dist/observability/OtlpMetrics.js.map +1 -1
  314. package/dist/observability/OtlpTracer.js +2 -2
  315. package/dist/observability/OtlpTracer.js.map +1 -1
  316. package/dist/persistence/PersistedQueue.d.ts +72 -3
  317. package/dist/persistence/PersistedQueue.d.ts.map +1 -1
  318. package/dist/persistence/PersistedQueue.js +142 -29
  319. package/dist/persistence/PersistedQueue.js.map +1 -1
  320. package/dist/persistence/RateLimiter.d.ts +12 -2
  321. package/dist/persistence/RateLimiter.d.ts.map +1 -1
  322. package/dist/persistence/RateLimiter.js +11 -4
  323. package/dist/persistence/RateLimiter.js.map +1 -1
  324. package/dist/process/CommandLine.d.ts +9 -9
  325. package/dist/process/CommandLine.js +6 -6
  326. package/dist/process/index.d.ts +1 -1
  327. package/dist/process/index.js +1 -1
  328. package/dist/reactivity/Atom.d.ts.map +1 -1
  329. package/dist/reactivity/Atom.js +24 -3
  330. package/dist/reactivity/Atom.js.map +1 -1
  331. package/dist/reactivity/AtomRpc.d.ts +24 -6
  332. package/dist/reactivity/AtomRpc.d.ts.map +1 -1
  333. package/dist/reactivity/AtomRpc.js.map +1 -1
  334. package/dist/rpc/OpenRpc.d.ts +384 -0
  335. package/dist/rpc/OpenRpc.d.ts.map +1 -0
  336. package/dist/rpc/OpenRpc.js +490 -0
  337. package/dist/rpc/OpenRpc.js.map +1 -0
  338. package/dist/rpc/Rpc.d.ts +22 -3
  339. package/dist/rpc/Rpc.d.ts.map +1 -1
  340. package/dist/rpc/Rpc.js.map +1 -1
  341. package/dist/rpc/RpcClient.d.ts +20 -21
  342. package/dist/rpc/RpcClient.d.ts.map +1 -1
  343. package/dist/rpc/RpcClient.js.map +1 -1
  344. package/dist/rpc/RpcServer.d.ts +23 -2
  345. package/dist/rpc/RpcServer.d.ts.map +1 -1
  346. package/dist/rpc/RpcServer.js +67 -25
  347. package/dist/rpc/RpcServer.js.map +1 -1
  348. package/dist/rpc/index.d.ts +5 -0
  349. package/dist/rpc/index.d.ts.map +1 -1
  350. package/dist/rpc/index.js +5 -0
  351. package/dist/rpc/index.js.map +1 -1
  352. package/dist/schema/SchemaCompiler/runtime.js +3 -2
  353. package/dist/schema/SchemaCompiler/runtime.js.map +1 -1
  354. package/dist/sql/SqlClient.d.ts +59 -6
  355. package/dist/sql/SqlClient.d.ts.map +1 -1
  356. package/dist/sql/SqlClient.js +64 -17
  357. package/dist/sql/SqlClient.js.map +1 -1
  358. package/dist/sql/Statement.d.ts +1 -1
  359. package/dist/sql/Statement.d.ts.map +1 -1
  360. package/dist/sql/Statement.js +9 -16
  361. package/dist/sql/Statement.js.map +1 -1
  362. package/dist/testing/TestClock.d.ts +2 -12
  363. package/dist/testing/TestClock.d.ts.map +1 -1
  364. package/dist/testing/TestClock.js +179 -169
  365. package/dist/testing/TestClock.js.map +1 -1
  366. package/dist/workflow/DurableClock.d.ts +27 -0
  367. package/dist/workflow/DurableClock.d.ts.map +1 -1
  368. package/dist/workflow/DurableClock.js +18 -2
  369. package/dist/workflow/DurableClock.js.map +1 -1
  370. package/dist/workflow/Workflow.d.ts.map +1 -1
  371. package/dist/workflow/Workflow.js +5 -4
  372. package/dist/workflow/Workflow.js.map +1 -1
  373. package/dist/workflow/WorkflowEngine.d.ts.map +1 -1
  374. package/dist/workflow/WorkflowEngine.js +14 -3
  375. package/dist/workflow/WorkflowEngine.js.map +1 -1
  376. package/package.json +3 -3
  377. package/src/DateTime.ts +35 -2
  378. package/src/Match.ts +10 -8
  379. package/src/Queue.ts +16 -11
  380. package/src/Rational.ts +1549 -0
  381. package/src/RcMap.ts +16 -14
  382. package/src/Runtime.ts +84 -4
  383. package/src/Schema.ts +188 -29
  384. package/src/SchemaAST.ts +29 -13
  385. package/src/SchemaParser.ts +2 -2
  386. package/src/SchemaRepresentation.ts +31 -3
  387. package/src/ScopedCache.ts +18 -1
  388. package/src/ai/AgUi.ts +485 -0
  389. package/src/ai/AgUiSchema.ts +1666 -0
  390. package/src/ai/AgUiServer.ts +451 -0
  391. package/src/ai/Chat.ts +87 -19
  392. package/src/ai/LanguageModel.ts +97 -23
  393. package/src/ai/McpServer.ts +244 -49
  394. package/src/ai/Prompt.ts +6 -2
  395. package/src/ai/Response.ts +19 -3
  396. package/src/ai/Tool.ts +426 -110
  397. package/src/ai/Toolkit.ts +71 -10
  398. package/src/ai/WorkloadIdentity.ts +318 -0
  399. package/src/ai/index.ts +24 -0
  400. package/src/ai/internal/mcpCore.ts +11 -15
  401. package/src/ai/internal/mcpProtocol/v2024_11_05.ts +8 -11
  402. package/src/ai/internal/mcpProtocol/v2025_03_26.ts +8 -11
  403. package/src/ai/internal/mcpProtocol/v2025_06_18.ts +8 -8
  404. package/src/ai/internal/mcpProtocol/v2025_11_25.ts +8 -8
  405. package/src/ai/internal/mcpProtocol/v2026_07_28.ts +76 -55
  406. package/src/ai/internal/mcpProtocol.ts +22 -3
  407. package/src/ai/internal/mcpProtocolRegistry.ts +5 -0
  408. package/src/ai/internal/mcpRuntime.ts +4 -2
  409. package/src/ai/internal/mcpSchema/v2024_11_05.ts +0 -2
  410. package/src/ai/internal/mcpSchema/v2025_03_26.ts +0 -2
  411. package/src/ai/internal/mcpSchema/v2025_06_18.ts +0 -2
  412. package/src/ai/internal/mcpSchema/v2025_11_25.ts +0 -2
  413. package/src/ai/internal/mcpSchema/v2026_07_28.ts +0 -2
  414. package/src/ai/internal/mcpSkills.ts +5 -20
  415. package/src/ai/internal/mcpStatefulRuntime.ts +0 -2
  416. package/src/ai/internal/structured-output.ts +7 -2
  417. package/src/cli/Argument.ts +86 -0
  418. package/src/cli/CliError.ts +7 -4
  419. package/src/cli/Command.ts +266 -36
  420. package/src/cli/CommandSpec.ts +508 -0
  421. package/src/cli/Completions.ts +5 -0
  422. package/src/cli/Flag.ts +106 -0
  423. package/src/cli/Param.ts +225 -23
  424. package/src/cli/index.ts +6 -0
  425. package/src/cli/internal/command.ts +3 -4
  426. package/src/cli/internal/completions/bash.ts +9 -7
  427. package/src/cli/internal/completions/descriptor.ts +37 -5
  428. package/src/cli/internal/completions/fish.ts +9 -3
  429. package/src/cli/internal/completions/powershell.ts +2 -1
  430. package/src/cli/internal/completions/zsh.ts +2 -2
  431. package/src/cli/internal/lexer.ts +14 -0
  432. package/src/cli/internal/parser.ts +28 -20
  433. package/src/cli/internal/wizard.ts +4 -0
  434. package/src/cluster/ClusterWorkflowEngine.ts +14 -15
  435. package/src/cluster/Entity.ts +24 -0
  436. package/src/cluster/MessageRetention.ts +111 -0
  437. package/src/cluster/MessageStorage.ts +73 -2
  438. package/src/cluster/ShardingConfig.ts +12 -1
  439. package/src/cluster/SqlMessageStorage.ts +271 -41
  440. package/src/cluster/index.ts +6 -0
  441. package/src/encoding/Base64.ts +26 -5
  442. package/src/encoding/Base64Url.ts +93 -10
  443. package/src/encoding/Csv.ts +11 -5
  444. package/src/encoding/Hex.ts +56 -42
  445. package/src/encoding/SchemaBinary.ts +76 -5
  446. package/src/encoding/Xml.ts +65 -16
  447. package/src/graphql/GraphQL.ts +7 -4
  448. package/src/http/Headers.ts +7 -2
  449. package/src/http/HttpClient.ts +118 -68
  450. package/src/http/HttpClientError.ts +82 -15
  451. package/src/http/HttpEffect.ts +101 -10
  452. package/src/http/HttpMiddleware.ts +15 -4
  453. package/src/http/HttpServer.ts +80 -0
  454. package/src/http/HttpServerRequest.ts +1 -19
  455. package/src/http/HttpStaticServer.ts +10 -16
  456. package/src/http/internal/requestState.ts +20 -0
  457. package/src/http-api/HttpApi.ts +80 -0
  458. package/src/http-api/HttpApiBuilder.ts +20 -2
  459. package/src/http-api/HttpApiClient.ts +13 -2
  460. package/src/http-api/HttpApiSchema.ts +90 -3
  461. package/src/index.ts +6 -0
  462. package/src/internal/base64.ts +42 -0
  463. package/src/internal/core.ts +3 -4
  464. package/src/internal/dateTime.ts +27 -13
  465. package/src/internal/effect.ts +45 -18
  466. package/src/internal/equal.ts +8 -0
  467. package/src/internal/matcher.ts +7 -3
  468. package/src/internal/schema/codegen.ts +8 -7
  469. package/src/internal/schema/fromJsonSchemaDocument.ts +175 -35
  470. package/src/internal/schema/interpreter.ts +10 -10
  471. package/src/internal/schema/parser.ts +8 -4
  472. package/src/internal/schema/toCodeDocument.ts +8 -0
  473. package/src/internal/schema/toEquivalence.ts +7 -5
  474. package/src/internal/schema/toFormatter.ts +3 -2
  475. package/src/internal/version.ts +1 -1
  476. package/src/observability/OtlpMetrics.ts +1 -1
  477. package/src/observability/OtlpTracer.ts +2 -2
  478. package/src/persistence/PersistedQueue.ts +199 -41
  479. package/src/persistence/RateLimiter.ts +17 -5
  480. package/src/process/CommandLine.ts +10 -10
  481. package/src/process/index.ts +1 -1
  482. package/src/reactivity/Atom.ts +25 -3
  483. package/src/reactivity/AtomRpc.ts +61 -45
  484. package/src/rpc/OpenRpc.ts +747 -0
  485. package/src/rpc/Rpc.ts +38 -6
  486. package/src/rpc/RpcClient.ts +45 -82
  487. package/src/rpc/RpcServer.ts +77 -28
  488. package/src/rpc/index.ts +6 -0
  489. package/src/schema/SchemaCompiler/runtime.ts +8 -3
  490. package/src/sql/SqlClient.ts +112 -26
  491. package/src/sql/Statement.ts +19 -23
  492. package/src/testing/TestClock.ts +204 -189
  493. package/src/workflow/DurableClock.ts +20 -3
  494. package/src/workflow/Workflow.ts +5 -3
  495. package/src/workflow/WorkflowEngine.ts +20 -5
@@ -0,0 +1,1549 @@
1
+ /**
2
+ * Exact rational numbers for ratios that decimal arithmetic cannot represent.
3
+ * A `Rational` stores a `bigint` numerator and denominator in lowest terms, so
4
+ * values such as one third survive multiplication, comparison and compounding
5
+ * without rounding. Use `BigDecimal` for decimal quantities such as money, and
6
+ * `Rational` when a ratio must stay exact until an explicit conversion.
7
+ *
8
+ * @stability unstable
9
+ * @since 4.1.3
10
+ */
11
+
12
+ import * as BigDecimal from "./BigDecimal.ts"
13
+ import * as BI from "./BigInt.ts"
14
+ import * as Equal from "./Equal.ts"
15
+ import * as Equ from "./Equivalence.ts"
16
+ import { dual } from "./Function.ts"
17
+ import * as Hash from "./Hash.ts"
18
+ import { type Inspectable, NodeInspectSymbol } from "./Inspectable.ts"
19
+ import * as Option from "./Option.ts"
20
+ import * as order from "./Order.ts"
21
+ import type { Ordering } from "./Ordering.ts"
22
+ import { type Pipeable, pipeArguments } from "./Pipeable.ts"
23
+ import { hasProperty } from "./Predicate.ts"
24
+
25
+ const TypeId = "~effect/Rational"
26
+
27
+ const bigint0 = BigInt(0)
28
+ const bigint1 = BigInt(1)
29
+ const bigint2 = BigInt(2)
30
+ const bigint10 = BigInt(10)
31
+ const FRACTION_REGEXP = /^([+-]?\d+)(?:\/([+-]?\d+))?$/
32
+ const QUOTIENT_BITS = 64
33
+ const MANTISSA_BITS = 53
34
+ const MIN_SUBNORMAL_EXPONENT = -1074
35
+
36
+ /**
37
+ * Represents an exact rational number as a reduced fraction.
38
+ *
39
+ * **Details**
40
+ *
41
+ * Every `Rational` is kept in canonical form: the numerator and denominator
42
+ * share no common factor, the denominator is positive, and zero is `0/1`.
43
+ * Two rationals are equal exactly when their fields are equal.
44
+ *
45
+ * **Example** (Inspecting Rational storage)
46
+ *
47
+ * ```ts import.meta.vitest
48
+ * import { Rational } from "effect"
49
+ *
50
+ * const r = Rational.makeUnsafe(6n, -4n)
51
+ *
52
+ * r.numerator // => -3n
53
+ * r.denominator // => 2n
54
+ * ```
55
+ *
56
+ * @stability unstable
57
+ * @category models
58
+ * @since 4.1.3
59
+ */
60
+ export interface Rational extends Equal.Equal, Pipeable, Inspectable {
61
+ readonly [TypeId]: typeof TypeId
62
+ readonly numerator: bigint
63
+ readonly denominator: bigint
64
+ }
65
+
66
+ const RationalProto: Omit<Rational, "numerator" | "denominator"> = {
67
+ [TypeId]: TypeId,
68
+ [Hash.symbol](this: Rational): number {
69
+ return Hash.combine(Hash.hash(this.numerator), Hash.hash(this.denominator))
70
+ },
71
+ [Equal.symbol](this: Rational, that: unknown): boolean {
72
+ return isRational(that) && Equivalence(this, that)
73
+ },
74
+ toString(this: Rational) {
75
+ return `Rational(${format(this)})`
76
+ },
77
+ toJSON(this: Rational) {
78
+ return {
79
+ _id: "Rational",
80
+ numerator: String(this.numerator),
81
+ denominator: String(this.denominator)
82
+ }
83
+ },
84
+ [NodeInspectSymbol](this: Rational) {
85
+ return this.toJSON()
86
+ },
87
+ pipe() {
88
+ return pipeArguments(this, arguments)
89
+ }
90
+ } as const
91
+
92
+ // Both arguments must already be coprime with a positive denominator.
93
+ const canonical = (numerator: bigint, denominator: bigint): Rational => {
94
+ const rational = Object.create(RationalProto)
95
+ rational.numerator = numerator
96
+ rational.denominator = denominator
97
+ return rational
98
+ }
99
+
100
+ // `denominator` must be non-zero.
101
+ const reduce = (numerator: bigint, denominator: bigint): Rational => {
102
+ if (denominator < bigint0) {
103
+ numerator = -numerator
104
+ denominator = -denominator
105
+ }
106
+ const divisor = BI.gcd(numerator, denominator)
107
+ return canonical(numerator / divisor, denominator / divisor)
108
+ }
109
+
110
+ // Both pairs must already be coprime with positive denominators. The sum can only
111
+ // share factors with the denominators' gcd (Knuth, TAOCP vol. 2, §4.5.1).
112
+ const sumCoprime = (n1: bigint, d1: bigint, n2: bigint, d2: bigint): Rational => {
113
+ const g = BI.gcd(d1, d2)
114
+ if (g === bigint1) {
115
+ return canonical(n1 * d2 + n2 * d1, d1 * d2)
116
+ }
117
+ const t = n1 * (d2 / g) + n2 * (d1 / g)
118
+ const e = BI.gcd(t, g)
119
+ return canonical(t / e, (d1 / g) * (d2 / e))
120
+ }
121
+
122
+ // Both pairs must already be coprime with positive denominators. Canonical fractions share factors
123
+ // only crosswise, so cancelling those first leaves the products coprime (Knuth, TAOCP vol. 2, §4.5.1).
124
+ const multiplyCoprime = (n1: bigint, d1: bigint, n2: bigint, d2: bigint): Rational => {
125
+ const g1 = BI.gcd(n1, d2)
126
+ const g2 = BI.gcd(n2, d1)
127
+ return canonical((n1 / g1) * (n2 / g2), (d1 / g2) * (d2 / g1))
128
+ }
129
+
130
+ const zero = canonical(bigint0, bigint1)
131
+ const one = canonical(bigint1, bigint1)
132
+
133
+ /**
134
+ * Checks whether a given value is a `Rational`.
135
+ *
136
+ * **Example** (Checking Rational values)
137
+ *
138
+ * ```ts import.meta.vitest
139
+ * import { Rational } from "effect"
140
+ *
141
+ * Rational.isRational(Rational.fromBigInt(1n)) // => true
142
+ * Rational.isRational("1/3") // => false
143
+ * ```
144
+ *
145
+ * @stability unstable
146
+ * @category guards
147
+ * @since 4.1.3
148
+ */
149
+ export const isRational = (u: unknown): u is Rational => hasProperty(u, TypeId)
150
+
151
+ /**
152
+ * Creates a `Rational` from a numerator and denominator, returning
153
+ * `Option.none()` when the denominator is zero.
154
+ *
155
+ * **Details**
156
+ *
157
+ * The result is reduced to lowest terms with a positive denominator, so
158
+ * `make(6n, -4n)` and `make(-3n, 2n)` produce the same value.
159
+ *
160
+ * **Example** (Creating rationals safely)
161
+ *
162
+ * ```ts import.meta.vitest
163
+ * import { Option, Rational } from "effect"
164
+ *
165
+ * Rational.make(6n, -4n) // => Option.some(Rational.makeUnsafe(-3n, 2n))
166
+ * Rational.make(1n, 0n) // => Option.none()
167
+ * ```
168
+ *
169
+ * @see {@link makeUnsafe} for throwing on a zero denominator
170
+ *
171
+ * @stability unstable
172
+ * @category constructors
173
+ * @since 4.1.3
174
+ */
175
+ export const make = (numerator: bigint, denominator: bigint): Option.Option<Rational> =>
176
+ denominator === bigint0 ? Option.none() : Option.some(reduce(numerator, denominator))
177
+
178
+ /**
179
+ * Creates a `Rational` from a numerator and denominator.
180
+ *
181
+ * **Gotchas**
182
+ *
183
+ * Throws a `RangeError` if the denominator is zero.
184
+ *
185
+ * **Example** (Creating rationals from bigint pairs)
186
+ *
187
+ * ```ts import.meta.vitest
188
+ * import { Rational } from "effect"
189
+ *
190
+ * Rational.format(Rational.makeUnsafe(2n, 6n)) // => "1/3"
191
+ * ```
192
+ *
193
+ * @see {@link make} for returning `Option.none` on a zero denominator
194
+ *
195
+ * @stability unstable
196
+ * @category constructors
197
+ * @since 4.1.3
198
+ */
199
+ export const makeUnsafe = (numerator: bigint, denominator: bigint): Rational => {
200
+ if (denominator === bigint0) {
201
+ throw new RangeError("Denominator must be non-zero")
202
+ }
203
+ return reduce(numerator, denominator)
204
+ }
205
+
206
+ /**
207
+ * Creates an integer `Rational` from a `bigint`.
208
+ *
209
+ * **Example** (Creating rationals from bigint)
210
+ *
211
+ * ```ts import.meta.vitest
212
+ * import { Rational } from "effect"
213
+ *
214
+ * Rational.format(Rational.fromBigInt(-3n)) // => "-3/1"
215
+ * ```
216
+ *
217
+ * @stability unstable
218
+ * @category constructors
219
+ * @since 4.1.3
220
+ */
221
+ export const fromBigInt = (n: bigint): Rational => canonical(n, bigint1)
222
+
223
+ /**
224
+ * Converts a `BigDecimal` to the exact `Rational` it denotes.
225
+ *
226
+ * **When to use**
227
+ *
228
+ * Use as the exact route from decimal text: `0.1` becomes `1/10`, which
229
+ * {@link fromNumber} cannot produce.
230
+ *
231
+ * **Example** (Converting decimals exactly)
232
+ *
233
+ * ```ts import.meta.vitest
234
+ * import { BigDecimal, Rational } from "effect"
235
+ *
236
+ * Rational.fromBigDecimal(BigDecimal.fromStringUnsafe("-12.50")) // => Rational.makeUnsafe(-25n, 2n)
237
+ * ```
238
+ *
239
+ * @see {@link toBigDecimal} for the rounded conversion back
240
+ *
241
+ * @stability unstable
242
+ * @category constructors
243
+ * @since 4.1.3
244
+ */
245
+ export const fromBigDecimal = (n: BigDecimal.BigDecimal): Rational => {
246
+ const normalized = BigDecimal.normalize(n)
247
+ return normalized.scale >= 0
248
+ ? reduce(normalized.value, bigint10 ** BigInt(normalized.scale))
249
+ : canonical(normalized.value * bigint10 ** BigInt(-normalized.scale), bigint1)
250
+ }
251
+
252
+ /**
253
+ * Creates a `Rational` from a finite `number`, returning `Option.none()` for
254
+ * `NaN`, `+Infinity` and `-Infinity`.
255
+ *
256
+ * **Details**
257
+ *
258
+ * The result is the exact binary value of the number, not its decimal display.
259
+ * Negative zero becomes `0/1`.
260
+ *
261
+ * **Gotchas**
262
+ *
263
+ * `0.1` is not one tenth: the nearest double is `3602879701896397 / 2^55`, and
264
+ * that is the value this function returns. Use {@link fromBigDecimal} with a
265
+ * decimal string when the decimal reading is intended.
266
+ *
267
+ * **Example** (Creating rationals from numbers)
268
+ *
269
+ * ```ts import.meta.vitest
270
+ * import { Option, Rational } from "effect"
271
+ *
272
+ * Rational.fromNumber(0.75) // => Option.some(Rational.makeUnsafe(3n, 4n))
273
+ * Rational.fromNumber(NaN) // => Option.none()
274
+ * ```
275
+ *
276
+ * @see {@link fromNumberUnsafe} for throwing when the number is not finite
277
+ *
278
+ * @stability unstable
279
+ * @category constructors
280
+ * @since 4.1.3
281
+ */
282
+ export const fromNumber = (n: number): Option.Option<Rational> => {
283
+ if (!Number.isFinite(n)) {
284
+ return Option.none()
285
+ }
286
+ if (n === 0) {
287
+ return Option.some(zero)
288
+ }
289
+ // Doubling is exact, and a finite double becomes integral within 1074 steps.
290
+ let scaled = n
291
+ let exponent = 0
292
+ while (!Number.isInteger(scaled)) {
293
+ scaled *= 2
294
+ exponent++
295
+ }
296
+ return Option.some(reduce(BigInt(scaled), bigint1 << BigInt(exponent)))
297
+ }
298
+
299
+ /**
300
+ * Creates a `Rational` from a finite `number`.
301
+ *
302
+ * **Gotchas**
303
+ *
304
+ * Throws a `RangeError` if the number is not finite. See {@link fromNumber}
305
+ * for why `0.1` does not become one tenth.
306
+ *
307
+ * **Example** (Creating rationals from trusted numbers)
308
+ *
309
+ * ```ts import.meta.vitest
310
+ * import { Rational } from "effect"
311
+ *
312
+ * Rational.fromNumberUnsafe(0.5) // => Rational.makeUnsafe(1n, 2n)
313
+ * ```
314
+ *
315
+ * @see {@link fromNumber} for returning `Option.none` when the number is not finite
316
+ *
317
+ * @stability unstable
318
+ * @category constructors
319
+ * @since 4.1.3
320
+ */
321
+ export const fromNumberUnsafe = (n: number): Rational =>
322
+ Option.getOrThrowWith(fromNumber(n), () => new RangeError(`Number must be finite, got ${n}`))
323
+
324
+ /**
325
+ * Parses an integer or fraction string into a `Rational` safely.
326
+ *
327
+ * **Details**
328
+ *
329
+ * Accepts `[+-]digits` or `[+-]digits/[+-]digits`. Reducible fractions and
330
+ * negative denominators are normalized. Whitespace, decimal points, exponents,
331
+ * the empty string and a zero denominator produce `Option.none()`.
332
+ *
333
+ * **Example** (Parsing fraction strings)
334
+ *
335
+ * ```ts import.meta.vitest
336
+ * import { Option, Rational } from "effect"
337
+ *
338
+ * Rational.fromString("-4/-6") // => Option.some(Rational.makeUnsafe(2n, 3n))
339
+ * Rational.fromString("3") // => Option.some(Rational.fromBigInt(3n))
340
+ * Rational.fromString("1.5") // => Option.none()
341
+ * ```
342
+ *
343
+ * @see {@link fromStringUnsafe} for parsing that throws on invalid input
344
+ * @see {@link format} for the inverse operation
345
+ *
346
+ * @stability unstable
347
+ * @category constructors
348
+ * @since 4.1.3
349
+ */
350
+ export const fromString = (s: string): Option.Option<Rational> => {
351
+ const match = FRACTION_REGEXP.exec(s)
352
+ if (match === null) {
353
+ return Option.none()
354
+ }
355
+ return make(BigInt(match[1]), match[2] === undefined ? bigint1 : BigInt(match[2]))
356
+ }
357
+
358
+ /**
359
+ * Parses an integer or fraction string into a `Rational`.
360
+ *
361
+ * **Gotchas**
362
+ *
363
+ * Throws an `Error` if the string is not a valid fraction. See
364
+ * {@link fromString} for the accepted grammar.
365
+ *
366
+ * **Example** (Parsing trusted fraction strings)
367
+ *
368
+ * ```ts import.meta.vitest
369
+ * import { Rational } from "effect"
370
+ *
371
+ * Rational.fromStringUnsafe("6/5") // => Rational.makeUnsafe(6n, 5n)
372
+ * ```
373
+ *
374
+ * @see {@link fromString} for returning `Option.none` on invalid input
375
+ *
376
+ * @stability unstable
377
+ * @category constructors
378
+ * @since 4.1.3
379
+ */
380
+ export const fromStringUnsafe = (s: string): Rational =>
381
+ Option.getOrThrowWith(fromString(s), () => new Error(`Invalid rational string: ${s}`))
382
+
383
+ /**
384
+ * Adds two rationals exactly.
385
+ *
386
+ * **Example** (Adding rationals)
387
+ *
388
+ * ```ts import.meta.vitest
389
+ * import { Rational } from "effect"
390
+ *
391
+ * Rational.sum(Rational.makeUnsafe(1n, 3n), Rational.makeUnsafe(1n, 6n)) // => Rational.makeUnsafe(1n, 2n)
392
+ * ```
393
+ *
394
+ * @stability unstable
395
+ * @category math
396
+ * @since 4.1.3
397
+ */
398
+ export const sum: {
399
+ /**
400
+ * Adds two rationals exactly.
401
+ *
402
+ * **Example** (Adding rationals)
403
+ *
404
+ * ```ts import.meta.vitest
405
+ * import { Rational } from "effect"
406
+ *
407
+ * Rational.sum(Rational.makeUnsafe(1n, 3n), Rational.makeUnsafe(1n, 6n)) // => Rational.makeUnsafe(1n, 2n)
408
+ * ```
409
+ *
410
+ * @stability unstable
411
+ * @category math
412
+ * @since 4.1.3
413
+ */
414
+ (that: Rational): (self: Rational) => Rational
415
+ /**
416
+ * Adds two rationals exactly.
417
+ *
418
+ * **Example** (Adding rationals)
419
+ *
420
+ * ```ts import.meta.vitest
421
+ * import { Rational } from "effect"
422
+ *
423
+ * Rational.sum(Rational.makeUnsafe(1n, 3n), Rational.makeUnsafe(1n, 6n)) // => Rational.makeUnsafe(1n, 2n)
424
+ * ```
425
+ *
426
+ * @stability unstable
427
+ * @category math
428
+ * @since 4.1.3
429
+ */
430
+ (self: Rational, that: Rational): Rational
431
+ } = dual(
432
+ 2,
433
+ (self: Rational, that: Rational): Rational =>
434
+ sumCoprime(self.numerator, self.denominator, that.numerator, that.denominator)
435
+ )
436
+
437
+ /**
438
+ * Sums all rationals in an iterable, returning zero for an empty collection.
439
+ *
440
+ * **Example** (Summing rationals)
441
+ *
442
+ * ```ts import.meta.vitest
443
+ * import { Rational } from "effect"
444
+ *
445
+ * const third = Rational.makeUnsafe(1n, 3n)
446
+ *
447
+ * Rational.sumAll([third, third, third]) // => Rational.fromBigInt(1n)
448
+ * ```
449
+ *
450
+ * @stability unstable
451
+ * @category math
452
+ * @since 4.1.3
453
+ */
454
+ export const sumAll = (collection: Iterable<Rational>): Rational => {
455
+ let out = zero
456
+ for (const n of collection) {
457
+ out = sum(out, n)
458
+ }
459
+ return out
460
+ }
461
+
462
+ /**
463
+ * Subtracts `that` from `self` exactly.
464
+ *
465
+ * **Example** (Subtracting rationals)
466
+ *
467
+ * ```ts import.meta.vitest
468
+ * import { Rational } from "effect"
469
+ *
470
+ * Rational.subtract(Rational.fromBigInt(1n), Rational.makeUnsafe(1n, 3n)) // => Rational.makeUnsafe(2n, 3n)
471
+ * ```
472
+ *
473
+ * @stability unstable
474
+ * @category math
475
+ * @since 4.1.3
476
+ */
477
+ export const subtract: {
478
+ /**
479
+ * Subtracts `that` from `self` exactly.
480
+ *
481
+ * **Example** (Subtracting rationals)
482
+ *
483
+ * ```ts import.meta.vitest
484
+ * import { Rational } from "effect"
485
+ *
486
+ * Rational.subtract(Rational.fromBigInt(1n), Rational.makeUnsafe(1n, 3n)) // => Rational.makeUnsafe(2n, 3n)
487
+ * ```
488
+ *
489
+ * @stability unstable
490
+ * @category math
491
+ * @since 4.1.3
492
+ */
493
+ (that: Rational): (self: Rational) => Rational
494
+ /**
495
+ * Subtracts `that` from `self` exactly.
496
+ *
497
+ * **Example** (Subtracting rationals)
498
+ *
499
+ * ```ts import.meta.vitest
500
+ * import { Rational } from "effect"
501
+ *
502
+ * Rational.subtract(Rational.fromBigInt(1n), Rational.makeUnsafe(1n, 3n)) // => Rational.makeUnsafe(2n, 3n)
503
+ * ```
504
+ *
505
+ * @stability unstable
506
+ * @category math
507
+ * @since 4.1.3
508
+ */
509
+ (self: Rational, that: Rational): Rational
510
+ } = dual(
511
+ 2,
512
+ (self: Rational, that: Rational): Rational =>
513
+ sumCoprime(self.numerator, self.denominator, -that.numerator, that.denominator)
514
+ )
515
+
516
+ /**
517
+ * Multiplies two rationals exactly.
518
+ *
519
+ * **Example** (Multiplying rationals)
520
+ *
521
+ * ```ts import.meta.vitest
522
+ * import { Rational } from "effect"
523
+ *
524
+ * Rational.multiply(Rational.makeUnsafe(1n, 3n), Rational.fromBigInt(3n)) // => Rational.fromBigInt(1n)
525
+ * ```
526
+ *
527
+ * @stability unstable
528
+ * @category math
529
+ * @since 4.1.3
530
+ */
531
+ export const multiply: {
532
+ /**
533
+ * Multiplies two rationals exactly.
534
+ *
535
+ * **Example** (Multiplying rationals)
536
+ *
537
+ * ```ts import.meta.vitest
538
+ * import { Rational } from "effect"
539
+ *
540
+ * Rational.multiply(Rational.makeUnsafe(1n, 3n), Rational.fromBigInt(3n)) // => Rational.fromBigInt(1n)
541
+ * ```
542
+ *
543
+ * @stability unstable
544
+ * @category math
545
+ * @since 4.1.3
546
+ */
547
+ (that: Rational): (self: Rational) => Rational
548
+ /**
549
+ * Multiplies two rationals exactly.
550
+ *
551
+ * **Example** (Multiplying rationals)
552
+ *
553
+ * ```ts import.meta.vitest
554
+ * import { Rational } from "effect"
555
+ *
556
+ * Rational.multiply(Rational.makeUnsafe(1n, 3n), Rational.fromBigInt(3n)) // => Rational.fromBigInt(1n)
557
+ * ```
558
+ *
559
+ * @stability unstable
560
+ * @category math
561
+ * @since 4.1.3
562
+ */
563
+ (self: Rational, that: Rational): Rational
564
+ } = dual(
565
+ 2,
566
+ (self: Rational, that: Rational): Rational =>
567
+ multiplyCoprime(self.numerator, self.denominator, that.numerator, that.denominator)
568
+ )
569
+
570
+ /**
571
+ * Multiplies all rationals in an iterable, returning one for an empty
572
+ * collection.
573
+ *
574
+ * **Example** (Compounding ratios)
575
+ *
576
+ * ```ts import.meta.vitest
577
+ * import { Rational } from "effect"
578
+ *
579
+ * Rational.multiplyAll([Rational.makeUnsafe(3n, 2n), Rational.makeUnsafe(4n, 3n)]) // => Rational.fromBigInt(2n)
580
+ * ```
581
+ *
582
+ * @stability unstable
583
+ * @category math
584
+ * @since 4.1.3
585
+ */
586
+ export const multiplyAll = (collection: Iterable<Rational>): Rational => {
587
+ let out = one
588
+ for (const n of collection) {
589
+ if (n.numerator === bigint0) {
590
+ return zero
591
+ }
592
+ out = multiply(out, n)
593
+ }
594
+ return out
595
+ }
596
+
597
+ /**
598
+ * Divides `self` by `that`, returning `Option.none()` when `that` is zero.
599
+ *
600
+ * **Example** (Dividing rationals safely)
601
+ *
602
+ * ```ts import.meta.vitest
603
+ * import { Option, Rational } from "effect"
604
+ *
605
+ * const third = Rational.makeUnsafe(1n, 3n)
606
+ *
607
+ * Rational.divide(third, Rational.fromBigInt(3n)) // => Option.some(Rational.makeUnsafe(1n, 9n))
608
+ * Rational.divide(third, Rational.fromBigInt(0n)) // => Option.none()
609
+ * ```
610
+ *
611
+ * @see {@link divideUnsafe} for division that throws when the divisor is zero
612
+ *
613
+ * @stability unstable
614
+ * @category math
615
+ * @since 4.1.3
616
+ */
617
+ export const divide: {
618
+ /**
619
+ * Divides `self` by `that`, returning `Option.none()` when `that` is zero.
620
+ *
621
+ * **Example** (Dividing rationals safely)
622
+ *
623
+ * ```ts import.meta.vitest
624
+ * import { Option, Rational } from "effect"
625
+ *
626
+ * const third = Rational.makeUnsafe(1n, 3n)
627
+ *
628
+ * Rational.divide(third, Rational.fromBigInt(3n)) // => Option.some(Rational.makeUnsafe(1n, 9n))
629
+ * Rational.divide(third, Rational.fromBigInt(0n)) // => Option.none()
630
+ * ```
631
+ *
632
+ * @see {@link divideUnsafe} for division that throws when the divisor is zero
633
+ *
634
+ * @stability unstable
635
+ * @category math
636
+ * @since 4.1.3
637
+ */
638
+ (that: Rational): (self: Rational) => Option.Option<Rational>
639
+ /**
640
+ * Divides `self` by `that`, returning `Option.none()` when `that` is zero.
641
+ *
642
+ * **Example** (Dividing rationals safely)
643
+ *
644
+ * ```ts import.meta.vitest
645
+ * import { Option, Rational } from "effect"
646
+ *
647
+ * const third = Rational.makeUnsafe(1n, 3n)
648
+ *
649
+ * Rational.divide(third, Rational.fromBigInt(3n)) // => Option.some(Rational.makeUnsafe(1n, 9n))
650
+ * Rational.divide(third, Rational.fromBigInt(0n)) // => Option.none()
651
+ * ```
652
+ *
653
+ * @see {@link divideUnsafe} for division that throws when the divisor is zero
654
+ *
655
+ * @stability unstable
656
+ * @category math
657
+ * @since 4.1.3
658
+ */
659
+ (self: Rational, that: Rational): Option.Option<Rational>
660
+ } = dual(
661
+ 2,
662
+ (self: Rational, that: Rational): Option.Option<Rational> =>
663
+ that.numerator === bigint0
664
+ ? Option.none()
665
+ : Option.some(
666
+ that.numerator < bigint0
667
+ ? multiplyCoprime(self.numerator, self.denominator, -that.denominator, -that.numerator)
668
+ : multiplyCoprime(self.numerator, self.denominator, that.denominator, that.numerator)
669
+ )
670
+ )
671
+
672
+ /**
673
+ * Divides `self` by `that`.
674
+ *
675
+ * **Gotchas**
676
+ *
677
+ * Throws a `RangeError` if the divisor is zero.
678
+ *
679
+ * **Example** (Dividing rationals)
680
+ *
681
+ * ```ts import.meta.vitest
682
+ * import { Rational } from "effect"
683
+ *
684
+ * Rational.divideUnsafe(Rational.makeUnsafe(-2n, 3n), Rational.makeUnsafe(4n, 9n)) // => Rational.makeUnsafe(-3n, 2n)
685
+ * ```
686
+ *
687
+ * @see {@link divide} for division that returns `Option.none` when the divisor is zero
688
+ *
689
+ * @stability unstable
690
+ * @category math
691
+ * @since 4.1.3
692
+ */
693
+ export const divideUnsafe: {
694
+ /**
695
+ * Divides `self` by `that`.
696
+ *
697
+ * **Gotchas**
698
+ *
699
+ * Throws a `RangeError` if the divisor is zero.
700
+ *
701
+ * **Example** (Dividing rationals)
702
+ *
703
+ * ```ts import.meta.vitest
704
+ * import { Rational } from "effect"
705
+ *
706
+ * Rational.divideUnsafe(Rational.makeUnsafe(-2n, 3n), Rational.makeUnsafe(4n, 9n)) // => Rational.makeUnsafe(-3n, 2n)
707
+ * ```
708
+ *
709
+ * @see {@link divide} for division that returns `Option.none` when the divisor is zero
710
+ *
711
+ * @stability unstable
712
+ * @category math
713
+ * @since 4.1.3
714
+ */
715
+ (that: Rational): (self: Rational) => Rational
716
+ /**
717
+ * Divides `self` by `that`.
718
+ *
719
+ * **Gotchas**
720
+ *
721
+ * Throws a `RangeError` if the divisor is zero.
722
+ *
723
+ * **Example** (Dividing rationals)
724
+ *
725
+ * ```ts import.meta.vitest
726
+ * import { Rational } from "effect"
727
+ *
728
+ * Rational.divideUnsafe(Rational.makeUnsafe(-2n, 3n), Rational.makeUnsafe(4n, 9n)) // => Rational.makeUnsafe(-3n, 2n)
729
+ * ```
730
+ *
731
+ * @see {@link divide} for division that returns `Option.none` when the divisor is zero
732
+ *
733
+ * @stability unstable
734
+ * @category math
735
+ * @since 4.1.3
736
+ */
737
+ (self: Rational, that: Rational): Rational
738
+ } = dual(
739
+ 2,
740
+ (self: Rational, that: Rational): Rational => Option.getOrThrowWith(divide(self, that), divisionByZero)
741
+ )
742
+
743
+ const divisionByZero = () => new RangeError("Division by zero")
744
+
745
+ /**
746
+ * Raises a rational to an integer power, returning `Option.none()` for zero
747
+ * raised to a negative exponent.
748
+ *
749
+ * **Details**
750
+ *
751
+ * A negative exponent inverts the fraction before raising it, and any value
752
+ * to the power zero is one, including zero itself.
753
+ *
754
+ * **Gotchas**
755
+ *
756
+ * The exponent must be a safe integer. Anything else, including `0.5`, `NaN`
757
+ * and `Infinity`, throws a `RangeError` instead of being rounded.
758
+ *
759
+ * **Example** (Raising to integer powers)
760
+ *
761
+ * ```ts import.meta.vitest
762
+ * import { Option, Rational } from "effect"
763
+ *
764
+ * Rational.pow(Rational.makeUnsafe(1n, 3n), -1) // => Option.some(Rational.fromBigInt(3n))
765
+ * Rational.pow(Rational.fromBigInt(0n), -1) // => Option.none()
766
+ * ```
767
+ *
768
+ * @see {@link powUnsafe} for throwing when zero is raised to a negative exponent
769
+ *
770
+ * @stability unstable
771
+ * @category math
772
+ * @since 4.1.3
773
+ */
774
+ export const pow: {
775
+ /**
776
+ * Raises a rational to an integer power, returning `Option.none()` for zero
777
+ * raised to a negative exponent.
778
+ *
779
+ * **Details**
780
+ *
781
+ * A negative exponent inverts the fraction before raising it, and any value
782
+ * to the power zero is one, including zero itself.
783
+ *
784
+ * **Gotchas**
785
+ *
786
+ * The exponent must be a safe integer. Anything else, including `0.5`, `NaN`
787
+ * and `Infinity`, throws a `RangeError` instead of being rounded.
788
+ *
789
+ * **Example** (Raising to integer powers)
790
+ *
791
+ * ```ts import.meta.vitest
792
+ * import { Option, Rational } from "effect"
793
+ *
794
+ * Rational.pow(Rational.makeUnsafe(1n, 3n), -1) // => Option.some(Rational.fromBigInt(3n))
795
+ * Rational.pow(Rational.fromBigInt(0n), -1) // => Option.none()
796
+ * ```
797
+ *
798
+ * @see {@link powUnsafe} for throwing when zero is raised to a negative exponent
799
+ *
800
+ * @stability unstable
801
+ * @category math
802
+ * @since 4.1.3
803
+ */
804
+ (exponent: number): (self: Rational) => Option.Option<Rational>
805
+ /**
806
+ * Raises a rational to an integer power, returning `Option.none()` for zero
807
+ * raised to a negative exponent.
808
+ *
809
+ * **Details**
810
+ *
811
+ * A negative exponent inverts the fraction before raising it, and any value
812
+ * to the power zero is one, including zero itself.
813
+ *
814
+ * **Gotchas**
815
+ *
816
+ * The exponent must be a safe integer. Anything else, including `0.5`, `NaN`
817
+ * and `Infinity`, throws a `RangeError` instead of being rounded.
818
+ *
819
+ * **Example** (Raising to integer powers)
820
+ *
821
+ * ```ts import.meta.vitest
822
+ * import { Option, Rational } from "effect"
823
+ *
824
+ * Rational.pow(Rational.makeUnsafe(1n, 3n), -1) // => Option.some(Rational.fromBigInt(3n))
825
+ * Rational.pow(Rational.fromBigInt(0n), -1) // => Option.none()
826
+ * ```
827
+ *
828
+ * @see {@link powUnsafe} for throwing when zero is raised to a negative exponent
829
+ *
830
+ * @stability unstable
831
+ * @category math
832
+ * @since 4.1.3
833
+ */
834
+ (self: Rational, exponent: number): Option.Option<Rational>
835
+ } = dual(2, (self: Rational, exponent: number): Option.Option<Rational> => {
836
+ if (!Number.isSafeInteger(exponent)) {
837
+ throw new RangeError(`Exponent must be a safe integer, got ${exponent}`)
838
+ }
839
+ if (exponent === 0) {
840
+ return Option.some(one)
841
+ }
842
+ if (exponent > 0) {
843
+ const e = BigInt(exponent)
844
+ return Option.some(canonical(self.numerator ** e, self.denominator ** e))
845
+ }
846
+ if (self.numerator === bigint0) {
847
+ return Option.none()
848
+ }
849
+ // Powers of coprime integers stay coprime, so only the sign needs fixing.
850
+ const e = BigInt(-exponent)
851
+ const numerator = self.numerator ** e
852
+ const denominator = self.denominator ** e
853
+ return Option.some(numerator < bigint0 ? canonical(-denominator, -numerator) : canonical(denominator, numerator))
854
+ })
855
+
856
+ /**
857
+ * Raises a rational to an integer power.
858
+ *
859
+ * **Gotchas**
860
+ *
861
+ * Throws a `RangeError` for zero raised to a negative exponent, and for an
862
+ * exponent that is not a safe integer.
863
+ *
864
+ * **Example** (Compounding a ratio)
865
+ *
866
+ * ```ts import.meta.vitest
867
+ * import { Rational } from "effect"
868
+ *
869
+ * // Twelve pure fifths overshoot seven octaves by the Pythagorean comma.
870
+ * Rational.powUnsafe(Rational.makeUnsafe(3n, 2n), 12) // => Rational.makeUnsafe(531441n, 4096n)
871
+ * ```
872
+ *
873
+ * @see {@link pow} for returning `Option.none` when zero is raised to a negative exponent
874
+ *
875
+ * @stability unstable
876
+ * @category math
877
+ * @since 4.1.3
878
+ */
879
+ export const powUnsafe: {
880
+ /**
881
+ * Raises a rational to an integer power.
882
+ *
883
+ * **Gotchas**
884
+ *
885
+ * Throws a `RangeError` for zero raised to a negative exponent, and for an
886
+ * exponent that is not a safe integer.
887
+ *
888
+ * **Example** (Compounding a ratio)
889
+ *
890
+ * ```ts import.meta.vitest
891
+ * import { Rational } from "effect"
892
+ *
893
+ * // Twelve pure fifths overshoot seven octaves by the Pythagorean comma.
894
+ * Rational.powUnsafe(Rational.makeUnsafe(3n, 2n), 12) // => Rational.makeUnsafe(531441n, 4096n)
895
+ * ```
896
+ *
897
+ * @see {@link pow} for returning `Option.none` when zero is raised to a negative exponent
898
+ *
899
+ * @stability unstable
900
+ * @category math
901
+ * @since 4.1.3
902
+ */
903
+ (exponent: number): (self: Rational) => Rational
904
+ /**
905
+ * Raises a rational to an integer power.
906
+ *
907
+ * **Gotchas**
908
+ *
909
+ * Throws a `RangeError` for zero raised to a negative exponent, and for an
910
+ * exponent that is not a safe integer.
911
+ *
912
+ * **Example** (Compounding a ratio)
913
+ *
914
+ * ```ts import.meta.vitest
915
+ * import { Rational } from "effect"
916
+ *
917
+ * // Twelve pure fifths overshoot seven octaves by the Pythagorean comma.
918
+ * Rational.powUnsafe(Rational.makeUnsafe(3n, 2n), 12) // => Rational.makeUnsafe(531441n, 4096n)
919
+ * ```
920
+ *
921
+ * @see {@link pow} for returning `Option.none` when zero is raised to a negative exponent
922
+ *
923
+ * @stability unstable
924
+ * @category math
925
+ * @since 4.1.3
926
+ */
927
+ (self: Rational, exponent: number): Rational
928
+ } = dual(2, (self: Rational, exponent: number): Rational => Option.getOrThrowWith(pow(self, exponent), divisionByZero))
929
+
930
+ /**
931
+ * Negates a rational.
932
+ *
933
+ * @stability unstable
934
+ * @category math
935
+ * @since 4.1.3
936
+ */
937
+ export const negate = (n: Rational): Rational => canonical(-n.numerator, n.denominator)
938
+
939
+ /**
940
+ * Returns the absolute value of a rational.
941
+ *
942
+ * @stability unstable
943
+ * @category math
944
+ * @since 4.1.3
945
+ */
946
+ export const abs = (n: Rational): Rational => n.numerator < bigint0 ? negate(n) : n
947
+
948
+ /**
949
+ * Returns the sign of a rational as an `Ordering`.
950
+ *
951
+ * @stability unstable
952
+ * @category math
953
+ * @since 4.1.3
954
+ */
955
+ export const sign = (n: Rational): Ordering => n.numerator === bigint0 ? 0 : n.numerator < bigint0 ? -1 : 1
956
+
957
+ const compare = (self: Rational, that: Rational): Ordering => {
958
+ // Denominators are positive, so cross-multiplying preserves the ordering.
959
+ const left = self.numerator * that.denominator
960
+ const right = that.numerator * self.denominator
961
+ return left < right ? -1 : left > right ? 1 : 0
962
+ }
963
+
964
+ /**
965
+ * An `Order` instance comparing rationals by exact value.
966
+ *
967
+ * **Example** (Comparing rationals)
968
+ *
969
+ * ```ts import.meta.vitest
970
+ * import { BigDecimal, Rational } from "effect"
971
+ *
972
+ * const third = Rational.makeUnsafe(1n, 3n)
973
+ * const nearlyThird = Rational.fromBigDecimal(BigDecimal.fromStringUnsafe("0.3333333333"))
974
+ *
975
+ * Rational.Order(third, nearlyThird) // => 1
976
+ * ```
977
+ *
978
+ * @stability unstable
979
+ * @category instances
980
+ * @since 4.1.3
981
+ */
982
+ export const Order: order.Order<Rational> = order.make(compare)
983
+
984
+ /**
985
+ * An `Equivalence` instance for rationals. Canonical form makes this a field
986
+ * comparison.
987
+ *
988
+ * @stability unstable
989
+ * @category instances
990
+ * @since 4.1.3
991
+ */
992
+ export const Equivalence: Equ.Equivalence<Rational> = Equ.make((self, that) =>
993
+ self.numerator === that.numerator && self.denominator === that.denominator
994
+ )
995
+
996
+ /**
997
+ * Checks whether two rationals are equal.
998
+ *
999
+ * @see {@link Equivalence} for passing rational equality to APIs that require an `Equivalence`
1000
+ *
1001
+ * @stability unstable
1002
+ * @category predicates
1003
+ * @since 4.1.3
1004
+ */
1005
+ export const equals: {
1006
+ /**
1007
+ * Checks whether two rationals are equal.
1008
+ *
1009
+ * @see {@link Equivalence} for passing rational equality to APIs that require an `Equivalence`
1010
+ *
1011
+ * @stability unstable
1012
+ * @category predicates
1013
+ * @since 4.1.3
1014
+ */
1015
+ (that: Rational): (self: Rational) => boolean
1016
+ /**
1017
+ * Checks whether two rationals are equal.
1018
+ *
1019
+ * @see {@link Equivalence} for passing rational equality to APIs that require an `Equivalence`
1020
+ *
1021
+ * @stability unstable
1022
+ * @category predicates
1023
+ * @since 4.1.3
1024
+ */
1025
+ (self: Rational, that: Rational): boolean
1026
+ } = dual(2, (self: Rational, that: Rational): boolean => Equivalence(self, that))
1027
+
1028
+ /**
1029
+ * Returns `true` if the first argument is less than the second.
1030
+ *
1031
+ * @stability unstable
1032
+ * @category predicates
1033
+ * @since 4.1.3
1034
+ */
1035
+ export const isLessThan: {
1036
+ /**
1037
+ * Returns `true` if the first argument is less than the second.
1038
+ *
1039
+ * @stability unstable
1040
+ * @category predicates
1041
+ * @since 4.1.3
1042
+ */
1043
+ (that: Rational): (self: Rational) => boolean
1044
+ /**
1045
+ * Returns `true` if the first argument is less than the second.
1046
+ *
1047
+ * @stability unstable
1048
+ * @category predicates
1049
+ * @since 4.1.3
1050
+ */
1051
+ (self: Rational, that: Rational): boolean
1052
+ } = order.isLessThan(Order)
1053
+
1054
+ /**
1055
+ * Returns `true` if the first argument is less than or equal to the second.
1056
+ *
1057
+ * @stability unstable
1058
+ * @category predicates
1059
+ * @since 4.1.3
1060
+ */
1061
+ export const isLessThanOrEqualTo: {
1062
+ /**
1063
+ * Returns `true` if the first argument is less than or equal to the second.
1064
+ *
1065
+ * @stability unstable
1066
+ * @category predicates
1067
+ * @since 4.1.3
1068
+ */
1069
+ (that: Rational): (self: Rational) => boolean
1070
+ /**
1071
+ * Returns `true` if the first argument is less than or equal to the second.
1072
+ *
1073
+ * @stability unstable
1074
+ * @category predicates
1075
+ * @since 4.1.3
1076
+ */
1077
+ (self: Rational, that: Rational): boolean
1078
+ } = order.isLessThanOrEqualTo(Order)
1079
+
1080
+ /**
1081
+ * Returns `true` if the first argument is greater than the second.
1082
+ *
1083
+ * @stability unstable
1084
+ * @category predicates
1085
+ * @since 4.1.3
1086
+ */
1087
+ export const isGreaterThan: {
1088
+ /**
1089
+ * Returns `true` if the first argument is greater than the second.
1090
+ *
1091
+ * @stability unstable
1092
+ * @category predicates
1093
+ * @since 4.1.3
1094
+ */
1095
+ (that: Rational): (self: Rational) => boolean
1096
+ /**
1097
+ * Returns `true` if the first argument is greater than the second.
1098
+ *
1099
+ * @stability unstable
1100
+ * @category predicates
1101
+ * @since 4.1.3
1102
+ */
1103
+ (self: Rational, that: Rational): boolean
1104
+ } = order.isGreaterThan(Order)
1105
+
1106
+ /**
1107
+ * Returns `true` if the first argument is greater than or equal to the second.
1108
+ *
1109
+ * @stability unstable
1110
+ * @category predicates
1111
+ * @since 4.1.3
1112
+ */
1113
+ export const isGreaterThanOrEqualTo: {
1114
+ /**
1115
+ * Returns `true` if the first argument is greater than or equal to the second.
1116
+ *
1117
+ * @stability unstable
1118
+ * @category predicates
1119
+ * @since 4.1.3
1120
+ */
1121
+ (that: Rational): (self: Rational) => boolean
1122
+ /**
1123
+ * Returns `true` if the first argument is greater than or equal to the second.
1124
+ *
1125
+ * @stability unstable
1126
+ * @category predicates
1127
+ * @since 4.1.3
1128
+ */
1129
+ (self: Rational, that: Rational): boolean
1130
+ } = order.isGreaterThanOrEqualTo(Order)
1131
+
1132
+ /**
1133
+ * Checks whether a rational lies within an inclusive range.
1134
+ *
1135
+ * @stability unstable
1136
+ * @category predicates
1137
+ * @since 4.1.3
1138
+ */
1139
+ export const between: {
1140
+ /**
1141
+ * Checks whether a rational lies within an inclusive range.
1142
+ *
1143
+ * @stability unstable
1144
+ * @category predicates
1145
+ * @since 4.1.3
1146
+ */
1147
+ (
1148
+ options: {
1149
+ minimum: Rational
1150
+ maximum: Rational
1151
+ }
1152
+ ): (self: Rational) => boolean
1153
+ /**
1154
+ * Checks whether a rational lies within an inclusive range.
1155
+ *
1156
+ * @stability unstable
1157
+ * @category predicates
1158
+ * @since 4.1.3
1159
+ */
1160
+ (
1161
+ self: Rational,
1162
+ options: {
1163
+ minimum: Rational
1164
+ maximum: Rational
1165
+ }
1166
+ ): boolean
1167
+ } = order.isBetween(Order)
1168
+
1169
+ /**
1170
+ * Restricts a rational to an inclusive range.
1171
+ *
1172
+ * @stability unstable
1173
+ * @category math
1174
+ * @since 4.1.3
1175
+ */
1176
+ export const clamp: {
1177
+ /**
1178
+ * Restricts a rational to an inclusive range.
1179
+ *
1180
+ * @stability unstable
1181
+ * @category math
1182
+ * @since 4.1.3
1183
+ */
1184
+ (
1185
+ options: {
1186
+ minimum: Rational
1187
+ maximum: Rational
1188
+ }
1189
+ ): (self: Rational) => Rational
1190
+ /**
1191
+ * Restricts a rational to an inclusive range.
1192
+ *
1193
+ * @stability unstable
1194
+ * @category math
1195
+ * @since 4.1.3
1196
+ */
1197
+ (
1198
+ self: Rational,
1199
+ options: {
1200
+ minimum: Rational
1201
+ maximum: Rational
1202
+ }
1203
+ ): Rational
1204
+ } = order.clamp(Order)
1205
+
1206
+ /**
1207
+ * Returns the smaller of two rationals.
1208
+ *
1209
+ * @stability unstable
1210
+ * @category math
1211
+ * @since 4.1.3
1212
+ */
1213
+ export const min: {
1214
+ /**
1215
+ * Returns the smaller of two rationals.
1216
+ *
1217
+ * @stability unstable
1218
+ * @category math
1219
+ * @since 4.1.3
1220
+ */
1221
+ (that: Rational): (self: Rational) => Rational
1222
+ /**
1223
+ * Returns the smaller of two rationals.
1224
+ *
1225
+ * @stability unstable
1226
+ * @category math
1227
+ * @since 4.1.3
1228
+ */
1229
+ (self: Rational, that: Rational): Rational
1230
+ } = order.min(Order)
1231
+
1232
+ /**
1233
+ * Returns the larger of two rationals.
1234
+ *
1235
+ * @stability unstable
1236
+ * @category math
1237
+ * @since 4.1.3
1238
+ */
1239
+ export const max: {
1240
+ /**
1241
+ * Returns the larger of two rationals.
1242
+ *
1243
+ * @stability unstable
1244
+ * @category math
1245
+ * @since 4.1.3
1246
+ */
1247
+ (that: Rational): (self: Rational) => Rational
1248
+ /**
1249
+ * Returns the larger of two rationals.
1250
+ *
1251
+ * @stability unstable
1252
+ * @category math
1253
+ * @since 4.1.3
1254
+ */
1255
+ (self: Rational, that: Rational): Rational
1256
+ } = order.max(Order)
1257
+
1258
+ /**
1259
+ * Checks whether a rational is zero.
1260
+ *
1261
+ * @stability unstable
1262
+ * @category predicates
1263
+ * @since 4.1.3
1264
+ */
1265
+ export const isZero = (n: Rational): boolean => n.numerator === bigint0
1266
+
1267
+ /**
1268
+ * Checks whether a rational is strictly negative.
1269
+ *
1270
+ * @stability unstable
1271
+ * @category predicates
1272
+ * @since 4.1.3
1273
+ */
1274
+ export const isNegative = (n: Rational): boolean => n.numerator < bigint0
1275
+
1276
+ /**
1277
+ * Checks whether a rational is strictly positive.
1278
+ *
1279
+ * @stability unstable
1280
+ * @category predicates
1281
+ * @since 4.1.3
1282
+ */
1283
+ export const isPositive = (n: Rational): boolean => n.numerator > bigint0
1284
+
1285
+ /**
1286
+ * Checks whether a rational is an integer, that is, whether its denominator is
1287
+ * one.
1288
+ *
1289
+ * @stability unstable
1290
+ * @category predicates
1291
+ * @since 4.1.3
1292
+ */
1293
+ export const isInteger = (n: Rational): boolean => n.denominator === bigint1
1294
+
1295
+ /**
1296
+ * Formats a rational as `numerator/denominator`.
1297
+ *
1298
+ * **Details**
1299
+ *
1300
+ * The output is always a fraction in lowest terms, so integers render as
1301
+ * `3/1` and zero as `0/1`. It is the inverse of {@link fromString}.
1302
+ *
1303
+ * **Example** (Formatting rationals)
1304
+ *
1305
+ * ```ts import.meta.vitest
1306
+ * import { Rational } from "effect"
1307
+ *
1308
+ * Rational.format(Rational.makeUnsafe(12n, 10n)) // => "6/5"
1309
+ * Rational.format(Rational.fromBigInt(0n)) // => "0/1"
1310
+ * ```
1311
+ *
1312
+ * @stability unstable
1313
+ * @category converting
1314
+ * @since 4.1.3
1315
+ */
1316
+ export const format = (n: Rational): string => `${n.numerator}/${n.denominator}`
1317
+
1318
+ const bitLength = (n: bigint): number => n.toString(2).length
1319
+
1320
+ /**
1321
+ * Converts a rational to the nearest JavaScript `number`.
1322
+ *
1323
+ * **Details**
1324
+ *
1325
+ * The ratio is rounded as a whole, so a numerator and denominator that each
1326
+ * exceed `Number.MAX_VALUE` still produce a finite result when their quotient
1327
+ * is finite. Every finite double round-trips through {@link fromNumber}.
1328
+ *
1329
+ * **Gotchas**
1330
+ *
1331
+ * This conversion is unsafe because most rationals are not representable as
1332
+ * doubles: the result is rounded, and very large magnitudes become
1333
+ * `Infinity`.
1334
+ *
1335
+ * **Example** (Converting rationals to numbers)
1336
+ *
1337
+ * ```ts import.meta.vitest
1338
+ * import { Rational } from "effect"
1339
+ *
1340
+ * Rational.toNumberUnsafe(Rational.makeUnsafe(1n, 3n)) // => 1 / 3
1341
+ * ```
1342
+ *
1343
+ * @see {@link toBigDecimal} for a conversion with an explicit scale
1344
+ *
1345
+ * @stability unstable
1346
+ * @category converting
1347
+ * @since 4.1.3
1348
+ */
1349
+ export const toNumberUnsafe = (n: Rational): number => {
1350
+ if (n.numerator === bigint0) {
1351
+ return 0
1352
+ }
1353
+ const negative = n.numerator < bigint0
1354
+ let numerator = negative ? -n.numerator : n.numerator
1355
+ let denominator = n.denominator
1356
+ // Shift so the integer quotient carries more bits than a double mantissa.
1357
+ const shift = QUOTIENT_BITS + bitLength(denominator) - bitLength(numerator)
1358
+ if (shift > 0) {
1359
+ numerator <<= BigInt(shift)
1360
+ } else if (shift < 0) {
1361
+ denominator <<= BigInt(-shift)
1362
+ }
1363
+ let quotient = numerator / denominator
1364
+ if (quotient * denominator !== numerator) {
1365
+ // A sticky bit keeps a truncated remainder from being mistaken for a tie.
1366
+ quotient |= bigint1
1367
+ }
1368
+ // Round once, at the destination precision: 53 bits for normal results, or
1369
+ // units of Number.MIN_VALUE for subnormal ones. The quotient has at least 64
1370
+ // bits, so at least 11 are dropped.
1371
+ const exponent = Math.max(bitLength(quotient) - shift - MANTISSA_BITS, MIN_SUBNORMAL_EXPONENT)
1372
+ const drop = BigInt(exponent + shift)
1373
+ let rounded = quotient >> drop
1374
+ const remainder = quotient & ((bigint1 << drop) - bigint1)
1375
+ const half = bigint1 << (drop - bigint1)
1376
+ if (remainder > half || (remainder === half && (rounded & bigint1) === bigint1)) {
1377
+ rounded += bigint1
1378
+ }
1379
+ // `rounded` is at most 2^53 and `exponent` is at least -1074, so both the
1380
+ // conversion and the power-of-two scaling are exact; overflow gives Infinity.
1381
+ const magnitude = Number(rounded) * 2 ** exponent
1382
+ return negative ? -magnitude : magnitude
1383
+ }
1384
+
1385
+ /**
1386
+ * Converts a rational to a `BigDecimal` rounded at the given scale.
1387
+ *
1388
+ * **Details**
1389
+ *
1390
+ * `scale` is the number of decimal places to keep; a negative scale rounds to
1391
+ * tens, hundreds and so on. `mode` defaults to `"half-from-zero"`, matching
1392
+ * `BigDecimal.round`.
1393
+ *
1394
+ * **Gotchas**
1395
+ *
1396
+ * Throws a `RangeError` if `scale` is not a safe integer.
1397
+ *
1398
+ * **Example** (Rounding rationals to decimals)
1399
+ *
1400
+ * ```ts import.meta.vitest
1401
+ * import { BigDecimal, Rational } from "effect"
1402
+ *
1403
+ * const twoThirds = Rational.makeUnsafe(2n, 3n)
1404
+ *
1405
+ * Rational.toBigDecimal(twoThirds, { scale: 3 }) // => BigDecimal.fromStringUnsafe("0.667")
1406
+ * Rational.toBigDecimal(twoThirds, { scale: 3, mode: "floor" }) // => BigDecimal.fromStringUnsafe("0.666")
1407
+ * ```
1408
+ *
1409
+ * @see {@link fromBigDecimal} for the exact conversion back
1410
+ *
1411
+ * @stability unstable
1412
+ * @category converting
1413
+ * @since 4.1.3
1414
+ */
1415
+ export const toBigDecimal: {
1416
+ /**
1417
+ * Converts a rational to a `BigDecimal` rounded at the given scale.
1418
+ *
1419
+ * **Details**
1420
+ *
1421
+ * `scale` is the number of decimal places to keep; a negative scale rounds to
1422
+ * tens, hundreds and so on. `mode` defaults to `"half-from-zero"`, matching
1423
+ * `BigDecimal.round`.
1424
+ *
1425
+ * **Gotchas**
1426
+ *
1427
+ * Throws a `RangeError` if `scale` is not a safe integer.
1428
+ *
1429
+ * **Example** (Rounding rationals to decimals)
1430
+ *
1431
+ * ```ts import.meta.vitest
1432
+ * import { BigDecimal, Rational } from "effect"
1433
+ *
1434
+ * const twoThirds = Rational.makeUnsafe(2n, 3n)
1435
+ *
1436
+ * Rational.toBigDecimal(twoThirds, { scale: 3 }) // => BigDecimal.fromStringUnsafe("0.667")
1437
+ * Rational.toBigDecimal(twoThirds, { scale: 3, mode: "floor" }) // => BigDecimal.fromStringUnsafe("0.666")
1438
+ * ```
1439
+ *
1440
+ * @see {@link fromBigDecimal} for the exact conversion back
1441
+ *
1442
+ * @stability unstable
1443
+ * @category converting
1444
+ * @since 4.1.3
1445
+ */
1446
+ (
1447
+ options: { readonly scale: number; readonly mode?: BigDecimal.RoundingMode }
1448
+ ): (self: Rational) => BigDecimal.BigDecimal
1449
+ /**
1450
+ * Converts a rational to a `BigDecimal` rounded at the given scale.
1451
+ *
1452
+ * **Details**
1453
+ *
1454
+ * `scale` is the number of decimal places to keep; a negative scale rounds to
1455
+ * tens, hundreds and so on. `mode` defaults to `"half-from-zero"`, matching
1456
+ * `BigDecimal.round`.
1457
+ *
1458
+ * **Gotchas**
1459
+ *
1460
+ * Throws a `RangeError` if `scale` is not a safe integer.
1461
+ *
1462
+ * **Example** (Rounding rationals to decimals)
1463
+ *
1464
+ * ```ts import.meta.vitest
1465
+ * import { BigDecimal, Rational } from "effect"
1466
+ *
1467
+ * const twoThirds = Rational.makeUnsafe(2n, 3n)
1468
+ *
1469
+ * Rational.toBigDecimal(twoThirds, { scale: 3 }) // => BigDecimal.fromStringUnsafe("0.667")
1470
+ * Rational.toBigDecimal(twoThirds, { scale: 3, mode: "floor" }) // => BigDecimal.fromStringUnsafe("0.666")
1471
+ * ```
1472
+ *
1473
+ * @see {@link fromBigDecimal} for the exact conversion back
1474
+ *
1475
+ * @stability unstable
1476
+ * @category converting
1477
+ * @since 4.1.3
1478
+ */
1479
+ (
1480
+ self: Rational,
1481
+ options: { readonly scale: number; readonly mode?: BigDecimal.RoundingMode }
1482
+ ): BigDecimal.BigDecimal
1483
+ } = dual(
1484
+ 2,
1485
+ (
1486
+ self: Rational,
1487
+ options: { readonly scale: number; readonly mode?: BigDecimal.RoundingMode }
1488
+ ): BigDecimal.BigDecimal => {
1489
+ const scale = options.scale
1490
+ if (!Number.isSafeInteger(scale)) {
1491
+ throw new RangeError(`Scale must be a safe integer, got ${scale}`)
1492
+ }
1493
+ let numerator = self.numerator
1494
+ let denominator = self.denominator
1495
+ if (scale >= 0) {
1496
+ numerator *= bigint10 ** BigInt(scale)
1497
+ } else {
1498
+ denominator *= bigint10 ** BigInt(-scale)
1499
+ }
1500
+ const quotient = numerator / denominator
1501
+ const remainder = numerator - quotient * denominator
1502
+ return BigDecimal.make(quotient + roundingAdjustment(quotient, remainder, denominator, options.mode), scale)
1503
+ }
1504
+ )
1505
+
1506
+ // `remainder` carries the sign of the value; returns the unit to add to the truncated quotient.
1507
+ const roundingAdjustment = (
1508
+ quotient: bigint,
1509
+ remainder: bigint,
1510
+ denominator: bigint,
1511
+ mode: BigDecimal.RoundingMode = "half-from-zero"
1512
+ ): bigint => {
1513
+ if (remainder === bigint0) {
1514
+ return bigint0
1515
+ }
1516
+ const negative = remainder < bigint0
1517
+ const awayFromZero = negative ? -bigint1 : bigint1
1518
+ switch (mode) {
1519
+ case "to-zero":
1520
+ return bigint0
1521
+ case "from-zero":
1522
+ return awayFromZero
1523
+ case "ceil":
1524
+ return negative ? bigint0 : bigint1
1525
+ case "floor":
1526
+ return negative ? -bigint1 : bigint0
1527
+ }
1528
+ const twiceRemainder = (negative ? -remainder : remainder) * bigint2
1529
+ if (twiceRemainder < denominator) {
1530
+ return bigint0
1531
+ }
1532
+ if (twiceRemainder > denominator) {
1533
+ return awayFromZero
1534
+ }
1535
+ switch (mode) {
1536
+ case "half-ceil":
1537
+ return negative ? bigint0 : bigint1
1538
+ case "half-floor":
1539
+ return negative ? -bigint1 : bigint0
1540
+ case "half-to-zero":
1541
+ return bigint0
1542
+ case "half-from-zero":
1543
+ return awayFromZero
1544
+ case "half-even":
1545
+ return quotient % bigint2 === bigint0 ? bigint0 : awayFromZero
1546
+ case "half-odd":
1547
+ return quotient % bigint2 === bigint0 ? awayFromZero : bigint0
1548
+ }
1549
+ }