Skip to content

Prolog interop

Patrick Hammer edited this page Feb 19, 2026 · 2 revisions

In addtion to what lib_import provides and is shown in prologimport.metta example:

translatePredicate: directly generates a Prolog goal. This can be used to achieve things like the following, for instance allowing tabling:

(= (injectPrologCode $code)
   (progn (translatePredicate (open_string $code $S))
          (translatePredicate (load_files user ((Predicate (stream $S)))))))
!(injectPrologCode ":- table fib/2.")

(= (fib $N)
   (if (< $N 2)
       $N
       (+ (fib (- $N 1))
          (fib (- $N 2)))))

!(test (fib 30) 832040)

Or allowing predicates to be defined as string and called from MeTTa:

!(import_prolog_function split_string)
(= (plfunc $code)
   (let (cons $func $_) (split_string $code "(" "")
        (progn (translatePredicate (atom_string $atom $func))
               (import_prolog_function $atom)
               (translatePredicate (open_string $code $S))
               (translatePredicate (load_files user ((Predicate (stream $S)))))
               (repra $func))))

!(plfunc "myfunc(A,B) :- B is 3*A.")
!(myfunc 3)

Clone this wiki locally