Getting started
Written by Pavol Durisek- Details
-
Category: Software
-
Published: 18 September 2013
-
Hits: 9181
Narsese grammar
This is the Narsese syntax and grammar used by the nars.dll library. It was adapted from Wang’s NAL Specification documentation.
⟨sentence⟩ ::= ⟨judgment⟩ | ⟨goal⟩ | ⟨question⟩ ⟨judgment⟩ ::= [⟨tense⟩] ⟨statement⟩. ⟨truth-value⟩ ⟨goal⟩ ::= ⟨statement⟩! ⟨desire-value⟩ ⟨question⟩ ::= (⟨term⟩ ⟨copula⟩ ⟨term⟩) | ⟨term⟩ | (¬ ⟨statement⟩) | (∧ ⟨statement⟩ ⟨statement⟩⁺) | (∨ ⟨statement⟩ ⟨statement⟩⁺) | (∧/ ⟨statement⟩ ⟨statement⟩⁺) | (∧| ⟨statement⟩ ⟨statement⟩⁺) | (⇑ ⟨word⟩ ⟨term⟩*) ⟨copula⟩ ::= → | ↔ | ∘→ | →∘ | ∘→∘ | ⇒ | ⇔ | /⇒ | \⇒ | |⇒ | /⇔ | |⇔ ⟨tense⟩ ::= /⇒ | \⇒ | |⇒ ⟨term⟩ ::= ⟨word⟩ | ⟨variable⟩ | ⟨statement⟩ | {⟨term⟩⁺} | [⟨term⟩⁺] | (∩ ⟨term⟩ ⟨term⟩⁺) | (∪ ⟨term⟩ ⟨term⟩⁺) | (- ⟨term⟩ ⟨term⟩) | (⊖ ⟨term⟩ ⟨term⟩) | (× ⟨term⟩ ⟨term⟩⁺) | (⊥ ⟨term⟩* ◊ ⟨term⟩*) | (⊤ ⟨term⟩* ◊ ⟨term⟩*) ⟨variable⟩ ::= ⟨independent-variable⟩ | ⟨dependent-variable⟩ | ⟨query-variable⟩ ⟨independent-variable⟩ ::= #⟨word⟩ ⟨dependent-variable⟩ ::= #[⟨word⟩(⟨independent-variable⟩*)] ⟨query-variable⟩ ::= ?[⟨word⟩] ⟨truth-value⟩ ::= <⟨real-number⟩, ⟨real-number⟩> ⟨desire-value⟩ ::= <⟨real-number⟩, ⟨real-number⟩> ⟨word⟩ ::= [_a-zA-Z0-9ɐ-ʯæ₀-₉]⁺
Special symbols of Narsese are not replaced by a combination of ASCII character but rather are encoded with UTF-8. Narsese code can be edited by any editor supporting UTF-8 encoding. NarsDevelop, provided on this website, is a simple GUI used togehter with the NARS library, which contains a simple editor of Narsese code with possibility to directly input special symbols used in Narsese by a keyboard shortcut. It has also syntaxhilighting and some debuging capability of Narsese code.
In case, compounds contain exactly two terms, operators ∧, ∨, ∩, ∪, -, ⊖, × can use both - prefix and infix form (e.g.: (∧, S1, S2) and (S1 ∧ s2) are equivalent).
Control and debug commands
The following is an extension of the grammar, used just during development and debugging and it is not part of the Narsese language.
space (ignored) ::= /\s\n\r\t+/ multiline-comment (ignored) ::= '/*' .. '*/' singleline-comment (ignored) ::= /\#[^\n\r]*/ ⟨line⟩ ::= ⟨sentence⟩ | ⟨control⟩ ⟨control⟩ ::= ⋙ ⟨commands⟩ ⟨commands⟩ ::= clear | sync | test ⟨sentence⟩ | words | compounds | statements | memory | question | inference | debug on | debug off | version
For performance reason, the answer from the system for these commands takes a mixture of text and binary form.
nars library uses C++ style single and multi-line comments.
Everything followed by a special symbol ⋙
are processed as a command.
- clear – clears system memory. Useful, if we want to test some test cases or examples, otherwise, previous experience of the system might affect results.
- sync – waits till all inference threads are in waiting state. This is useful only for the test command (which implicitly uses it). All tasks run concurrently, and might happen, that incidentally a hint would be given before conclusion is found. It might be.
- test ⟨sentence⟩ - test the last outcome from the system against the expected one (sentence) in terms of syntax and truth value.
- debug on | debug off – with debug on, we get some additional information about the inference process. Some commands requires to be debug set on.
- words – gets words from the memory.
Output takes a mix of text and binary form:
Length [Bytes] 3 4 termSize1 n 1 ... Content "<w>" termSize termId02 word03 '\0'4 ... 1termSize is a size of termId - internal representation of a term. termSize currently is represented by 8 bytes (64 bits).
2A binary representation (identifier) of a term.
3A text (string) representation of a term.
4Null termination of a string. - compounds – gets compound terms from the memory.
Length [Bytes] 3 4 termSize n 1 ... Content "<c>" termSize termId0 compound0 '\0' ... - statements – gets statements from the memory.
Length [Bytes] 3 4 termSize n 1 ... Content "<s>" termSize termId0 statement0 '\0' ... - memory – print system memory in pure Narsese. Similar to 'statements', but every type of the same statement is printed separately. The output from this command can be used as input to the system for memory reloading.
Length [Bytes] 3 n 1 ... Content "<m>" statement0 '\n' ... - question – information about question derivation of the last question entering to the system. To able to use this command, debug must be on. If the question can’t be solved by any inference process, sub questions are generated during inference process which leads to the original question. This process is done recursively, until at least one question can be solved. Then tracking back we get the solution for the original question.
Header Length [Bytes] 3 4 4 4 4 Content "<q>" termSize numberOfEdges numberOfNodes orderSize A list of edges Length [Bytes] termSize termSize ... Content node0 node1 ... A list of nodes Length [Bytes] termSize termSize 1 ... Content termId statement '\0' ... Processing sequence (order of processing sub questions) Length [Bytes] termSize termSize ... Content termId attribute ... NarsDevelop transforms these information into an SVG graph wich can be saved into a computer storage.
- inference – Shows inference processes leading from a question to the final conclusion.
Header Length [Bytes] 3 4 4 4 Content "<n>" statementsOffset statementsNum opcodeNum Inference Length [Bytes] 4 termSize termSize ... 4 ... Content premiseNum premise0 premise1 ... opcode ... Statements Length [Bytes] termSize n 1 ... Content termId statement0 '\0' ... Operation Code Length [Bytes] 4 n0 1 n1 1 ... Content OpCode description0 '\0' description1 '\0' ... - version – prints out the version of the nars library in form of a string.
Length [Bytes] 3 n 1 Content <v> library version '\0' - Other outputs:
- Output from internal questions
Length [Bytes] 3 n 1 Content "<i>" Narsese statement '\0' - Time and elapsed cycles for the last external question.
Length [Bytes] 3 4 4 Content "<t>" Elapsed time [μs] number of inference cycles - Error reporting.
Length [Bytes] 3 4 4 4 n 1 Content "<e>" row column error type1 description '\0' - 0x1 - syntax error
- 0x2 - different answer from expected one (applies, when the
test
command is used) - 0x4 - different truth value (same as above)
- 0x8 - different tense (same as above)
1Possible type of error:
- Output from internal questions
Direct input of special symbols
NarsDevelop has some special shortcuts to directly enter special symbols of Narsese using keyboard as it is shown in the following table.
Keyboard shortcut | UTF-8 output | Keyboard shortcut | UTF-8 output |
---|---|---|---|
Alt + 0 | "∘" | Ctrl + 0 | "◊" |
Alt + 1 | "→" | Ctrl + 1 | "⇒" |
Alt + 2 | "↔" | Ctrl + 2 | "⇔" |
Alt + 3 | "∩" | Ctrl + 3 | "∧" |
Alt + 4 | "∪" | Ctrl + 4 | "∨" |
Alt + 5 | "⊥" | Ctrl + 5 | "⊤" |
Alt + 6 | "¬" | ||
Alt + 7 | "⇑" | ||
Alt + 8 | "⋙" | ||
Alt + * | "×" | ||
Alt + - | "⊖" |