Skip to content

Commit 9e38f63

Browse files
committed
fix: Addressed few failing test cases and code optimizations
1 parent 98b349a commit 9e38f63

7 files changed

Lines changed: 74 additions & 96 deletions

utilities/general-helpers.metta

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,7 @@
711711
;; Converts a tuple of pairs into ordered multimap
712712
;; Ex: ((k1 v1) (k2 v2)) -> (cons (k1 v1) (cons (k2 v2) ()))
713713
;; (: expToMMap (-> Expression (MultiMap ($k $v)) (-> $k $k Bool) (MultiMap ($k $v))))
714+
;; TODO: Change this to simpler function
714715
(= (expToMMap () $map $compFunc) ())
715716
(= (expToMMap $tuple $map $compFunc)
716717
(let*
@@ -953,9 +954,6 @@
953954
(= (coll.append NilOMS $el) (eval (OMS.insert $el NilOMS)))
954955
(= (coll.append (ConsOMS $x $xs) $el) (eval (OMS.insert $el (ConsOMS $x $xs))))
955956

956-
;; Ordered Set insertion should stay explicit through `OS.insert` because raw cons-lists
957-
;; are also used for normal lists and pair-backed containers after the cons migration.
958-
959957
;; Deme — append ScoredInstance to its InstanceSet
960958
(= (coll.append (mkDeme $rep (mkSInstSet $list) $demeId) $el)
961959
(mkDeme $rep (mkSInstSet (coll.append $list $el)) $demeId))

utilities/list-methods.metta

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
(= (List.sum $xs) (List.foldr + 0 $xs))
2929

3030
; (: List.append (-> $a (List $a) (List $a)))
31-
(= (List.append $val ()) (cons-atom $val ()))
31+
(= (List.append $val ()) ($val))
3232
(= (List.append $val (cons $head $tail))
3333
(cons-atom $head (List.append $val $tail)))
3434

@@ -40,15 +40,15 @@
4040
(index-atom $xs $idx)))
4141

4242
; (: List.insert (-> $a (List $a) (List $a)))
43-
(= (List.insert $x ()) (cons-atom $x ()))
43+
(= (List.insert $x ()) ($x))
4444
(= (List.insert $x (cons $head $tail))
4545
(if (< $x $head)
46-
(cons-atom $x (cons-atom $head $tail))
47-
(cons-atom $head (List.insert $x $tail))))
46+
(cons $x (cons $head $tail))
47+
(cons $head (List.insert $x $tail))))
4848

4949
;; Sort a list in ascending order by default.
5050
(= (List.sort ()) ())
51-
(= (List.sort $xs) (List.sort $xs <))
51+
(= (List.sort (cons $x $xs)) (List.sort (cons $x $xs) <))
5252
(= (List.sort $xs $op)
5353
(selectionSort $xs (size-atom $xs) $op))
5454

@@ -64,7 +64,7 @@
6464
(let $new-head ($f $head $arg)
6565
(if (== $new-head ())
6666
(List.mapOverArg $f $tail $arg)
67-
(cons-atom $new-head (List.mapOverArg $f $tail $arg)))))
67+
(cons $new-head (List.mapOverArg $f $tail $arg)))))
6868

6969
; (: List.filter (-> (-> $a Bool) (List $a) (List $a)))
7070
(= (List.filter $p $xs) (filter-atom $xs $p))
@@ -86,20 +86,25 @@
8686
(= (List.max $xs) (List.max >= $xs))
8787

8888
; (: List.contains (-> $a (List $a) Bool))
89-
(= (List.contains $a ()) False)
90-
(= (List.contains $a (cons $head $tail))
91-
(if (== $a $head) True (List.contains $a $tail)))
89+
(= (List.contains $a $list) (is-member $a $list))
9290

9391
; (: List.replaceAt (-> (List $a) Number $a (List $a)))
9492
(= (List.replaceAt () $n $elem) ())
9593
(= (List.replaceAt (cons $head $tail) $n $elem)
9694
(if (== $n 0)
97-
(cons-atom $elem $tail)
98-
(cons-atom $head (List.replaceAt $tail (- $n 1) $elem))))
95+
(cons $elem $tail)
96+
(cons $head (List.replaceAt $tail (- $n 1) $elem))))
9997

