Obtaining value using stab lens

I would like to write a function that turns a function (a β†’ b) into a function (s β†’ t) using Lens stab (edit: I realized that this function already exists with Setter s t a band is called overor %~, but it does not answer the question below using lens to get the value) . It seems simple, but I get a confusing type error. To make an even more minimal example, consider the following function, which simply returns the value extracted from the second argument by the lens:

f :: Lens s t a b -> s -> a
f l s = s ^. l

This does not compile. There are 2 errors, as in the second argument ^. (namely, l):

  • Failed to match type 't' with 's'
  • Failed to match type 'a' with 'b'

However, the following compilations:

f :: Getter s a -> s -> a
f l s = s ^. l

, Getter s = t, a = b. Lens s t a b a s?

+4
2

^. , . , f:

f :: Lens s t a b -> s -> a 
f l s = getConst $ l Const s

, Lens: forall (f :: * -> *). Functor f => (a -> f b) -> s -> f t.

+5

.

f :: Lens s t a b -> s -> a
f l s = getConst (l Const s)

(^.). , (^.) , , .

+4

Source: https://habr.com/ru/post/1617750/


All Articles