Skip to content

Instantly share code, notes, and snippets.

@fragamus
Created July 6, 2019 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fragamus/65b43876ffc202d25248d9243c9a7f38 to your computer and use it in GitHub Desktop.
Save fragamus/65b43876ffc202d25248d9243c9a7f38 to your computer and use it in GitHub Desktop.
I am using generic lens to access this algebraic data type's elements by position. It works for position 1 and fails for position 2.
putStrLn $ show $ ast ^. position @1
putStrLn $ show $ ast ^. position @2
Here's the algebraic data type:
data Module l
= Module l (Maybe (ModuleHead l)) [ModulePragma l] [ImportDecl l] [Decl l]
-- ^ an ordinary Haskell module
| XmlPage l (ModuleName l) [ModulePragma l] (XName l) [XAttr l] (Maybe (Exp l)) [Exp l]
-- ^ a module consisting of a single XML document. The ModuleName never appears in the source
-- but is needed for semantic purposes, it will be the same as the file name.
| XmlHybrid l (Maybe (ModuleHead l)) [ModulePragma l] [ImportDecl l] [Decl l]
(XName l) [XAttr l] (Maybe (Exp l)) [Exp l]
-- ^ a hybrid module combining an XML document with an ordinary module
deriving (Eq,Ord,Show,Typeable,Data,Foldable,Traversable,Functor,Generic)
Here is the error I get when accessing position 2:
• Couldn't match type ‘ModuleName SrcSpanInfo’
with ‘Maybe (ModuleHead SrcSpanInfo)’
arising from a functional dependency between:
constraint ‘generic-lens-1.1.0.0:Data.Generics.Product.Internal.GLens.GLens
(Data.Generics.Product.Positions.HasTotalPositionPSym 2)
(GHC.Generics.K1
(generic-lens-1.1.0.0:Data.Generics.Internal.Families.Has.Pos 2)
(ModuleName SrcSpanInfo))
(GHC.Generics.K1
(generic-lens-1.1.0.0:Data.Generics.Internal.Families.Has.Pos 2)
(ModuleName SrcSpanInfo))
(Maybe (ModuleHead SrcSpanInfo))
(Maybe (ModuleHead SrcSpanInfo))’
arising from a use of ‘position’
instance ‘generic-lens-1.1.0.0:Data.Generics.Product.Internal.GLens.GLens
pred (GHC.Generics.K1 r a) (GHC.Generics.K1 r b) a b’
at <no location info>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment