Skip to main content

NSR-L Language Reference

NSR-L (Neuro-Symbolic Reasoning Language) is a Prolog-inspired logic programming language designed for symbolic reasoning, constraint specification, and knowledge representation in AI systems.

Table of Contents

  1. Overview
  2. Lexical Elements
  3. Terms
  4. Logical Expressions
  5. Statements
  6. Operators
  7. Built-in Predicates
  8. List Operations
  9. Arithmetic
  10. Constraints
  11. Negation
  12. Quantifiers
  13. Comments
  14. Complete Grammar
  15. Examples

Overview

NSR-L combines classical logic programming with modern features for AI guardrails:
  • First-order logic with variables, predicates, and quantifiers
  • Prolog-style rules with Head :- Body syntax
  • Classical and non-monotonic negation (~ and \+)
  • Hard and soft constraints for flexible validation
  • Arithmetic evaluation with the is operator
  • List operations for data manipulation
  • Belief revision with confidence intervals

Hello World


Lexical Elements

Atoms

Atoms are constant identifiers. They must start with a lowercase letter or be quoted.

Variables

Variables start with an uppercase letter or underscore.
Important: Variables starting with _ suppress singleton variable warnings.

Numbers

Strings


Terms

Terms are the fundamental data structures in NSR-L.

Constants

Variables

Compound Terms (Functors)

Lists

Lists use square bracket notation with | for head/tail decomposition.

Logical Expressions

Atomic Propositions

Examples:

Conjunction (AND)

Use & or , for conjunction:

Disjunction (OR)

Use | or ; for disjunction:

Implication

Negation


Statements

NSR-L programs consist of three types of statements:

Facts

Facts are ground assertions (no variables, or universally quantified).

Rules

Rules define logical implications with head and body.

Queries

Queries ask the system to prove or find solutions.

Operators

Logical Operators

Comparison Operators

Arithmetic Operators

Operator Precedence (lowest to highest)

  1. :-, ->, <->
  2. ;, |
  3. ,, &
  4. \+, not, ~
  5. =, \=, <, >, =<, >=, =:=, =\=
  6. +, -
  7. *, /, //, mod
  8. Unary -

Built-in Predicates

Unification & Comparison

Type Checking

Arithmetic Evaluation

Arithmetic Functions

Control


List Operations

NSR-L provides Prolog-standard list predicates.

member/2

Check if element is in list, or enumerate elements.

append/3

Concatenate lists or split a list.

length/2

Get or check list length.

reverse/2

Reverse a list.

nth0/3, nth1/3

Access element by index.

last/2

Get last element.

sort/2, msort/2

Sort lists.

sumlist/2

Sum numeric elements.

flatten/2

Flatten nested lists.

permutation/2

Generate or check permutations.

Arithmetic

The is Operator

Evaluates arithmetic expressions and binds result.

Arithmetic Comparisons

Examples


Constraints

NSR-L supports hard, soft, and weighted constraints for validation.

Constraint Types

Defining Constraints in Rules

Constraint API (HTTP)


Negation

NSR-L supports two forms of negation:

Classical Negation (~)

Strong negation that asserts the opposite is true.

Negation as Failure (\+ or not)

Weak negation - true if the goal cannot be proven.

Difference

Closed World Assumption

NAF follows the closed-world assumption: if something cannot be proven true, it is assumed false.

Quantifiers

NSR-L supports first-order quantifiers.

Universal Quantification (forall)

Equivalent rule form:

Existential Quantification (exists)

Can be expressed as:

Scoping


Comments

Line Comments

Multi-line Comments (block style in code)


Complete Grammar

EBNF Grammar


Examples

Example 1: Family Relationships

Example 2: Customer Service Rules

Example 3: Arithmetic and Lists

Example 4: Belief Revision and Constraints


Error Messages

NSR-L provides helpful error messages with source locations:

Common Errors


API Endpoints

Example API Call


Best Practices

1. Use Descriptive Predicate Names

2. Handle Edge Cases

3. Use Anonymous Variables

4. Document with Comments

5. Avoid Infinite Recursion


  • Temporal Agent Orchestration - Integration guide
  • Recursive Policy Learning - Self-improving rules