Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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):

  • Couldn't match type 't' with 's'
  • Couldn't match type 'a' with 'b'

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?

like image 206
Guillaume Chérel Avatar asked Jan 08 '23 02:01

Guillaume Chérel


2 Answers

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.

like image 103
rampion Avatar answered Jan 12 '23 12:01

rampion


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.

like image 20
Reid Barton Avatar answered Jan 12 '23 12:01

Reid Barton