Sys2DSL (System 2 Domain-Specific Language) is a minimal, uniform language where every statement encodes semantic relationships as Subject-Verb-Object triplets. All concepts, facts, and operations are represented as vectors.

Core Principle

"Everything is a Vector"

Atoms, operators, facts, roles, and theories are ALL represented as hypervectors. The DSL provides human-readable syntax that compiles to vector operations.

Position Binding Model

Every statement is encoded using position vectors that tag each argument:

Binding Formula:
destination = Operator BIND (Pos1 BIND arg1) BIND (Pos2 BIND arg2) BIND (Pos3 BIND arg3) BIND ...

Each argument is tagged with its position vector (Pos1, Pos2, etc.) before being bound into the result (XOR in dense-binary). This encodes argument order without using permutation.

Notation: We use the explicit word BIND for binding and avoid math symbols. When we refer to bitwise XOR explicitly, we write XOR.

Example:

@rel loves John Mary
# Encoded as: rel = loves BIND (Pos1 BIND John) BIND (Pos2 BIND Mary)
# Note: loves(John, Mary) ≠ loves(Mary, John) because Pos1 BIND John ≠ Pos1 BIND Mary

Basic Statement Syntax

Four statement forms with different storage behavior:

Form In Scope In KB Use Case
operator arg1 arg2 No Yes Simple facts (anonymous, auto-persisted)
@var operator arg1 arg2 Yes No Temporary variable (for references)
@var:name operator arg1 arg2 Yes Yes Named persistent fact
@:name operator arg1 arg2 No Yes KB only (no local reference needed)

Examples

# Anonymous fact (stored in KB only)
isA Dog Animal
loves John Mary

# Scope-only variable (can reference later)
@fact1 loves John Mary
@rule1 Implies $fact1 $conclusion

# Named persistent fact (both scope and KB)
@Person:Person __Category
@myRule:inheritance Implies (isA ?x Dog) (isA ?x Animal)

# Discard result (execute but don't store)
@_ Load $Commerce

# KB-only export (no local variable needed)
@:Pythagorean_Theorem Implies $premise $conclusion

Token Types

Token Syntax Purpose Example
@var Declaration Create and assign variable @f1 loves John Mary
@var:name Declare + KB Create variable AND add to KB as name @Person:Person __Atom
@:name KB only Add to KB without local variable @:myFact isA Dog Animal
@_ Discard Execute but don't store result @_ Load $Theory
$var Reference Access existing variable @f2 isA $John Person
?var Hole Unknown value to find @q loves ?who Mary
name Literal Atom from vocabulary John, loves
# Comment Ignored until end of line # This is a comment

Abstraction Levels

Names follow a prefix convention indicating abstraction level:

Level Prefix Purpose User Example
L0 ___ HDC primitives Runtime only ___Bind
L1 __ Structural operations Core theory __Atom, __Bundle
L2 _ Semantic primitives Knowledge engineers _ptrans, _atrans
L3+ (none) Domain concepts End users Person, loves

Comments

# Line comment (shell style)
// Line comment (C++ style)

/* Block comment
   can span multiple lines */

Theory Definition

Theories are named collections of atoms, macros, and rules. Four syntax variants supported:

Primary Syntax (Recommended)

@TheoryName theory GEOMETRY INIT_MODE
    # statements
end

Bracket Syntax

theory Name {
    # statements
}

Begin/End Syntax

theory Name begin
    # statements
end

Legacy Bracket Syntax

theory Name [
    # statements
]

Theory Parameters

Parameter Values Description
GEOMETRY 2048, 8192, 32768, etc. Vector dimension (Dense-Binary) or k value (SPHDC)
INIT_MODE deterministic, random How atoms are initialized

Complete Theory Example

@Animals theory 32768 deterministic

    # Categories
    @Animal:Animal __Category
    @Dog:Dog __Category
    @Cat:Cat __Category

    # Taxonomy
    isA Dog Animal
    isA Cat Animal

    # Properties
    @bark:bark __Relation
    @meow:meow __Relation

    # Rules (build complex rules step by step)
    @isDog isA ?x Dog
    @canBark canDo ?x bark
    @barkRule Implies $isDog $canBark

