Abbie's Haskell compiler
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
745 B

  1. {-# LANGUAGE RankNTypes #-}
  2. module Ahc.Data.Lens where
  3. import Control.Applicative
  4. import Data.Functor.Identity
  5. type Lens s t a b = forall f. Functor f => (a -> f b) -> s -> f t
  6. type Lens' s a = Lens s s a a
  7. get :: Lens s t a b -> s -> a
  8. get l = getConst . l Const
  9. over :: Lens s t a b -> (a -> b) -> s -> t
  10. over l m = runIdentity . l (Identity . m)
  11. set :: Lens s t a b -> b -> s -> t
  12. set l b a = over l (const b) a
  13. (.~) :: Lens s t a b -> b -> s -> t
  14. (.~) = set
  15. infixr 4 .~
  16. (%~) :: Lens s t a b -> (a -> b) -> s -> t
  17. (%~) = over
  18. infixr 4 %~
  19. (^.) :: s -> Lens s t a b -> a
  20. x ^. l = get l x
  21. (&) :: t1 -> (t1 -> t2) -> t2
  22. x & f = f x
  23. infixr 0 &
  24. lens :: (s -> a) -> (s -> b -> t) -> Lens s t a b
  25. lens sa sbt afb s = sbt s <$> afb (sa s)