Nasty.AST.Intent (Nasty v0.3.0)

View Source

Intent node representing the semantic intent extracted from natural language.

Intent is the bridge between natural language AST and code AST, capturing the action, target, and parameters needed for code generation.

Intent Types

  • :action - Imperative command to perform an operation (e.g., "Sort the list")
  • :query - Interrogative question requiring a boolean answer (e.g., "Is X greater than Y?")
  • :definition - Declarative statement defining a value (e.g., "X is 5")
  • :conditional - Conditional statement with condition and consequence (e.g., "If X then Y")

Examples

# Action intent: "Sort the numbers"
%Intent{
  type: :action,
  action: "sort",
  target: "numbers",
  arguments: [],
  confidence: 0.95
}

# Query intent: "Is the count greater than 10?"
%Intent{
  type: :query,
  action: "is_greater_than",
  target: "count",
  arguments: [10],
  confidence: 0.90
}

# Definition intent: "The result equals X plus Y"
%Intent{
  type: :definition,
  action: "assign",
  target: "result",
  arguments: ["+", "x", "y"],
  confidence: 0.88
}

Summary

Types

Semantic constraint for filtering or predicates.

Intent type classification.

t()

Functions

Checks if intent represents an action (imperative command).

Adds a constraint to the intent.

Returns all arguments including target if present.

Checks if intent represents a conditional statement.

Checks if intent represents a definition (declarative statement).

Checks if intent has high confidence (>= 0.8).

Checks if intent represents a query (interrogative question).

Sets the confidence score for the intent.

Types

constraint()

@type constraint() :: {atom(), term()} | {atom(), atom(), term()}

Semantic constraint for filtering or predicates.

Examples:

  • {:comparison, :greater_than, 5}
  • {:equality, "admin"}
  • {:membership, ["active", "pending"]}

intent_type()

@type intent_type() :: :action | :query | :definition | :conditional

Intent type classification.

  • :action - Imperative command (function call)
  • :query - Interrogative question (assertion/test)
  • :definition - Declarative statement (variable assignment)
  • :conditional - Conditional logic (if/case expression)

t()

@type t() :: %Nasty.AST.Intent{
  action: String.t(),
  arguments: [term()],
  confidence: float(),
  constraints: [constraint()],
  language: Nasty.AST.Node.language(),
  metadata: map(),
  span: Nasty.AST.Node.span(),
  target: String.t() | nil,
  type: intent_type()
}

Functions

action?(arg1)

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

Checks if intent represents an action (imperative command).

Examples

iex> intent = %Nasty.AST.Intent{type: :action, ...}
iex> Nasty.AST.Intent.action?(intent)
true

add_constraint(intent, constraint)

@spec add_constraint(t(), constraint()) :: t()

Adds a constraint to the intent.

Examples

iex> intent = %Nasty.AST.Intent{...}
iex> intent = Nasty.AST.Intent.add_constraint(intent, {:comparison, :greater_than, 5})
iex> intent.constraints
[{:comparison, :greater_than, 5}]

all_arguments(intent)

@spec all_arguments(t()) :: [term()]

Returns all arguments including target if present.

Examples

iex> intent = %Nasty.AST.Intent{target: "list", arguments: ["fn", "x"]}
iex> Nasty.AST.Intent.all_arguments(intent)
["list", "fn", "x"]

conditional?(arg1)

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

Checks if intent represents a conditional statement.

Examples

iex> intent = %Nasty.AST.Intent{type: :conditional, ...}
iex> Nasty.AST.Intent.conditional?(intent)
true

definition?(arg1)

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

Checks if intent represents a definition (declarative statement).

Examples

iex> intent = %Nasty.AST.Intent{type: :definition, ...}
iex> Nasty.AST.Intent.definition?(intent)
true

high_confidence?(intent)

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

Checks if intent has high confidence (>= 0.8).

Examples

iex> intent = %Nasty.AST.Intent{confidence: 0.9, ...}
iex> Nasty.AST.Intent.high_confidence?(intent)
true

new(type, action, language, span, opts \\ [])

Creates a new intent.

Examples

iex> span = Nasty.AST.Node.make_span({1, 0}, 0, {1, 15}, 15)
iex> intent = Nasty.AST.Intent.new(:action, "sort", :en, span, target: "list")
iex> intent.type
:action
iex> intent.action
"sort"
iex> intent.target
"list"

query?(arg1)

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

Checks if intent represents a query (interrogative question).

Examples

iex> intent = %Nasty.AST.Intent{type: :query, ...}
iex> Nasty.AST.Intent.query?(intent)
true

set_confidence(intent, confidence)

@spec set_confidence(t(), float()) :: t()

Sets the confidence score for the intent.

Examples

iex> intent = %Nasty.AST.Intent{...}
iex> intent = Nasty.AST.Intent.set_confidence(intent, 0.95)
iex> intent.confidence
0.95