end

Graph Definition

Graphs are named procedures that process arguments and return a result:

@GraphName:exportName graph param1 param2 param3
    # Statements using parameters
    @local1 someOp $param1 $param2
    @local2 otherOp $local1 $param3

    # Return value
    return $local2
end

When a statement uses an operator that has a graph, the graph is executed first:

@tx sell Alice Bob Car 100
# 1. Execute sell graph with args (Alice, Bob, Car, 100) → graphResult
# 2. Bind: tx = sell BIND graphResult

Graph Examples

# Define a person creation graph
@DefinePerson:person graph name age occupation
    @p1 isA $name Person
    @p2 hasAge $name $age
    @p3 hasJob $name $occupation
    @result __Bundle $p1 $p2 $p3
    return $result
end

# Use the graph
@john person John 30 Engineer
@mary person Mary 25 Doctor

# Graph with holes for pattern matching
@:FindKiller graph victim
    @result kill ?murderer $victim
    return $result
end

Query Syntax

Queries use ? prefix for unknown values (holes):

# Single hole - "Who loves Mary?"
@q1 loves ?who Mary

# Multiple holes - "Who bought what from whom?"
@q2 buy ?buyer ?item ?seller

# Mixed known and unknown
@q3 sell Alice ?what Bob

Query Results

QueryResult {
  success: true,
  bindings: {
    "who": { answer: "John", similarity: 0.78, alternatives: [...] }
  },
  confidence: 0.78,
  ambiguous: false
}

FindAll and CSP Queries

In addition to similarity-based queries (best match), Sys2DSL supports enumeration queries that return ALL matches.

FindAll Pattern

# Standard query - returns best match
@q loves ?who Mary
# Result: { who: 'John' } (single best)

# FindAll - returns ALL matches
@all findAll seatedAt ?person Table1
# Result: [{ person: 'Alice' }, { person: 'Bob' }]

# Find all entities of a type
@guests findAllOfType Guest
# Result: ['Alice', 'Bob', 'Carol']

CSP Solver (Constraint Satisfaction)

For combinatorial problems requiring ALL valid solutions:

# Define problem in KB
isA Alice Guest
isA Bob Guest
isA Table1 Table
isA Table2 Table
conflictsWith Alice Bob

# Solve via API
session.solveWeddingSeating()
# Result: [
#   { Alice: 'Table1', Bob: 'Table2' },
#   { Alice: 'Table2', Bob: 'Table1' }
# ]
Method Returns Use Case
query Best match "Who loves Mary?"
findAll All matches "List everyone at Table1"
solveCSP All valid assignments "Find all seating arrangements"

See DS16 - CSP Solver for full documentation.

Built-in Operations

L0: HDC Primitives (Runtime)

Operation Description
___Bind A B XOR of two vectors
___Bundle [A B C] Majority vote of vectors
___Similarity A B Similarity score (0.0-1.0)

L1: Structural Operations (Core Theory)

Operation Description
__Atom Create new atom
__Bundle Create set/superposition
__Role name vector Tag vector with semantic role
__Category Define type category

L2: Logic Operations

Operation Description Example
Implies A B If A then B @r Implies (isA ?x Human) (isA ?x Mortal)
And A B Both A and B @c And $cond1 $cond2
Or A B Either A or B @d Or $opt1 $opt2
Not A Negation of A @n Not $fact
ForAll ?x P Universal quantification ForAll ?x (Implies (Human ?x) (Mortal ?x))
Exists ?x P Existential quantification Exists ?x (loves ?x Mary)

Session Commands

# Load a theory
@_ Load $TheoryName

# Create session-local fact
@myFact someRelation Subject Object

# Query knowledge base
@result query loves ?who Mary

# Prove a goal
@proof prove isA Socrates Mortal

Complete Example