10098
; (: List.prepend (-> $a (List $a) (List $a)))
101-
(= (List.prepend $a $list) (cons-atom $a $list))
102-
99+
(= (List.prepend $a $list) (cons $a $list))
100+
101+
;; Find an element in a list
102+
;; Params:
103+
;; $list: The list to search
104+
;; $target: The element to find
105+
;; Returns:
106+
;; The index of the first found element or
107+
;; -1 If the element can't be found
103108
; (: List.index (-> (List $a) $a Number))
104109
(= (List.index () $target) -1)
105110
(= (List.index (cons $x $xs) $target)
@@ -116,6 +121,7 @@
116121
(= (List.tail ()) ())
117122
(= (List.tail (cons $x $xs)) $xs)
118123

124+
;; INFO: The previous overloaded version doesn't work here after changing to expression.
119125
; (: List.sub (-> (List Number) (List Number) (List Number)))
120126
(= (List.sub $xs $ys)
121127
(if (== $xs ())
@@ -138,22 +144,23 @@
138144
(= (List.zip () (cons $y $ys)) ())
139145
(= (List.zip (cons $x $xs) ()) ())
140146
(= (List.zip (cons $x $xs) (cons $y $ys))
141-
(cons-atom ($x $y) (List.zip $xs $ys)))
147+
(cons ($x $y) (List.zip $xs $ys)))
142148

143149
; (: List.zipWith (-> (-> $a $b $c) (List $a) (List $b) (List $c)))
144150
(= (List.zipWith $f () ()) ())
145151
(= (List.zipWith $f () (cons $y $ys)) ())
146152
(= (List.zipWith $f (cons $x $xs) ()) ())
147153
(= (List.zipWith $f (cons $x $xs) (cons $y $ys))
148-
(cons-atom ($f $x $y) (List.zipWith $f $xs $ys)))
154+
(cons ($f $x $y) (List.zipWith $f $xs $ys)))
149155

150156
; (: List.drop (-> Number (List $a) (List $a)))
151157
(= (List.drop $n ()) ())
152158
(= (List.drop $n (cons $x $xs))
153159
(if (== $n 0)
154-
(cons-atom $x $xs)
160+
(cons $x $xs)
155161
(List.drop (- $n 1) $xs)))
156162

163+
;; INFO: Behavior has changed from previous List.flatten. Investigation needed if causes an error.
157164
; (: List.flatten (-> (List (List $a)) (List $a)))
158165
(= (List.flatten ()) ())
159166
(= (List.flatten (cons $x $xs))
@@ -165,7 +172,7 @@
165172
(= (List.repeat $n $a)
166173
(if (== $n 0)
167174
()
168-
(cons-atom $a (List.repeat (- $n 1) $a))))
175+
(cons $a (List.repeat (- $n 1) $a))))
169176

170177
; (: List.concat (-> (List $a) (List $a) (List $a)))
171178
(= (List.concat $list1 $list2) (append $list1 $list2))
@@ -174,19 +181,19 @@
174181
(= (List.removeAtIdx () $idx) ())
175182
(= (List.removeAtIdx (cons $head $tail) $idx)
176183
(if (< $idx 0)
177-
(cons-atom $head $tail)
184+
(cons $head $tail)
178185
(if (== $idx 0)
179186
$tail
180-
(cons-atom $head (List.removeAtIdx $tail (- $idx 1))))))
187+
(cons $head (List.removeAtIdx $tail (- $idx 1))))))
181188

182189
; (: List.isMember (-> $a (List $a) Bool))
183190
(= (List.isMember $elem $list) (List.contains $elem $list))
184191

