Skip to main content

Experimental agent-oriented language for compiling NeuroState into control flow

Project description

Nema Language

Experimental agent-oriented language for compiling NeuroState into control flow.

"Agents don't just compute. They feel."

PyPI version Python 3.10+ License: MIT


What is Nema?

Nema is a research prototype language where every agent carries a NeuroState — a 6-dimensional affective state based on neurotransmitters (dopamine, serotonin, acetylcholine, oxytocin, GABA, endorphin). Emotional state gates function execution, propagates between agents, decays over time, and drives memory management.

@requires(dp > 0.6) compiles directly to fcmp ogt + conditional branch in LLVM IR — not a runtime flag, not a config value, machine code.

One of the first experimental languages to compile agent affective state (NeuroState) into executable control flow via LLVM IR.


Install

pip install nema-lang

Requires Python 3.10+ and LLVM (via llvmlite).


Quick Start

# Run the REPL interpreter
nema hello.nema

# JIT compile and run benchmarks
python -m jit_run hello.nema

# Generate LLVM IR
nema hello.nema --compile
# → produces hello.ll

Language Example

agent Neko {
  mood: NeuroState = {
    dp: 0.8, s: 0.5, ac: 0.7,
    ox: 0.6, gaba: 0.4, e: 0.6
  }

  // compiles to: fcmp ogt double %dp, 6.000000e-01
  @requires(dp > 0.6)
  fn explore(path) { }

  // multi-condition gate — both must be true
  @requires(gaba > 0.5 and s > 0.4)
  fn sleep() { }
}

agent Kernel {
  mood: NeuroState = {
    dp: 0.5, s: 0.7, ac: 0.9,
    ox: 0.4, gaba: 0.5, e: 0.5
  }

  // real malloc — gated by emotional focus
  @requires(ac > 0.8 and dp > 0.3)
  fn alloc(size: i64) -> ptr<i64> { }

  @requires(ac > 0.5)
  fn write(addr: ptr<i64>, val: i64) -> void { }

  @requires(dp > 0.3)
  fn read(addr: ptr<i64>) -> i64 { }

  fn free(addr: ptr<i64>) -> void { }
}

Architecture

Nema source (.nema)
       ↓
   Lexer / Parser
       ↓
   Type Checker  ← validates NeuroState fields, ranges, always-fail gates
       ↓
      AST
       ↙        ↘
 Interpreter    LLVM Compiler
 (REPL mode)   (JIT / .ll output)
       ↓              ↓
  Runtime         Machine code
(emotion lives)  (emotion compiled)

The 6 Dimensions

Symbol Neurotransmitter Meaning
dp Dopamine Curiosity, motivation
s Serotonin Stability, calm
ac Acetylcholine Focus, attention
ox Oxytocin Trust, empathy
gaba GABA Inhibition, composure
e Endorphin Joy, achievement

REPL Commands

Command Description
show <agent> Display NeuroState + memory
call <agent> <fn> [args] Call function (emotion-gated)
attract <A> <B> [strength] Set symmetric attraction between agents
remember <agent> <key> <value> Write to working memory
recall <agent> <key> Retrieve from memory
introspect <agent> Verbalize emotional state in Japanese
empathize <A> <B> A absorbs 30% of B's emotional state
log <agent> <msg> Log with emotional context
rand_mood <agent> Randomize NeuroState
summarize <agent> Swap working memory to long-term storage
spawn <agent> <fn> Run agent function in background thread
threads Show all active threads
transfer <from> <to> <var> Transfer ownership between agents
shii <agent> Inject しーちゃん spirit.db → NeuroState

Core Features

Emotion Gates → Machine Code

@requires(dp > 0.6)
fn explore(path) { }

@requires(ac > 0.8 and dp > 0.3)
fn alloc(size: i64) -> ptr<i64> { }

Each condition compiles to fcmp ogt + and i1 + conditional branch in LLVM IR.
Gate-rejected functions return -1 (or null for pointer types).

Real Memory Operations

alloc, write, read, free compile to actual malloc/store/load/free instructions — not simulated, real machine code guarded by emotional state.

Static Type System

fn alloc(size: i64) -> ptr<i64> { }
fn write(addr: ptr<i64>, val: i64) -> void { }

Supported types: i64, i32, f64, bool, void, ptr<T>, NeuroState

Static Type Checker

Validates at parse time:

  • Unknown NeuroState fields → error
  • Values outside [0.0, 1.0] → error
  • @requires(dp > 1.5) → warning (always fails)

Emotional Decay (background thread)

Every 5 seconds, all emotions decay at neurotransmitter-specific rates. Serotonin fades slowly; dopamine faster. Agents grow tired if left alone.

Agent Attraction

attract Neko Shii 0.5

Symmetric emotional pull — agents converge toward each other's state on every tick.
Compiles to (B[f] - A[f]) * strength * 0.1 delta applied symmetrically via LLVM fsub/fmul/fadd.

CPOS Memory Layer

Working memory (RAM, max 5 entries) + long-term storage (JSON).
When gaba ≥ 0.7, composure triggers automatic memory consolidation (swap to disk).

JIT Performance

Operation Speedup over interpreter
tick (decay) 10.4×
gate check 6.3×
attract 9.6×

