Skip to content

Commit

Permalink
merge upstream/main
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrickTench committed Nov 26, 2024
2 parents a819cbc + 7dd9d53 commit 7978699
Show file tree
Hide file tree
Showing 18 changed files with 379 additions and 33 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ jobs:
# Set the remote URL with authentication
git remote set-url origin https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git
git pull
# Stage and commit changes
git add current_test_results.txt
git commit -m "Update test results"
Expand Down
1 change: 1 addition & 0 deletions src/canary/metta_eval.pl
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,7 @@
make_empty(RetType,[],NoResult).

eval_20(_Eq,_RetType1,_Depth,_Self,['call!'|S], TF):- !, eval_call(S,TF).
eval_20(_Eq,_RetType1,_Depth,_Self,['call-p!'|S], TF):- !, eval_call(S,TF).
eval_20(_Eq,_RetType1,_Depth,_Self,['call-fn!'|S], R):- !, eval_call_fn(S,R).
eval_20(_Eq,_RetType1,_Depth,_Self,['call-fn-nth!',Nth,S], R):-
length(Left,Nth),
Expand Down
3 changes: 2 additions & 1 deletion src/canary/metta_testing.pl
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@
% Converts the base file name to uppercase.
string_upper(Base, UpperBase),
% Replaces underscores with hyphens in the base name.
string_replace(UpperBase, "_", "-", NoUnderscore),
string_replace(UpperBase, "_MW", "", NOMW),
string_replace(NOMW, "_", "-", NoUnderscore),
% Replaces underscores with hyphens in the parent directory name.
string_replace(UpperParentDirBase, "_", "-", NoUnderscoreParent),
% Formats the test number as a zero-padded two-digit string.
Expand Down
48 changes: 16 additions & 32 deletions src/canary/stdlib_mettalog.metta
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,7 @@
(@param "Expression containing atoms of Number type")))
(@return "Minimum value in the expression. Error if expression contains non-numeric value or is empty."))
(: min-atom (-> Expression Number))
(= (min-atom $L) (call-fn min_list $L))
(iz min-atom Builtin)
(= (min-atom $L) (call-fn! min_list $L))

(iz max-atom MeTTa)
(@doc max-atom
Expand All @@ -369,8 +368,7 @@
(@param "Expression containing atoms of Number type")))
(@return "Maximum value in the expression. Error if expression contains non-numeric value or is empty."))
(: max-atom (-> Expression Number))
(= (min-atom $L) (call-fn max_list $L))
(iz max-atom Builtin)
(is-op-1 max-atom max_list)

(iz size-atom MeTTa)
(@doc size-atom
Expand All @@ -379,8 +377,7 @@
(@param "Expression to measure the size of")))
(@return "Size of the expression"))
(: size-atom (-> Expression Integer))
(= (size-atom $L) (call-fn length $L))
(iz size-atom Builtin)
(is-op-1 size-atom length)

(iz index-atom MeTTa)
(@doc index-atom
Expand All @@ -390,7 +387,7 @@
(@param "Index")))
(@return "Atom at the specified index in the expression. Error if index is out of bounds."))
(: index-atom (-> Expression Number Atom))
(= (index-atom $L $N) (call-fn nth0 $N $L))
(= (index-atom $L $N) (call-fn! nth0 $N $L))
(iz index-atom Builtin)

(iz powi-math MeTTa)
Expand All @@ -400,18 +397,20 @@
(@param "Base. Could be a float.")
(@param "Power. Must be an integer.")))
(@return "Result of base raised to the power"))
(: powi-math (-> Number Number Number))
;(is-math-op-2 powi-math pow)
(= (powi-math $B $P) (call-fn pow $B $P)))
(= (powi-math $B $P) (call-fn! pow $B $P))

(==> (is-math-op-1 $m $p) (is-op-1 $m $p))
(==> (is-math-op-1 $m $_) (: $m (-> Number Number)))
(==> (is-math-op-2 $m $p) (is-op-2 $m $p))
(==> (is-math-op-2 $m $_) (: $m (-> Number Number Number)))
(==> (is-math-pred $m $p) (is-pred $m $p))
(==> (is-math-pred $m $_) (: $m (-> Number Bool)))

