Skip to main content

ELIXIR_RUNTIME

Constant ELIXIR_RUNTIME 

Source
pub const ELIXIR_RUNTIME: &str = r#"defmodule OxiLean.Runtime do
  @moduledoc """
  OxiLean runtime support for Elixir-compiled code.

  Provides:
  - Algebraic-data-type helpers (tagged tuples)
  - Basic numeric utilities
  - Functional combinators
  """

  # ---- ADT constructors -------------------------------------------------

  @doc "Wrap a value in a tagged tuple for algebraic data types."
  def adt(tag, fields) when is_atom(tag) do
    List.to_tuple([tag | fields])
  end

  @doc "Extract the tag from an ADT tagged tuple."
  def adt_tag(t) when is_tuple(t), do: elem(t, 0)

  # ---- Numeric utilities ------------------------------------------------

  @doc "Integer power: base^exp (exp >= 0)."
  def ipow(_base, 0), do: 1
  def ipow(base, exp) when rem(exp, 2) == 0 do
    half = ipow(base, div(exp, 2))
    half * half
  end
  def ipow(base, exp), do: base * ipow(base, exp - 1)

  @doc "Clamp a value to [lo, hi]."
  def clamp(v, lo, hi), do: max(lo, min(hi, v))

  # ---- Functional combinators -------------------------------------------

  @doc "Function identity."
  def id(x), do: x

  @doc "Constant function: always returns `a`."
  def const(a, _b), do: a

  @doc "Flip argument order of a two-argument function."
  def flip(f, a, b), do: f.(b, a)

  @doc "Compose two functions: `compose(f, g).(x) == f.(g.(x))`."
  def compose(f, g), do: fn x -> f.(g.(x)) end

  # ---- Option/Maybe helpers ---------------------------------------------

  @doc "Wrap in `{:some, v}` or return `:none`."
  def some(v), do: {:some, v}
  def none, do: :none

  @doc "Unwrap an option, returning `default` on `:none`."
  def option_get({:some, v}, _default), do: v
  def option_get(:none, default), do: default

  # ---- List helpers -----------------------------------------------------

  @doc "Safe head: returns `{:some, head}` or `:none`."
  def list_head([h | _]), do: {:some, h}
  def list_head([]), do: :none

  @doc "Safe tail: returns `{:some, tail}` or `:none`."
  def list_tail([_ | t]), do: {:some, t}
  def list_tail([]), do: :none

  @doc "Zip two lists into a list of 2-tuples."
  def zip([], _), do: []
  def zip(_, []), do: []
  def zip([h1 | t1], [h2 | t2]), do: [{h1, h2} | zip(t1, t2)]
end
"#;