ExDNA.AST.AntiUnifier (ExDNA v1.5.0)

Copy Markdown View Source

Computes the anti-unification (most specific generalization) of two ASTs.

Given two AST fragments, anti-unification finds the largest common structure and replaces every position where the trees diverge with a hole — a generated variable. The result is the pattern that, when the holes are filled with the right values, reconstructs either original.

This directly produces the shape of a function that could replace both fragments: the common structure becomes the body, and the holes become parameters.

Example

iex> a = quote do: Enum.map(list, fn x -> x * 2 end)
iex> b = quote do: Enum.map(items, fn y -> y * 3 end)
iex> {pattern, holes} = ExDNA.AST.AntiUnifier.anti_unify(a, b)
iex> length(holes)
3

Each hole is %{var: atom, values: [left_ast, right_ast]}.

Summary

Functions

Compute the anti-unification of two AST fragments.

Types

hole()

@type hole() :: %{var: atom(), values: [Macro.t()]}

result()

@type result() :: {Macro.t(), [hole()]}

Functions

anti_unify(ast_a, ast_b)

@spec anti_unify(Macro.t(), Macro.t()) :: result()

Compute the anti-unification of two AST fragments.

Returns {generalized_ast, holes} where holes are the positions that differ.