Skip to content

Instantly share code, notes, and snippets.

@patham9
Last active January 24, 2026 02:03
Show Gist options
  • Select an option

  • Save patham9/fe1889971e8cd2de11e40acd97d394f0 to your computer and use it in GitHub Desktop.

Select an option

Save patham9/fe1889971e8cd2de11e40acd97d394f0 to your computer and use it in GitHub Desktop.
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)))
(= (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