Home Knowledge Base Precondition inference

Precondition inference is the process of automatically determining the required conditions that must be true before a function executes correctly — discovering input constraints, state requirements, and assumptions that functions depend on, without requiring manual specification writing.

What Is a Precondition?

Why Infer Preconditions?

How Precondition Inference Works

Example: Precondition Inference

def divide(a, b):
    return a / b

# Inferred precondition: b != 0
# (Otherwise ZeroDivisionError)

def get_element(arr, index):
    return arr[index]

# Inferred preconditions:
# - arr != null
# - 0 <= index < len(arr)
# (Otherwise IndexError)

def withdraw(account, amount):
    if amount <= 0:
        raise ValueError("Amount must be positive")
    if account.balance < amount:
        raise InsufficientFundsError()
    account.balance -= amount

# Inferred preconditions:
# - amount > 0
# - account.balance >= amount

Static Precondition Inference

def process_user(user):
    # Code checks user.age
    if user.age < 18:
        return "Minor"
    else:
        return "Adult"

# Inferred precondition: user != null AND user.age is defined
# (Otherwise AttributeError)

Dynamic Precondition Inference

# Function:
def sqrt(x):
    return x ** 0.5

# Test inputs:
sqrt(4) → 2.0 (success)
sqrt(0) → 0.0 (success)
sqrt(-1) → complex number or error (failure)

# Inferred precondition: x >= 0

Symbolic Execution for Preconditions

def abs_value(x):
    if x < 0:
        return -x
    else:
        return x

# Symbolic execution:
# Path 1: x < 0 → return -x (requires x < 0)
# Path 2: x >= 0 → return x (requires x >= 0)
# Combined precondition: true (no restriction, works for all x)

def safe_divide(a, b):
    if b == 0:
        raise ValueError()
    return a / b

# Symbolic execution:
# Path 1: b == 0 → exception
# Path 2: b != 0 → return a/b (success)
# Precondition for success: b != 0

LLM-Based Precondition Inference

Example: LLM Inferring Preconditions

def binary_search(arr, target):
    left, right = 0, len(arr) - 1
    while left <= right:
        mid = (left + right) // 2
        if arr[mid] == target:
            return mid
        elif arr[mid] < target:
            left = mid + 1
        else:
            right = mid - 1
    return -1

# LLM-inferred preconditions:
"""
Preconditions:
  - arr is not null/None
  - arr is sorted in ascending order
  - target is comparable with elements of arr
  
Without these preconditions:
  - If arr is None: AttributeError
  - If arr is unsorted: incorrect result (not an error, but wrong answer)
  - If target is incomparable: TypeError
"""

Applications

``python def withdraw(account, amount): assert amount > 0, "Amount must be positive" assert account.balance >= amount, "Insufficient funds" # ... rest of function ``

Challenges

Evaluation

Precondition inference is a valuable program analysis technique — it automatically discovers function requirements, improving documentation, enabling verification, and helping developers use APIs correctly.

precondition inferencesoftware engineering

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.