Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functor #1

Closed
mxswd opened this issue Jun 3, 2014 · 13 comments
Closed

Functor #1

mxswd opened this issue Jun 3, 2014 · 13 comments

Comments

@mxswd
Copy link
Contributor

mxswd commented Jun 3, 2014

With these 2 problems:

  1. No implicit resolution of instances.
  2. No kinds.

I propose explicitly providing FA and FB and writing lots of code.

Alternatively, in xsharpx and functional java, you just write map on everything and don't inherit.

I'm not sure what is the best approach. Ideas?

@cartazio
Copy link
Contributor

cartazio commented Jun 3, 2014

i'm not sure, but it does look like the proper generic map can be defined at the use sites

my current strawman for functor is

protocol Functor {
 typealias A
 typealias FA
 typealias B
 typealias FB
 func fmap(FA,A->B)->FB

}

based upon my reading of the spec, anything that defines the "right" generic map functor for itself (which is expressible at the data type defn site), will be construed to have this protocol. But i've not tried yet

@mxswd
Copy link
Contributor Author

mxswd commented Jun 3, 2014

Yeah, i've changed it to have all the type aliases. It will need to be that way.

@cartazio
Copy link
Contributor

cartazio commented Jun 3, 2014

theres a twist!

i'm having trouble with a toy example

protocol Functor {
 typealias A
 typealias FA
 typealias B
 typealias FB
 func fmap(FA,A->B)->FB

}


class MyArray<A>{
    init (marr : Array<A>){ container=marr }

    var container : Array<A> = []
    func fmap<B>(f: A->B, inmap:MyArray<A>)->MyArray<B>{
        return MyArray(inmap.container.map(f))
    }

}

i get the message

MyArray<A> does't conform to protocol B
in the return line

@cartazio
Copy link
Contributor

cartazio commented Jun 3, 2014

ahah! got it working


class MyArray<A>{
    init (marr : Array<A>){ container=marr }

    var container : Array<A> = []
    func fmap<B>(f: A->B, inmap:MyArray<A>)->MyArray<B>{
        let newArray : MyArray<B>(inmap.container.map(f))
        return newArray
    }

}

@tonymorris
Copy link

The test is whether you can write a function that works on all functors, without repetition.

e.g.

(<$) ::
  Functor f =>
  a
  -> f b
  -> f a
a <$ b = 
  const a <$> b

@cartazio
Copy link
Contributor

cartazio commented Jun 3, 2014

yup, thats not possible right now afaict :'(

@jonsterling
Copy link

This actually sounds a lot more harmful than not writing it; it's not just boilerplate, it's more problematic than that.

And it's still not clear that the (f b) part can be expressed at all.

Sent from my iPhone

On Jun 2, 2014, at 5:17 PM, Maxwell Swadling notifications@github.com wrote:

With these 2 problems:

No implicit resolution of instances.
No kinds.
I propose explicitly providing FA and FB and writing lots of code.

Alternatively, in xsharpx and functional java, you just write map on everything and don't inherit.

I'm not sure what is the best approach. Ideas?


Reply to this email directly or view it on GitHub.

@joshvera
Copy link
Contributor

joshvera commented Jun 3, 2014

I think our best hope is to file radars. The developers have acknowledged this and it seems like they'd be open to making these changes.

@mxswd
Copy link
Contributor Author

mxswd commented Jun 3, 2014

Absolutely, if anyone has radr numbers I can add keep track of them on the README.

@jonsterling
Copy link

Yeah... Until the problems with the language get fixed, I think we should remove Functor as well as any other thing which depends on faked higher-kinded quantification. As implemented, they are not even typesafe, and are probably more harmful than helpful.

@cobbal
Copy link
Contributor

cobbal commented Jun 8, 2014

Using unit types as associated types for ensuring the same functor, I've managed to get something working. There are some weird looking casts, but I think it's actually safe(ish, assuming convention is followed for U).

protocol Functor {
    typealias A
    typealias U
    class func fmap<FA : Functor where FA.U == U>(f : FA.A -> A, _ a : FA) -> Self
}
extension Array : Functor {
    typealias A = T
    typealias U = Array<()>
    static func fmap<FA : Functor where FA.U == U>(f : FA.A -> A, _ a : FA) -> Array {
        return (a as FA.A[]).map(f)
    }
}

I'm not quite sure if this is any better than the currently attempted ways at doing it though. Anyone smarter than me have thoughts on it?

@kastiglione kastiglione mentioned this issue Jun 9, 2014
@mxswd
Copy link
Contributor Author

mxswd commented Jun 9, 2014

My initial feedback is:

  • it looks like a good approach.
  • I'm going to try this to get lens functor definitions in terms of fmap.

I think there is a correction for fmap type:

fmap<FA : Functor where FA.U == U>(f : FA.A -> B, _ a : FA)
                                               ^ **A -> B**

@CodaFi CodaFi mentioned this issue Nov 4, 2014
@CodaFi
Copy link
Member

CodaFi commented Aug 27, 2015

Because the general case is impossible, we have settled for a middle ground of brute force and explicit typealiases with more ceremonial than literal meaning.

With the bitterness of disappointment in my tools, I will be closing this.

@CodaFi CodaFi closed this as completed Aug 27, 2015
@CodaFi CodaFi added the cantfix label Aug 27, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants