1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
(datatype Math (Num i64))
(sort MathToMath (UnstableFn (Math) Math))
(sort Maths (MultiSet Math))
(let $xs (multiset-of (Num 1) (Num 2) (Num 3)))
;; verify equal to other ordering
(check (=
(multiset-of (Num 3) (Num 2) (Num 1))
$xs
))
;; verify not equal to different counts
(check (!=
(multiset-of (Num 3) (Num 2) (Num 1) (Num 1))
$xs
))
;; Unclear why check won't work if this is defined inline
(let $inserted (multiset-insert $xs (Num 4)))
;; insert
(check (=
(multiset-of (Num 1) (Num 2) (Num 3) (Num 4))
$inserted
))
;; contains and not contains
(check (multiset-contains $xs (Num 1)))
(check (multiset-not-contains $xs (Num 4)))
;; remove last
(check (=
(multiset-of (Num 1) (Num 3))
(multiset-remove $xs (Num 2))
))
;; remove one of
(check (= (multiset-of (Num 1)) (multiset-remove (multiset-of (Num 1) (Num 1)) (Num 1))))
;; length
(check (= 3 (multiset-length $xs)))
;; length repeated
(check (= 3 (multiset-length (multiset-of (Num 1) (Num 1) (Num 1)))))
;; pick
(check (= (Num 1) (multiset-pick (multiset-of (Num 1)))))
;; map
(constructor square (Math) Math)
(rewrite (square (Num x)) (Num (* x x)))
(let $squared-xs (unstable-multiset-map (unstable-fn "square") $xs))
(run 1)
(check (=
(multiset-of (Num 1) (Num 4) (Num 9))
$squared-xs
))
;; sum
(check (=
(multiset-sum (multiset-of (Num 1) (Num 2) (Num 3)) (multiset-of (Num 1) (Num 2) (Num 4)))
(multiset-of (Num 1) (Num 4) (Num 2) (Num 3) (Num 2) (Num 1))
))
;; verify that sum computes length
(check (=
(multiset-length (multiset-sum (multiset-of (Num 1) (Num 2) (Num 3)) (multiset-of (Num 1) (Num 2) (Num 4))))
6
))