185192
; (: List.partialSort (-> (-> $a $a Bool) (List $a) Number (List $a) (List $a)))
186193
(= (List.partialSort $comparator () $n $acc) $acc)
187-
(= (List.partialSort $comparator $xs $n $acc)
188-
(let* (($max (List.max $comparator $xs))
189-
($unsortedList (List.delete $max $xs))
194+
(= (List.partialSort $comparator (cons $x $xs) $n $acc)
195+
(let* (($max (List.max $comparator (cons $x $xs)))
196+
($unsortedList (List.delete $max (cons $x $xs)))
190197
($sortedList (List.append $max $acc)))
191198
(if (<= (- $n 1) 0)
192199
(List.appendList $unsortedList $sortedList)
@@ -203,7 +210,7 @@
203210
(= (List.delete $a (cons $x $xs))
204211
(if (== $x $a)
205212
$xs
206-
(cons-atom $x (List.delete $a $xs))))
213+
(cons $x (List.delete $a $xs))))
207214

208215
; (: List.takeN (-> Number (List $a) (List $a)))
209216
(= (List.takeN $n ()) ())
@@ -222,7 +229,7 @@
222229
; (: List.generate (-> Number Number (List Number)))
223230
(= (List.generate $length $element)
224231
(if (> $length 0)
225-
(cons-atom $element (List.generate (- $length 1) $element))
232+
(cons $element (List.generate (- $length 1) $element))
226233
()))
227234

228235
;; Remove an element from a list by index.
@@ -231,7 +238,7 @@
231238
(if (== $i 0)
232239
$xs
233240
(chain (List.pop (- $i 1) $xs) $res
234-
(if-error $res (cons-atom $x ()) (cons-atom $x $res)))))
241+
(if-error $res (cons $x ()) (cons $x $res)))))
235242

236243
(= (List.splitAt $i $xs)
237244
((List.takeN $i $xs) (List.drop $i $xs)))

utilities/map.metta

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@
22
(: Map (-> ($k $v) Type))
33

44
;; An insertion function that also allows map to act as a counter
5-
(= (Map.insertCounter $key ()) (cons-atom ($key 1) ()))
5+
(= (Map.insertCounter $key ()) (cons ($key 1) ()))
66
(= (Map.insertCounter $key (cons ($curKey $curVal) $tail))
77
(if (== $key $curKey)
8-
(cons-atom ($curKey (+ $curVal 1)) $tail)
8+
(cons ($curKey (+ $curVal 1)) $tail)
99
(if (< $key $curKey)
10-
(cons-atom ($key 1) (cons-atom ($curKey $curVal) $tail))
11-
(cons-atom ($curKey $curVal) (Map.insertCounter $key $tail)))))
10+
(cons ($key 1) (cons ($curKey $curVal) $tail))
11+
(cons ($curKey $curVal) (Map.insertCounter $key $tail)))))
1212

1313
;; insert an element to a Map using custom comparison functions (equality + less-than)
14-
(= (Map.insert ($key $value) () $fEq $fLt) (cons-atom ($key $value) ()))
14+
(= (Map.insert ($key $value) () $fEq $fLt) (cons ($key $value) ()))
1515
(= (Map.insert ($key $value) (cons ($curKey $curVal) $tail) $fEq $fLt)
1616
(if ($fEq $key $curKey)
17-
(cons-atom ($key $value) $tail)
17+
(cons ($key $value) $tail)
1818
(if ($fLt $key $curKey)
19-
(cons-atom ($key $value) (cons-atom ($curKey $curVal) $tail))
20-
(cons-atom ($curKey $curVal) (Map.insert ($key $value) $tail $fEq $fLt)))))
19+
(cons ($key $value) (cons ($curKey $curVal) $tail))
20+
(cons ($curKey $curVal) (Map.insert ($key $value) $tail $fEq $fLt)))))
2121

2222
;; insert an element to a Map
23-
(= (Map.insert ($key $value) ()) (cons-atom ($key $value) ()))
23+
(= (Map.insert ($key $value) ()) (cons ($key $value) ()))
2424
(= (Map.insert ($key $value) (cons ($curKey $curVal) $tail))
2525
(if (== $key $curKey)
2626
(cons-atom ($key $value) $tail)
@@ -35,8 +35,8 @@
3535

3636
;; Get a pair from a Map using index
3737
(= (Map.getByIdx () $i) (Error () "Index out of bounds"))
38-
(= (Map.getByIdx $map $i)
39-
(List.getByIdx $map $i))
38+
(= (Map.getByIdx (cons $x $xs) $i)
39+
(List.getByIdx (cons $x $xs) $i))
4040

4141
;; Check if a key is in the Map
4242
(= (Map.contains $key ()) False)
@@ -54,7 +54,7 @@
5454
(if (== $key $curKey)
5555
$tail
5656
(chain (Map.remove $key $tail) $res
57-
(if-error $res $res (cons-atom ($curKey $curVal) $res)))))
57+
(if-error $res $res (cons ($curKey $curVal) $res)))))
5858

