|
8 | 8 |
|
9 | 9 | import Darwin |
10 | 10 |
|
11 | | -// A HList can be thought of like a tuple, but with list-like operations on the types. |
12 | | - |
| 11 | +/// An HList can be thought of like a tuple, but with list-like operations on the types. Unlike |
| 12 | +/// tuples there is no simple construction syntax as with the `(,)` operator. But what HLists lack |
| 13 | +/// in convenience they gain in flexibility. |
| 14 | +/// |
| 15 | +/// An HList is a purely static entity. All its attributes including its length, the type of each |
| 16 | +/// element, and compatible operations on said elements exist fully at compile time. HLists, like |
| 17 | +/// regular lists, support folds, maps, and appends, only at the type rather than term level. |
13 | 18 | public protocol HList { |
14 | 19 | typealias Head |
15 | | - typealias Tail // : HList can't show Nothing is in HList, recursive defn. |
16 | | - static func isNil() -> Bool |
17 | | - static func makeNil() -> Self |
18 | | - static func makeCons(h: Head, t: Tail) -> Self |
19 | | - static func length() -> Int |
| 20 | + typealias Tail |
| 21 | + |
| 22 | + static var isNil : Bool { get } |
| 23 | + static var length : Int { get } |
20 | 24 | } |
21 | 25 |
|
22 | | -public struct HCons<H, T: HList> : HList { |
| 26 | +/// The cons HList node. |
| 27 | +public struct HCons<H, T : HList> : HList { |
23 | 28 | public typealias Head = H |
24 | 29 | public typealias Tail = T |
25 | 30 |
|
26 | | - public let head: H |
27 | | - public let tail: T |
| 31 | + public let head : H |
| 32 | + public let tail : T |
28 | 33 |
|
29 | | - public init(h: H, t: T) { |
| 34 | + public init(h : H, t : T) { |
30 | 35 | head = h |
31 | 36 | tail = t |
32 | 37 | } |
33 | 38 |
|
34 | | - public static func isNil() -> Bool { |
| 39 | + public static var isNil : Bool { |
35 | 40 | return false |
36 | 41 | } |
37 | 42 |
|
38 | | - public static func makeNil() -> HCons<H, T> { |
39 | | - return undefined() // impossible |
40 | | - } |
41 | | - |
42 | | - public static func makeCons(h: Head, t: Tail) -> HCons<H, T> { |
43 | | - return HCons<H, T>(h: h, t: t) |
44 | | - } |
45 | | - |
46 | | - public static func length() -> Int { |
47 | | - return (1 + Tail.length()) |
| 43 | + public static var length : Int { |
| 44 | + return (1 + Tail.length) |
48 | 45 | } |
49 | 46 | } |
50 | 47 |
|
| 48 | +/// The Nil HList node. |
51 | 49 | public struct HNil : HList { |
52 | 50 | public typealias Head = Nothing |
53 | 51 | public typealias Tail = Nothing |
54 | 52 |
|
55 | 53 | public init() {} |
56 | 54 |
|
57 | | - public static func isNil() -> Bool { |
| 55 | + public static var isNil : Bool { |
58 | 56 | return true |
59 | 57 | } |
60 | 58 |
|
61 | | - public static func makeNil() -> HNil { |
62 | | - return HNil() |
| 59 | + public static var length : Int { |
| 60 | + return 0 |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/// `HAppend` is a type-level append of two `HList`s. They are instantiated with the type of the |
| 65 | +/// first list (XS), the type of the second list (YS) and the type of the result (XYS). When |
| 66 | +/// constructed, `HAppend` provides a safe append operation that yields the appropriate HList for |
| 67 | +/// the given types. |
| 68 | +public struct HAppend<XS, YS, XYS> { |
| 69 | + public let append : (XS, YS) -> XYS |
| 70 | + |
| 71 | + private init(_ append : (XS, YS) -> XYS) { |
| 72 | + self.append = append |
63 | 73 | } |
64 | 74 |
|
65 | | - public static func makeCons(h: Head, t: Tail) -> HNil { |
66 | | - return undefined() // impossible |
| 75 | + /// Creates an HAppend that appends Nil to a List. |
| 76 | + public static func makeAppend<L : HList>() -> HAppend<HNil, L, L> { |
| 77 | + return HAppend<HNil, L, L> { (_, l) in return l } |
67 | 78 | } |
68 | 79 |
|
69 | | - public static func length() -> Int { |
70 | | - return 0 |
| 80 | + /// Creates an HAppend that appends two non-HNil HLists. |
| 81 | + public static func makeAppend<T, A : HList, B : HList, C : HList>(h : HAppend<A, B, C>) -> HAppend<HCons<T, A>, B, HCons<T, C>> { |
| 82 | + return HAppend<HCons<T, A>, B, HCons<T, C>> { (c, l) in |
| 83 | + return HCons(h: c.head, t: h.append(c.tail, l)) |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +/// `HMap` is a type-level map of a function (F) over an `HList`. An `HMap` must, at the very least, |
| 89 | +/// takes values of its input type (A) to values of its output type (R). The function parameter (F) |
| 90 | +/// does not necessarily have to be a function, and can be used as an index for extra information |
| 91 | +/// that the map function may need in its computation. |
| 92 | +public struct HMap<F, A, R> { |
| 93 | + public let map : (F, A) -> R |
| 94 | + |
| 95 | + public init(_ map : (F, A) -> R) { |
| 96 | + self.map = map |
| 97 | + } |
| 98 | + |
| 99 | + /// Returns an `HMap` that leaves all elements in the HList unchanged. |
| 100 | + public static func identity<T>() -> HMap<(), T, T> { |
| 101 | + return HMap<(), T, T> { (_, x) in |
| 102 | + return x |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /// Returns an `HMap` that applies a function to the elements of an HList. |
| 107 | + public static func apply<T, U>() -> HMap<T -> U, T, U> { |
| 108 | + return HMap<T -> U, T, U> { (f, x) in |
| 109 | + return f(x) |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + /// Returns an `HMap` that composes two functions, then applies the new function to elements of |
| 114 | + /// an `HList`. |
| 115 | + public static func compose<X, Y, Z>() -> HMap<(), (X -> Y, Y -> Z), X -> Z> { |
| 116 | + return HMap<(), (X -> Y, Y -> Z), X -> Z> { (_, fs) in |
| 117 | + return fs.1 • fs.0 |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + /// Returns an `HMap` that creates an `HCons` node out of a tuple of the head and tail of an `HList`. |
| 122 | + public static func hcons<H, T : HList>() -> HMap<(), (H, T), HCons<H, T>> { |
| 123 | + return HMap<(), (H, T), HCons<H, T>> { (_, p) in |
| 124 | + return HCons(h: p.0, t: p.1) |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /// Returns an `HMap` that uses an `HAppend` operation to append two `HList`s together. |
| 129 | + public static func happend<A, B, C>() -> HMap<HAppend<A, B, C>, (A, B), C> { |
| 130 | + return HMap<HAppend<A, B, C>, (A, B), C> { (f, p) in |
| 131 | + return f.append(p.0, p.1) |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +/// `HFold` is a type-level right fold over the values in an `HList`. Like an `HMap`, an HFold |
| 137 | +/// carries a context (of type G). The actual fold takes values of type V and an accumulator A to |
| 138 | +/// values of type R. |
| 139 | +/// |
| 140 | +/// Using an `HFold` necessitates defining the type of its starting and ending data. For example, a |
| 141 | +/// fold that reduces `HCons<Int -> Int, HCons<Int -> Int, HCons<Int -> Int, HNil>>>` to `Int -> Int` |
| 142 | +/// through composition will define two `typealias`es: |
| 143 | +/// |
| 144 | +/// typealias FList = HCons<Int -> Int, HCons<Int -> Int, HCons<Int -> Int, HNil>>> |
| 145 | +/// |
| 146 | +/// typealias FBegin = HFold<(), Int -> Int, FList, Int -> Int> |
| 147 | +/// typealias FEnd = HFold<(), Int -> Int, HNil, Int -> Int> |
| 148 | +/// |
| 149 | +/// The fold above doesn't depend on a context, and carries values of type `Int -> Int`, contained |
| 150 | +/// in a list of type `FList`, to an `HNil` node and an ending value of type `Int -> Int`. |
| 151 | +public struct HFold<G, V, A, R> { |
| 152 | + public let fold : (G, V, A) -> R |
| 153 | + |
| 154 | + private init(fold : (G, V, A) -> R) { |
| 155 | + self.fold = fold |
| 156 | + } |
| 157 | + |
| 158 | + /// Creates an `HFold` object that folds a function over an `HNil` node. |
| 159 | + /// |
| 160 | + /// This operation returns the starting value of the fold. |
| 161 | + public static func makeFold<G, V>() -> HFold<G, V, HNil, V> { |
| 162 | + return HFold<G, V, HNil, V> { (f, v, n) in |
| 163 | + return v |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + /// Creates an `HFold` object that folds a function over an `HCons` node. |
| 168 | + public static func makeFold<H, G, V, T : HList, R, RR>(p : HMap<G, (H, R), RR>, h : HFold<G, V, T, R>) -> HFold<G, V, HCons<H, T>, RR> { |
| 169 | + return HFold<G, V, HCons<H, T>, RR> { (f, v, c) in |
| 170 | + return p.map(f, (c.head, h.fold(f, v, c.tail))) |
| 171 | + } |
71 | 172 | } |
72 | 173 | } |
73 | 174 |
|
74 | | -// TODO: map and reverse |
| 175 | +/// Uncomment if Swift decides to allow tuple patterns. rdar://20989362 |
| 176 | +///// HCons<HCons<...>> Matcher (Induction Step): If we've hit this overloading, we should have a cons |
| 177 | +///// node, or at least something that matches HCons<HNil> |
| 178 | +//public func ~=<H : HList where H.Head : Equatable, H.Tail : HList, H.Tail.Head : Equatable, H.Tail.Tail : HList>(pattern : (H.Head, H.Tail), predicate : H) -> Bool { |
| 179 | +// if H.isNil { |
| 180 | +// return false |
| 181 | +// } |
| 182 | +// |
| 183 | +// if let p = (predicate as? HCons<H.Head, H.Tail>), let ps = (p.tail as? HCons<H.Tail.Head, H.Tail.Tail>), let pt = (pattern.1 as? HCons<H.Tail.Head, H.Tail.Tail>) { |
| 184 | +// return (p.head == predicate.0) && ((ps.head, ps.tail) ~= pt) |
| 185 | +// } else if let p = (predicate as? HCons<H.Head, H.Tail>), let ps = (p.tail as? HNil) { |
| 186 | +// return (p.head == pattern.0) |
| 187 | +// } |
| 188 | +// return error("Pattern match on HList expected HCons<HSCons<...>> or HCons<HNil> but got neither.") |
| 189 | +//} |
| 190 | +// |
| 191 | +///// HCons<HNil> or HNil Matcher |
| 192 | +//public func ~=<H : HList where H.Head : Equatable, H.Tail : HList>(pattern : (H.Head, H.Tail), predicate : H) -> Bool { |
| 193 | +// if H.isNil { |
| 194 | +// return false |
| 195 | +// } |
| 196 | +// if let p = (predicate as? HCons<H.Head, H.Tail>) { |
| 197 | +// return (p.head == pattern.0) |
| 198 | +// } else if let p = (predicate as? HNil) { |
| 199 | +// return false |
| 200 | +// } |
| 201 | +// return error("Pattern match on HList expected HCons<HNil> or HNil but got neither.") |
| 202 | +//} |
| 203 | +// |
| 204 | +///// HNil matcher. |
| 205 | +//public func ~=<H : HList>(pattern : (), predicate : H) -> Bool { |
| 206 | +// return H.isNil |
| 207 | +//} |
0 commit comments