Skip to content

Instantly share code, notes, and snippets.

@LightAndLight
Last active July 24, 2017 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LightAndLight/39feef47be3eeb63f829a29970f47094 to your computer and use it in GitHub Desktop.
Save LightAndLight/39feef47be3eeb63f829a29970f47094 to your computer and use it in GitHub Desktop.
bindOf
bindOf :: Monad m => Lens s t a b -> m s -> (a -> m b) -> m t
bindOf l ms f = do
s <- ms
b <- f (getConst $ l Const s)
pure $ s & l .~ b
@tonymorris
Copy link

bindOf :: forall s a m. Monad m => Lens s s a a -> m s -> (a -> m a) -> m s
bindOf l ms f = do
  s <- ms
  let typeof_s :: s
      typeof_s = s
  b <- f (s ^. l)
  let typeof_s_caret_dot_l :: a
      typeof_s_caret_dot_l = s ^. l
      typeof_f_s_caret_dot_l :: m a
      typeof_f_s_caret_dot_l = f (s ^. l)
      typeof_b :: a
      typeof_b = b
      typeof_l_dot_tilde_b :: s -> s
      typeof_l_dot_tilde_b = l .~ b
      typeof_s_amp_l_dot_tilde_b :: s
      typeof_s_amp_l_dot_tilde_b = s & l .~ b
  pure $ s & l .~ b

@tonymorris
Copy link

tonymorris commented Jul 24, 2017

bindOf :: forall s a. Lens s s a a -> ()
bindOf l =
  let y :: s -> a
      y = (^. l)
  in  ()

bindOW :: forall s a b. Lens s s a b -> ()
bindOW l =
  let y :: s -> b
      y = undefined -- (^. l)
  in  ()

@LightAndLight
Copy link
Author

Correct:

bindOf :: Monad m => Lens s t a b -> m s -> (a -> m b) -> m t
bindOf l ms f = do
  s <- ms
  b <- f (getConst $ l Const s)
  pure $ s & l .~ b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment