Skip to content

Instantly share code, notes, and snippets.

@patham9
Last active August 3, 2023 13:01
Show Gist options
  • Save patham9/ee21bf485a2565ebcd0450ccccc557c8 to your computer and use it in GitHub Desktop.
Save patham9/ee21bf485a2565ebcd0450ccccc557c8 to your computer and use it in GitHub Desktop.
ONA-like procedure learning for Hyperon in MeTTa (FIXED) (simplified POC to get used to MeTTa)
// Term registration whether it is an operation or not
(= (isop opleft) True)
(= (isop opright) True)
(= (isop opnop) True)
(= (isop ballleft) False)
(= (isop ballhit) False)
(= (isop ballright) False)
// induce implications from event stream
(= (induce $Memory Nil) $Memory)
(= (induce $Memory (Cons $Event Nil)) $Memory)
(= (induce $Memory (Cons $Event (Cons $Op Nil))) $Memory)
(= (induce $Memory (Cons $Event (Cons $Op (Cons $LastEvent $FIFO))))
(if (and (and (not (isop $Event)) (not (isop $LastEvent))) (isop $Op))
(induce (Cons (Sentence (Implication $LastEvent $Op $Event) (Truth 1 0)) $Memory) (Cons $LastEvent $FIFO))
(induce $Memory (Cons $LastEvent $FIFO))))
// evidence from anticipation failure
(= (reviseNeg (Sentence $Implication $Truth) Nil) $Truth)
(= (reviseNeg (Sentence $Implication $Truth) (Cons $Event Nil)) $Truth)
(= (reviseNeg (Sentence $Implication $Truth) (Cons $Event (Cons $Op Nil))) $Truth)
(= (reviseNeg (Sentence (Implication $S $A $P) (Truth $WP $WN)) (Cons $Event (Cons $Op (Cons $LastEvent $FIFO))))
(if (and (and (not (== $P $Event)) (== $S $LastEvent)) (== $A $Op))
(reviseNeg (Sentence (Implication $S $Op $P) (Truth $WP (+ 1 $WN))) (Cons $LastEvent $FIFO))
(reviseNeg (Sentence (Implication $S $Op $P) (Truth $WP $WN)) (Cons $LastEvent $FIFO))))
// revise all items when existing multiple times
(= (revisePos $SearchTerm $Truth Nil) $Truth)
(= (revisePos $SearchTerm (Truth $WP $WN) (Cons (Sentence $Term $Truth) $MemoryTail))
(if (== $SearchTerm $Term)
(revisePos $SearchTerm (Truth (+ 1 $WP) $WN) $MemoryTail)
(revisePos $SearchTerm (Truth $WP $WN) $MemoryTail)))
// Whether the term is included in the list of terms (should be a hashmap lookup ideally)
(= (included $SearchTerm Nil) False)
(= (included $SearchTerm (Cons $Term $TermListTail))
(if (== $SearchTerm $Term)
True
(included $SearchTerm $TermListTail)))
// Revise the procedureal implications
(= (revise $FIFO $AlreadyHAndledTerms Nil) Nil)
(= (revise $FIFO $AlreadyHAndledTerms (Cons (Sentence $Term $Truth) $MemoryTail))
(if (included $Term $AlreadyHAndledTerms)
(revise $FIFO (Cons $Term $AlreadyHAndledTerms) $MemoryTail)
(Cons (Sentence $Term (revisePos $Term (reviseNeg (Sentence $Term $Truth) $FIFO) $MemoryTail)) (revise $FIFO (Cons $Term $AlreadyHAndledTerms) $MemoryTail))))
// process events by doing induction + revision on the events
(= (processEvents $FIFO) (revise $FIFO Nil (induce Nil $FIFO)))
!(processEvents (Cons ballhit (Cons opleft (Cons ballleft (Cons opleft (Cons ballleft (Cons opnop (Cons ballhit (Cons opleft (Cons ballleft Nil))))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment