haskell - Generalising ($) like Control.Category generalises (.) -
i had thought generalise ($) control.category generalises (.), , i've done code @ end of post (also ideone).
in code i've created class called functionobject. class has function ($) following signature:
($) :: f b -> -> b   naturally make (->) instance of class $ continues work ordinary functions.
but allows make special functions that, example, know own inverse, example below shows.
i've concluded there's 1 of 3 possibilities:
- i'm first think of it.
 - someone else has done , i'm reinventing wheel.
 - it's bad idea.
 
option 1 seems unlikely, , searches on hayoo didn't reveal option 2, suspect option 3 likely, if explain why good.
import prelude hiding ((.), ($)) import control.category ((.), category)  class functionobject f   ($) :: f b -> -> b  infixr 0 $  instance functionobject (->)   f $ x = f x  data invertiblefunction b =     invertiblefunction (a -> b) (b -> a)  instance category invertiblefunction   (invertiblefunction f f') . (invertiblefunction g g') =     invertiblefunction (f . g) (g' . f')  instance functionobject invertiblefunction   (invertiblefunction f _) $ x = f $ x  inverse (invertiblefunction f f') = invertiblefunction f' f  add :: (num n) => n -> invertiblefunction n n add n = invertiblefunction (+n) (subtract n)  main =   print $ add 2 $ 5 -- 7   print $ inverse (add 2) $ 5 -- 3      
there 2 abstractions used things in haskell, 1 usings arrows , other applicatives. both can broken down smaller parts used in base.
if go in arrow direction , break down capabilities of arrows component pieces, you'd have separate class arrows able lift arbitrary functions arrow.
class arrowarr     arr :: (b -> c) -> b c   this opposite of arrowarr, arrows arbitrary arrow can dropped function.
class arrowfun     ($) :: b c -> (b -> c)   if split arr off of arrow left arrow categories can construct , deconstruct tuples.
class category => arrowlike     fst   :: (b, d) b     snd   :: (d, b) b     (&&&) :: b c -> b c' -> b (c,c')   if go in applicative direction copointed "applicative without pure" (which goes name apply). 
class copointed p source     copoint :: p ->  class functor f => apply f   (<.>) :: f (a -> b) -> f -> f b   when go way typically drop category functions , instead have type constructor c a representing values (including function values) constructed according set of rules.
Comments
Post a Comment