Files
swift-mirror/test/ModuleInterface/Inputs/Swiftskell.swift
Kavon Farvardin bd1330715c [NCGenerics] only print ~Copyable in interface
We can't simply emit the desugared, expanded version of the requirements
because there's no way to pretty-print the type `some ~Copyable` when
the `~Copyable`'s get replaced with the absence of `Copyable`. We'd be
left with just `some _` or need to invent a new top type so we can write
`some Top`. Thus, it's best to simply reverse the expansion of default
requirements when emitting a swiftinterface file.
2023-12-12 16:40:26 -08:00

96 lines
2.0 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// --------------------------------
// A Swift × Haskell stdlib flavour
// --------------------------------
/// MARK: GHC.Show
public protocol Show: ~Copyable {
borrowing func show() -> String
}
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 {
static func /=(_ a: borrowing Self, _ b: borrowing Self) -> Bool {
return !(a == b)
}
}
public extension Eq where Self: Equatable {
static func ==(_ a: borrowing Self, _ b: borrowing Self) -> Bool {
return a == b
}
}
/// MARK: Iteration
public protocol Generator: ~Copyable {
associatedtype Element: ~Copyable
func next() -> Element
}
public struct Vector<T: ~Copyable> {
subscript(_ i: UInt) -> T {
fatalError("todo")
}
}
/// MARK: Data.Maybe
public enum Maybe<Value: ~Copyable> {
case just(Value)
case nothing
}
extension Maybe: Show {
public borrowing func show() -> String {
fatalError("need borrowing switches")
}
}
extension Maybe: Eq where Value: Eq {
public static func ==(_ a: borrowing Self, _ b: borrowing Self) -> Bool {
fatalError("need borrowing switches")
}
}
public func maybe<A: ~Copyable, B>(_ defaultVal: B,
_ fn: (consuming A) -> B)
-> (consuming Maybe<A>) -> B {
return { (_ mayb: consuming Maybe<A>) -> B in
switch consume mayb {
case .just(_):
fatalError("waiting for bugfix here")
// return fn(val)
case .nothing:
return defaultVal
}
}
}
@inlinable
public func fromMaybe<A>(_ defaultVal: A) -> (Maybe<A>) -> A {
return maybe(defaultVal, {$0})
}
public func isJust<A: ~Copyable>(_ m: borrowing Maybe<A>) -> Bool {
fatalError("need borrowing switches")
}
@inlinable
public func isNothing<A: ~Copyable>(_ m: borrowing Maybe<A>) -> Bool {
return !isJust(m)
}