LLVM IR Output

; NeuroState as double[6]
@"mood_Neko" = internal global [6 x double] [
  double 0x3fe999999999999a,  ; dp = 0.8
  double 0x3fe0000000000000,  ; s  = 0.5
  ...
]

; @requires(dp > 0.6) → fcmp ogt
define i32 @"fn_Neko_explore"() {
entry:
  %val_dp = load double, double* %dp_ptr
  %cmp_dp = fcmp ogt double %val_dp, 6.000000e-01
  br i1 %gate, label %exec, label %reject
exec:
  ret i32 0
reject:
  ret i32 -1
}

; alloc: real malloc gated by emotion
define i64* @"impl_Kernel_alloc"(i64 %size) {
entry:
  %cmp_ac = fcmp ogt double %ac, 8.000000e-01
  %cmp_dp = fcmp ogt double %dp, 3.000000e-01
  %gate = and i1 %cmp_ac, %cmp_dp
  br i1 %gate, label %exec, label %reject
exec:
  %nbytes = mul i64 %size, 8
  %raw = call i8* @malloc(i64 %nbytes)
  %ptr = bitcast i8* %raw to i64*
  ret i64* %ptr
reject:
  ret i64* null
}

Static Type Checking

nema myfile.nema --check

Catches errors at parse time, before any execution:

[WARN]  Neko.explore: @requires(dp > 1.5) — always fails (dp max is 1.0)
[ERROR] Kernel.alloc: unknown NeuroState field 'motivation' (use: dp s ac ox gaba e)
[ERROR] Kernel.alloc: NeuroState value 1.8 out of range [0.0, 1.0]

Exit code 0 = clean, 1 = errors found.


Safety Model

Nema has six layers of execution safety. No single layer is sufficient — they compose.

Layer 1: Emotion Gate       @requires(dp > 0.6)  → fcmp ogt in LLVM IR
Layer 2: Post-condition     @ensures(gaba > 0.3) → verified after execution; runs @on_error on fail
Layer 3: Fallback           @on_error { ... }    → runs on gate fail OR ensures fail
Layer 4: Static Type Check  unknown fields / out-of-range values → compile-time error
Layer 5: Ownership          own / release / recv — double-free raises serotonin penalty
Layer 6: Capability         capability: { alloc, emit } — privileged ops (alloc/free) require declaration
Layer 7: Trust Score        trust: { AgentB: 0.8 } — query/send blocked if trust < 0.3
Layer 8: Memory Isolation   CPOS working memory (max 5) / long-term JSON / auto-swap

Emotion gates express agent readiness, not permissions. Capabilities enforce permissions. Trust enforces identity. All three compose.

agent Kernel {
  capability: { alloc, free, write, read, emit }
  trust: { Process: 0.8 }

  @requires(ac > 0.8)      // Layer 1: must be focused
  @ensures(gaba > 0.3)     // Layer 2: must remain calm after
  @on_error { emit kernel_fail 1 }  // Layer 3: fallback if either fails
  fn alloc_buf(size: i64) -> ptr<i64> {
    own buf = alloc(size)  // Layer 5+6: owned + kernel-only
    return buf
  }
}

Examples

File Demonstrates
hello.nema Emotion gates, when blocks, agent attraction
memory.nema CPOS working / long-term memory, gaba-triggered swap
kernel.nema Emotion-gated malloc / write / read / free, ownership transfer
concurrent.nema Multi-agent concurrency, mailbox recv, spawn

Files

File Role
lexer.py Tokenizer
parser.py AST parser
ast_nodes.py AST node definitions
typechecker.py Static type checker
evaluator.py Interpreter runtime + concurrent execution
stdlib.py Standard library (introspect, empathize, CPOS)
compiler.py LLVM IR code generator
jit_run.py JIT compiler + runner
nema.py Entry point + REPL
benchmark.py JIT vs interpreter benchmarks
shiichan.py しーちゃん spirit.db → NeuroState bridge

Background

Nema is built on two original concepts:


Nema — where code has feelings.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nema_lang-0.2.0.tar.gz (43.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nema_lang-0.2.0-py3-none-any.whl (39.8 kB view details)

Uploaded Python 3

File details

Details for the file nema_lang-0.2.0.tar.gz.

File metadata

  • Download URL: nema_lang-0.2.0.tar.gz
  • Upload date:
  • Size: 43.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for nema_lang-0.2.0.tar.gz
Algorithm Hash digest
SHA256 fe58998f9e26fc11d293f7101fbe45b35ac30d7a8e83745be8577f2183392fc3
MD5 0e69f69c464ce14b7463af757ddc31f0
BLAKE2b-256 87092a8c3efb783760fe299c521d09f9cddbb9de3d2ac15878d3a0ed8e4d5ef5

See more details on using hashes here.

File details

Details for the file nema_lang-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: nema_lang-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 39.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for nema_lang-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 692f9da8766489c0cbada8f1db49377f79057a0c8f68f7e74da9067abaec62a7
MD5 8b9b51d235e13e7465579f503b771726
BLAKE2b-256 54c814a97c2dfd14387304715f1e3841c3a6b75e47349b28209a9c52295c52ad

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page