Skip to main content

QWED-Logic DSL Reference

Quick reference for the QWED-Logic Domain Specific Language.

Overview

QWED-Logic is an S-expression based DSL for expressing logical constraints. It's verified by the Z3 SMT solver for mathematical correctness.

(AND (GT x 5) (LT y 10))

Why S-expressions? They're unambiguous, easy to parse, and map directly to Z3 constraints.


Basic Syntax

(OPERATOR arg1 arg2 ...)

All expressions are enclosed in parentheses. The operator comes first, followed by arguments.


Logical Operators

OperatorMeaningExample
ANDLogical AND(AND a b c)
ORLogical OR(OR a b)
NOTLogical NOT(NOT a)
IMPLIESImplication (a → b)(IMPLIES a b)
IFFBiconditional (a ↔ b)(IFF a b)
XORExclusive OR(XOR a b)

Examples

# Both conditions must be true
(AND (GT x 0) (LT x 100))

# At least one must be true
(OR (EQ status "active") (EQ status "pending"))

# If raining, then umbrella
(IMPLIES raining has_umbrella)

Comparison Operators

OperatorMeaningExample
EQEqual (=)(EQ x 5)
NENot equal (≠)(NE x 0)
GTGreater than(GT x 5)
LTLess than(LT x 10)
GEGreater or equal (≥)(GE x 0)
LELess or equal (≤)(LE x 100)

Examples

# x is between 0 and 100 (exclusive)
(AND (GT x 0) (LT x 100))

# x equals y
(EQ x y)

# Price is at least $10
(GE price 10)

Arithmetic Operators

OperatorMeaningExample
PLUSAddition (+)(PLUS x y)
MINUSSubtraction (-)(MINUS x y)
TIMESMultiplication (*)(TIMES x y)
DIVDivision (/)(DIV x y)
MODModulo (%)(MOD x 2)
POWPower (^)(POW x 2)
ABSAbsolute value(ABS x)

Examples

# Check if x + y = 10
(EQ (PLUS x y) 10)

# x squared equals 16
(EQ (POW x 2) 16)

# Even number check
(EQ (MOD x 2) 0)

Quantifiers

OperatorMeaningExample
FORALLFor all (∀)(FORALL (x) (GT x 0))
EXISTSThere exists (∃)(EXISTS (x) (EQ x 5))

Examples

# All elements are positive
(FORALL (x) (GT x 0))

# There exists a solution
(EXISTS (x y) (AND (EQ (PLUS x y) 10) (GT x 0) (GT y 0)))

Special Types

Boolean Literals

TRUE
FALSE

Integer Constraints

(INT x)          # x is an integer
(NAT x) # x is a natural number (≥ 0)
(POS x) ; x is positive

Array/List Operations

(LEN arr)              # Length of array
(GET arr i) # Get element at index i
(SET arr i val) # Set element at index i
(SUM arr) # Sum of all elements

Complete Examples

Example 1: Age Validation

# User must be 18-65 years old
(AND
(GE age 18)
(LE age 65)
)

Python:

result = client.verify_logic("(AND (GE age 18) (LE age 65))")
# SAT with model: {age: 30}

Example 2: Budget Constraint

# Total spending must not exceed budget
(LE
(PLUS food transport entertainment)
budget
)

Example 3: Password Rules

# Password must be 8-20 chars with at least 1 uppercase
(AND
(GE (LEN password) 8)
(LE (LEN password) 20)
(GT uppercase_count 0)
)

Example 4: Scheduling Constraint

# Meetings can't overlap
(OR
(LE meeting1_end meeting2_start)
(LE meeting2_end meeting1_start)
)

Example 5: Quadratic Equation

# x² - 5x + 6 = 0 has solutions
(EXISTS (x)
(EQ (PLUS (POW x 2) (TIMES -5 x) 6) 0)
)

Verification Results

StatusMeaningModel
SATSatisfiable — solution exists{x: 6, y: 4}
UNSATUnsatisfiable — no solutionnull
UNKNOWNSolver timeout or undecidablenull

Reading Results

result = client.verify_logic("(AND (GT x 5) (LT x 10))")

print(result.status) # "SAT"
print(result.model) # {"x": 6}
print(result.verified) # True

API Usage

Python

from qwed import QWEDClient

client = QWEDClient(api_key="qwed_your_key")

# Simple constraint
result = client.verify_logic("(GT x 5)")

# Complex constraint
result = client.verify_logic("""
(AND
(GE x 0)
(LE x 100)
(EQ (MOD x 7) 0)
)
""")

if result.status == "SAT":
print(f"Solution: x = {result.model['x']}")

CLI

# Verify a constraint
qwed verify-logic "(AND (GT x 5) (LT x 10))"

# From file
echo "(EQ (PLUS x y) 10)" > constraint.dsl
qwed verify-logic -f constraint.dsl

HTTP API

curl -X POST https://api.qwedai.com/v1/verify/logic \
-H "Authorization: Bearer qwed_your_key" \
-H "Content-Type: application/json" \
-d '{
"expression": "(AND (GT x 5) (LT y 10))",
"format": "dsl"
}'

Common Patterns

Range Check

(AND (GE x min) (LE x max))

Non-Empty String

(GT (LEN str) 0)

Mutual Exclusion

(NOT (AND a b))     # a and b can't both be true

At Most One

(LE (PLUS (IF a 1 0) (IF b 1 0) (IF c 1 0)) 1)

Exactly One

(EQ (PLUS (IF a 1 0) (IF b 1 0) (IF c 1 0)) 1)

Error Handling

ErrorCauseFix
PARSE_ERRORInvalid syntaxCheck parentheses matching
UNKNOWN_OPERATORTypo in operatorUse valid operator names
TYPE_ERRORIncompatible typesCheck operand types
TIMEOUTConstraint too complexSimplify or set longer timeout

Grammar (EBNF)

expression     = atom | compound
compound = "(" operator expression* ")"
atom = variable | number | boolean | string
operator = "AND" | "OR" | "NOT" | "IMPLIES" | "IFF" | "XOR"
| "EQ" | "NE" | "GT" | "LT" | "GE" | "LE"
| "PLUS" | "MINUS" | "TIMES" | "DIV" | "MOD" | "POW"
| "FORALL" | "EXISTS" | "LEN" | "GET" | "SET"
variable = [a-zA-Z_][a-zA-Z0-9_]*
number = [-]?[0-9]+("."[0-9]+)?
boolean = "TRUE" | "FALSE"
string = "\"" [^\"]* "\""

Full Specification

See QWED-Logic DSL Specification for the complete formal grammar.