Add spec/stdlib.sx: 46 primitives become library functions

The irreducible primitive set drops from 79 to 33. Everything that can
be expressed in SX is now a library function in stdlib.sx, loaded after
evaluator.sx and before render.sx.

Moved to stdlib.sx (pure SX, no host dependency):
- Logic: not
- Comparison: != <= >= eq? eqv? equal?
- Predicates: nil? boolean? number? string? list? dict? continuation?
  empty? odd? even? zero? contains?
- Arithmetic: inc dec abs ceil round min max clamp
- Collections: first last rest nth cons append reverse flatten range
  chunk-every zip-pairs vals has-key? merge assoc dissoc into
- Strings: upcase downcase string-length substring string-contains?
  starts-with? ends-with? split join replace
- Text: pluralize escape assert parse-datetime

Remaining irreducible primitives (33):
  + - * / mod floor pow sqrt = < > type-of symbol-name keyword-name
  str slice index-of upper lower trim char-from-code list dict concat
  get len keys dict-set! append! random-int json-encode format-date
  parse-int format-decimal strip-tags sx-parse error apply

All hosts: JS 957+1080, Python 744, OCaml 952 — zero regressions.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-16 08:55:57 +00:00
parent 06666ac8c4
commit 4ce4762237
5 changed files with 397 additions and 24 deletions

View File

@@ -1,36 +1,38 @@
;; ==========================================================================
;; primitives.sx — Specification of all SX built-in pure functions
;; primitives.sx — Irreducible primitive set
;;
;; Each entry declares: name, parameter signature, and semantics.
;; Bootstrap compilers implement these natively per target.
;; These are the functions that CANNOT be written in SX because they
;; require host-native capabilities: native arithmetic, type inspection,
;; host string library, host math, host I/O, host data structures.
;;
;; This file is a SPECIFICATION, not executable code. The define-primitive
;; form is a declarative macro that bootstrap compilers consume to generate
;; native primitive registrations.
;; Everything else lives in spec/stdlib.sx as library functions.
;;
;; The primitive set is the out-of-band floor. The fewer primitives,
;; the tighter the strange loop and the more of the system is auditable,
;; verifiable, portable SX.
;;
;; Format:
;; (define-primitive "name"
;; :params (param1 param2 &rest rest)
;; :returns "type"
;; :doc "description"
;; :body (reference-implementation ...))
;; :doc "description")
;;
;; Typed params use (name :as type) syntax:
;; (define-primitive "+"
;; :params (&rest (args :as number))
;; :returns "number"
;; :doc "Sum all arguments.")
;; Typed params use (name :as type) syntax.
;; Modules: (define-module :name) scopes subsequent entries.
;;
;; Untyped params default to `any`. Typed params enable the gradual
;; type checker (types.sx) to catch mistyped primitive calls.
;;
;; The :body is optional — when provided, it gives a reference
;; implementation in SX that bootstrap compilers MAY use for testing
;; or as a fallback. Most targets will implement natively for performance.
;;
;; Modules: (define-module :name) scopes subsequent define-primitive
;; entries until the next define-module. Bootstrappers use this to
;; selectively include primitive groups.
;; Functions moved to stdlib.sx (no longer primitives):
;; Comparison: != <= >= eq? eqv? equal?
;; Predicates: nil? boolean? number? string? list? dict?
;; continuation? empty? odd? even? zero? contains?
;; Arithmetic: inc dec abs ceil round min max clamp
;; Collections: first last rest nth cons append reverse flatten
;; range chunk-every zip-pairs vals has-key? merge
;; assoc dissoc into
;; Strings: upcase downcase string-length substring
;; string-contains? starts-with? ends-with?
;; split join replace
;; Logic: not
;; Text: pluralize escape assert parse-datetime
;; ==========================================================================