Isomorphism lenses

后端 未结 2 1915
长情又很酷
长情又很酷 2021-02-14 07:16

I would be interested in a small example of van Laarhoven\'s isomorphism lenses, applied to a data type like data BValue = BValue { π :: Float, σ :: Float, α :: Float } de

2条回答
  •  -上瘾入骨i
    2021-02-14 07:56

    From van Laarhoven's post, the Lens type is

    data Lens a b = forall r . Lens (Iso a (b, r))
    

    So in our case a is BValue and we want to construct some leneses that pick out one or more of the elements. So for example, let's build a lens that picks out π.

    piLens :: Lens BValue Float
    

    So it's going to be a lens from a BValue to a Float (namely the first one in the triple, with label pi.)

    piLens = Lens (Iso {fw = piFwd, bw = piBwd})
    

    A lens picks out two things: a residual type r (omitted here because we don't have to explicitly specify an existential type in haskell), and an isomorphism. An isomorphism is in turn composed of a forward and a backward function.

    piFwd :: BValue ->  (Float, (Float, Float))
    piFwd (BValue {pi, sigma, alpha}) = (pi, (sigma, alpha))
    

    The forward function just isolates the component that we want. Note that my residual type here is the "rest of the value", namely a pair of the sigma and alpha floats.

    piBwd :: (Float, (Float, Float)) -> BValue
    piBwd (pi, (sigma, alpha)) = BValue { pi = pi, sigma = sigma, alpha = alpha }
    

    The backward function is analogous.

    So now we have defined a lens for manipulating the pi component of a BValue.

    The other seven lenses are similar. (7 lenses: pick out sigma and alpha, pick out all possible pairs (disregarding the order), pick out all of BValue and pick out ()).

    The one bit I'm not sure about is strictness: I am a little worried that the fw and bw functions I have written are too strict. Not sure.

    We are not done yet:

    We still need to check that piLens actually respects the lens laws. The nice thing about van Laarhoven's definition of Lens is that we only have to check the isomorphism laws; the lens laws follow via the calculation in his blog post.

    So our proof obligations are:

    1. fw piLens . bw piLens = id
    2. bw piLens . fw piLens = id

    Both proofs follow directly from the definitions of piFwd and piBwd and laws about composition.

提交回复
热议问题