aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/data/collection/set/ordered.lux
blob: edb7c6539362c9cac639f5681f909945a83d66b7 (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
(.using
 [library
  [lux {"-" has list}
   [abstract
    [equivalence {"+" Equivalence}]
    [order {"+" Order}]]
   [data
    [collection
     ["[0]" list ("[1]#[0]" mix)]
     [dictionary
      ["/" ordered]]]]
   [type
    [abstract {"-" pattern}]]]])

(abstract: .public (Set a)
  (/.Dictionary a a)

  (def: .public empty
    (All (_ a) (-> (Order a) (Set a)))
    (|>> /.empty abstraction))

  (def: .public (member? set elem)
    (All (_ a) (-> (Set a) a Bit))
    (/.key? (representation set) elem))

  (template [<type> <name> <alias>]
    [(def: .public <name>
       (All (_ a) (-> (Set a) <type>))
       (|>> representation <alias>))]

    [(Maybe a) min /.min]
    [(Maybe a) max /.max]
    [Nat size  /.size]
    [Bit empty? /.empty?]
    )

  (def: .public (has elem set)
    (All (_ a) (-> a (Set a) (Set a)))
    (|> set representation (/.has elem elem) abstraction))

  (def: .public (lacks elem set)
    (All (_ a) (-> a (Set a) (Set a)))
    (|> set representation (/.lacks elem) abstraction))

  (def: .public list
    (All (_ a) (-> (Set a) (List a)))
    (|>> representation /.keys))

  (def: .public (of_list order list)
    (All (_ a) (-> (Order a) (List a) (Set a)))
    (list#mix has (..empty order) list))

  (def: .public (union left right)
    (All (_ a) (-> (Set a) (Set a) (Set a)))
    (list#mix ..has right (..list left)))

  (def: .public (intersection left right)
    (All (_ a) (-> (Set a) (Set a) (Set a)))
    (|> (..list right)
        (list.only (..member? left))
        (..of_list (the /.#order (representation right)))))

  (def: .public (difference param subject)
    (All (_ a) (-> (Set a) (Set a) (Set a)))
    (|> (..list subject)
        (list.only (|>> (..member? param) not))
        (..of_list (the /.#order (representation subject)))))

  (implementation: .public equivalence
    (All (_ a) (Equivalence (Set a)))
    
    (def: (= reference sample)
      (# (list.equivalence (# (representation reference) equivalence))
         = (..list reference) (..list sample))))
  )

(def: .public (sub? super sub)
  (All (_ a) (-> (Set a) (Set a) Bit))
  (|> sub
      ..list
      (list.every? (..member? super))))

(def: .public (super? sub super)
  (All (_ a) (-> (Set a) (Set a) Bit))
  (sub? super sub))