(==> (is-op-1 $m $p) (= ($m $a) (call-fn $p $a)))
(==> (is-op-2 $m $p) (= ($m $a $b) (call-fn $p $a $b)))
(==> (is-pred-1 $m $p) (= ($m $a) (call-p $p $a)))
(==> (is-pred-2 $m $p) (= ($m $a $b) (call-p $p $a $b)))
(==> (is-op-1 $m $p) (= ($m $a) (call-fn! $p $a)))
(==> (is-op-2 $m $p) (= ($m $a $b) (call-fn! $p $a $b)))
(==> (is-pred-1 $m $p) (= ($m $a) (call-p! $p $a)))
(==> (is-pred-2 $m $p) (= ($m $a $b) (call-p! $p $a $b)))

(iz powf-math MeTTa)
(@doc powf-math
Expand All @@ -420,16 +419,15 @@
(@param "Base. Could be afloat.")
(@param "Power. Could be afloat.")))
(@return "Result of base raisedtothepower "))
(: powf-math (-> Number Number Number))
(is-math-op-2 powf-math pow)
(is-math-op-2 pow-math pow)

(iz sqrt-math MeTTa)
(@doc sqrt-math
(@desc "Returns square root for input number (first argument) which should be >= 0")
(@params (
(@param "Input number")))
(@return "Result of a square root function"))
(: sqrt-math (-> Number Number))
(is-math-op-1 sqrt-math sqrt)

(iz abs-math MeTTa)
Expand All @@ -438,27 +436,23 @@
(@params (
(@param "Input number")))
(@return "Absolute value"))
(: abs-math (-> Number Number))
(is-math-op-1 abs-math abs)


(iz log-math MeTTa)
(@doc log-math
(@desc "Returns result of a logarithm function given base (first argument) and input number (second argument)")
(@params (
(@param "Base")
(@param "Input number")))
(@return "Result of log function"))
(: log-math (-> Number Number Number))
(is-math-op-1 log-math log)
(is-math-op-2 log-math log2)

(iz trunc-math MeTTa)
(@doc trunc-math
(@desc "Returns integer part of the input value (first argument)")
(@params (
(@param "Float value")))
(@return "Integer part of float"))
(: trunc-math (-> Number Number))
(is-math-op-1 trunc-math trunc)

(iz ceil-math MeTTa)
Expand All @@ -467,7 +461,6 @@
(@params (
(@param "Float value")))
(@return "Integer value greater than or equal to the input"))
(: ceil-math (-> Number Number))
(is-math-op-1 ceil-math ceil)

(iz floor-math MeTTa)
Expand All @@ -476,7 +469,6 @@
(@params (
(@param "Float value")))
(@return "Integer value less than or equal to the input"))
(: floor-math (-> Number Number))
(is-math-op-1 floor-math floor)

(iz round-math MeTTa)
Expand All @@ -485,7 +477,6 @@
(@params (
(@param "Float value")))
(@return "Nearest integer to the input"))
(: round-math (-> Number Number))
(is-math-op-1 round-math round)

(iz sin-math MeTTa)
Expand All @@ -494,7 +485,6 @@
(@params (
(@param "Angle in radians")))
(@return "Result of the sine function"))
(: sin-math (-> Number Number))
(is-math-op-1 sin-math sin)

(iz asin-math MeTTa)
Expand All @@ -503,7 +493,6 @@
(@params (
(@param "Float number")))
(@return "Result of the arcsine function"))
(: asin-math (-> Number Number))
(is-math-op-1 asin-math asin)

(iz cos-math MeTTa)
Expand All @@ -512,7 +501,6 @@
(@params (
(@param "Angle in radians")))
(@return "Result of the cosine function"))
(: cos-math (-> Number Number))
(is-math-op-1 cos-math cos)

(iz acos-math MeTTa)
Expand All @@ -521,7 +509,6 @@
(@params (
(@param "Float number")))
(@return "Result of the arccosine function"))
(: acos-math (-> Number Number))
(is-math-op-1 acos-math acos)

