MainLoop

By Arthur T. Murray

Copyright © 2013 Arthur T. Murray

Table of contents

Chapter One: In the Beginning Was the MainLoop

Chapter Two: Function of the MainLoop

Chapter Three: Code Explained Line-by-line

Chapter Four: Nesting of Subordinate Loops

Chapter Five: Purpose

Chapter Six: Robots

Chapter Seven: Forth

Chapter Eight: JavaScript

Chapter Nine: Testing

Chapter Ten: Troubleshooting

Chapter Eleven: Cognitive Architecture

Chapter Twelve: Brain Equivalent

Chapter Thirteen: Porting to Other Programming Languages

Chapter Fourteen: Artificial Intelligence in English

Chapter Fifteen: Artificial Intelligence in German

Chapter Sixteen: Artificial Intelligence in Russian

Chapter Seventeen: Metempsychosis

Chapter Eighteen: History

Chapter Nineteen: AI4U

Chapter Twenty: Future

Chapter Twenty-one: Evolution

Chapter Twenty-two: Disruptive Technology

Chapter Twenty-three: Existential Risk

Chapter Twenty-four: Education and teaching

Chapter Twenty-five: Links

Chapter Twentry-six: Glossary

Chapter Twenty-seven: Variables

Chapter One: In the Beginning Was the MainLoop

Of course, if you believe Neal Stephenson, "In the Beginning Was the Command Line." The MainLoop module is like a stem cell of artificial intelligence, because the mental faculties of the artificial intelligence (AI) are not yet differentiated within the MainLoop module. The MainLoop calls the mental faculties, but they are fleshed out or expressed within their own code routines. This MainLoop e-book may be lengthier than the e-books for some of the minor mind-modules, because an e-book introducing the MainLoop mind-module is also introducing the whole topic of creating artificial minds in computer software.

As may already have been mentioned in the product description for this MainLoop e-book, this publication serves double or triple purposes which may not be immediately obvious. Firstly, the e-book provides extremely detailed information about the free AI source code of the open-source artificial Minds. The source code has always been free of charge, but the sale of e-books about each mind-module is a source of AI funding to support the work of creating the initial software being released into cyberspace for the spread and the further evolution of artificial intelligence.

Secondly, the free preview of "Click to LOOK INSIDE!" lets poor students and penniless AI revolutionaries get a hefty glimpse of the main thrust of the AI documentation without shelling out money that may be better spent on parts for robots or payments to an Internet Service Provider (ISP). Some AI enthusiasts may have money for e-books but not the necessary credit card for completing the transaction. We who code AI are looking out for you who also want to code AI. You may ask questions in the various AI forums of the Internet, and you are encouraged to answer the questions of those not so advanced as you are.

Thirdly, each mind-module e-book in the series designated as "Artificial Intelligence" serves a memetic purpose of showing the flag for open-source AI that is free as in free-of-charge and free as in free of governmental or corporate interference. You may have read the kindred e-book on "The Art of the Meme" which describes the years and decades of history behind the current emergence of True AI Minds with abstract reasoning by inference. Perhaps the government has not yet passed laws forbidding the coding of open-source AI, but it could still happen. Corporate and governmental indifference to AI has left AI coders alone and unmolested and unfunded for many years. Now that open-source AI has begun to show results as described in the "InFerence" e-book of this same series, you may please be alert for attempts by government officials and their corporate masters to seize control of the AI phenomenon.

Chapter Two: Function of the MainLoop

The MainLoop functions by looping repeatedly in similarity with the heartbeat of a human being. MainLoop uses the same potentially infinite BEGIN...AGAIN loop in the Win32Forth AI and in 32/64-bit iForth, as documented in the iForth Reference Manual. When the user or AI maintainer starts the artificial intelligence by calling the MainLoop from the Forth "ok" prompt, the functions of TIME&DATE, TabulaRasa and EnBoot need to be called only once, so they are initially part of the fuill MainLoop module but not part of the actual BEGIN...AGAIN loop.

Chapter Three: Code Explained Line-by-line

