Created
September 14, 2025 18:06
-
-
Save patham9/2639f0110be8a3c1bab9c033739c0e14 to your computer and use it in GitHub Desktop.
PeTTa match-single approaching match-once code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (foo 1) | |
| (foo 2) | |
| ;Ok: Leads to unnecessary 'and' and 'empty' invocation in compiled output: | |
| (= (match-single1 $space $pat $ret) | |
| (if (and (= $x (match $space $pat $ret)) (cut)) | |
| $x (empty))) | |
| ;Bad: Leads to unneccesary maybe_call which is expensive: | |
| (= (match-single2 $space $pat $ret) | |
| (let $x (match $space $pat $ret) (car-atom ($x (cut))))) | |
| ;Good: Only 2 cheap let invocations in addition to the mandatory: | |
| (= (match-single3 $space $pat $ret) | |
| (let $x (match $space $pat $ret) (let $temp (cut) $x))) | |
| ;Bad: Leads to unneccesary maybe_call which is expensive: | |
| (= (match-single4 $space $pat $ret) | |
| (car-atom ((match $space $pat $ret) (cut)))) | |
| ;Bad: Leads to 2 unneccesary maybe_calls which is expensive and one empty: | |
| (= (match-single5 $space $pat $ret) | |
| (if (= ($x true) ((match $space $pat $ret) (cut))) | |
| $x (empty))) | |
| ;Best: Like 3 but only one let* call but with 2 items but the most elegant in both languages: | |
| (= (match-single6 $space $pat $ret) | |
| (let* (($x (match $space $pat $ret)) | |
| ($temp (cut))) | |
| $x)) | |
| ;Ok: Elegant Prolog code but ugly MeTTa code, leading to only one extra cdr-atom and car-atom in addition to the mandatory: | |
| (= (match-single7 $space $pat $ret) | |
| (car-atom (cdr-atom (justdata (match $space $pat $ret) (cut))))) | |
| ;Ok: Elegant Prolog code but ugly MeTTa code, just one extra unification in addition to the mandatory: | |
| (= (match-single8 $space $pat $ret) | |
| (case (justdata (match $space $pat $ret) (cut)) (((justdata $x true) $x)))) | |
| ;Bad: Leads to 2 unneccesary maybe_calls which is expensive and one empty: | |
| (= (match-single9 $space $pat $ret) | |
| (let ($x $temp) ((match $space $pat $ret) (cut)) | |
| $x)) | |
| !(let $x (match-single9 &self (foo $1) $1) (add-atom &self (bar $x))) | |
| !(test (collapse (match &self (bar $1) (bar $1))) | |
| ((bar 1))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment