Last active
January 24, 2026 02:03
-
-
Save patham9/fe1889971e8cd2de11e40acd97d394f0 to your computer and use it in GitHub Desktop.
PeTTa v1.0.x: Resumable computation with budget limits
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
| !(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))) | |
| (= (InjectBudgetLimitCheck) | |
| (let $L (collapse (match &self $option | |
| (if (= $option (quote (= $head $body))) | |
| (let (cons $f $rest) $head | |
| (if (not (is-member $f (quote (BudgetLimitCheck AssignBudgetLimit InjectBudgetLimitCheck CallWithBudgetLimitInternal CallWithBudgetLimit)))) | |
| (progn (remove-atom &self $option) | |
| $option)))))) | |
| (let $f (superpose $L) | |
| (if (= $f (quote (= $head $body))) | |
| (add-atom &self (= $head (progn (BudgetLimitCheck) $body))))))) | |
| (= (CallWithBudgetLimitInternal $var $predicate) | |
| (let $compute (translatePredicate (reset $predicate $signal $cont)) | |
| (if (is-var $signal) | |
| $var | |
| (progn (println! "Out of inferences. Do you want to buy another 10000? Answer (yes) or (no):") | |
| (let $response (catch (readln!)) | |
| (if (== $response (yes)) | |
| (progn (AssignBudgetLimit 10000) | |
| (CallWithBudgetLimitInternal $var $cont)) | |
| (Ok bye))))))) | |
| (: CallWithBudgetLimit (-> Expression Atom)) | |
| (= (CallWithBudgetLimit $G) | |
| (let $ext (Predicate (union-atom $G ($X))) | |
| (CallWithBudgetLimitInternal $X $ext))) | |
| ;;;;;;;;;;; EXAMPLE: | |
| ;1. Let user define MeTTa code to execute: | |
| (= (fib $N) | |
| (if (< $N 2) | |
| $N | |
| (+ (fib (- $N 1)) | |
| (fib (- $N 2))))) | |
| ;2. Inject compute budget check into user code: | |
| !(InjectBudgetLimitCheck) | |
| ;3. Assign a budget limit to the user: | |
| !(AssignBudgetLimit 10000) | |
| ;Call function while allowing the user to buy more inferences to continue computation when running out of inferences: | |
| !(CallWithBudgetLimit (fib 15)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment