Guillaume Chérel
Guillaume Chérel

Reputation: 1518

Getting value with a Lens s t a b

I would like write a function which turns a function (a -> b) into a function (s -> t) with the help of a Lens s t a b (edit: I realised that this function already exists with Setter s t a b and is called over or %~, but it doesn't answer the question below of using a Lens to get a value). It seems simple, but I'm getting 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 doesn't compile. There are 2 errors, both in the second argument of ^. (namely l):

However, the following compiles:

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

Then, I realised that in the hierarchy of lens types, the arrow between Lens and Getter specifies s=t, a=b. Is there no way to use a general Lens s t a b to get a value of type a from a value of type s?

Upvotes: 4

Views: 353

Answers (2)

Reid Barton
Reid Barton

Reputation: 15009

There is a way.

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

This is also exactly the definition of (^.). I'm not sure why the type of (^.) is restricted in this way, possibly just for simplicity.

Upvotes: 4

rampion
rampion

Reputation: 89053

The problem with using ^. isn't its implementation, but its type signature. If we lift its definition, we can use it for f:

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

This is most easily understood if you expand the definition of Lens : forall (f :: * -> *). Functor f => (a -> f b) -> s -> f t.

Upvotes: 5

Related Questions