5959
;; Get all the values from the map
6060
(= (Map.values $map)

utilities/ordered-multimap.metta

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
(: MultiMap (-> ($k $v) Type))
33

44
;; insert an element to the Ordered multimap using custom function
5-
(= (MultiMap.insert ($key $value) () $fn) (cons-atom ($key $value) ()))
5+
(= (MultiMap.insert ($key $value) () $fn) (cons ($key $value) ()))
66
(= (MultiMap.insert ($key $value) (cons ($curKey $curVal) $tail) $fn)
77
(if ($fn $key $curKey)
8-
(cons-atom ($key $value) (cons-atom ($curKey $curVal) $tail))
9-
(cons-atom ($curKey $curVal) (MultiMap.insert ($key $value) $tail $fn))))
8+
(cons ($key $value) (cons ($curKey $curVal) $tail))
9+
(cons ($curKey $curVal) (MultiMap.insert ($key $value) $tail $fn))))
1010

1111
;; default insert function using <=
1212
(= (MultiMap.insert $newValue $mMap) (MultiMap.insert $newValue $mMap <=))
1313

1414
;; Get the key value pair given an index.
1515
(= (MultiMap.getByIdx $idx ()) (Error () (Can't find $idx)))
16-
(= (MultiMap.getByIdx $idx $mmap) (List.getByIdx $mmap $idx))
16+
(= (MultiMap.getByIdx $idx (cons $x $xs)) (List.getByIdx (cons $x $xs) $idx))
1717

1818
;; Get the first found value with the given key from the Ordered multimap using key
1919
(= (MultiMap.findOne $key ()) (Error $key "not found"))
@@ -24,7 +24,7 @@
2424
(= (MultiMap.findAll $key ()) ())
2525
(= (MultiMap.findAll $key (cons ($curKey $curVal) $tail))
2626
(if (== $key $curKey)
27-
(cons-atom $curVal (MultiMap.findAll $key $tail))
27+
(cons $curVal (MultiMap.findAll $key $tail))
2828
(MultiMap.findAll $key $tail)))
2929

3030
;; Check if a key is in the Ordered multimap
@@ -43,14 +43,14 @@
4343
(if (== $key $curKey)
4444
$tail
4545
(chain (MultiMap.removeOne $key $tail) $res
46-
(if-error $res $res (cons-atom ($curKey $curVal) $res)))))
46+
(if-error $res $res (cons ($curKey $curVal) $res)))))
4747

4848
;; Remove all key-value pairs with a given key from the Ordered multimap
4949
(= (MultiMap.removeAll $key ()) ())
5050
(= (MultiMap.removeAll $key (cons ($curKey $curVal) $tail))
5151
(if (== $key $curKey)
5252
(MultiMap.removeAll $key $tail)
53-
(cons-atom ($curKey $curVal) (MultiMap.removeAll $key $tail))))
53+
(cons ($curKey $curVal) (MultiMap.removeAll $key $tail))))
5454

5555
;; Get all the values from the multimap
5656
(= (MultiMap.values $mmap)

utilities/ordered-set.metta

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22
(: OS (-> $a Type))
33

44
;; Insert an element into an ordered set using comparator returning G/E/L.
5-
(= (OS.insert $comparator $x ()) (cons-atom $x ()))
5+
;; TODO: comparison operators used in the original MOSES implementation works by finding the difference between the scores and
6+
;; two exemplars are deemed equal if this difference value is within a certain range which is a samll number
7+
(= (OS.insert $comparator $x ()) ($x))
68
(= (OS.insert $comparator $new (cons $x $xs))
79
(case ($comparator $new $x)
8-
((G (cons-atom $new (cons-atom $x $xs)))
9-
(E (cons-atom $x $xs))
10-
(L (cons-atom $x (OS.insert $comparator $new $xs))))))
10+
((G (cons $new (cons $x $xs)))
11+
(E (cons $x $xs))
12+
(L (cons $x (OS.insert $comparator $new $xs))))))
1113

1214
;; Length of an OS
1315
(= (OS.length $os) (size-atom $os))
1416

