1
1
mirror of https://github.com/github/semantic.git synced 2024-11-29 11:02:26 +03:00

Merge branch 'master' into side-by-side-diffs

This commit is contained in:
Rob Rix 2015-10-13 13:18:05 -04:00
commit b9d96134ea
15 changed files with 105 additions and 236 deletions

3
.gitmodules vendored
View File

@ -4,3 +4,6 @@
[submodule "prototype/External/Assertions"]
path = prototype/External/Assertions
url = https://github.com/robrix/Assertions.git
[submodule "prototype/External/Stream"]
path = prototype/External/Stream
url = https://github.com/antitypical/Stream.git

View File

@ -9,9 +9,23 @@
/* Begin PBXBuildFile section */
D40B89C41BC319070078E098 /* Matrix.swift in Sources */ = {isa = PBXBuildFile; fileRef = D40B89C31BC319070078E098 /* Matrix.swift */; settings = {ASSET_TAGS = (); }; };
D40B89C81BC439000078E098 /* Assertions.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D40B89C71BC439000078E098 /* Assertions.framework */; };
D42F096B1BCCC41600B95610 /* Either.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D42F09671BCCC41600B95610 /* Either.framework */; };
D42F096C1BCCC41600B95610 /* Memo.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D42F09681BCCC41600B95610 /* Memo.framework */; };
D42F096D1BCCC41600B95610 /* Prelude.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D42F09691BCCC41600B95610 /* Prelude.framework */; };
D42F096E1BCCC41600B95610 /* Stream.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D42F096A1BCCC41600B95610 /* Stream.framework */; };
D42F096F1BCCC5C900B95610 /* Either.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D42F09671BCCC41600B95610 /* Either.framework */; };
D42F09701BCCC5C900B95610 /* Memo.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D42F09681BCCC41600B95610 /* Memo.framework */; };
D42F09711BCCC5C900B95610 /* Prelude.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D42F09691BCCC41600B95610 /* Prelude.framework */; };
D42F09721BCCC5C900B95610 /* Stream.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D42F096A1BCCC41600B95610 /* Stream.framework */; };
D42F09731BCCC5CE00B95610 /* Either.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D42F09671BCCC41600B95610 /* Either.framework */; };
D42F09741BCCC5CE00B95610 /* Memo.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D42F09681BCCC41600B95610 /* Memo.framework */; };
D42F09751BCCC5CE00B95610 /* Prelude.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D42F09691BCCC41600B95610 /* Prelude.framework */; };
D42F09761BCCC5CE00B95610 /* Stream.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D42F096A1BCCC41600B95610 /* Stream.framework */; };
D42F09771BCCC5DC00B95610 /* Either.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D42F09671BCCC41600B95610 /* Either.framework */; };
D42F09781BCCC5DC00B95610 /* Memo.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D42F09681BCCC41600B95610 /* Memo.framework */; };
D42F09791BCCC5DC00B95610 /* Prelude.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D42F09691BCCC41600B95610 /* Prelude.framework */; };
D42F097A1BCCC5DC00B95610 /* Stream.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D42F096A1BCCC41600B95610 /* Stream.framework */; };
D432D4711BA9AC0B00F3FABC /* SESTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D432D4701BA9AC0B00F3FABC /* SESTests.swift */; };
D432D4731BA9C55300F3FABC /* Stream.swift in Sources */ = {isa = PBXBuildFile; fileRef = D432D4721BA9C55300F3FABC /* Stream.swift */; };
D432D4751BA9D6A400F3FABC /* Memo.swift in Sources */ = {isa = PBXBuildFile; fileRef = D432D4741BA9D6A400F3FABC /* Memo.swift */; };
D4413FEF1BB06D4C00E3C3C1 /* Dictionary.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4413FEE1BB06D4C00E3C3C1 /* Dictionary.swift */; };
D4413FF11BB08FDC00E3C3C1 /* JSON.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4413FF01BB08FDC00E3C3C1 /* JSON.swift */; };
D45A36C91BBC667D00BE3DDE /* Category.swift in Sources */ = {isa = PBXBuildFile; fileRef = D45A36C81BBC667D00BE3DDE /* Category.swift */; settings = {ASSET_TAGS = (); }; };
@ -21,11 +35,10 @@
D49FCBC61BBF214300C5E9C3 /* Patch.swift in Sources */ = {isa = PBXBuildFile; fileRef = D49FCBC51BBF214300C5E9C3 /* Patch.swift */; settings = {ASSET_TAGS = (); }; };
D49FCBC81BBF2C4300C5E9C3 /* Algorithm.swift in Sources */ = {isa = PBXBuildFile; fileRef = D49FCBC71BBF2C4300C5E9C3 /* Algorithm.swift */; settings = {ASSET_TAGS = (); }; };
D4AAE50E1B5AE22E004E581F /* Doubt.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D4AAE4FD1B5AE22E004E581F /* Doubt.framework */; };
D4AAE5401B5AE2D0004E581F /* Array.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4AAE5351B5AE2D0004E581F /* Array.swift */; };
D4AAE5401B5AE2D0004E581F /* RangeReplaceableCollectionType.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4AAE5351B5AE2D0004E581F /* RangeReplaceableCollectionType.swift */; };
D4AAE5421B5AE2D0004E581F /* Doc.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4AAE5371B5AE2D0004E581F /* Doc.swift */; };
D4AAE5451B5AE2D0004E581F /* Operators.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4AAE53A1B5AE2D0004E581F /* Operators.swift */; };
D4AAE5471B5AE2D0004E581F /* Prelude.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4AAE53C1B5AE2D0004E581F /* Prelude.swift */; };
D4AAE5481B5AE2D0004E581F /* String.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4AAE53D1B5AE2D0004E581F /* String.swift */; };
D4AAE5471B5AE2D0004E581F /* Optional.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4AAE53C1B5AE2D0004E581F /* Optional.swift */; };
D4AAE5491B5AE2D0004E581F /* StringLiteralConvertible.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4AAE53E1B5AE2D0004E581F /* StringLiteralConvertible.swift */; };
D4AAE54A1B5AE2D0004E581F /* Syntax.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4AAE53F1B5AE2D0004E581F /* Syntax.swift */; };
D4C2B12F1BB33D050096F92A /* Doubt.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D4AAE4FD1B5AE22E004E581F /* Doubt.framework */; };
@ -81,9 +94,11 @@
/* Begin PBXFileReference section */
D40B89C31BC319070078E098 /* Matrix.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Matrix.swift; sourceTree = "<group>"; };
D40B89C71BC439000078E098 /* Assertions.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Assertions.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D42F09671BCCC41600B95610 /* Either.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Either.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D42F09681BCCC41600B95610 /* Memo.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Memo.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D42F09691BCCC41600B95610 /* Prelude.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Prelude.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D42F096A1BCCC41600B95610 /* Stream.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Stream.framework; sourceTree = BUILT_PRODUCTS_DIR; };
D432D4701BA9AC0B00F3FABC /* SESTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SESTests.swift; sourceTree = "<group>"; };
D432D4721BA9C55300F3FABC /* Stream.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Stream.swift; sourceTree = "<group>"; };
D432D4741BA9D6A400F3FABC /* Memo.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Memo.swift; sourceTree = "<group>"; };
D435B7471BB31949000902F6 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
D435B7521BB31BBC000902F6 /* BoundsCheckedArray.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BoundsCheckedArray.swift; sourceTree = "<group>"; };
D435B7581BB32117000902F6 /* doubt-swift.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "doubt-swift.app"; sourceTree = BUILT_PRODUCTS_DIR; };
@ -104,11 +119,10 @@
D4AAE5011B5AE22E004E581F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
D4AAE50D1B5AE22E004E581F /* DoubtTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = DoubtTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
D4AAE5141B5AE22E004E581F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
D4AAE5351B5AE2D0004E581F /* Array.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Array.swift; sourceTree = "<group>"; };
D4AAE5351B5AE2D0004E581F /* RangeReplaceableCollectionType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RangeReplaceableCollectionType.swift; sourceTree = "<group>"; };
D4AAE5371B5AE2D0004E581F /* Doc.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Doc.swift; sourceTree = "<group>"; };
D4AAE53A1B5AE2D0004E581F /* Operators.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Operators.swift; sourceTree = "<group>"; };
D4AAE53C1B5AE2D0004E581F /* Prelude.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Prelude.swift; sourceTree = "<group>"; };
D4AAE53D1B5AE2D0004E581F /* String.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = String.swift; sourceTree = "<group>"; };
D4AAE53C1B5AE2D0004E581F /* Optional.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Optional.swift; sourceTree = "<group>"; };
D4AAE53E1B5AE2D0004E581F /* StringLiteralConvertible.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StringLiteralConvertible.swift; sourceTree = "<group>"; };
D4AAE53F1B5AE2D0004E581F /* Syntax.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Syntax.swift; sourceTree = "<group>"; };
D4D7F3161BBB22E500AAB0C0 /* Hash.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Hash.swift; sourceTree = "<group>"; };
@ -130,6 +144,10 @@
D4C2B1321BB33D080096F92A /* SWXMLHash.framework in Frameworks */,
D4C2B1331BB33D080096F92A /* SwiftXPC.framework in Frameworks */,
D4C2B1341BB33D080096F92A /* SourceKittenFramework.framework in Frameworks */,
D42F096F1BCCC5C900B95610 /* Either.framework in Frameworks */,
D42F09701BCCC5C900B95610 /* Memo.framework in Frameworks */,
D42F09711BCCC5C900B95610 /* Prelude.framework in Frameworks */,
D42F09721BCCC5C900B95610 /* Stream.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -137,6 +155,10 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
D42F096B1BCCC41600B95610 /* Either.framework in Frameworks */,
D42F096C1BCCC41600B95610 /* Memo.framework in Frameworks */,
D42F096D1BCCC41600B95610 /* Prelude.framework in Frameworks */,
D42F096E1BCCC41600B95610 /* Stream.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -146,6 +168,10 @@
files = (
D40B89C81BC439000078E098 /* Assertions.framework in Frameworks */,
D4AAE50E1B5AE22E004E581F /* Doubt.framework in Frameworks */,
D42F09771BCCC5DC00B95610 /* Either.framework in Frameworks */,
D42F09781BCCC5DC00B95610 /* Memo.framework in Frameworks */,
D42F09791BCCC5DC00B95610 /* Prelude.framework in Frameworks */,
D42F097A1BCCC5DC00B95610 /* Stream.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -154,6 +180,10 @@
buildActionMask = 2147483647;
files = (
D4DF970D1BC5E1B40040F41F /* Doubt.framework in Frameworks */,
D42F09731BCCC5CE00B95610 /* Either.framework in Frameworks */,
D42F09741BCCC5CE00B95610 /* Memo.framework in Frameworks */,
D42F09751BCCC5CE00B95610 /* Prelude.framework in Frameworks */,
D42F09761BCCC5CE00B95610 /* Stream.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -164,6 +194,11 @@
isa = PBXGroup;
children = (
D435B7471BB31949000902F6 /* main.swift */,
D4413FE71BB055AE00E3C3C1 /* Commandant.framework */,
D4413FDA1BB0536700E3C3C1 /* Result.framework */,
D4413FD81BB0534000E3C3C1 /* SWXMLHash.framework */,
D4413FD61BB0531E00E3C3C1 /* SwiftXPC.framework */,
D4413FD41BB052ED00E3C3C1 /* SourceKittenFramework.framework */,
);
path = "doubt-swift";
sourceTree = "<group>";
@ -193,15 +228,12 @@
D4AAE4FF1B5AE22E004E581F /* Doubt */ = {
isa = PBXGroup;
children = (
D4AAE5351B5AE2D0004E581F /* Array.swift */,
D4AAE5351B5AE2D0004E581F /* RangeReplaceableCollectionType.swift */,
D4AAE5371B5AE2D0004E581F /* Doc.swift */,
D4AAE53A1B5AE2D0004E581F /* Operators.swift */,
D4AAE53C1B5AE2D0004E581F /* Prelude.swift */,
D4AAE53D1B5AE2D0004E581F /* String.swift */,
D4AAE53C1B5AE2D0004E581F /* Optional.swift */,
D4AAE53E1B5AE2D0004E581F /* StringLiteralConvertible.swift */,
D4AAE53F1B5AE2D0004E581F /* Syntax.swift */,
D432D4721BA9C55300F3FABC /* Stream.swift */,
D432D4741BA9D6A400F3FABC /* Memo.swift */,
D4413FEE1BB06D4C00E3C3C1 /* Dictionary.swift */,
D4413FF01BB08FDC00E3C3C1 /* JSON.swift */,
D40B89C31BC319070078E098 /* Matrix.swift */,
@ -222,13 +254,12 @@
D4AAE5001B5AE22E004E581F /* Supporting Files */ = {
isa = PBXGroup;
children = (
D4413FE71BB055AE00E3C3C1 /* Commandant.framework */,
D4413FDA1BB0536700E3C3C1 /* Result.framework */,
D4413FD81BB0534000E3C3C1 /* SWXMLHash.framework */,
D4413FD61BB0531E00E3C3C1 /* SwiftXPC.framework */,
D4413FD41BB052ED00E3C3C1 /* SourceKittenFramework.framework */,
D4AAE5011B5AE22E004E581F /* Info.plist */,
D4DF96F01BC54C970040F41F /* Doubt.modulemap */,
D42F09671BCCC41600B95610 /* Either.framework */,
D42F09681BCCC41600B95610 /* Memo.framework */,
D42F09691BCCC41600B95610 /* Prelude.framework */,
D42F096A1BCCC41600B95610 /* Stream.framework */,
);
name = "Supporting Files";
sourceTree = "<group>";
@ -425,23 +456,20 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
D4AAE5471B5AE2D0004E581F /* Prelude.swift in Sources */,
D4AAE5471B5AE2D0004E581F /* Optional.swift in Sources */,
D4413FEF1BB06D4C00E3C3C1 /* Dictionary.swift in Sources */,
D4AAE5481B5AE2D0004E581F /* String.swift in Sources */,
D4D7F3171BBB22E500AAB0C0 /* Hash.swift in Sources */,
D45A36C91BBC667D00BE3DDE /* Category.swift in Sources */,
D4DF96ED1BC46B630040F41F /* SES.swift in Sources */,
D4AAE5401B5AE2D0004E581F /* Array.swift in Sources */,
D4AAE5401B5AE2D0004E581F /* RangeReplaceableCollectionType.swift in Sources */,
D49FCBC21BBEF2C600C5E9C3 /* Fix.swift in Sources */,
D49FCBC41BBEF98E00C5E9C3 /* Free.swift in Sources */,
D4AAE54A1B5AE2D0004E581F /* Syntax.swift in Sources */,
D432D4731BA9C55300F3FABC /* Stream.swift in Sources */,
D49FCBC61BBF214300C5E9C3 /* Patch.swift in Sources */,
D4AAE5421B5AE2D0004E581F /* Doc.swift in Sources */,
D45A36CD1BBC75DF00BE3DDE /* Info.swift in Sources */,
D49FCBC81BBF2C4300C5E9C3 /* Algorithm.swift in Sources */,
D4DF970C1BC5DF9E0040F41F /* BoundsCheckedArray.swift in Sources */,
D432D4751BA9D6A400F3FABC /* Memo.swift in Sources */,
D40B89C41BC319070078E098 /* Matrix.swift in Sources */,
D4AAE5491B5AE2D0004E581F /* StringLiteralConvertible.swift in Sources */,
D4413FF11BB08FDC00E3C3C1 /* JSON.swift in Sources */,

View File

@ -22,4 +22,16 @@
<FileRef
location = "group:External/SourceKitten/Carthage/Checkouts/SWXMLHash/SWXMLHash.xcodeproj">
</FileRef>
<FileRef
location = "group:External/Stream/Stream.xcodeproj">
</FileRef>
<FileRef
location = "group:External/Stream/Carthage/Checkouts/Memo/Memo.xcodeproj">
</FileRef>
<FileRef
location = "group:External/Stream/Carthage/Checkouts/Either/Either.xcodeproj">
</FileRef>
<FileRef
location = "group:External/Stream/Carthage/Checkouts/Prelude/Prelude.xcodeproj">
</FileRef>
</Workspace>

View File

@ -86,11 +86,20 @@ public enum Algorithm<A, B> {
case let .Roll(.Recursive(a, b, f)):
// Recur structurally into both terms, if compatible, patching paired sub-terms. This is akin to the shape of unification, except that it computes a patched tree instead of a substitution. Its also a little like a structural zip on the pair of terms.
//
// At the moment, there are no restrictions on whether terms are compatible, and there is no structure to exploit in terms; therefore, this simplifies to copying if equal, and replacing otherwise.
return f(Term.equals(equals)(a, b)
? Diff(b)
// At the moment, there are no restrictions on whether terms are compatible.
if Term.equals(equals)(a, b) { return f(Diff(b)).evaluate(equals, recur: recur) }
switch (a.out, b.out) {
case let (.Indexed(a), .Indexed(b)) where a.count == b.count:
return f(.Indexed(zip(a, b).map(recur))).evaluate(equals, recur: recur)
case let (.Keyed(a), .Keyed(b)) where Array(a.keys) == Array(b.keys):
return f(.Keyed(Dictionary(elements: b.keys.map { ($0, recur(a[$0]!, b[$0]!)) }))).evaluate(equals, recur: recur)
default:
// This must not call `recur` with `a` and `b`, as that would infinite loop if actually recursive.
: Diff.Pure(.Replace(a, b))).evaluate(equals, recur: recur)
return f(Diff.Pure(.Replace(a, b))).evaluate(equals, recur: recur)
}
case let .Roll(.ByKey(a, b, f)):
let recur = {
@ -153,3 +162,6 @@ extension Algorithm where A: Equatable, B: FreeConvertible, B.RollType == A, B.P
return evaluate(==)
}
}
import Prelude

View File

@ -1,5 +0,0 @@
extension Array {
static func cons(head: Element, _ tail: Array) -> Array {
return [head] + tail
}
}

View File

@ -105,7 +105,7 @@ extension SequenceType where Generator.Element == Doc {
return combine(x, fold(rest.value))
}
}
return fold(Stream(sequence: self))
return fold(Stream(self))
}
public func spread() -> Doc {
@ -210,3 +210,7 @@ private func == (left: Layout, right: Layout) -> Bool {
return false
}
}
import Memo
import Stream

View File

@ -195,3 +195,6 @@ extension Free where A: CustomJSONConvertible, B: PatchConvertible, B.Info == A
return JSON { $0.patch.JSON }
}
}
import Prelude

