Skip to content

Commit

Permalink
Merge branch 'trueagi-io:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
royward authored Oct 4, 2024
2 parents 5688ebf + be752c4 commit 997e72f
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 1 deletion.
46 changes: 45 additions & 1 deletion src/canary/stdlib_mettalog.metta
Original file line number Diff line number Diff line change
Expand Up @@ -1630,6 +1630,14 @@
(@return "Result of Form if execution completes within Time. Raises an exception otherwise."))
(: max-time! (-> Number Atom Atom))

;; Public MeTTa
(@doc sleep!
(@desc "Sleeps for N seconds.")
(@params (
(@param "N")))
(@return "Returns True after sleeping completes."))
(: sleep! (-> Number Bool))

;; Public MeTTa
(@doc limit!
(@desc "Executes Form generating at most Limit results. Results are returned as soon as they become available.")
Expand All @@ -1639,6 +1647,23 @@
(@return "First Limit results of Form."))
(: limit! (-> Number Atom Atom))

;; Public MeTTa
(@doc number-of
(@desc "Returns the number of results Form generates")
(@params (
(@param "Form")))
(@return "Number of results of Form."))
(: number-of (-> Atom Number))

;; Public MeTTa
(@doc offset!
(@desc "Executes Form ignoring the first Count results. Results are returned as soon as they become available.")
(@params (
(@param "Count")
(@param "Form")))
(@return "Results of Form after ignoring the first Count results that are generated."))
(: offset! (-> Number Atom Atom))

;; Public MeTTa
(@doc call!
(@desc "Trampolines to Prolog's call. Only works when the predicate and each arg are provided separately. e.g. !(call! ls /) will print the root directory but !(call! ls(/)) will fail.")
Expand All @@ -1653,4 +1678,23 @@
(@params (
(@param "String")))
(@return "A list of the binding values. If there are no bindings but the Prolog query is True, returns the empty list."))
(: call-string! (-> String Atom))
(: call-string! (-> String Atom))

;; Public MeTTa
(@doc call-cleanup!
(@desc "Same as (setup-call-cleanup! true Form Cleanup). setup-call-cleanup! is recommended instead if Cleanup is intended to undo prior side-effects - place those side-effects in Setup.")
(@params (
(@param "Form")
(@param "Cleanup")))
(@return "Result of Form."))
(: call-cleanup! (-> Atom Atom Atom))

;; Public MeTTa
(@doc setup-call-cleanup!
(@desc "Executes Setup, then Form, then finally Cleanup. Setup is protected from interrupts (e.g. max-time!). In most uses, Setup will perform temporary side-effects required by Form that are finally undone by Cleanup. Cleanup is run even if Form raises an exception.")
(@params (
(@param "Setup")
(@param "Form")
(@param "Cleanup")))
(@return "Result of Form."))
(: setup-call-cleanup! (-> Atom Atom Atom Atom))
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
;; setup

!(bind! &var (new-state 0))
(= (increment)
(sequential ((do (change-state! &var (+ 1 (get-state &var))))
(get-state &var))))

;; call-cleanup! returns result of Form, calls cleanup

!(assertEqualToResult (sequential ((call-cleanup! (increment)
(increment))
(increment)))
(1 3))

;; cleanup

!(change-state! &var 0)

;; call-cleanup! still calls cleanup if evaluation errors

!(assertEqualToResult (catch (call-cleanup! (throw foo)
(increment))
foo
(increment))
(2))

;; setup-call-cleanup! does Setup, then Form, then Cleanup, returning result of Form

!(assertEqualToResult (sequential ((setup-call-cleanup! (change-state! &var 0)
(increment)
(increment))
(increment)))
(1 3))

;; setup-call-cleanup! Setup is protected from interrupts, Cleanup is called if Setup is interrupted (but Form is not)

!(assertEqualToResult (catch (max-time! 1
(setup-call-cleanup! (sequential ((sleep! 2)
(change-state! &var 0)))
((increment) (increment))
(increment)))
time_limit_exceeded
(time_limit_exceeded (get-state! &var)))
((time_limit_exceeded 1)))

!(assertEqualToResult (not (== (get-state! &var) 0)) (True))

!(assertEqualToResult (catch (max-time! 1
(setup-call-cleanup! ((sleep! 2)
(change-state! &var 0))
((increment) (increment))
(increment)))
time_limit_exceeded
(time_limit_exceeded (get-state! &var)))
((time_limit_exceeded 1)))

;; setup-call-cleanup! returns Form for each result of Setup

!(change-state! &var 0)
!(assertEqualToResult (setup-call-cleanup! (superpose (1 2 3 4))
(increment)
ignore)
(1 2 3 4))

;; setup-call-cleanup! returns Form then calls Cleanup for each result of Setup

!(change-state! &var 0)
!(assertEqualToResult (setup-call-cleanup! (superpose (1 2 3 4))
(increment)
(increment))
(1 3 5 7))
47 changes: 47 additions & 0 deletions tests/baseline_compat/hyperon-mettalog_sanity/limit_tests.metta
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,50 @@

!(assertEqualToResult (limit! (sequential (1 2 3)) (sequential ((increment) (increment) (increment)))) (1 2 3 4 5 6))
!(assertEqualToResult (get-state! &var) (6))

;; limit preventing infinite recursion

(= (infinite) 1)
(= (infinite) (+ (infinite) 1))
!(assertEqualToResult (limit! 4 (infinite)) (1 2 3 4))

;; basic number-of with 1 result

!(assertEqualToResult (number-of (+ 1 2)) (1))

;; basic number-of with superpose

!(assertEqualToResult (number-of (superpose (1 2 3))) (3))

;; number-of with limit

!(change-state! &var 0)
!(assertEqualToResult (number-of (limit! 2 (sequential ((increment) (increment) (increment))))) (2))
!(assertEqualToResult (get-state! &var) (2))
!(change-state! &var 0)

!(assertEqualToResult (number-of (limit! 4 (infinite))) (4))

;; number-of with variable

(= (test A) (input A))
(= (test B) (input B))

!(assertEqualToResult (number-of (test $x)) (2))

;; offset! ignores first N results

!(assertEqualToResult (offset! 2 (sequential (1 2 3 4))) (3 4))

;; offset! first N results are still generated, just discarded

!(change-state! &var 0)
!(assertEqualToResult (offset! 2 (sequential ((increment) (increment) (increment) (increment)))) (3 4))

;; offset! and limit!

!(assertEqualToResult (offset! 2 (limit! 4 (infinite))) (3 4))

;; limit! and offset!

!(assertEqualToResult (limit! 4 (offset! 2 (infinite))) (3 4 5 6))

0 comments on commit 997e72f

Please sign in to comment.