# [Haskell-cafe] Transforming from on State-transformer to another

Olaf Klinke olf at aatal-apotheke.de
Fri Nov 22 12:59:00 UTC 2019

```> Am 21.11.2019 um 22:56 schrieb Jacques Carette <carette at mcmaster.ca>:
>
> On 2019-Nov.-21 15:57 , Olaf Klinke wrote:
>>> Is there a way to have a function of type (a -> b) -> State a c -> State
>>> b c ?
>> djinn says no. The reason ist that the a occurs both positively and negatively in State a c, that is, on the left and the right hand side of the -> in State.
>>
>>> In the particular case of interest, I am interested in is actually b ->
>>> State a c -> (State (a,b) c), which is of course a special case of the
>>> above.
>> djinn says yes. The suggested term is
>>
>> \f -> \(a,b) -> let (a,c) = f a in ((a,b),c)
>
>
> Thanks.  I tried to play with the online djinn to ask about exactly this - and failed!  How should I be asking?

I downloaded the djinn package and built it with cabal.

>
> Also, something is weird about that term, since it doesn't match "b -> State a c -> (State (a,b) c)" at all.  It does match "(b -> State a c) -> (State (a,b) c)" which is a completely different type (and not interesting...)
Sorry, I tried to simplify djinn's output and failed. The term should be

\f -> \(a,b) -> let (c,a') = f a in (c,(a',b))
:: (a -> (c, a3)) -> (a, b) -> (c, (a3, b))

where you can let a3 = a to get

State a c -> State (a,b) c

That is, it maps a transformer of state a into a transformer of state (a,b) that leaves the b component unchanged.

Olaf

```