# ============ Theory Definition ============
@Family theory 32768 deterministic

    # Types
    @Person:Person __Category
    @Relationship:Relationship __Category

    # Relation atoms
    @loves:loves __Relation
    @parent:parent __Relation
    @sibling:sibling __Relation

    # Rule: siblings share parent
    # Using reference-based composition (clearer for complex logic)
    @p1 parent ?p ?x
    @p2 parent ?p ?y
    @notSame Not (equals ?x ?y)
    @siblingCond And $p1 (And $p2 $notSame)
    @siblingConc sibling ?x ?y
    @siblingRule Implies $siblingCond $siblingConc

end

# ============ Session Usage ============

# Load theory
@_ Load $Family

# Add facts
@f1 parent Alice Charlie
@f2 parent Alice Diana
@f3 parent Bob Charlie
@f4 parent Bob Diana
@f5 loves Charlie Diana

# Query: Who are Charlie's siblings?
@q sibling Charlie ?who

# Result: ?who = Diana (confidence: 0.82)

Syntax Diagram

Statement Structure
@dest operator arg1 arg2 ... destination verb/relation arguments

Error Messages

Error Cause Solution
Undefined reference: $name Variable not in scope Define variable first or load theory
Unexpected token: X Syntax error Check statement format
Theory not found: Name Theory file missing Verify theory path
Low confidence: 0.45 Query match is weak Refine query or add facts

Loading Theories

Load a theory (namespace) using the Core Load operator:

@_ Load $Commerce
@_ Load $Animals

Rules (Implies)

Define rules as Implies statements:

@cond isA ?x Human
@concl isA ?x Mortal
@rule Implies $cond $concl

List Syntax

Lists use brackets with comma-separated items:

@items __Bundle [Cat, Dog, Bird]
@numbers __List [1, 2, 3, 4, 5]

Compound Expressions (Parenthesized)

Sys2DSL supports parenthesized expressions for nested graph calls:

# Inline nested graph call:
@causal __Role Causes (__Pair $cause $effect)

# Nested logic operators:
@rule Implies (And (isA ?x Human) (isA ?x Adult)) (canVote ?x)

Syntax: (operator arg1 arg2 ...)

Compound expressions evaluate the nested graph call first, then pass the result as an argument to the outer expression. This is useful for:

Alternative: Reference-Based Composition

For complex logic or when sub-expressions need names for debugging, you can also define parts separately:

# Reference-based (recommended for complex logic):
@cond1 hasState Door Open
@cond2 isA Door Entrance
@combined And $cond1 $cond2
@conclusion canEnter Person Door
@rule Implies $combined $conclusion

The reference-based approach is preferred when sub-expressions need inspection or when the logic is complex. Inline parentheses are best for simple structural patterns.

Formal Grammar (EBNF)

Complete grammar specification for the Sys2DSL parser:

program      = { statement | graph_def | theory_block } ;

destination  = "_" | ident [ ":" , ident ] | ":" , ident ;
statement    = "@" , destination , operation , { argument } , [ "#" , text ] , NL ;
operation    = ident | "$" , ident ;
argument     = "$" , ident | "?" , ident | ident | number | list | compound ;

	list         = "[" , argument , { [ "," ] , argument } , "]" ;
compound     = "(" , ident , { argument } , ")" ;

graph_def    = "@" , ( ident [ ":" , ident ] | ":" , ident ) , "graph" , { ident } , NL ,
               { statement } ,
               "return" , "$" , ident , NL ,
               "end" , NL ;

theory_primary = "@" , ident , "theory" , number , init_type , NL ,
                 { statement | graph_def } ,
                 "end" , NL ;

theory_brace   = "theory" , ident , "{" , NL ,
                 { statement | graph_def } ,
                 "}" , NL ;

theory_begin   = "theory" , ident , "begin" , NL ,
                 { statement | graph_def } ,
                 "end" , NL ;

theory_block   = theory_primary | theory_brace | theory_begin ;

init_type    = "random" | "deterministic" ;
ident        = ( letter | "_" ) , { letter | digit | "_" } ;
number       = digit , { digit } ;

Related Documentation