Ragex.Retrieval.CrossLanguage (Ragex v0.8.0)

View Source

Cross-language semantic search using MetaAST equivalence.

Enables finding semantically equivalent code constructs across different programming languages by comparing their MetaAST representations.

Examples

# Find map operations in any language
CrossLanguage.search_equivalent(:elixir, "Enum.map", [:python, :javascript])
# => [python list comprehension, javascript Array.map]

# Find all implementations of a pattern
CrossLanguage.find_all_implementations({:collection_op, :map, _, _})
# => Results from all languages with map/transform operations

Summary

Functions

Find all implementations of a MetaAST pattern across languages.

Group results by their semantic equivalence classes.

Search for semantically equivalent constructs across languages.

Suggest cross-language alternatives for a code snippet.

Functions

find_all_implementations(meta_ast_pattern, opts \\ [])

@spec find_all_implementations(
  term(),
  keyword()
) :: {:ok, [map()]} | {:error, term()}

Find all implementations of a MetaAST pattern across languages.

Searches the entire codebase for nodes matching the given MetaAST pattern, regardless of language.

Parameters

  • meta_ast_pattern - The MetaAST pattern to search for
  • opts - Search options

Options

  • :limit - Maximum results (default: 20)
  • :threshold - Semantic similarity threshold for fuzzy matching (default: 0.7)
  • :languages - Filter by languages (default: all)
  • :node_type - Filter by node type (default: all)

Examples

# Find all map/transform operations
pattern = {:collection_op, :map, :_, :_}
CrossLanguage.find_all_implementations(pattern)

group_by_equivalence(results)

@spec group_by_equivalence([map()]) :: [[map()]]

Group results by their semantic equivalence classes.

Returns groups of results that are semantically equivalent to each other.

Examples

results = [elixir_map, python_comprehension, js_map, elixir_filter, python_filter]

CrossLanguage.group_by_equivalence(results)
# => [
#   [elixir_map, python_comprehension, js_map],  # map operations
#   [elixir_filter, python_filter]               # filter operations
# ]

search_equivalent(source_language, source_construct, target_languages \\ [], opts \\ [])

@spec search_equivalent(atom(), term(), [atom()], keyword()) ::
  {:ok, map()} | {:error, term()}

Search for semantically equivalent constructs across languages.

Given a source construct in one language, finds equivalent constructs in other languages using MetaAST comparison.

Parameters

  • source_language - The source language (:elixir, :python, etc.)
  • source_construct - The construct to search for (function name, node_id, or MetaAST)
  • target_languages - List of languages to search in (default: all supported)
  • opts - Search options

Options

  • :limit - Maximum results per language (default: 5)
  • :threshold - Semantic similarity threshold (default: 0.6)
  • :include_source - Include source language results (default: false)
  • :strict_equivalence - Require exact AST match (default: false)

Returns

{:ok, results} where results is a map: %{language => [result]}

Examples

# Find Python equivalents of Elixir Enum.map
CrossLanguage.search_equivalent(:elixir, {:Enum, :map, 2}, [:python, :javascript])
{:ok, %{
  python: [%{node_id: "list_comprehension", score: 0.95, ...}],
  javascript: [%{node_id: "Array.map", score: 0.92, ...}]
}}

suggest_alternatives(source, target_languages, opts \\ [])

@spec suggest_alternatives(map(), [atom()], keyword()) ::
  {:ok, [map()]} | {:error, term()}

Suggest cross-language alternatives for a code snippet.

Given a code construct, suggests equivalent implementations in other languages.

Examples

source = %{
  language: :python,
  code: "[x * 2 for x in items]",
  meta_ast: {:collection_op, :map, ...}
}

CrossLanguage.suggest_alternatives(source, [:elixir, :javascript])
# => [
#   %{language: :elixir, suggestion: "Enum.map(items, &(&1 * 2))", ...},
#   %{language: :javascript, suggestion: "items.map(x => x * 2)", ...}
# ]