Swiftpack.co - maxeeem/NARS-Swift as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by maxeeem.
maxeeem/NARS-Swift 0.1.0
Swift implementation of Pei Wang's Non-Axiomatic Logic
⭐️ 0
πŸ•“ 13 weeks ago
.package(url: "https://github.com/maxeeem/NARS-Swift.git", from: "0.1.0")

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.


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 Swiftlang.xyz 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.

Command-line tool

You can build nar with

swift build

Compiled binary is located in the /.build folder.

Swift Package Manager

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

.package(url: "https://github.com/maxeeem/NARS-Swift", 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.




Stars: 0
Last commit: 1 week ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics