ExMaude.Term (ExMaude v0.3.0)

View Source

Structured representation of a Maude term.

This module provides a rich type for representing Maude terms in Elixir, including their value, sort (type), and optionally the module they came from.

Structure

A term contains:

  • :value - The string representation of the term's value
  • :sort - The Maude sort (type) of the term
  • :module - The module the term belongs to (optional)
  • :raw - The raw output from Maude (for debugging)

Examples

# Parse a reduction result
{:ok, term} = ExMaude.Term.parse("result Nat: 42")
term.value   #=> "42"
term.sort    #=> "Nat"

# Create directly
term = %ExMaude.Term{value: "true", sort: "Bool"}

Summary

Functions

Creates a new Term struct.

Parses Maude output into a Term struct.

Checks if the term has a specific sort.

Attempts to convert the term value to an Elixir boolean.

Attempts to convert the term value to an Elixir float.

Attempts to convert the term value to an Elixir integer.

Returns the term value as a string (always succeeds).

Types

t()

@type t() :: %ExMaude.Term{
  module: String.t() | nil,
  raw: String.t() | nil,
  sort: String.t(),
  value: String.t()
}

Functions

new(value, sort, opts \\ [])

@spec new(String.t(), String.t(), keyword()) :: t()

Creates a new Term struct.

Examples

iex> term = ExMaude.Term.new("42", "Nat")
...> {term.value, term.sort}
{"42", "Nat"}

parse(output, module \\ nil)

@spec parse(String.t(), String.t() | nil) :: {:ok, t()} | {:error, :no_result}

Parses Maude output into a Term struct.

Examples

iex> ExMaude.Term.parse("result Nat: 6")
{:ok, %ExMaude.Term{value: "6", sort: "Nat", module: nil, raw: "result Nat: 6"}}

iex> ExMaude.Term.parse("result Bool: true")
{:ok, %ExMaude.Term{value: "true", sort: "Bool", module: nil, raw: "result Bool: true"}}

sort?(term, expected_sort)

@spec sort?(t(), String.t()) :: boolean()

Checks if the term has a specific sort.

Examples

iex> term = ExMaude.Term.new("42", "Nat")
...> ExMaude.Term.sort?(term, "Nat")
true

iex> term = ExMaude.Term.new("42", "Nat")
...> ExMaude.Term.sort?(term, "Bool")
false

to_boolean(arg1)

@spec to_boolean(t()) :: {:ok, boolean()} | {:error, :not_boolean}

Attempts to convert the term value to an Elixir boolean.

Only works for terms with Bool sort.

Examples

iex> term = ExMaude.Term.new("true", "Bool")
...> ExMaude.Term.to_boolean(term)
{:ok, true}

to_float(arg1)

@spec to_float(t()) :: {:ok, float()} | {:error, :not_float}

Attempts to convert the term value to an Elixir float.

Only works for terms with Float sort.

Examples

iex> term = ExMaude.Term.new("3.14", "Float")
...> ExMaude.Term.to_float(term)
{:ok, 3.14}

to_integer(arg1)

@spec to_integer(t()) :: {:ok, integer()} | {:error, :not_numeric}

Attempts to convert the term value to an Elixir integer.

Only works for terms with numeric sorts (Nat, Int, NzNat, etc.).

Examples

iex> term = ExMaude.Term.new("42", "Nat")
...> ExMaude.Term.to_integer(term)
{:ok, 42}

iex> term = ExMaude.Term.new("true", "Bool")
...> ExMaude.Term.to_integer(term)
{:error, :not_numeric}

to_string(term)

@spec to_string(t()) :: String.t()

Returns the term value as a string (always succeeds).