-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathSwiftskell.swift
296 lines (255 loc) · 7.15 KB
/
Swiftskell.swift
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// --------------------------------
// A Swift × Haskell stdlib flavour
// --------------------------------
/// MARK: GHC.Show
public protocol Show: ~Copyable {
borrowing func show() -> String
}
extension CustomStringConvertible {
public func show() -> String { return description }
}
extension Int: Show {}
public func print(_ s: borrowing some Show & ~Copyable) {
print(s.show())
}
/// MARK: Data.Eq
public protocol Eq: ~Copyable {
static func ==(_ a: borrowing Self, _ b: borrowing Self) -> Bool
static func /=(_ a: borrowing Self, _ b: borrowing Self) -> Bool
}
public extension Eq where Self: ~Copyable {
static func /=(_ a: borrowing Self, _ b: borrowing Self) -> Bool {
return !(a == b)
}
}
/// MARK: Result
public enum Either<Success: ~Copyable, Failure: Error>: ~Copyable {
case success(Success)
case failure(Failure)
}
extension Either: Copyable where Success: Copyable {}
extension Either where Failure == Swift.Error {
public init(catching body: () throws -> Success) {
do {
self = .success(try body())
} catch {
self = .failure(error)
}
}
}
/// MARK: Iteration
public protocol Generator: ~Copyable {
associatedtype Element: ~Copyable
func next() -> Maybe<Element>
}
/// Eager assertion function, to avoid autoclosures.
public func check(_ result: Bool, _ string: String? = nil,
_ file: String = #file, _ line: Int = #line) {
if result { return }
var msg = "assertion failure (\(file):\(line))"
if let extra = string {
msg += ":\t" + extra
}
fatalError(msg)
}
// MARK: Tuples
public enum Pair<L: ~Copyable, R: ~Copyable>: ~Copyable {
case pair(L, R)
}
extension Pair: Copyable where L: Copyable, R: Copyable {}
/// MARK: Data.Maybe
public enum Maybe<Wrapped: ~Copyable>: ~Copyable {
case just(Wrapped)
case nothing
}
extension Maybe: Copyable where Wrapped: Copyable {}
extension Maybe: Show where Wrapped: Show & ~Copyable {
public borrowing func show() -> String {
switch self {
case let .just(elm):
return elm.show()
case .nothing:
return "<nothing>"
}
}
}
extension Maybe: Eq where Wrapped: Eq, Wrapped: ~Copyable {
public static func ==(_ a: borrowing Self, _ b: borrowing Self) -> Bool {
switch a {
case let .just(a1):
switch b {
case let .just(b1):
return a1 == b1
case .nothing:
return false
}
case .nothing:
switch b {
case .just:
return false
case .nothing:
return true
}
}
}
}
public func isJust<A: ~Copyable>(_ m: borrowing Maybe<A>) -> Bool {
switch m {
case .just:
return true
case .nothing:
return false
}
}
@inlinable
public func isNothing<A: ~Copyable>(_ m: borrowing Maybe<A>) -> Bool {
return !isJust(m)
}
public struct UnownedRef<Instance: AnyObject> {
@usableFromInline
internal unowned(unsafe) var _value: Instance
}
/// Provides underlying support so that you can create recursive enums, because
/// noncopyable enums do not yet support indirect cases.
public struct Box<Wrapped: ~Copyable>: ~Copyable {
private let _pointer: UnsafeMutablePointer<Wrapped>
init(_ wrapped: consuming Wrapped) {
_pointer = .allocate(capacity: 1)
_pointer.initialize(to: wrapped)
}
deinit {
_pointer.deinitialize(count: 1)
_pointer.deallocate()
}
consuming func take() -> Wrapped {
let wrapped = _pointer.move()
_pointer.deallocate()
discard self
return wrapped
}
var borrow: Wrapped {
_read { yield _pointer.pointee }
}
consuming func map(_ transform: (consuming Wrapped) -> Wrapped) -> Self {
_pointer.initialize(to: transform(_pointer.move()))
return self
}
}
/// MARK: Data.List
///
/// A singly-linked list
public enum List<Element: ~Copyable>: ~Copyable {
case cons(Element, Box<List<Element>>)
case empty
public init(_ head: consuming Element,
_ tail: consuming List<Element>) {
self = .cons(head, Box(tail))
}
public init() { self = .empty }
}
/// Pure Iteration
extension List where Element: ~Copyable {
/// Performs forward iteration through the list, accumulating a result value.
/// Returns f(xn,...,f(x2, f(x1, init))...), or `init` if the list is empty.
public borrowing func foldl<Out>(
init initial: consuming Out,
_ f: (borrowing Element, consuming Out) -> Out) -> Out
where Out: ~Copyable {
func loop(_ acc: consuming Out, _ lst: borrowing Self) -> Out {
switch lst {
case .empty:
return acc
case let .cons(elm, tail):
return loop(f(elm, acc), tail.borrow)
}
}
return loop(initial, self)
}
/// Performs reverse iteration through the list, accumulating a result value.
/// Returns f(x1, f(x2,...,f(xn, init)...)) or `init` if the list is empty.
public borrowing func foldr<Out>(
init initial: consuming Out,
_ f: (borrowing Element, consuming Out) -> Out) -> Out
where Out: ~Copyable {
switch self {
case .empty:
return initial
case let .cons(elm, tail):
return f(elm, tail.borrow.foldr(init: initial, f))
}
}
// Forward iteration without accumulating a result.
public borrowing func forEach(_ f: (borrowing Element) -> Void) -> Void {
switch self {
case .empty: return
case let .cons(elm, tail):
f(elm)
return tail.borrow.forEach(f)
}
}
}
/// Initialization
extension List where Element: ~Copyable {
// Generates a list of elements [f(0), f(1), ..., f(n-1)] from left to right.
// For n < 0, the empty list is created.
public init(length n: Int, _ f: (Int) -> Element) {
guard n > 0 else {
self = .empty
return
}
let cur = n-1
let elm = f(cur)
self = List(elm, List(length: cur, f))
}
}
/// Basic utilities
extension List where Element: ~Copyable {
/// Is this list empty?
///
/// Complexity: O(1)
public var isEmpty: Bool {
borrowing get {
switch self {
case .empty: true
case .cons(_, _): false
}
}
}
/// How many elements are in this list?
///
/// Complexity: O(n)
public borrowing func length() -> Int {
return foldl(init: 0) { $1 + 1 }
}
/// Pop the first element off the list, if present.
///
/// Complexity: O(1)
public consuming func pop() -> Optional<Pair<Element, List<Element>>> {
switch consume self {
case .empty: .none
case let .cons(elm, tail): .pair(elm, tail.take())
}
}
/// Push an element onto the front of the list.
///
/// Complexity: O(1)
public consuming func push(_ newHead: consuming Element) -> List<Element> {
return List(newHead, self)
}
/// Produces a new list that is the reverse of this list.
///
/// Complexity: O(n)
public consuming func reverse() -> List<Element> {
var new = List<Element>()
while case let .pair(head, tail) = pop() {
new = new.push(head)
self = tail
}
return new
}
}
extension List: Show where Element: Show & ~Copyable {
public borrowing func show() -> String {
return "[" + foldl(init: "]", { $0.show() + ", " + $1 })
}
}