woylie

woylie

I just released Spek, a boolean expression engine based on the one written for LetMe 2.0.0.

Features

  • Expression structs and builder functions for boolean logic: AllOf, AnyOf, Not, Literal, Check.
  • Evaluation of boolean expressions with optional early stopping and optional evaluation tree output.
  • Optimization of boolean expressions using boolean algebra transformation.
  • Macros for concisely defining reusable check functions.

The readme is quite comprehensive, so let me just post some code examples here.

Defining a check module

defmodule DeviceChecks do
	import Spek.Macros

	defcheck device_online(device, reason: :device_offline) do
		device.online?
	end

	defcheck battery_above_20(device, reason: :battery_too_low) do
		device.battery_level > 20
	end

	defcheck charging(device) do
		device.charging?
	end

	defcheck low_power_mode_enabled(device) do
		device.low_power_mode?
	end
end

(The use of the defcheck macro is optional.)

Composing, optimizing, and evaluating expressions

iex> import Spek
iex> battery_safe =
...>   all_of([
...>     DeviceChecks.device_online_check(),
...>     DeviceChecks.battery_above_20_check()
...>   ])
iex> charging_safe =
...>   all_of([
...>     DeviceChecks.device_online_check(),
...>     DeviceChecks.charging_check()
...>   ])
iex> rule =
...>   any_of([
...>     battery_safe,
...>     charging_safe,
...>     DeviceChecks.low_power_mode_enabled_check()
...>   ])
%Spek.AnyOf{
	children: [
		%Spek.AllOf{
			children: [
				%Spek.Check{
					module: DeviceChecks,
					fun: :device_online,
					args: [:ctx]
				},
				%Spek.Check{
					module: DeviceChecks,
					fun: :battery_above_20,
					args: [:ctx]
				}
			]
		},
		%Spek.AllOf{
			children: [
				%Spek.Check{
					module: DeviceChecks,
					fun: :device_online,
					args: [:ctx]
				},
				%Spek.Check{
					module: DeviceChecks,
					fun: :charging,
					args: [:ctx]
				}
			]
		},
		%Spek.Check{
			module: DeviceChecks,
			fun: :low_power_mode_enabled,
			args: [:ctx]
		}
	]
}
iex> rule = optimize(rule)
%Spek.AnyOf{
	children: [
		%Spek.AllOf{
			children: [
				%Spek.Check{
					module: DeviceChecks,
					fun: :device_online,
					args: [:ctx]
				},
				%Spek.AnyOf{
					children: [
						%Spek.Check{
							module: DeviceChecks,
							fun: :battery_above_20,
							args: [:ctx]
						},
						%Spek.Check{
							module: DeviceChecks,
							fun: :charging,
							args: [:ctx]
						}
					]
				}
			]
		},
		%Spek.Check{
			module: DeviceChecks,
			fun: :low_power_mode_enabled,
			args: [:ctx]
		}
	]
}
iex> device = %{
...>   online?: true,
...>   battery_level: 12,
...>   charging?: false,
...>   low_power_mode?: false
...> }
iex> Spek.eval?(rule, device)
false
iex> device = %{
...>   online?: false,
...>   battery_level: 25,
...>   charging?: false,
...>   low_power_mode?: false
...> }
iex> Spek.eval_tree(rule, device)
{
	:error,
	%Spek.EvaluationError{
		expression: %Spek.AnyOf{
			children: [
				%Spek.AllOf{
					satisfied?: false,
					children: [
						%Spek.Check{
							module: DeviceChecks,
							fun: :device_online,
							args: [:ctx],
							result: {:error, :device_offline},
							satisfied?: false
						}
					]
				},
				%Spek.Check{
					module: DeviceChecks,
					fun: :low_power_mode_enabled,
					args: [:ctx],
					result: {:error, :failed},
					satisfied?: false
				}
			],
			satisfied?: false
		},
		message: "rule evaluation failed"
	}
}

Feedback

Your feedback is appreciated, especially when it comes to API design or feature ideas.

What may come in a future release:

  • Serialization/deserialization of expressions
  • Extract errors of an evaluated expression into a list of error reasons

https://github.com/woylie/spek

Showing Posts 1 to 3

Asd

Asd

Hi, congratulations on the release :tada:. I have a question: when and what to use this library for?

woylie

woylie OP

Use cases can be:

  • Complex domain rules with composable conditions
  • Specification pattern implementations
  • Workflow, pipeline, and feature gating conditions
  • User-configurable decision systems
  • Auditable decision logs with per-check results and success/failure reasons
woylie

woylie OP

Version 0.2.0 adds various functions to collect evaluation results into flat lists.

— All posts loaded —

Where Next? Top

Trending in Announcing Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
261 13183 112
New
achempion
Hi, I would like to tell about my initiative to further maintain and develop Waffle project which is the fork of Arc library. The progre...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
charlenopires
Hi everyone :waving_hand: I’ve been working on PhiaUI, an open-source UI component library for Phoenix LiveView, and I wanted to share i...
New

Other Trending Topics Top

OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
bartblast
If you haven’t tried Hologram yet, what’s stopped you? Here’s why I’m asking. Building a framework turns out to be two jobs, the framewo...
New
mat-hek
Popcorn, the library that runs Elixir code in the browser, is now stable :popcorn: This is possible because it now builds on a very stab...
New
AstonJ
With advancements in AI gathering pace and set to increasingly accelerate, perhaps it’s worth having an annual thread on the topic - feel...
New
sntran
In my third year of college, around 2010, I found Erlang. A few years later, I found Elixir. I liked the ideas at once: many small proces...
New
gtcode
I’ve been tinkering with Lynx (by José) via hold_ledger. I used AI to write the code, a separate specification, the tests, and the Lean p...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews