Swift implementation of NARS

55 views
Skip to first unread message

Maxim Tarasov

unread,
Apr 19, 2023, 11:24:04 PM4/19/23
to open-nars
Hello everybody!

I wanted to share with you the project I have been working on over the last 2 years.

When I first got interested in NARS I started by experimenting with OpenNARS and ONA, trying to understand how they function. I eventually got the book by Dr. Wang and began reading it to try and get a deeper understanding. A year and a half ago at a NARS workshop in San Francisco I saw Christian Hahm present his experiment with MNIST using his Python implementation. And he probably doesn’t remember this but at one point during the presentation he asked a question: “why make another NARS implementation?” And then proceeded to answer it himself — because this is the best way to learn. It is funny how an off-hand comment like that can have an impact beyond the original intention. I decided then that to really learn about NARS, I needed to try and build it myself. 

They say the best tool is the one you know how to use, and when it comes to programming languages, Swift is the one I know best and that’s what I chose. It isn’t to say that it is better than other languages, but it does have some nice features that I’ve grown to appreciate. One of the things it allows you to do is define custom operators and in the context of Narsese it seemed natural to define copulas and compound connectors as operators to make expressing Narsese terms more natural in the code. So for example, `bird -> animal` is not only valid in Narsese but can also be a valid Swift expression at compile time. It is similar to, and was partially inspired by, the work Patrick Hammer did in ONA with his rule language. In NARS-Swift the idea is taken even further.

The other inspiration came from the original NARS implementation in Prolog. Rule based languages are a natural fit for the logic of NARS but if my Java and C skills are so-so, when it comes to logic programming, I am completely lost. Thankfully, a friend told me about miniKanren which is similar to Prolog in some ways (and different in others) but most importantly it is not designed as a standalone language, but as a DSL to be embedded in other host languages. And now that I had Narsese embedded in Swift it seemed natural to continue this nesting further and use another embedded language to do the logic processing. So similar to NARS Prolog, rule application is handled by a specialized language and the thing I had to figure out is how to translate between them.

I learned a lot while building the system, through many iterations and testing. I want to say thank you to everyone who helped answer my questions on this mailing list and helped me in my learning journey.

As it stands, the system is not complete, but I am happy with its current state. There is still work to be done with temporal reasoning as well as goals and operations. If you like to check it out, I recently added an online demo which is made possible by compiling the code to WebAssembly and running it entirely inside the browser.

It was a long time coming and I hope you find this useful in some way, I know I did! Let me know your thoughts and comments and have a great day!

Maxim


Pei Wang

unread,
Apr 20, 2023, 9:13:56 AM4/20/23
to open...@googlegroups.com
Hi Maxim,

Congratulations on the achievement!

Regards,

Pei


--
You received this message because you are subscribed to the Google Groups "open-nars" group.
To unsubscribe from this group and stop receiving emails from it, send an email to open-nars+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/open-nars/2af0b08d-ddfe-4629-a140-4fe2209819ebn%40googlegroups.com.

Christian Hahm

unread,
Apr 21, 2023, 12:50:59 AM4/21/23
to open-nars
Dear Maxim,

That is really fantastic! I look forward to seeing this system used in future demos and research, especially if we can compare its performance to the other NARS systems, such as in an application like Pong. Your current GUI demo is already quite sleek, especially how it can run in a browser, and the emojis are a really nice touch for understandability :D. I do have a few questions:
  • Can you explain a bit more about the Narsese Swift expressions?
  • Can the system currently derive goals (e.g., so as to play Pong)? If so, how does it select goals for inference? Also what is the punctuation to query desire-value (if it is implemented yet)?
  • And what are your future plans for experimenting with the system?

Regards,
Christian

Maxim Tarasov

unread,
Apr 21, 2023, 12:56:15 PM4/21/23
to open-nars
Hi Christian,

Thank you for your kind feedback!

Regarding your comments:

> I look forward to seeing this system used in future demos and research, especially if we can compare its performance to the other NARS systems, such as in an application like Pong. 
Yes, absolutely! This is also why I imported many test cases from OpenNARS to use them as a guide during development. In one of the conversations we had with Patrick he mentioned how it would be great to have a common test suite to compare different NARS implementations.
Pong is on the horizon, once goal derivation and temporal reasoning are a bit more stable. I was initially thinking of using the GUI version you built and may still do it locally just to test, but ultimately I think it would be best to leverage something like AI Gym and once pong works, try the system on other tasks like grid world.
  • Can you explain a bit more about the Narsese Swift expressions? 
It really is quite close to OpenNARS syntax. As a base line, what I call Canonical, I used the syntax as described in the book, with the special unicode characters like  ⋂ and ¬. That's what you see in the output. For input this would be too cumbersome to type so I did the similar thing to OpenNARS and used simpler notation for connectors, and for copulas I went closer to the book so Swift version doesn't use double arrows like --> and ==> while the OpenNARS version does. There are a few other small differences along the same lines, the main purpose is to keep it as close to the book as possible while maintaining readability.
  • Can the system currently derive goals (e.g., so as to play Pong)? If so, how does it select goals for inference? Also what is the punctuation to query desire-value (if it is implemented yet)?
Yes and no :) Goal derivation is in its infancy at the moment. In general, the approach I have taken when developing the system has been to write the simplest implementation first and add complexity later. To be honest, I'm surprised that it works as well as it does considering some of the naive code in there, like this one for calculating priority -- clearly this needs to be handled better.
As for desire-value, this is not implemented yet, one of the many to-dos in the code :)
  • And what are your future plans for experimenting with the system?
Finishing temporal inference and goal derivation. Testing it on common examples like pong and grid world etc. Experimenting with natural language understanding. Building an app to overcome some of the limitations of WebAssembly. I also have some ideas about externalizing the storage and parallelizing inference across multiple nodes. So you know, Napoleonic plans :) and of course to have fun along the way!


I'll also mention that any help is appreciated, I am always open to collaboration in terms of working on code or running experiments!

Happy Friday!
Maxim
Reply all
Reply to author
Forward
0 new messages