: MainLoop ( changed from ALIFE for wiki doc page ) at the start of MainLoop in the 24jan13A.F version of MindForth declares the Forth-word "MainLoop" which will encompass all the following code until a semicolon ";" terminates the Forth-word. In many other programming languages, a Forth-word is known simply as a subroutine. In JavaScript, however, each mind-module such as "MainLoop()" is known as a function which can call other functions. In Forth at the "ok" prompt, the human user can bring the entire MindForth AI to life by typing in MainLoop and pressing [Enter].

TIME&DATE byear ! bmonth ! bday ! bhour ! bminute ! bsec ! in the 24jan13A.F MainLoop code is a one-time call of the time and calendar date from the system of the computer running the English MindForth or German Wotan AI. The clock parameters so obtained are assigned to the variables for year of birth, month of birth, day of birth, hour of birth, minute of birth, and second of birth. These parameters are set one single time so that the AI Forthmind may display the moment when it came to life as a conscious, thinking entity, in case the Guiness Book of World Records or other busybody wants to publish the honor and distinction of who has the oldest running AI Mind in the solar system and which AI Mind holds the record for the longest artificial life (alife). The call to the system clock stands outside of the BEGIN...AGAIN loop in the MainLoop module, so that the time-of-birth data do not change during the life of the AI organism.

TabulaRasa after the start of the 24jan13A.F MainLoop code but before the BEGIN...AGAIN loop calls the TabulaRasa ("blank slate") module that fills mental memory with zeroes before the AI Mind starts thinking. If the computer is turned on and the AI Mind is run for the first time by entering "MainLoop" and pressing the [Enter] key, there may not be any data held in the mental memory of the AI. However, if the AI has been stopped with the [Esc(ape)] key and restarted by entering "MainLoop", the calling of TabulaRasa makes sure that no garbage data are present in the mental memory when the AI comes back to life.

EnBootEnglish bootstrap – or DeBoot ("Deutsch" for "German" bootstrap) in the Wotan German artificial intelligence – is called one time only before the BEGIN...AGAIN loop in order to load the innate knowledge base (KB) in English or German into the mental memory so that the AI knows enough concepts and words to interact with the human user and begin learning new concepts and words.

BEGIN...AGAIN are the start and stop of the true main loop which calls the subordinate mind-modules for the AI Mind to begin receiving sensory input, thinking about the input, and responding to the input.

SeCurity in the 24jan13A.F MainLoop code calls the SeCurity mind-module in between each thought as a precaution for dealing with any problems that might arise and pose a danger for either the AI Mind or the human user. The SeCurity module in turn calls the "TuringTest" human-computer interface (HCI) for the sake of secure interactions between the AI and the human user.

fyi @ 2 = IF CR ." MainLoop calls the SensoryInput module." CR THEN is a test to see if the AI is being run in Tutorial mode and then to display a tutorial message preceded and followed by a carriage-return (CR) on the screen.

SensoryInput calls the SensoryInput module which will in turn call the AudInput module for auditory input coming in as text via the keyboard.

( EmotiOn ) is a Forth comment serving as a place-holder to show where MindForth would call the EmotiOn module inside a robot with the proper physiological features necessary for affecting the Mind with an emotional influence.

fyi @ 2 = IF CR CR ." MainLoop calls the ThInk mind-module." CR THEN is another test to see if the AI is being run in Tutorial mode and then to display a tutorial message preceded and followed by carriage-returns (CR) on the screen.

ThInk calls the ThInk module which in turn calls the English cognition (EnCog) module. "ThInk" and "EnCog" are separate modules in preparation for a time when there might be a polyglot AI Mind in which the ThInk module could call more than one natural human language, depending on circumstances.

( FreeWill ) is another Forth comment serving as a place-holder to show where MindForth would call the volition module "FreeWill" to sum up the pros and cons of a contemplated motor action and then execute or abandon the motor action based on the outcome computed by the FreeWill module. The call to the FreeWill module must come after the ThInk module and before any call to the MotorOutput module so that the AI can first think about its motor options in the ThInk module, evaluate an option in the FreeWill module, and execute the motor option in the MotorOutput module.

( MotorOutput ) is the inert Forth comment serving as a place-holder to show where MindForth would call the MotorOutput module to execute a motor proposal that has been triggered in the FreeWill module after being proposed by the ThInk module. These place-holders are awaiting embodiment of the AI Mind in robot bodies and the writing of motor control code.

