typelift / Swiftx

Functional data types and functions for any project
BSD 3-Clause "New" or "Revised" License
219 stars 28 forks source link

Consider giving the Y-combinator, `fix`, two type parameters. #29

Closed DanielAsher closed 9 years ago

DanielAsher commented 9 years ago

Currently fix is defined so:

public func fix<A>(f : ((A -> A) -> A -> A)) -> A -> A {
    return { x in f(fix(f))(x) }
}

I have found the following fix more useful:

public func fix<T, U>(f: (T -> U) -> T -> U) -> T -> U {
    return { x in f(fix(f))(x) }
}

Could this act as a replacement, or addition, to the current fix?

cheers,

Daniel

CodaFi commented 9 years ago

Hm, it's an interesting proposal. What use cases have you run into where that version is more useful?

N.B. I kept fix polymorphic in only 1 type variable because fix usually looks like (A -> A) -> A, from η expansion our fix follows.

pthariensflame commented 9 years ago

I'm in favor of this proposal. It's strictly more general without changing any of the existing uses.

DanielAsher commented 9 years ago

Hi CodaFi,

thanks for the quick response. I'm trying out parser combinators, and loving it.

I was using robrix's Prelude, but Swiftz is swift-2.0 compatible, and looks to be a leader in this space.

Bit of a newbie, but you can see an example of my use case here

I'd be happy to create a PR if desired.

Will a fix that supports throws be a useful addition? I think I'll have an answer for you shortly...

CodaFi commented 9 years ago

@DanielAsher Would you be up for submitting a pull request, or shall I do it?

DanielAsher commented 9 years ago

@CodaFi I be rather excited to submit a PR - it would be my first!

I'd like to check out fix's behaviour with throws before I do though...

DanielAsher commented 9 years ago

@CodaFi @pthariensflame thanks so much for quick and insightful responses :+1: