NARS-Swift gh

Swift implementation of Pei Wang’s Non-Axiomatic Logic.

Intelligence is the ability for a system to adapt to its environment and to work with insufficient knowledge and resources.

Assumption of Insufficient Knowledge and Resources, or AIKR, is the fundamental feature of NARS — a Non-Axiomatic Reasoning System.

Online Demo

You can try the system online in your browser. It relies on a number of alpha-stage libraries to translate Swift to JavaScript. You can see some of the capabilities in this short video.

The entire system is compiled to WebAssembly and loaded in memory. As such it has serious performance limitations and is intended for demonstration purposes only. Enjoy!


You need to install Swift for your platform. Supported platforms include macOS, Linux, and Windows. For ARM-based devices like Raspberry Pi, you can use the repo.

Once you have the Swift runtime installed, clone this repo and type

swift run

This will launch the default nar executable.

Project structure

├── Code.playground
│   └── Sources
│       ├── NAL
│       └── NARS
└── Sources
    ├── Narsese
    └── nar

Use cases

There are three primary ways to use NARS-Swift.


If you have a Mac or an iPad, the easiest way to get started is to run the included Code.playground in Swift Playgrounds app. It will allow you to experiment with the system and explore its capabilities without needing to install anything.

Make sure “Enable Results” is disabled. Performance will be very slow otherwise.

Command-line tool

You can build nar with

swift build

Compiled binary is located in the /.build folder.

You can specify a dialect at startup, for example nar --dialect opennars, to use the syntax of OpenNARS.

    --dialect [default: swift] - Narsese Dialect [canonical, ona, opennars, swift]

Swift Package Manager

For more advanced uses, add the following dependency to your Package.swift file:

.package(url: "", from: "0.1.0")

There are four modules to choose from depending on your use case.

import NAL if you want just the logic.

import NARS if you want the complete system.

import Narsese for converting strings of text into Narsese terms.

Finally, there is nar command line tool that puts all of the above modules together.


The system consists of two parts – the logic part and the control part, with the latter dependent on the logic.

“The representation language of NARS is called Narsese, which serves both the roles of internal representation and external communication for NARS.” In NARS-Swift, we embed Narsese in the programming language of the system (Swift) as a DSL or Domain Specific Language, so statements in Swift Narsese dialect are both valid Narsese and valid Swift code.


In Narsese, statements represent relations between terms, and inference rules are applied to statements when they share a common term. The simplest type of term is a word, a Copula connects two terms to form a statement, and you can use a Connector to create a compound containing two or more terms (there are certain cases where compounds consist of only one term). In addition to the types mentioned above, there are variable and operation terms.

public indirect enum Term {
    case symbol(String) /// <word>
    case statement(Term, Copula, Term)
    case compound(Connector, [Term])

public enum Copula {
    //// Primary
    case inheritance       =    "->" // NAL 1
    case similarity        =   "<–>"     // 2
    case implication       =    "=>"     // 5
    case equivalence       =   "<=>"     // 5

Several extensions to the language allow writing Narsese statements like (bird --> animal), which are simultaneously valid Swift code. Having embedded Narsese as a DSL in Swift, it is now possible to express the inference rules of NAL directly.

case .deduction:
    return [(M --> P,     S --> M, S --> P, tf),
            (P --> M,     M --> S, P --> S, tfi)]
case .induction:
    return [(M --> P,     M --> S, S --> P, tf),
            (M --> P,     M --> S, P --> S, tfi)]

During inference, several extensions transform Narsese into logic terms, and the solver produces a set of substitutions matching the rule’s pattern. Later, we reverse the process to obtain Narsese statements from logic terms. Another DSL called miniKanren helps decide which rules apply to any two statements. It is a relational programming language designed to be small and embeddable, and in NARS-Swift, we use Dimitri Racordon’s implementation.

For external communication, it is often convenient to express Narsese as a string of text. While technically not part of the core system, that functionality is highly desirable and is implemented as part of NARS+, extending the system’s capabilities. A third-party library Covfefe by Palle Klewitz translates Narsese grammar defined in Backus-Naur Form into an Abstract Syntax Tree (AST) which we then convert to Narsese data structures.

For additional details regarding the logic, see the preprint on EasyChair.