diff --git a/lib/haskell/eval.sx b/lib/haskell/eval.sx index df6584fc..a88789b2 100644 --- a/lib/haskell/eval.sx +++ b/lib/haskell/eval.sx @@ -809,6 +809,11 @@ env "truncate" (hk-mk-builtin "truncate" (fn (x) (truncate x)) 1)) + (dict-set! env "recip" (hk-mk-builtin "recip" (fn (x) (/ 1 x)) 1)) + (dict-set! + env + "fromRational" + (hk-mk-builtin "fromRational" (fn (x) x) 1)) (let ((--sx-to-hk-- (fn (lst) (if (empty? lst) (list "[]") (list ":" (first lst) (--sx-to-hk-- (rest lst)))))) (--words-- diff --git a/lib/haskell/tests/numerics.sx b/lib/haskell/tests/numerics.sx index 1b467fc7..96874fdf 100644 --- a/lib/haskell/tests/numerics.sx +++ b/lib/haskell/tests/numerics.sx @@ -156,4 +156,13 @@ (hk-deep-force (hk-run "main = truncate (negate 3.7)")) -3) +(hk-test "recip 4.0 = 0.25" (hk-deep-force (hk-run "main = recip 4.0")) 0.25) + +(hk-test "1.0 / 4.0 = 0.25" (hk-deep-force (hk-run "main = 1.0 / 4.0")) 0.25) + +(hk-test + "fromRational 0.5 = 0.5 (identity)" + (hk-deep-force (hk-run "main = fromRational 0.5")) + 0.5) + {:fails hk-test-fails :pass hk-test-pass :fail hk-test-fail} diff --git a/plans/haskell-completeness.md b/plans/haskell-completeness.md index 3ae70ee3..0a413be3 100644 --- a/plans/haskell-completeness.md +++ b/plans/haskell-completeness.md @@ -162,7 +162,9 @@ No OCaml changes are needed. The view type is fully representable as an SX dict. tests like `show 42 = "42"` rely on this rendering. Documented in `numerics.sx`._ - [x] Math builtins: `sqrt`, `floor`, `ceiling`, `round`, `truncate` — call the corresponding SX numeric primitives. -- [ ] `Fractional` typeclass stub: `(/)`, `recip`, `fromRational`. +- [x] `Fractional` typeclass stub: `(/)`, `recip`, `fromRational`. _(/) + already a binop; `recip x = 1 / x` and `fromRational x = x` registered as + builtins in the post-prelude block._ - [ ] `Floating` typeclass stub: `pi`, `exp`, `log`, `sin`, `cos`, `(**)` (power operator, maps to SX exponentiation). - [ ] Tests in `lib/haskell/tests/numeric.sx` (≥ 15 tests: fromIntegral @@ -301,6 +303,10 @@ No OCaml changes are needed. The view type is fully representable as an SX dict. _Newest first._ +**2026-05-07** — Phase 10 Fractional stub (recip, fromRational): +- `(/)` already a binop. Added `recip` and `fromRational` as builtins + post-prelude. 3 new tests in `numerics.sx` (now 31/31). + **2026-05-07** — Phase 10 math builtins (sqrt/floor/ceiling/round/truncate): - Inserted in the post-prelude `begin` block so they override the prelude's identity stubs. `ceiling` is the only one needing a definition (SX doesn't