diff --git a/prolog/metta_lang/stdlib_mettalog.metta b/prolog/metta_lang/stdlib_mettalog.metta index 2db4c8ee37..7521e80430 100644 --- a/prolog/metta_lang/stdlib_mettalog.metta +++ b/prolog/metta_lang/stdlib_mettalog.metta @@ -107,17 +107,17 @@ For example: ; Enable the built-in function `size-atom` that takes an atom and returns the size as a predicate with arity 2 - (add-atom &dyn-space (predicate-arity size-atom 2)) + (predicate-arity size-atom 2) ; Now `size-atom` can be used as a predicate in pattern matching - (match &dyn-space '(size-atom (a b c) $size) + !(match &dyn-space '(size-atom (a b c) $size) (The abc tuple was len $size)) ; This pattern will resolve `Size = 3` and execute the action. Additionally, by running `size-atom` in reverse, you can compute a new atom based on a desired size: - (match &dyn-space '(size-atom $new-atom 4) + !(match &dyn-space '(size-atom $new-atom 4) (The new atom is $new-atom)) ; This resolves `$new-atom` to a tuple of size 4, such as ($1 $2 $3 $4). @@ -141,33 +141,34 @@ This reverse functionality is made possible because predicates can describe rela For example: ; Declare the built-in predicate `max` with arity 3 - !(add-atom &dyn-space (predicate-arity max 3)) - + (predicate-arity max 3) ; Enable `max` as a function - !(add-atom &dyn-space (function-arity max 2)) + (function-arity max 2) ; Define the rules for `max` - !(add-atom &dyn-space (= (max $X $Y $X) (<= $X $Y))) - !(add-atom &dyn-space (= (max $X $Y $Y) (> $X $Y))) + (= (max $X $Y $Y) (<= $X $Y)) + (= (max $X $Y $X) (> $X $Y)) ; Using `max` declaratively as a predicate - !(match &dyn-space (max (5 10) $max) + !(match &self (max (5 10) $max) (The maximum is $max)) - ; This resolves `$max = 10`. + [(The maximum is 10)] ; Using `max` procedurally as a function !(max 5 10) - ; Returns: 10. + [10] + ; Reverse execution with `max` - !(== (max $a $b) 10) ; as a function - !(max $a $b 10) ; or as a predicate - ; Returns: a pair such as (8 10) or (10 5) where the maximum is 10. + !(let True (== (max $a $b) 10) ($a $b)) ; as a function + [(#(exists $a (=< $a 10)) 10), (10 #(exists $b (=< 10 $b )))] + !(match &self (max $a $b 10) ($a $b)) ; or as a predicate + [(#(exists $a (=< $a 10)) 10), (10 #(exists $b (=< 10 $b )))] This dual behavior allows predicates to act as functions, bridging procedural and declarative paradigms. By defining `function-arity`, the function automatically resolves using the logic of the associated predicate.")