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

Injection / Bijection wanted? #25

Closed
sritchie opened this issue Jun 8, 2014 · 20 comments
Closed

Injection / Bijection wanted? #25

sritchie opened this issue Jun 8, 2014 · 20 comments

Comments

@sritchie
Copy link

sritchie commented Jun 8, 2014

Hey guys,

I'm the author of Twitter's Bijection library in Scala, and have found the Injection and Bijection typeclasses to be extremely useful. I'd love to get these, and a few other typeclasses, into swiftz. Here's some extremely early playing around:

// Playing around with the Injection typeclass.
protocol Injection {
    typealias T
    typealias U
    func apply(T) -> U
    func invert(U) -> T?
}

class IntInjection: Injection {
    func apply(i: Int) -> String {
        return String(i)
    }
    func invert(s: String) -> Int? {
        return s.toInt()
    }
}

let i = IntInjection()
i.apply(10)
i.invert("10")

Would you guys be interested in taking this pull req? I don't want to send it over if this is beyond the library scope.

One other Q:

  • Would you take pull reqs for implementations of associative datastructures like those in Algebird? Or is Swiftz meant to contain typeclasses only?
@sritchie
Copy link
Author

sritchie commented Jun 8, 2014

Another extension to Monoid I was playing around with:

// This protocol allows you to call plus directly on an instance.
protocol Addable {
    func plus(x: Self) -> Self
}

// And an example of how to implement Addable for some type.
// This feels repetitive... there's certainly some better way to write this thing.
extension Int: Addable {
    var monoid: IntMonoid { return IntMonoid() }
    func plus(x: Int) -> Int {
        return monoid.plus(self, x);
    }
}

Let me know!

@cartazio
Copy link
Contributor

cartazio commented Jun 9, 2014

@sritchie if you renamed your Injection stuff to "read and show" or "printable" and "readable" yes. They're not bijections.

@sritchie addable is just Semigroup. We have that merged in already https://github.com/maxpow4h/swiftz/blob/master/swiftz/Semigroup.swift

I'm pretty sure we've got a lot of semigroup / monoid stuff,but having decent counting structures could be cool. though it might be a good idea to be conservative on how much gets in, i'm not sure how often ios/os x applications need server class approximate counting data structures.

I assume you mean the stuff in https://github.com/twitter/algebird/tree/develop/algebird-core/src/main/scala/com/twitter/algebird ?

@mxswd
Copy link
Contributor

mxswd commented Jun 9, 2014

@sritchie Hey!

I am absolutely taking any PRs of associative data structures, like in Algebird.

As @cartazio mentioned, I have added a couple of instances semigroup and monoid. They are here. https://github.com/maxpow4h/swiftz/blob/27031fea2ce7496715e4d5345149fd8f156b24e5/swiftzTests/swiftzTests.swift#L268-L277

More useful instances would be very good! I just added those ~3 to define a Monoid type that can satisfy lots of different problems. (The technique I use in Monoid stops you from needing an Int, Int8, etc. version of everything).

As for Bijection / Injection, they would be very useful! My only ask, is if you could name them Isomorphism and Prism respectively.

Thanks! 😃

@jonsterling
Copy link

@maxpow4h I was not aware that prisms were injections?

[Sorry folks, pressed the wrong button there!]

@cartazio
Copy link
Contributor

cartazio commented Jun 9, 2014

@maxpow4h oh, those are Lens Isos? I thought it was A->String and String->A only, rather than proper Isos ala lens

@mxswd
Copy link
Contributor

mxswd commented Jun 9, 2014

@jonsterling That protocol is a Simple Prism. I.e. Prism s s a a.
But I don't think we can support polymorphic lenses any time soon ;)

@cartazio
Copy link
Contributor

cartazio commented Jun 9, 2014

ohh, yeah, they are Isos and prisms, my bad :)

@sritchie
Copy link
Author

Oh man, somehow i wasn't getting updates for this thread. Glad I checked in. Let me read up, one sec.

@sritchie
Copy link
Author

@cartazio As the author of that library, I COMPLETELY understand, and will for all time, that those examples are not Bijections. That doesn't mean that bijection isn't a useful typeclass. (The Bijection library is mostly full of injections,btw.)

@maxpow4h about the Monoid and Semigroup, I did a bit more playing around and came up with this:

https://gist.github.com/sritchie/27bdf9329027d5cdf02e#file-algebra-swift-L70

Using "Self" removes the need for the duplication in the type parameter. You also don't have to supply the monoid or semigroup instance as the first argument anymore.

Happy to make that change too and update the tests if you're interested.

@sritchie
Copy link
Author

@cartazio that's correct about those algebird data structures, I'd love to get some of those in.

@sritchie
Copy link
Author

@maxpow4h about the Injection as a Prism, I'll have to read up.

@cartazio
Copy link
Contributor

@sritchie the idea is the a-> Either a b is an injection (aka prism)
because you can't write a safe Either a b -> a

@sritchie
Copy link
Author

@cartazio yeah, I get that (the injection vs bijection thing). It's a prism vs an injection because of the extra type parameter on Either, correct?

@cartazio
Copy link
Contributor

http://hackage.haskell.org/package/lens-4.2/docs/Control-Lens-Prism.html#g:2 shows some machinery for prisms

replicating it inline

type Prism s t a b = forall p f. (Choice p, Applicative f) => p a (f b) -> p s (f t)

-- some ways to construct prisms
prism :: (b -> t) -> (s -> Either t a) -> Prism s t a b

--- a way to construct a simple prism
prism' :: (b -> s) -> (s -> Maybe a) -> Prism s s a b

@cartazio
Copy link
Contributor

so in the really simple case we could have

prism' :: (a-> s) -> (s -> Maybe a) -> Prism s s a a

where s= Either a b
might be one choice in s

(this is just a teeny piece of the awesome generality of lens (which I myself am still learning))

so a simple prism might be formed out of

foo :: a-> Either a b 
bar :: Either a b -> Maybe a

@mxswd
Copy link
Contributor

mxswd commented Jun 12, 2014

Yeah, so #34 has convinced me Control.Lens is not useful in Swift, but Data.Lens is.

So a "Prism", which would be a Function, becomes a class.

protocol Prism {
  let apply: A -> B
  let invert: B -> A?
}

exactly the same as the --Bijection-- Injection class definition.

Edited that, accidentally hit ctrl enter

@mxswd
Copy link
Contributor

mxswd commented Jun 12, 2014

We are talking about the same things, I would just prefer to use the Lens version of terminology 😃

@cartazio
Copy link
Contributor

'Bijection class definition.' dont you mean injection?

@mxswd
Copy link
Contributor

mxswd commented Jun 12, 2014

Yes.

@CodaFi
Copy link
Member

CodaFi commented Feb 6, 2015

Advancement of the Data.Lens stuff makes this issue a bit irrelevant at the moment. Closing.

@CodaFi CodaFi closed this as completed Feb 6, 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

5 participants