diff --git a/prolog/metta_lang/stdlib_mettalog.metta b/prolog/metta_lang/stdlib_mettalog.metta index ac7b0f52aa..8558ff36af 100644 --- a/prolog/metta_lang/stdlib_mettalog.metta +++ b/prolog/metta_lang/stdlib_mettalog.metta @@ -99,24 +99,29 @@ (iz predicate-arity MeTTaLog) + (@doc predicate-arity (@desc "Specifies the arity (number of arguments) for a given predicate, allowing it to be queriable in the system's match framework. This is particularly useful for enabling built-in functions, such as `size-atom`, to be used as predicates in declarative contexts and run in reverse to compute inputs based on outputs. + 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)) + ; Now `size-atom` can be used as a predicate in pattern matching (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) (The new atom is $new-atom)) ; This resolves `$new-atom` to a tuple of size 4, such as ($1 $2 $3 $4). + This reverse functionality is made possible because predicates can describe relationships, allowing you to infer inputs from outputs.") (@params ( (@param "Predicate symbol" "The name of the predicate whose arity is being defined.") @@ -128,33 +133,43 @@ This reverse functionality is made possible because predicates can describe rela (function-arity predicate-arity 1) + + (@doc function-arity (@desc "Defines the arity of a function, allowing predicates or built-in facts to also behave as callable functions. This enables procedural-style execution where the last argument of the predicate becomes the function's return value, and the system internally resolves the function using a `match` query. + For example: - ; Declare the built-in predicate `max` with arity 2 - (predicate-arity max 2) + ; Declare the built-in predicate `max` with arity 3 + (predicate-arity max 3) + ; Enable `max` as a function (add-atom &dyn-space (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)) + ; Using `max` declaratively as a predicate (match &dyn-space (max (5 10) $max) (The maximum is $max)) ; This resolves `$max = 10`. + ; Using `max` procedurally as a function (max 5 10) ; Returns: 10. + ; Reverse execution with `max` - (max $pair 10) + (== (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. + 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.") (@params ( (@param "Function symbol" "The name of the function or predicate to enable as a callable function.") @@ -164,6 +179,7 @@ For example: (predicate-arity function-arity 2) (function-arity function-arity 1) + ;; If Function (iz If MeTTa)