-
Notifications
You must be signed in to change notification settings - Fork 22
Project structure
The project is organized as follows:
In the heart of PeTTa are the Parser and Translator.
Turns S-expression strings into nested Prolog lists (AST) by using S-expression grammar definition.
Simple example:
Input: "(a b (+ 1.0 2.0))"
Output: [a,b,[+,1.0,2.0]]
Complex example:
Input = "
(= (fib $N)
(if (< $N 2)
$N
(+ (reduce (fib (- $N 1)))
(reduce (fib (- $N 2))))))"
translated to:
[ =, [fib, N],
[ if,
[<, N, 2],
N,
[+, [fib, [-, N, 1]], [fib, [-, N, 2]]]
]
]
Takes the AST nested list, and recursively unrolls it into a list of Prolog goals which then become a Prolog clause.
Example:
Input: [a,b,[+,1.0,2.0]]
intermediate variables:
Head: dummy(Out)
Goals list: [Out=[a,b,A], +(1.0, 2.0, A)]
translated to clause:
Output: dummy([a,b,A]) :- +(1.0, 2.0, A).
Complex example:
Input =
[ =, [fib, N],
[ if,
[<, N, 2],
N,
[+, [fib, [-, N, 1]], [fib, [-, N, 2]]]
]
]
intermediate variables:
Head = fib(N,Out)
Goals list =
[(<(N,2,true) -> Out=N
; -(N,1,C),
fib(C,D),
-(N,2,E),
fib(E,F),
+(D,F,Out))]
translating to clause:
fib(N, Out) :-
( <(N, 2, true)
-> Out=N
; -(N, 1, C),
fib(C, D),
-(N, 2, E),
fib(E, F),
+(D, F, Out)
).
In addition to performing syntactic mappings from the MeTTa constructs to Prolog (e.g. if to ->, superpose to ;) and mapping to standard library functions where necessary, the translator also has to make a decision of when to create predicate invocations versus construction of lists. Please see https://github.com/patham9/PeTTa/wiki/Smart-dispatch to understand this crucial component.