Search documentation of Metastatic

# Metastatic.Analysis.Purity.Result (Metastatic v0.2.0)

View Source

Result structure for purity analysis.

Contains information about whether a function/code block is pure or impure, what side effects were detected, confidence level, and locations of impure operations.

## Fields

- `:pure?` - Boolean indicating if the code is pure (no side effects detected)
- `:effects` - List of detected effect atoms (`:io`, `:mutation`, `:random`, `:time`, `:network`, `:database`, `:exception`)
- `:confidence` - Confidence level (`:high`, `:medium`, `:low`)
- `:impure_locations` - List of `{:line, line_number, effect_type}` tuples
- `:summary` - Human-readable summary string
- `:unknown_calls` - List of function calls that couldn't be classified

## Examples

    # Pure function
    iex> %Metastatic.Analysis.Purity.Result{
    ...>   pure?: true,
    ...>   effects: [],
    ...>   confidence: :high,
    ...>   impure_locations: [],
    ...>   summary: "Function is pure"
    ...> }

    # Impure function with I/O
    iex> %Metastatic.Analysis.Purity.Result{
    ...>   pure?: false,
    ...>   effects: [:io],
    ...>   confidence: :high,
    ...>   impure_locations: [{:line, 42, :io}],
    ...>   summary: "Function is impure due to I/O operations"
    ...> }

    # Unknown purity (unclassified function calls)
    iex> %Metastatic.Analysis.Purity.Result{
    ...>   pure?: false,
    ...>   effects: [],
    ...>   confidence: :low,
    ...>   impure_locations: [],
    ...>   summary: "Function purity unknown - contains unclassified calls",
    ...>   unknown_calls: ["custom_function"]
    ...> }

# Summary

## Types

confidence()
effect()
location()
t()

## Functions

impure(effects, locations)

Creates a new impure result with the given effects and locations.

merge(results)

Merges multiple results, keeping impurity if any result is impure.

pure()

Creates a new pure result.

unknown(calls)

Creates a result with unknown purity (low confidence).

# Types

# confidence()

    @type confidence() :: :high | :medium | :low

# effect()

    @type effect() ::
      :io
      | :mutation
      | :random
      | :time
      | :network
      | :database
      | :exception
      | :unknown

# location()

    @type location() :: {:line, non_neg_integer(), effect()}

# t()

    @type t() :: %Metastatic.Analysis.Purity.Result{
      confidence: confidence(),
      effects: [effect()],
      impure_locations: [location()],
      pure?: boolean(),
      summary: String.t(),
      unknown_calls: [String.t()]
    }

# Functions

# impure(effects, locations)

    @spec impure([effect()], [location()]) :: t()

Creates a new impure result with the given effects and locations.

## Examples

    iex> Metastatic.Analysis.Purity.Result.impure([:io], [{:line, 10, :io}])
    %Metastatic.Analysis.Purity.Result{
      pure?: false,
      effects: [:io],
      confidence: :high,
      impure_locations: [{:line, 10, :io}],
      summary: "Function is impure due to I/O operations",
      unknown_calls: []
    }

# merge(results)

    @spec merge([t()]) :: t()

Merges multiple results, keeping impurity if any result is impure.

## Examples

    iex> r1 = Metastatic.Analysis.Purity.Result.pure()
    iex> r2 = Metastatic.Analysis.Purity.Result.impure([:io], [{:line, 10, :io}])
    iex> result = Metastatic.Analysis.Purity.Result.merge([r1, r2])
    iex> result.pure?
    false
    iex> result.effects
    [:io]

# pure()

    @spec pure() :: t()

Creates a new pure result.

## Examples

    iex> Metastatic.Analysis.Purity.Result.pure()
    %Metastatic.Analysis.Purity.Result{
      pure?: true,
      effects: [],
      confidence: :high,
      impure_locations: [],
      summary: "Function is pure",
      unknown_calls: []
    }

# unknown(calls)

    @spec unknown([String.t()]) :: t()

Creates a result with unknown purity (low confidence).

## Examples

    iex> Metastatic.Analysis.Purity.Result.unknown(["custom_func"])
    %Metastatic.Analysis.Purity.Result{
      pure?: false,
      effects: [],
      confidence: :low,
      impure_locations: [],
      summary: "Function purity unknown - contains unclassified calls: custom_func",
      unknown_calls: ["custom_func"]
    }