blob: 8cafd922e229330bcd55a884916c88963cea77ae (
plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
(.module:
[lux #*
[abstract
[equivalence (#+ Equivalence)]
[order (#+ Order)]]
[data
[collection
["." list ("#@." fold)]
[dictionary
["/" ordered]]]]
[type
abstract]])
(abstract: #export (Set a)
(/.Dictionary a a)
(def: #export new
(All [a] (-> (Order a) (Set a)))
(|>> /.new :abstraction))
(def: #export (member? set elem)
(All [a] (-> (Set a) a Bit))
(|> set :representation (/.contains? elem)))
(template [<name> <alias>]
[(def: #export <name>
(All [a] (-> (Set a) (Maybe a)))
(|>> :representation <alias>))]
[min /.min]
[max /.max]
)
(template [<name> <alias>]
[(def: #export <name>
(-> (Set Any) Nat)
(|>> :representation <alias>))]
[size /.size]
[depth /.depth]
)
(def: #export (add elem set)
(All [a] (-> a (Set a) (Set a)))
(|> set :representation (/.put elem elem) :abstraction))
(def: #export (remove elem set)
(All [a] (-> a (Set a) (Set a)))
(|> set :representation (/.remove elem) :abstraction))
(def: #export to-list
(All [a] (-> (Set a) (List a)))
(|>> :representation /.keys))
(def: #export (from-list &order list)
(All [a] (-> (Order a) (List a) (Set a)))
(list@fold add (..new &order) list))
(def: #export (union left right)
(All [a] (-> (Set a) (Set a) (Set a)))
(list@fold ..add right (..to-list left)))
(def: #export (intersection left right)
(All [a] (-> (Set a) (Set a) (Set a)))
(|> (..to-list right)
(list.filter (..member? left))
(..from-list (get@ #/.&order (:representation right)))))
(def: #export (difference param subject)
(All [a] (-> (Set a) (Set a) (Set a)))
(|> (..to-list subject)
(list.filter (|>> (..member? param) not))
(..from-list (get@ #/.&order (:representation subject)))))
(structure: #export equivalence (All [a] (Equivalence (Set a)))
(def: (= reference sample)
(:: (list.equivalence (:: (:representation reference) &equivalence))
= (..to-list reference) (..to-list sample))))
)
(def: #export (sub? super sub)
(All [a] (-> (Set a) (Set a) Bit))
(|> sub
..to-list
(list.every? (..member? super))))
(def: #export (super? sub super)
(All [a] (-> (Set a) (Set a) Bit))
(sub? super sub))
|