1517
;; OS.getByIdx -- get by index
16-
(= (OS.getByIdx $idx ()) (Error $idx "empty set/index out of range"))
1718
(= (OS.getByIdx $idx $os)
1819
(if (or (< $idx 0) (>= $idx (size-atom $os)))
1920
(Error 0 "empty set/index out of range")
@@ -24,7 +25,7 @@
2425
(= (OS.takeN $n (cons $x $xs))
2526
(if (<= $n 0)
2627
()
27-
(cons-atom $x (OS.takeN (- $n 1) $xs))))
28+
(cons $x (OS.takeN (- $n 1) $xs))))
2829

2930
;; OS.contains -- check if an ordered list contains a particular element
3031
(= (OS.contains $os $el) (is-member $el $os))
@@ -37,17 +38,18 @@
3738
(chain (OS.removeByIdx $xs (- $index 1)) $res
3839
(if (== $res ())
3940
($x)
40-
(if-error $res $res (cons-atom $x $res))))))
41+
(if-error $res $res (cons $x $res))))))
4142

4243
;; Check if an Exemplar is a member of an ordered set
4344
(= (OS.isMember $exemplar $os) (OS.contains $os $exemplar))
4445

4546
;; Retrieves the first N elements from an ordered set.
4647
(= (OS.getTopN $n ()) (Error () "empty ordered set/ fewer items than required"))
47-
(= (OS.getTopN $n $os)
48-
(if (> $n (size-atom $os))
49-
(Error () "empty ordered set/ fewer items than required")
50-
(OS.takeN $n $os)))
48+
(= (OS.getTopN $n (cons $x $xs))
49+
(if (== $n 1)
50+
(cons $x ())
51+
(chain (OS.getTopN (- $n 1) $xs) $res
52+
(if-error $res $res (cons $x $res)))))
5153

5254
;; Comparator helpers
5355
(= (genericCompare $atom1 $atom2)
@@ -94,12 +96,8 @@
9496
(OS.difference $tail $set2 $newResult))))
9597

9698
;; Convert OrderedSet to Expression
97-
(= (osToExpression $os $acc) (append $os $acc))
99+
;; (= (osToExpression $os $acc) (append $os $acc))
100+
(= (osToExpression $os $acc) $os)
98101

99102
;; Convert Expression to OrderedSet
100-
(= (expressionToOS $expr $currentOS)
101-
(if (== $expr ())
102-
$currentOS
103-
(let ($head $tail) (decons-atom $expr)
104-
(chain (OS.insert genericCompare $head $currentOS) $newOS
105-
(expressionToOS $tail $newOS)))))
103+
(= (expressionToOS $expr $currentOS) $expr)

utilities/tests/general-helper-functions-test.metta

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -532,32 +532,6 @@
532532
!(println! ("testing coll.getByIdx"))
533533
!(assertEqual (coll.getByIdx (cons 10 (cons 20 (cons 30 ()))) 1) 20)
534534

535-
;; --------------------------------
536-
;; Map — keys/values/items/find/getByKey/length/contains
537-
;; --------------------------------
538-
!(println! ("testing coll.length"))
539-
; Raw cons-backed Map semantics are ambiguous after the container constructor migration.
540-
; They are covered by utilities/tests/map-test.metta.
541-
542-
;; --------------------------------
543-
;; MultiMap — findOne/findAll/length/contains
544-
;; --------------------------------
545-
!(println! ("testing coll.length"))
546-
; Raw cons-backed MultiMap semantics are ambiguous after the container constructor migration.
547-
; They are covered by utilities/tests/ordered-multimap-test.metta.
548-
549-
;; ;; --------------------------------
550-
;; ;; OrderedSet — contains/getByIdx/removeByIdx/takeN
551-
;; ;; --------------------------------
552-
!(println! ("testing coll.length "))
553-
; Raw cons-backed OrderedSet semantics are ambiguous after the container constructor migration.
554-
; They are covered by utilities/tests/ordered-set-test.metta.
555-
556-
;; ;; --------------------------------
557-
;; OrderedMultiset — length/append/contains/getByIdx/remove
558-
;; --------------------------------
559-
560-
; Covered by utilities/tests/ordered-multiset-test.metta
561535

562536
;; Cscore comparisons via apply (inline mkCscore)
563537
!(println! ("apply >"))

0 commit comments

Comments
 (0)