michaelt / text-pipes

Text pipes
BSD 3-Clause "New" or "Revised" License
10 stars 13 forks source link

Generalize types of lenses and isos #12

Open Gabriella439 opened 10 years ago

Gabriella439 commented 10 years ago

As you probably know, these lenses and isos actually have more general types using four type parameters instead of two. Would it be possible to generalize these all to the four type parameter versions? I often find myself wanting the more general types.

An example of this is a "head" utility for streaming text. I'd like to write this:

over (utf8 . lines) (takes 10)

... but this only type-checks if you use the more general type for those two lenses.

I plan on implementing a similar change for pipes-bytestring.

Gabriella439 commented 10 years ago

I've updated pipes-bytestring to show what I mean, and you can find the example of the updated lines function here:

https://github.com/Gabriel439/Haskell-Pipes-ByteString-Library/blob/master/src/Pipes/ByteString.hs#L970

I can write up the changes to pipes-text for you, but I just wanted to see if you were okay with this change first.

michaelt commented 10 years ago

There will be more cleanup needed, but with the changes I still can't get

over (utf8 . lines) (takes 10)

to typecheck:

>>> :t over (utf8 . lines)
over (utf8 . lines)
  :: Monad m =>
     (FreeT (Producer Text m) m (Producer ByteString m x)
      -> FreeT (Producer Text m) m (Producer ByteString m y))
     -> Producer ByteString m x -> Producer ByteString m y

>>> :t takes 10
takes 10 :: (Functor f, Monad m) => FreeT f m () -> FreeT f m ()

>>> :t over (utf8 . lines) (takes 10)

<interactive>:1:22:
    Couldn't match type ‘()’ with ‘Pipes.Proxy X () () ByteString m x’
    Expected type: FreeT (Producer Text m) m (Producer ByteString m x)
                   -> FreeT (Producer Text m) m (Producer ByteString m y)
      Actual type: FreeT (Producer Text m) m ()
                   -> FreeT (Producer Text m) m ()
    In the second argument of ‘over’, namely ‘(takes 10)’
    In the expression: over (utf8 . lines) (takes 10)
Gabriella439 commented 10 years ago

Oh yeah, you're right. It doesn't solve that particular problem. I feel sheepish. -.-

Then put this on hold until I come up with a more compelling use case for this.