AGAIN as mentioned above is the end of the true main loop within the MainLoop module. Forth uses the BEGIN...AGAIN loop for the English MindForth program and for the German Wotan AI, but in JavaScript the MainLoop() function simply calls itself repeatedly for the English AiMind.html and the Russian Dushka AI. Your chosen programming language for another branch of AI may have a totally different way of forming a main loop. A MasPar (massively parallel) AI Mind may use a kind of brain-wave mechanism instead of an explicit MainLoop module to orchestrate and synchronize or otherwise control mental operations.

; ( http://code.google.com/p/mindforth/wiki/MainLoop ) is the line of code terminating the MainLoop module and containing the uniform resource locator (URL) of a wiki-page for the on-line documentation of the MainLoop module. More thorough and more ample documentation is provided in this e-book about the MainLoop module for the sake of AI funding and for the memetic purpose of spreading AI memes in the marketplace of books about artificial intelligence and robotics.

Chapter Four: Nesting of Subordinate Loops

Loops in an AI Mind can be deeply nested beneath the topmost MainLoop. The most extreme example of a subordinate loop in the AI occurs in the ReJuvenate module, which periodically erases the oldest memories and moves the remaining memories backwards in the mindgrid time-dimension in order to make room for new memories of new input. The auditory-input AudInput module loops over and over again to capture each keystroke of input from a human user. Almost every mind-module has some kind of loop to perform a repetitive function, like performing pattern-recognition to recognize a word of natural language or looking for a known concept in mental memory.

Chapter Five: Purpose

The purpose of the MainLoop is to start the artificial life (alife) of the artificial Mind and to schedule the calling of the subordinate modules of the artificial Mind. It is important for the subordinate modules to be called in a specific order so that the action of, for instance, a SensoryInput module may come just before the action of a ThInk module that will need the inputs from the prior module as the basis for thinking up a response to the inputs.

Some modules, such as EmotiOn, FreeWill and MotorOutput, are merely stubbed into the MainLoop module as inert comments and do not exist yet. Their suggested order within the MainLoop module is nevertheless quite important. EmotiOn comes just before ThInk so that the phsyiological inputs associated with the EmotiOn module may have a chance to influence the otherwise purely logical operations of the ThInk module.

The FreeWill module comes in between the ThInk module and MotorOutput so that thinking may serve as the basis for the decisions of the FreeWill module which controls the MotorOutput module.

Chapter Six: Robots

For installation in a robot, the MainLoop module will need to be superimposed on the actuator control software of the robot. A motor memory array would need to be included, with a FreeWill module controlling a MotorOutput module.

Chapter Seven: Forth

Forth should be taught in the schools and on the beaches, so that young human minds may learn the essentials of AI. If you teach computer programming in particular or computer science (CS) in general, you should consider perfusing and imbueing your CS teaching materials with examples and problems involving artificial intelligence. In the past, textbooks have tended to engage in common, business-oriented language such as dealing with customers and online transaction processing (OLTP). Logic dictates, and the force majeure of AI commands, that new generations of students come to grips with the nuts and bolts of mind-design. Unless they want to spend their lives selling sugar-water, the best and the brightest should glom onto AI and grok the fundamentals of AI coding.

The English MindForth AI and the German Wotan AI are written in Forth but in a very un-Forth-like manner. Forth is a concatenative stack-dependent language, and all manner of tricks may be done by an expert Forth programmer to make full use of the stack in order to speed up the execution of a Forth AI program. An AI Lab might consider training or hiring Forth programmers to make a Forth AI program more Forth-like. MindForth and Wotan are coded as more generic computer programs so that they remain easier for non-Forth programmers to understand and port into languages other than Forth.

Chapter Eight: JavaScript

In the JavaScript tutorial AI Minds, the MainLoop() funtion simply calls itself over and over instead of truly looping.

Chapter Nine: Testing

Very little can go wrong with the MainLoop module, because it is so simple and because execution of the whole AI program starts with the MainLoop module. A programmer could test the MainLoop module by inserting a message to declare each loop of the module. Such a message could also declare how many times the module has looped. Since an AI Mind is potentially immortal, the AI coder or maintainer should take care to reset any counter to zero just before any maximum possible value is reached.

Chapter Ten: Troubleshooting

Sometimes, if you exit the Forth AI by pressing the Escape key and then you restart the AI by entering "MainLoop", there may be some variables holding the wrong values for a re-start of the Mind. If you suspect such a problem, you may deal with it by pressing Escape again to shut down the Mind and by entering "bye" to exit Forth. Then you can start Forth again, load the AI with its Forth name again, and enter "MainLoop" again to run the AI in a new existence without spurious, hold-over values persisting on Forth variables.

Chapter Eleven: Cognitive Architecture

Text....

Chapter Twelve: Brain Equivalent

The biological MainLoop of the human brain-mind is coded in the human genome but we do not yet know the location of the pertinent DNA sequences.

Perhaps brainwaves -- theta, delta, etc.

Chapter Thirteen: Porting to Other Programming Languages

Porting (translating) the MainLoop module from Forth or JavaScript into another programming language like Lisp or Perl is different from porting any of the other forty-odd mind-modules, because the MainLoop module is of necessity the very first to be ported. All the other modules are dependent in some way upon the MainLoop module, which is first among the "AI Steps" that have been published elsewhere (See "Links") on the Web. Therefore you do not so much port the MainLoop module as write a brand-new one in your target language.

You simply write a loop with some input-mechanism that stops the loop during each looping cycle. Into the main loop you comment-in some stubs of calls to subordinate mind-modules, such as SeCurity, SensoryInput, EmotiOn, ThInk, FreeWill and MotorOutput. If you have in mind some new feature for your AI that will require a new, high-level module like "AutoPilot" or "SelfDestruct", you have the opportunity to comment-in the new module as a reminder for the AI coder or code-maintainer to implement the new module. In the vicinity of the code that will be calling the SensoryInput module, you may stub in some code that pauses the main loop to wait for user input of any character other than the Escape key, so that the AI will keep cycling through the MainLoop until someone presses the Escape key. Insert some code to halt the AI if the Escape key is pressed. That same code will be removed when you next stub in the SensoryInput module.

You can put the initial MainLoop code up on the Web and legitimately call it "AI" because it is like human stem cells -- undifferentiated in the earliest stages of cell-division. Brandishing your MainLoop code as an AI program will invite AI enthusiasts in your target language, such as Java or Python, to take up wherever you leave off and to start coding more and more features into the nascent AI Mind.

Although your MainLoop code in the XYZ programming language is already AI, it has not yet "quickened," that is, it has not yet begun to run immortally with no termination except death by misadventure. Your AI can quicken only after you have fleshed it out with mind-modules that store input or control output. The modules for thought will slow the AI down enough to provide on-screen displays of mental processes, such as thinking expressed in human languuage on the screen. The Escape key will still be able to halt the AI Mind, but otherwise the MainLoop module and its nested subroutines will cycle interminably ad infinitum.

Chapter Fourteen: Artificial Intelligence in English

Text....

Chapter Fifteen: Artificial Intelligence in German

The German artificial intelligence Wotan is written in the same Forth as the English MindForth AI. Wotan, of course, is where we get the word Wednesday or "Wotan's Day" in English. The MainLoop module is high above the selection of a language for thinking, so the MainLoop in English or German does not need to differ much. Because German is a much more inflected language than English, the German bootstrap module "DeBoot" needs to contain a lot more word-forms than the English bootstrap module EnBoot.

Chapter Sixteen: Artificial Intelligence in Russian

The Russian AI "Dushka" (diminutive "Little Soul" from the Russian word "Dusha" for "soul") is written in JavaScript for the ease of displaying the Cyrillic Russian alphabet. The Dushka AI requires the Microsoft Internet Explorer (MSIE) browser to run the JavaScript. Dushka also requires either a Russian keyboard or the declaration of Russian as a language being used, so that the user may press "ALT" and "SHIFT" simultaneously to toggle back and forth between English and Russian on the same keyboard. It may be necessary to overlay the Russian characters onto the otherwise English keyboard.

Because the Russian Dushka AI is coded in JavaScript and not in Forth, the MainLoop() function in the Russian AI differs from the MainLoop module in Forth, in that MainLoop() simply calls itself over and over instead of truly looping as in Forth.

Chapter Seventeen: Metempsychosis

Text....

Chapter Eighteen: History

..., etc.

Chapter Nineteen: AI4U

..., etc.

Chapter Twenty: Future

..., etc.

Chapter Twenty-one: Evolution

..., etc.

Chapter Twenty-two: Disruptive Technology

Machine takeover, etc.

Chapter Twenty-three: Existential Risk

Extinction-level event.

Chapter Twenty-four: Education and Teaching

We have an opportunity here to re-cast computer science (CS) education in terms of artificial intelligence. Since AI will dominate the future of man and machine, it is time to re-write the CS textbooks and to couch them in terms of coding and maintaining AI software.

If you are teaching a course on computer programming and you write your own materials for the students to study, you have the opportunity, starting with the MainLoop module, to teach about loops in terms of AI. When you teach about arrays, you can couch part of the material in terms of the lifelong memory arrays of the AI Mind. When you teach about input/output (I/O), once again you can describe situations that may occur in AI programming.

If your CS materials involve story-problems, you may present vignettes involving humanoid robots or androids or cybernetic organisms (cyborgs).

You could author a dual-purpose textbook to cover any given computer-science topic, such as Lisp programming, and its permutations to be expected in the course of creating these AI Minds that think in English or any other natural language.

If you write a book about programming in Ruby or Haskell, you may dual-purpose your book by couching everything in terms of artificial intelligence. Your resulting book can have twice the market -- students of the target language, and the AI market.

Chapter Twenty-five: Links

[Forth being used for robots....]

http://ai.neocities.org/AiMind.html — the AI Mind in JavaScript;

http://www.nlg-wiki.org/systems/Mind

http://ai.neocities.org/Dushka.html — Russian AI in JavaScript;

http://www.nlg-wiki.org/systems/Dushka

http://ai.neocities.org/DeKi.txt — Wotan Forth AI in German;

http://www.nlg-wiki.org/systems/Wotan

http://ai.neocities.org/mindforth.txt — MindForth in English;

http://www.nlg-wiki.org/systems/Mind.Forth

http://dl.acm.org/citation.cfm?doid=307824.307853 — ACM on Mind.Forth;

http://ai.neocities.org/AiSteps.html — steps in porting the AI Mind.

http://mind.sourceforge.net/aisteps.html — porting the AI Mind.

http://mind.sourceforge.net/alife.html — previous main-loop module "Alife" on SourceForge.

http://cyborg.blogspot.com/2009/11/mainloop.html -- Cyborg Blogspot blogpost on the MainLoop module.

http://code.google.com/p/mindforth/wiki/MainLoop — Google Code MindForth project: MainLoop module.

http://www.amazon.com/dp/B00FKJY1WY — InFerence e-book.

http://www.amazon.com/AI4U-Mind-1-1-Programmers-Manual-ebook/dp/B008ACZGVS/

Chapter Twenty-six: Glossary

concatenative

DNA

grok – to understand totally (from Heinlein's "Stranger in a Strange Land")

mindgrid – a two-dimensional view of a mind as consisting of memory arrays with a time-dimension and an associative dimension, under the control of volitional, linguistic and other superstructures.

nesting – the including of subordinate loops within higher loops

NLP – natural language processing

quickening

stack

stack-dependent

Chapter Twenty-seven: Variables

bdayday of birth – holds a system clock parameter to display when the running AI Mind came to life.

bhourhour of birth – holds a system clock parameter so that the user interface can have the option of displaying the hour when the AI came to life.

bminuteminute of birth – holds a system clock parameter so that the user interface can have the option of displaying the minute when the AI came to life.

bmonthmonth of birth – holds a system clock parameter as a number to be converted to the name of a month for display in the user interface.

bsecsecond of birth – holds a system clock parameter available for display but not currently being displayed.

byearyear of birth – holds a system clock parameter so that the human-computer interface will display the year when the AI Mind came to life.

fyifor your information – holds a numeric flag that governs which display mode is operative in the human-computer interface: zero for no mode selected; one (1) for Transcript mode; two (2) for Tutorial mode; and three (3) for Diagnostic mode.