Skip to content

Commit

Permalink
Fix *-arity exmales in stdlib_mettalog.metta docs
Browse files Browse the repository at this point in the history
  • Loading branch information
TeamSPoon authored Dec 11, 2024
1 parent 1ca959c commit 4dd9c4a
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions prolog/metta_lang/stdlib_mettalog.metta
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand All @@ -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.")
Expand Down

0 comments on commit 4dd9c4a

Please sign in to comment.