Skip to content

Add a first-class configurational expression system - #10

Merged
DiogoRibeiro7 merged 5 commits into
mainfrom
feat/expression-system
Aug 10, 2026
Merged

Add a first-class configurational expression system#10
DiogoRibeiro7 merged 5 commits into
mainfrom
feat/expression-system

Conversation

@DiogoRibeiro7

@DiogoRibeiro7 DiogoRibeiro7 commented Aug 10, 2026

Copy link
Copy Markdown
Owner

Replaces string handling of QCA notation with a typed tree: tokenizer, recursive-descent parser, AST, canonical form and evaluation.

Design

Built on the existing sets.py nodes rather than beside them, so Condition/Negation/Intersection/Union remain the node types and nothing is duplicated. Conjunction and Disjunction are exported as the names the configurational literature uses. New nodes: Implication and Configuration.

parse_expression("A*~B + C")     # tree
parse_expression("A*B -> Y")     # Implication
evaluate_expression("A*~B", df)  # membership
simplify_expression("A + A*B")   # A

Two things worth review

A real bug fixed. The old printer dropped grouping: (A+B)*C rendered as A+B*C, which re-parses as A+(B*C) — a different set. The printer now re-inserts parentheses by precedence, and round-tripping is property-tested.

The complement laws are deliberately not applied. In Boolean algebra A*~A is empty; for fuzzy sets it is not — at A = 0.5, min(A, 1-A) = 0.5. Simplification is restricted to laws that hold for min/max/1−x: associativity, commutativity, idempotence, double negation and absorption. A test pins A*~A and A+~A as irreducible, and a property test asserts simplification never changes membership on real data.

Safety

Parsing is structural — no eval, no exec. An expression from a config file or user input cannot execute anything; there is a test asserting that an __import__ payload is a syntax error. Errors carry the offending position.

Tests

45 new tests: tokenizer, precedence, parenthesisation, implications, malformed input, canonical form, absorption, configurations and minterm round-trips, plus two Hypothesis properties — text → tree → text → tree stays semantically identical, and simplification preserves membership.

225 tests pass at 100% coverage; mypy strict and docs build clean.


Summary by cubic

Adds a first-class configurational expression system that parses QCA notation into a typed tree with evaluation, simplification, and safe, correct printing. This replaces string handling, improves safety, and makes expressions comparable and testable.

  • New Features

    • Parse QCA expressions into a typed AST (*, +, ~, ->), reusing Condition/Negation/Intersection/Union; new nodes: Implication, Configuration (also export Conjunction/Disjunction).
    • Evaluate memberships with evaluate_expression(...); evaluate sufficiency claims via Implication.evaluate_relation(...).
    • Simplify using fuzzy-safe laws (associativity, commutativity, idempotence, double negation, absorption) with simplify_expression(...); provide canonical form and equivalent(...). Complement laws are intentionally not applied.
    • Structural parsing only; no eval. Errors raise ExpressionSyntaxError with precise positions.
    • Configuration helpers: Configuration.from_minterm(...), .to_expression(), big-endian minterm mapping.
    • Export the API from setqca (parse_expression, evaluate_expression, simplify_expression, Implication, Configuration, etc.). Added guide at docs/guide/expressions.md and extensive property tests.
  • Bug Fixes

    • Fixed printer losing parentheses: (A+B)*C no longer renders as A+B*C. Printing now respects precedence and round-trips correctly.

Written for commit 7478c9e. Summary will update on new commits.

Review in cubic

@DiogoRibeiro7
DiogoRibeiro7 merged commit dca177d into main Aug 10, 2026
15 checks passed
@DiogoRibeiro7
DiogoRibeiro7 deleted the feat/expression-system branch August 10, 2026 22:59
@codecov

codecov Bot commented Aug 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant