Skip to content

Initial set of primitives #31

Description

@pjcavalcanti

Initial set of primitives

Implement the primitives described in the list below. See Notes to coordinate with other tasks.

Note on JIT (#27)

  • If common functions are kept as defs (e.g. map / fold / length / concat / split),
    a JIT could accelerate them without turning them into primitives.

Note on FFI (#28)

  • read_file / write_file might be implemented via FFI with C,
    instead of dedicated primitives.

Proposed primitives

Debug / observability (can inspect without consuming the term)

  • Rationale: inspect terms in lazy mode without altering evaluation.
  • %show(x: a) -> String
    • Stringifies without reducing and without consuming the term.
    • Use the current printing format (normalize mode / no collapse).
  • %trace(msgs: List<String>, x: a) -> a
    • Prints the concatenation of msgs and returns x without consuming it.
    • Use the current printing format (normalize mode / no collapse).

Control / error

  • Rationale: explicit failures and error messages.
  • %panic(msg: String) -> a
    • Aborts with a message; only triggers if evaluated.

IO

  • Rationale: basic input/output effects with explicit errors.
  • %log(s: String) -> #Nil (existing)
    • String = list of #Chr.
    • Kept as-is (returns []).
  • %read_file(path: String) -> #Ok{String} | #Err{String}
    • IO with explicit error; no silent failure.
  • %write_file(path: String, data: String) -> #Ok{} | #Err{String}
    • IO with explicit error.
  • %read_bytes(path: String) -> #Ok{List<#U8{NUM}>} | #Err{String}
    • Byte = #U8{n} with n stored in 8 bits.
  • %write_bytes(path: String, data: List<#U8{NUM}>) -> #Ok{} | #Err{String}
    • Byte = #U8{n} with n stored in 8 bits.

Effects / nondeterminism

  • Rationale: time and RNG utilities while keeping clear semantics.
  • %time_ms() -> NUM
    • Monotonic; effectful; result depends on when it is demanded.
  • %rand(seed: NUM, bits: NUM) -> #Pair{seed2, value}
    • Pure/deterministic PRNG; bits defines width.
    • Supported term bit sizes:
      • 7 (TAG), 24 (EXT), 32 (VAL).
    • Supported common sizes: 8 / 16 / 64.
    • bits outside this set: panic.

Common FP utilities

  • Rationale: basic library building blocks; decide whether they remain defs or become primitives.
  • Lists: map, fold, length, concat, split
  • Strings: num_to_str, str_to_num

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions