2

I want to learn how to use Control.Lens package. I tried to use an official tutorial.

First of all, I haven't Control.Lens.Tutorial package. Is it important to reproduce the examples?

I started GHCi and loaded TemplateHaskell extension:

Prelude> :set -XTemplateHaskell

Then I tried to reproduce first steps of the tutorial:

Prelude> import Control.Lens hiding (element)
Prelude Control.Lens> import Control.Lens.TH
Prelude Control.Lens Control.Lens.TH> 
Prelude Control.Lens Control.Lens.TH> data Point = Point { _x :: Double, _y :: Double } deriving (Show)
Prelude Control.Lens Control.Lens.TH> data Atom = Atom { _element :: String, _point :: Point } deriving (Show)
Prelude Control.Lens Control.Lens.TH> $(makeLenses ''Point)

and got an error:

<interactive>:7:3: error:
    • Couldn't match type ‘[Language.Haskell.TH.Syntax.Dec]’
                     with ‘Language.Haskell.TH.Syntax.Exp’
      Expected type: Language.Haskell.TH.Lib.Internal.ExpQ
        Actual type: Language.Haskell.TH.Lib.Internal.DecsQ
    • In the expression: makeLenses ''Point
      In the untyped splice: $(makeLenses ''Point)

GHC 8.6.4, lens 4.19.2 installed from Cabal.

Isn't the tutorial outdated a bit? If it is, where can I read a practical introduction to lenses in Haskell, with simple usage examples and smallest possible amount of category theory?

  • 1
    `ghci` only works with template haskell that generates expressions as far as I know, not delcarations. After all originally one had to prefix definitions with `let` since it was in some sort of `do` block. – Willem Van Onsem May 11 '20 at 21:06
  • 1
    @WillemVanOnsem: with definitions in a file it looks working, thank you! – Yuriy Al. Shirokov May 11 '20 at 21:32

1 Answers1

2

  :{
  data Point = Point { _x :: Double, _y :: Double } deriving (Show)
  data Atom = Atom { _element :: String, _point :: Point } deriving (Show)
  makeLenses ''Point
  :}

Roland Puntaier
  • 3,250
  • 30
  • 35