View File

@ -46,3 +46,6 @@ private func constructRowMajor<A>(width: Int, height: Int, @noescape forEach: (I
}
return values
}
import Memo

View File

@ -1,61 +0,0 @@
public struct Memo<A>: CustomDebugStringConvertible, CustomStringConvertible {
public init(unevaluted: () -> A) {
self.init(.Unevaluated(unevaluted))
}
public init(evaluated: A) {
self.init(.Evaluated(evaluated))
}
private init(_ thunk: Thunk<A>) {
_value = MutableBox(thunk)
}
public var value: A {
return _value.value.value()
}
private var _value: MutableBox<Thunk<A>>
public func map<B>(transform: A -> B) -> Memo<B> {
return Memo<B> { transform(self.value) }
}
public func flatMap<B>(transform: A -> Memo<B>) -> Memo<B> {
return Memo<B> { transform(self.value).value }
}
public var description: String {
return String(value)
}
public var debugDescription: String {
return String(reflecting: value)
}
}
private final class MutableBox<A> {
init(_ value: A) {
self.value = value
}
var value: A
}
private enum Thunk<A> {
case Evaluated(A)
case Unevaluated(() -> A)
mutating func value() -> A {
switch self {
case let .Evaluated(a):
return a
case let .Unevaluated(f):
let a = f()
self = .Evaluated(a)
return a
}
}
}

