Skip to content

Instantly share code, notes, and snippets.

@patham9
patham9 / gist:fe1889971e8cd2de11e40acd97d394f0
Last active January 24, 2026 02:03
PeTTa v1.0.x: Resumable computation with budget limits
!(import_prolog_function statistics)
(= (BudgetLimitCheck)
(if (> (- (statistics inferences) (get-state &initinferences)) (get-state &inferenceslimit))
(translatePredicate (shift (pay-for-inferences)))
True))
(= (AssignBudgetLimit $N)
(progn (change-state! &initinferences (statistics inferences))
(change-state! &inferenceslimit $N)))
@patham9
patham9 / gist:54af0acff57a1d0be82d339c5e818b47
Last active January 23, 2026 20:21
PeTTa from Python (after "pip install sexpdata")
from sexpdata import loads
from petta import *
metta = PeTTa()
expstring = metta.process_metta_string("!(hello \"world\" (1.0 1))")[0]
print(loads(expstring))
#Output:
#[Symbol('hello'), 'world', [1.0, 1]]
@patham9
patham9 / gist:94aff6fe0467d1ff236f451cceb0702c
Last active January 8, 2026 23:27
PeTTa v1.0 (cat *.pl in src folder)
:- use_module(library(readutil)). % read_file_to_string/3
:- use_module(library(pcre)). % re_replace/4
:- current_prolog_flag(argv, Args), ( (memberchk(silent, Args) ; memberchk('--silent', Args) ; memberchk('-s', Args))
-> assertz(silent(true)) ; assertz(silent(false)) ).
%Read Filename into string S and process it (S holds MeTTa code):
load_metta_file(Filename, Results) :- load_metta_file(Filename, Results, '&self').
load_metta_file(Filename, Results, Space) :- read_file_to_string(Filename, S, []),
process_metta_string(S, Results, Space).
git clone https://github.com/jazzbox35/MettaWamJam
cd MettaWamJam
docker build -t mettawamjam:latest .
docker run --rm -d --name mwj -p 127.0.0.1:5000:5000 mettawamjam:latest
then run:
python3 RunMeTTaCode.py
python3 RunMutex_and_Transaction.py
python3 RunMM2Code.py
@patham9
patham9 / syntaxhighlightme.c
Last active November 27, 2025 21:30
NARS-GPT: Whale disease example
//Input: Whales are mammals that live in the ocean.
<(whale * ocean) --> live_in>. :|: {1.0 0.9}
//Input: Animals that live in the ocean are marine animals.
<(animal * ocean) --> live_in>. :|: {1.0 0.9}
<animal --> marine_animal>. :|: {1.0 0.9}
//Input: Marine mammals are exposed to pollutant P.
<(marine_animal * pollutant_p) --> are_exposed_to>. :|: {1.0 0.9}
@patham9
patham9 / gist:58b63d02717b034822f0bb92cced97fd
Created October 3, 2025 08:23
Behavior of once(concurrent_and( vs. first_solution(
%Finding divisors
'find-divisor'(A, B, C) :-
*(B, B, D),
>(D, A, E),
( E==true
-> C=A
; C=F,
'%'(A, B, G),
==(0, G, H),
( H==true
@patham9
patham9 / gist:2639f0110be8a3c1bab9c033739c0e14
Created September 14, 2025 18:06
PeTTa match-single approaching match-once code
(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:
@patham9
patham9 / gist:d9cb14eca9e3adfca97b29340e0c723d
Created September 11, 2025 22:01
Exponential pattern generator
((step app)
(, (num $1) )
(, (num (M $1))
(num (W $1)) ))
((step app)
(, (num (M $1)),
(num (W $1)) )
(, (num (C $1)) ))
@patham9
patham9 / gist:589fd717bf9ee0ae4e0ff6ec73bb1da6
Created August 17, 2025 12:34
MM2/MORK: getting started
Needed:
pip3 install requests-sse
client.py:
Replace /target/debug/mork_server with /target/release/mork_server
@patham9
patham9 / gist:56ccaf963266406c33d27826ee6a6f4b
Created July 31, 2025 22:07
PLN deriver efficiency: use derivations only for inference in each recursion.
;; Exhaustive-until-depth deriver
(= (Derive $derivationsLast $beliefs $depth $maxdepth)
(if (> $depth $maxdepth)
$beliefs
(let $derivations
(collapse (superpose ((let* (((Sentence $x $Ev1) (superpose $derivationsLast))
((Sentence $y $Ev2) (superpose $beliefs))
($stamp (TupleConcat $Ev1 $Ev2)))
(if (StampDisjoint $Ev1 $Ev2)
(case (|- $x $y) ((($T $TV) (Sentence ($T $TV) $stamp)))) (empty)))