(iz tan-math MeTTa)
Expand All @@ -530,7 +517,6 @@
(@params (
(@param "Angle in radians")))
(@return "Result of the tangent function"))
(: tan-math (-> Number Number))
(is-math-op-1 tan-math tan)

(iz atan-math MeTTa)
Expand All @@ -539,16 +525,14 @@
(@params (
(@param "Float number")))
(@return "Result of the tangent function"))
(: atan-math (-> Number Number))
(is-math-op-1 atan-math atan)

(iz isnan-math MeTTa)
(@doc isnan-math
(@desc "Returns True if input value is NaN. False - otherwise")
(@params (
(@param "Number")))
(@return "True/False"))
(: isnan-math (-> Number Bool))
(@return "True/False"))
(is-math-pred isnan-math is_NaN)

(iz isinf-math MeTTa)
Expand All @@ -557,7 +541,7 @@
(@params (
(@param "Number")))
(@return "True/False"))
(: isinf-math (-> Number Bool))
; this is deduced: (: isinf-math (-> Number Bool))
(is-math-pred isinf-math is_Inf)

(iz random-int MeTTa)
Expand Down
125 changes: 125 additions & 0 deletions tests/baseline_compat/hyperon-mettalog_sanity/auto_coerce_mw.metta
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@

;; Collection of functions operating on numbers

;; Define max
(: max (-> Number Number Number))
(= (max $x $y) (if (> $x $y) $x $y))

;; Define min
(: min (-> Number Number Number))
(= (min $x $y) (if (< $x $y) $x $y))

;; Clamp a number to be within a certain range
(: clamp (-> Number Number Number Number))
(= (clamp $x $l $u) (max $l (min $u $x)))

;; Define abs
(: abs (-> Number Number))
(= (abs $x) (if (< $x 0) (* -1 $x) $x))

;; Define lte
(: lte (-> Number Number Bool))
(= (lte $x $y) (or (< $x $y) (== $x $y)))

;; Define gte
(: gte (-> Number Number Bool))
(= (gte $x $y) (or (> $x $y) (== $x $y)))

;; Define approximately equal
(: approxEq (-> Number Number Number Bool))
(= (approxEq $x $y $epsilon) (lte (abs (- $x $y)) $epsilon))


;; Tests for max
!(assertEqual (max 3 5) 5) ;; Test when the second number is larger
!(assertEqual (max 7 2) 7) ;; Test when the first number is larger
!(assertEqual (max 4 4) 4) ;; Test when both numbers are equal

;; Tests for min
!(assertEqual (min 3 5) 3) ;; Test when the first number is smaller
!(assertEqual (min 7 2) 2) ;; Test when the second number is smaller
!(assertEqual (min 4 4) 4) ;; Test when both numbers are equal

;; Tests for clamp
!(assertEqual (clamp 5 0 10) 5) ;; Test when value is within the range
!(assertEqual (clamp -1 0 10) 0) ;; Test when value is below the lower bound
!(assertEqual (clamp 15 0 10) 10) ;; Test when value is above the upper bound
!(assertEqual (clamp 0 0 10) 0) ;; Test edge case at the lower bound
!(assertEqual (clamp 10 0 10) 10) ;; Test edge case at the upper bound

;; Tests for abs
!(assertEqual (abs 5) 5) ;; Test positive number
!(assertEqual (abs -5) 5) ;; Test negative number
!(assertEqual (abs 0) 0) ;; Test zero

;; Tests for lte
!(assertEqual (lte 3 5) True) ;; Test when first number is less
!(assertEqual (lte 5 5) True) ;; Test when numbers are equal
!(assertEqual (lte 7 5) False) ;; Test when first number is greater

;; Tests for gte
!(assertEqual (gte 7 5) True) ;; Test when first number is greater
!(assertEqual (gte 5 5) True) ;; Test when numbers are equal
!(assertEqual (gte 3 5) False) ;; Test when first number is less

