Как я могу использовать (^? ix 0) в независимом геттере?

Извините за плохо сформулированный заголовок, но я даже не знаю, как его правильно задать.

Как я могу повернуть это?

instPublicIP :: Instance -> Maybe Text
instPublicIP inst =
  inst ^. insNetworkInterfaces ^? ix 0 . iniAssociation . _Just . iniaPublicIP . _Just

в это

instPublicIP' :: Lens' Instance (Maybe Text)
instPublicIP' = insNetworkInterfaces ^? ix 0 . iniAssociation . _Just . iniaPublicIP . _Just

Когда я пытаюсь это сделать, я получаю следующую ошибку:

Main.hs:198:3:
    Couldn't match expected type ‘(Maybe Text -> f (Maybe Text))
                                  -> Instance -> f Instance’
                with actual type ‘Maybe Text’
    Relevant bindings include
      instPublicIP' :: (Maybe Text -> f (Maybe Text))
                       -> Instance -> f Instance
        (bound at app/Main.hs:197:1)
    In the expression:
      insNetworkInterfaces
      ^? ix 0 . iniAssociation . _Just . iniaPublicIP . _Just
    In an equation for ‘instPublicIP'’:
        instPublicIP'
          = insNetworkInterfaces
            ^? ix 0 . iniAssociation . _Just . iniaPublicIP . _Just

Main.hs:198:27:
    Couldn't match type ‘InstanceNetworkInterface’
                   with ‘Instance -> f0 Instance’
    Expected type: (InstanceNetworkInterface
                    -> Const (Data.Monoid.First Text) InstanceNetworkInterface)
                   -> (([InstanceNetworkInterface] -> f0 [InstanceNetworkInterface])
                       -> Instance -> f0 Instance)
                   -> Const
                        (Data.Monoid.First Text)
                        (([InstanceNetworkInterface] -> f0 [InstanceNetworkInterface])
                         -> Instance -> f0 Instance)
      Actual type: (IxValue
                      (([InstanceNetworkInterface] -> f0 [InstanceNetworkInterface])
                       -> Instance -> f0 Instance)
                    -> Const
                         (Data.Monoid.First Text)
                         (IxValue
                            (([InstanceNetworkInterface] -> f0 [InstanceNetworkInterface])
                             -> Instance -> f0 Instance)))
                   -> (([InstanceNetworkInterface] -> f0 [InstanceNetworkInterface])
                       -> Instance -> f0 Instance)
                   -> Const
                        (Data.Monoid.First Text)
                        (([InstanceNetworkInterface] -> f0 [InstanceNetworkInterface])
                         -> Instance -> f0 Instance)
    In the first argument of ‘(.)’, namely ‘ix 0’
    In the second argument of ‘(^?)’, namely
      ‘ix 0 . iniAssociation . _Just . iniaPublicIP . _Just’

person Joe Hillenbrand    schedule 14.10.2015    source источник
comment
Попробуйте сочинять с помощью (.) вместо (^?).   -  person Rein Henrichs    schedule 14.10.2015
comment
@ReinHenrichs пробовал это. Не удалось вывести (аппликатив f), возникающий в результате использования «ix», из контекста (функтор f)   -  person Joe Hillenbrand    schedule 14.10.2015
comment
Подождите, это означает, что это Traversal! Я понял!   -  person Joe Hillenbrand    schedule 14.10.2015


Ответы (1)


Оказывается, мне просто нужно заменить ^? на . и изменить Lens' на Traversal'

instPublicIP' :: Traversal' Instance (Maybe Text)
instPublicIP' = insNetworkInterfaces . ix 0 . iniAssociation . _Just . iniaPublicIP
person Joe Hillenbrand    schedule 14.10.2015