View File

@ -1,22 +1,3 @@
func fix<T, U>(f: (T -> U) -> T -> U) -> T -> U {
return { f(fix(f))($0) }
}
func id<A>(a: A) -> A {
return a
}
func const<A, B>(a: A)(_ b: B) -> A {
return a
}
func >>> <T, U, V> (f: T -> U, g: U -> V) -> T -> V {
return { g(f($0)) }
}
extension Optional {
static func equals(param: (Wrapped, Wrapped) -> Bool)(_ left: Wrapped?, _ right: Wrapped?) -> Bool {
switch (left, right) {

View File

@ -0,0 +1,5 @@
extension RangeReplaceableCollectionType {
static func cons(head: Generator.Element, _ tail: Self) -> Self {
return [ head ] + tail
}
}

View File

@ -77,3 +77,7 @@ public func SES<A>(a: [Fix<A>], _ b: [Fix<A>], equals: (A, A) -> Bool, recur: (F
return Array(matrix[0, 0]!.value.map { diff, _ in diff })
}
import Memo
import Stream

View File

@ -1,112 +0,0 @@
public enum Stream<A>: NilLiteralConvertible, SequenceType {
case Nil
indirect case Cons(A, Memo<Stream>)
public init<S: SequenceType where S.Generator.Element == A>(sequence: S) {
self = Stream(generator: sequence.generate())
}
public init<G: GeneratorType where G.Element == A>(var generator: G) {
self = Stream { generator.next() }
}
public init(_ f: () -> A?) {
self = f().map { Stream.Cons($0, Memo { Stream(f) }) } ?? Stream.Nil
}
public static func pure(a: A) -> Stream {
return .Cons(a, Memo(evaluated: .Nil))
}
public func analysis<B>(@noescape ifCons ifCons: (A, Memo<Stream>) -> B, @noescape ifNil: () -> B) -> B {
switch self {
case let .Cons(first, rest):
return ifCons(first, rest)
case .Nil:
return ifNil()
}
}
public var uncons: (first: A, rest: Memo<Stream>)? {
return analysis(ifCons: { $0 }, ifNil: { nil })
}
public var first: A? {
return uncons?.first
}
public var rest: Memo<Stream> {
return analysis(ifCons: { $1 }, ifNil: { Memo(evaluated: .Nil) })
}
public var isEmpty: Bool {
return uncons == nil
}
public func map<B>(transform: A -> B) -> Stream<B> {
return analysis(
ifCons: { .Cons(transform($0), $1.map { $0.map(transform) }) },
ifNil: const(nil))
}
public func flatMap<B>(transform: A -> Stream<B>) -> Stream<B> {
return analysis(
ifCons: { transform($0).concat($1.map { $0.flatMap(transform) }) },
ifNil: const(nil))
}
public func concat(other: Memo<Stream>) -> Stream {
return analysis(
ifCons: { .Cons($0, $1.map { $0.concat(other.value) }) },
ifNil: { other.value })
}
public func concat(other: Stream) -> Stream {
return concat(Memo(evaluated: other))
}
public func fold<Result>(initial: Result, combine: (A, Memo<Result>) -> Result) -> Result {
return analysis(
ifCons: { combine($0, $1.map { $0.fold(initial, combine: combine) }) },
ifNil: const(initial))
}
public static func unfold<State>(state: State, _ unspool: State -> (A, State)?) -> Stream {
return unspool(state).map { value, next in .Cons(value, Memo { self.unfold(next, unspool) }) } ?? .Nil
}
public func zipWith<S: SequenceType>(sequence: S) -> Stream<(A, S.Generator.Element)> {
return Stream<(A, S.Generator.Element)>.unfold((self, Stream<S.Generator.Element>(sequence: sequence))) {
guard let (x, xs) = $0.uncons, (y, ys) = $1.uncons else { return nil }
return ((x, y), (xs.value, ys.value))
}
}
public func take(n: Int) -> Stream {
return Stream.unfold((Memo(evaluated: self), n)) { stream, n in
guard let (x, xs) = stream.value.uncons else { return nil }
return n > 0
? (x, (xs, n - 1))
: nil
}
}
public init(nilLiteral: ()) {
self = .Nil
}
public func generate() -> AnyGenerator<A> {
var current = Memo(evaluated: self)
return anyGenerator {
let next = current.value.first
current = current.value.rest
return next
}
}
}

View File

@ -1,9 +0,0 @@
extension String {
func from(offset: String.Index.Distance) -> String {
return String(characters[startIndex.advancedBy(offset, limit: endIndex)..<endIndex])
}
func to(offset: String.Index.Distance) -> String {
return String(characters[startIndex..<startIndex.advancedBy(offset, limit: endIndex)])
}
}

1
prototype/External/Stream vendored Submodule

@ -0,0 +1 @@
Subproject commit fe6f1b69296bd2cfff734f7dda786fc6743b5665