std:Set

lib/Set.omg

lib/Set.omg83 lines
1{-  Finite sets.
2
3    A `Set a` is a list of elements, each at most once, in the order they
4    were first inserted.  As for `std:Map`, there are no type classes: the
5    functions that compare elements take the test of equality as their first
6    argument, e.g. `Text/equal` or `std:Natural.equal`.
7
8        let Set = std:Set
9        let seen = Set.fromList Text/equal [ "b", "a", "b" ]
10        in  Set.member Text/equal "a" seen
11
12    The elements can be of any universe: a `Set Type` is a set of types, and
13    lives in `Type 1`.  Of two elements that the test says are equal, a set
14    keeps the one it was given first.
15
16    Testing, inserting or deleting one element takes a time proportional to
17    the size of the set; `fromList`, `union`, `intersection` and
18    `difference` take the product of the sizes.
19-}
20
21let Set : forall {l : Level} -> Type l -> Type l = \a -> List a
22
23let empty : forall {l : Level} {a : Type l} -> Set a = \{l} {a} -> ([] : Set a)
24
25let singleton : forall {l : Level} {a : Type l} -> a -> Set a = \x -> [ x ]
26
27let size : forall {l : Level} {a : Type l} -> Set a -> Natural = std:List.length
28
29-- The elements, in the order they were first inserted.
30let toList : forall {l : Level} {a : Type l} -> Set a -> List a = \s -> s
31
32let member
33    : forall {l : Level} {a : Type l} -> (a -> a -> Bool) -> a -> Set a -> Bool
34    = std:List.elem
35
36-- The set with the element (the same set if it has one equal to it).
37let insert
38    : forall {l : Level} {a : Type l} -> (a -> a -> Bool) -> a -> Set a -> Set a
39    = \{l} {a} equal x s -> if member equal x s then s else s # [ x ]
40
41-- The set without the element (the same set if it does not have it).
42let delete
43    : forall {l : Level} {a : Type l} -> (a -> a -> Bool) -> a -> Set a -> Set a
44    = \{l} {a} equal x -> std:List.filter (\(y : a) -> std:Bool.not (equal y x))
45
46-- The set of the elements of a list: the list without its repetitions.
47let fromList
48    : forall {l : Level} {a : Type l} -> (a -> a -> Bool) -> List a -> Set a
49    = \{l} {a} equal -> std:List.foldLeft (\(s : Set a) (x : a) -> insert equal x s) empty
50
51-- The elements of either set: those of the first, then the others.
52let union
53    : forall {l : Level} {a : Type l} -> (a -> a -> Bool) -> Set a -> Set a -> Set a
54    = \{l} {a} equal first second ->
55        first # std:List.filter (\(x : a) -> std:Bool.not (member equal x first)) second
56
57-- The elements of the first set that are in the second.
58let intersection
59    : forall {l : Level} {a : Type l} -> (a -> a -> Bool) -> Set a -> Set a -> Set a
60    = \{l} {a} equal first second -> std:List.filter (\(x : a) -> member equal x second) first
61
62-- The elements of the first set that are not in the second.
63let difference
64    : forall {l : Level} {a : Type l} -> (a -> a -> Bool) -> Set a -> Set a -> Set a
65    = \{l} {a} equal first second ->
66        std:List.filter (\(x : a) -> std:Bool.not (member equal x second)) first
67
68let filter : forall {l : Level} {a : Type l} -> (a -> Bool) -> Set a -> Set a = std:List.filter
69
70in  { Set
71    , empty
72    , singleton
73    , size
74    , toList
75    , member
76    , insert
77    , delete
78    , fromList
79    , union
80    , intersection
81    , difference
82    , filter
83    }
Your result
Complete file: lib/Set.omg · with imports