defmodule Tilly.BDD.StringBoolOps do @moduledoc """ BDD operations module for sets of strings. Elements are strings, and leaf values are booleans. """ @doc """ Compares two strings. Returns `:lt`, `:eq`, or `:gt`. """ def compare_elements(elem1, elem2) when is_binary(elem1) and is_binary(elem2) do cond do elem1 < elem2 -> :lt elem1 > elem2 -> :gt true -> :eq end end @doc """ Checks if two strings are equal. """ def equal_element?(elem1, elem2) when is_binary(elem1) and is_binary(elem2) do elem1 == elem2 end @doc """ Hashes a string. """ def hash_element(elem) when is_binary(elem) do # erlang.phash2 is suitable for term hashing :erlang.phash2(elem) end @doc """ The leaf value representing an empty set of strings (false). """ def empty_leaf(), do: false @doc """ The leaf value representing the universal set of strings (true). """ def any_leaf(), do: true @doc """ Checks if a leaf value represents an empty set. """ def is_empty_leaf?(leaf_val) when is_boolean(leaf_val) do leaf_val == false end @doc """ Computes the union of two leaf values. `typing_ctx` is included for interface consistency, but not used for boolean leaves. """ def union_leaves(_typing_ctx, leaf1, leaf2) when is_boolean(leaf1) and is_boolean(leaf2) do leaf1 or leaf2 end @doc """ Computes the intersection of two leaf values. `typing_ctx` is included for interface consistency, but not used for boolean leaves. """ def intersection_leaves(_typing_ctx, leaf1, leaf2) when is_boolean(leaf1) and is_boolean(leaf2) do leaf1 and leaf2 end @doc """ Computes the negation of a leaf value. `typing_ctx` is included for interface consistency, but not used for boolean leaves. """ def negation_leaf(_typing_ctx, leaf) when is_boolean(leaf) do not leaf end # def difference_leaves(_typing_ctx, leaf1, leaf2) when is_boolean(leaf1) and is_boolean(leaf2) do # leaf1 and (not leaf2) # end @doc """ Tests a leaf value to determine if it represents an empty, full, or other set. Returns `:empty`, `:full`, or `:other`. """ def test_leaf_value(true), do: :full def test_leaf_value(false), do: :empty # def test_leaf_value(_other), do: :other end