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
- Overview
- Lexical Elements
- Terms
- Logical Expressions
- Statements
- Operators
- Built-in Predicates
- List Operations
- Arithmetic
- Constraints
- Negation
- Quantifiers
- Comments
- Complete Grammar
- 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 :- Bodysyntax - Classical and non-monotonic negation (
~and\+) - Hard and soft constraints for flexible validation
- Arithmetic evaluation with the
isoperator - 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._ 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
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
| Operator | Meaning | Example |
|---|---|---|
& | Conjunction (AND) | a(X) & b(X) |
, | Conjunction (AND) | a(X), b(X) |
| | Disjunction (OR) | a(X) | b(X) |
; | Disjunction (OR) | a(X) ; b(X) |
~ | Classical negation | ~alive(X) |
\+ | Negation as failure | \+ found(X) |
not | Negation as failure | not(found(X)) |
-> | Implication | human(X) -> mortal(X) |
:- | Rule implication | head :- body |
<-> | Biconditional | a <-> b |
Comparison Operators
| Operator | Meaning | Example |
|---|---|---|
= | Unification | X = foo |
\= | Not unifiable | X \= Y |
== | Identical | X == Y |
\== | Not identical | X \== Y |
< | Less than | X < 10 |
> | Greater than | X > 0 |
=< or <= | Less or equal | X =< 100 |
>= | Greater or equal | X >= 0 |
=:= | Arithmetic equal | X + 1 =:= Y |
=\= | Arithmetic not equal | X =\= 0 |
Arithmetic Operators
| Operator | Meaning | Example |
|---|---|---|
+ | Addition | X + Y |
- | Subtraction | X - Y |
* | Multiplication | X * Y |
/ | Division | X / Y |
// | Integer division | X // Y |
mod or % | Modulo | X mod Y |
Operator Precedence (lowest to highest)
:-,->,<->;,|,,&\+,not,~=,\=,<,>,=<,>=,=:=,=\=+,-*,/,//,mod- 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
| Type | Behavior | Use Case |
|---|---|---|
| Hard | Violation = rejection | Prohibited phrases, security rules |
| Soft | Violation = confidence penalty | Style guidelines, recommendations |
| Weighted | Contributes to score | Optimization objectives |
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)
Existential Quantification (exists)
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
| Error | Cause | Fix |
|---|---|---|
Singleton variable 'X' | Variable used only once | Rename to _X or use it |
Unexpected token | Syntax error | Check operators and parentheses |
Unbound variable | Variable not instantiated | Ensure variable is bound before arithmetic |
Division by zero | Arithmetic error | Add guard: Y \= 0 |
Wrong arity | Wrong number of arguments | Check predicate definition |
API Endpoints
| Endpoint | Method | Purpose |
|---|---|---|
/api/v1/nsrl/parse | POST | Parse NSR-L code (syntax check) |
/api/v1/nsrl/assert | POST | Add facts/rules to knowledge base |
/api/v1/nsrl/execute | POST | Execute queries |
/api/v1/nsrl/prove | POST | Prove entailment |
/api/v1/nsrl/retract | POST | Remove facts/rules |
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
Related Documentation
- YSE Beauty API Workflow - Practical examples
- Temporal Agent Orchestration - Integration guide
- Recursive Policy Learning - Self-improving rules