Abstract. A singer sings a song. But a record player can not sing. The record it plays captures only the output from the singer’s ability to sing, not the ability itself. Likewise, a program does not capture its programmer’s ability to program. If an AGI system is expected to possess the ability to write that program, then that program can not be the AGI system. If the AGI system is not expected to possess that ability, then it would not be an AGI system. Proposed in this paper is a self-programming AGI system where programs result as a side effect of an unrelated dynamical process. The system is supported by a new mathematical theory, where a partially ordered set is used as a universal knowledge base, and a functional is defined over the knowledge base. When the dynamical process minimizes the functional, the knowledge becomes organized and structures emerge. The structures are, in turn, a partially ordered set, and have their own functional. Iteration results in a nested inheritance hierarchy like the ones used in object-oriented programs. This hierarchy is the resulting program. There is evidence suggesting that the brain works in the same way. The dynamics is easy to simulate on any computer.