ExDatalog.Term (ExDatalog v0.2.0)

Copy Markdown View Source

Term types used in Datalog atom arguments.

A term is one of:

  • A logic variable: {:var, name} — matches any value and binds it to name.
  • A constant: {:const, value} — a ground value (integer, string, or atom).
  • A wildcard: :wildcard — an anonymous variable that matches any value without binding.

Examples

iex> ExDatalog.Term.var("X")
{:var, "X"}

iex> ExDatalog.Term.const(:alice)
{:const, :alice}

iex> ExDatalog.Term.const(42)
{:const, 42}

iex> ExDatalog.Term.wildcard()
:wildcard

Summary

Functions

Constructs a constant term.

Returns true if the term is a constant.

Returns true if the term is a valid ExDatalog.Term.t().

Constructs a logic variable term.

Returns true if the term is a logic variable.

Returns all variable names present in a list of terms.

Constructs an anonymous wildcard term.

Returns true if the term is a wildcard.

Types

t()

@type t() :: {:var, var_name()} | {:const, value()} | :wildcard

value()

@type value() :: integer() | String.t() | atom() | list()

var_name()

@type var_name() :: String.t()

Functions

const(value)

@spec const(value()) :: {:const, value()}

Constructs a constant term.

Accepts integers, strings, and atoms. Floats are not supported — Datalog relations use discrete values. If you pass a float, const/1 raises ArgumentError.

Note that true, false, and nil are valid Elixir atoms and are accepted as constants, producing {:const, true}, {:const, false}, and {:const, nil} respectively. Use these with care — nil in particular may be confused with an absent value downstream.

Examples

iex> ExDatalog.Term.const(:alice)
{:const, :alice}

iex> ExDatalog.Term.const(42)
{:const, 42}

iex> ExDatalog.Term.const("hello")
{:const, "hello"}

const?(arg1)

@spec const?(t()) :: boolean()

Returns true if the term is a constant.

Examples

iex> ExDatalog.Term.const?({:const, :alice})
true

iex> ExDatalog.Term.const?({:var, "X"})
false

iex> ExDatalog.Term.const?(:wildcard)
false

valid?(arg1)

@spec valid?(term()) :: boolean()

Returns true if the term is a valid ExDatalog.Term.t().

Examples

iex> ExDatalog.Term.valid?({:var, "X"})
true

iex> ExDatalog.Term.valid?({:const, 42})
true

iex> ExDatalog.Term.valid?(:wildcard)
true

iex> ExDatalog.Term.valid?(:bad)
false

var(name)

@spec var(var_name()) :: {:var, var_name()}

Constructs a logic variable term.

Examples

iex> ExDatalog.Term.var("X")
{:var, "X"}

iex> ExDatalog.Term.var("ParentNode")
{:var, "ParentNode"}

var?(arg1)

@spec var?(t()) :: boolean()

Returns true if the term is a logic variable.

Examples

iex> ExDatalog.Term.var?({:var, "X"})
true

iex> ExDatalog.Term.var?({:const, :alice})
false

iex> ExDatalog.Term.var?(:wildcard)
false

variables(terms)

@spec variables([t()]) :: [var_name()]

Returns all variable names present in a list of terms.

Examples

iex> ExDatalog.Term.variables([{:var, "X"}, {:const, :alice}, {:var, "Y"}, :wildcard])
["X", "Y"]

wildcard()

@spec wildcard() :: :wildcard

Constructs an anonymous wildcard term.

A wildcard matches any value but does not bind it to a name. Wildcards may appear in rule bodies but not in rule heads.

Examples

iex> ExDatalog.Term.wildcard()
:wildcard

wildcard?(arg1)

@spec wildcard?(t()) :: boolean()

Returns true if the term is a wildcard.

Examples

iex> ExDatalog.Term.wildcard?(:wildcard)
true

iex> ExDatalog.Term.wildcard?({:var, "X"})
false