Skip to content

Commit

Permalink
ideally fixed part of the doc for predicate-arity
Browse files Browse the repository at this point in the history
  • Loading branch information
TeamSPoon committed Dec 11, 2024
1 parent 3c1cba3 commit 31840da
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions prolog/metta_lang/stdlib_mettalog.metta
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand All @@ -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.")
Expand All @@ -164,6 +179,7 @@ For example:
(predicate-arity function-arity 2)
(function-arity function-arity 1)


;; If Function

(iz If MeTTa)
Expand Down

0 comments on commit 31840da

Please sign in to comment.