Swift 1.2: runtime crash with van-laarhoven lenses.

Originator:rix.rob
Number:rdar://20475584 Date Originated:08-Apr-2015 08:07 PM
Status:Open Resolved:
Product:Developer Tools Product Version:Xcode 6.3 (6D570)
Classification:Crash/Hang/Data Loss Reproducible:Always
 
Summary:
I have an implementation of van Laarhoven lenses parameterized by an fmap function for some functor. It works very well with fmap as the first argument, except that then you can’t compose them with normal function composition operators (because the lenses aren’t returning fmap). So I decided to make fmap the last argument, and function composition compiles, hooray! Only now it crashes at runtime, boo.


Steps to Reproduce:
1. Compile & run this program:

struct Identity<A> { let value: A }
struct Const<A, B> { let value: A }

func fmap<A, B>(f: A -> B)(_ identity: Identity<A>) -> Identity<B> {
	return Identity(value: f(identity.value))
}

func fmap<A, B>(f: A -> B)(_ const: Const<A, B>) -> Const<A, B> {
	return const
}

// really Const()
func _Const<A, B>(a: A) -> Const<A, B> {
	return Const(value: a)
}
func const<A, B>(a: A)(_: B) -> A {
	return a
}

// really Identity()
func _Identity<A>(a: A) -> Identity<A> {
	return Identity(value: a)
}

func getConst<A, B>(c: Const<A, B>) -> A {
	return c.value
}

func runIdentity<A>(i: Identity<A>) -> A {
	return i.value
}


func view<S, A>(lens: (A -> Const<A, S>) -> S -> ((A -> S) -> Const<A, S> -> Const<A, S>) -> Const<A, S>)(_ s: S) -> A {
	return getConst(lens(_Const)(s)(fmap))
}

func over<S, A>(lens: (A -> Identity<A>) -> S -> ((A -> S) -> Identity<A> -> Identity<S>) -> Identity<S>)(_ f: A -> A)(_ s: S) -> S {
	return runIdentity(lens({ _Identity(f($0)) })(s)(fmap))
}

func set<S, A>(lens: (A -> Identity<A>) -> S -> ((A -> S) -> Identity<A> -> Identity<S>) -> Identity<S>)(_ x: A)(_ y: S) -> S {
	return over(lens)(const(x))(y)
}

func _1<A, B, C, D>(f: A -> C)(_ x: A, _ y: B)(_ fmap: (A -> (A, B)) -> C -> D) -> D {
	return fmap({ ($0, y) })(f(x))
}

func _2<A, B, C, D>(f: B -> C)(_ x: A, _ y: B)(_ fmap: (B -> (A, B)) -> C -> D) -> D {
	return fmap({ (x, $0) })(f(y))
}


public func >>> <T, U, V> (f: T -> U, g: U -> V) -> T -> V {
	return { g(f($0)) }
}

public func <<< <T, U, V> (f: U -> V, g: T -> U) -> T -> V {
	return { f(g($0)) }
}


infix operator >>> {
	associativity right
	precedence 170
}
infix operator <<< {
	associativity right
	precedence 170
}


println(view(_1)((1, 2))) // => 1
println(over(_1)({ $0 * 4 })((1, 2))) // => (4, 2)
println(set(_1)(3)((1, 2))) // => (3, 2)

println(view(_2)("hello", 5))


Expected Results:
No crash; running.

Actual Results:
Crash.

Regression:
N/A

Notes:
N/A

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!