-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathOption.qll
More file actions
44 lines (36 loc) · 1.15 KB
/
Option.qll
File metadata and controls
44 lines (36 loc) · 1.15 KB
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
/** Provides a module for constructing optional versions of types. */
/** A type with `toString`. */
private signature class TypeWithToString {
string toString();
}
/**
* Constructs an `Option` type that is a disjoint union of the given type and an
* additional singleton element.
*/
module Option<TypeWithToString T> {
private newtype TOption =
TNone() or
TSome(T c)
/**
* An option type. This is either a singleton `None` or a `Some` wrapping the
* given type.
*/
class Option extends TOption {
/** Gets a textual representation of this element. */
string toString() {
this = TNone() and result = "(none)"
or
exists(T c | this = TSome(c) and result = c.toString())
}
/** Gets the wrapped element, if any. */
T asSome() { this = TSome(result) }
/** Holds if this option is the singleton `None`. */
predicate isNone() { this = TNone() }
}
/** The singleton `None` element. */
class None extends Option, TNone { }
/** A wrapper for the given type. */
class Some extends Option, TSome { }
/** Gets the given element wrapped as an `Option`. */
Some some(T c) { result = TSome(c) }
}