;; Tests for approxEq
!(assertEqual (approxEq 3.0 3.1 0.1) False) ;; Test when difference exceeds epsilon
!(assertEqual (approxEq 3.0 3.05 0.1) True) ;; Test when difference is within epsilon
!(assertEqual (approxEq 3.0 3.0 0.1) True) ;; Test when numbers are exactly equal
!(assertEqual (approxEq 3.0 2.9 0.1) True) ;; Test when difference is just within epsilon
!(assertEqual (approxEq 3.0 2.8 0.1) False) ;; Test when difference is slightly beyond epsilon

;; Combined test cases
!(assertEqual (clamp (max 3 7) 0 5) 5) ;; Clamp the maximum of two numbers
!(assertEqual (clamp (min -3 2) -5 5) -3) ;; Clamp the minimum of two numbers
!(assertEqual (abs (min -3 2)) 3) ;; Absolute value of the minimum
!(assertEqual (approxEq (abs -5) 5 0) True) ;; Absolute value and approximate equality


;; Define Nat
(: Nat Type)
(: Z Nat)
(: S (-> Nat Nat))

;; Define cast functions between Nat and Number
(: fromNumber (-> Number Nat))
(= (fromNumber $n) (if (<= $n 0) Z (S (fromNumber (- $n 1)))))
(: fromNat (-> Nat Number))
(= (fromNat Z) 0)
(= (fromNat (S $k)) (+ 1 (fromNat $k)))

!(pragma! coerce auto)

;; Tests for max
!(assertEqual (max (S Z) (S (S Z))) (S (S Z))) ;; max(1, 2) = 2
!(assertEqual (max (S (S Z)) Z) (S (S Z))) ;; max(2, 0) = 2
!(assertEqual (max (S Z) (S Z)) (S Z)) ;; max(1, 1) = 1

;; Tests for min
!(assertEqual (min (S Z) (S (S Z))) (S Z)) ;; min(1, 2) = 1
!(assertEqual (min (S (S Z)) Z) Z) ;; min(2, 0) = 0
!(assertEqual (min (S Z) (S Z)) (S Z)) ;; min(1, 1) = 1

;; Tests for clamp
!(assertEqual (clamp (S Z) Z (S (S (S Z)))) (S Z)) ;; clamp(1, 0, 3) = 1
!(assertEqual (clamp Z (S Z) (S (S Z))) (S Z)) ;; clamp(0, 1, 2) = 1
!(assertEqual (clamp (S (S (S Z))) Z (S (S Z))) (S (S Z))) ;; clamp(3, 0, 2) = 2

;; Tests for lte
!(assertEqual (lte (S Z) (S (S Z))) True) ;; lte(1, 2) = True
!(assertEqual (lte (S (S Z)) (S (S Z))) True) ;; lte(2, 2) = True
!(assertEqual (lte (S (S Z)) Z) False) ;; lte(2, 0) = False

;; Tests for gte
!(assertEqual (gte (S (S Z)) (S Z)) True) ;; gte(2, 1) = True
!(assertEqual (gte (S Z) (S Z)) True) ;; gte(1, 1) = True
!(assertEqual (gte Z (S Z)) False) ;; gte(0, 1) = False

;; Tests for approxEq
!(assertEqual (approxEq (S Z) (S Z) Z) True) ;; approxEq(1, 1, 0) = True
!(assertEqual (approxEq (S Z) (S (S Z)) Z) False) ;; approxEq(1, 2, 0) = False

;; Combined test cases
!(assertEqual (clamp (max (S Z) (S (S Z))) Z (S (S (S Z)))) (S (S Z))) ;; max(1, 2) clamped to [0, 3] = 2
!(assertEqual (clamp (min (S (S Z)) (S (S Z))) (S Z) (S (S (S Z)))) (S (S Z))) ;; min(2, 2) clamped to [1, 3] = 2
!(assertEqual (approxEq (max Z (min (S (S Z)) (S Z))) (S Z) Z) True) ;; approxEq(max(0, min(2, 1)), 1, 0) = True
Loading

0 comments on commit 7978699

Please sign in to comment.