Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7404321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:14:22+00:00 2026-05-29T05:14:22+00:00

I am working on this problem and had previously asked related question. Implementation of

  • 0

I am working on this problem and had previously asked related question. Implementation of State Monad To further refine my code i tried to implement it using only one increment function.

module StateExample where
import Control.Monad.State

data GlobState = GlobState { c1 :: Int, c2:: Int, c3:: Int} deriving (Show)

newGlobState:: GlobState
newGlobState = GlobState { c1=0,c2=0,c3=0 }

incr :: String-> State GlobState ()
incr x = do
     modify(\g -> g {x =x g + 1})

main:: IO()
main =  do
    let a1= flip execState newGlobState $ do
        incr c1
        incr c2
        incr c1
    print a

But here i am getting an error

`x' is not a (visible) constructor field name

How can i remove this error?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:14:23+00:00Added an answer on May 29, 2026 at 5:14 am

    You have hit a weakness in Haskell: records are not first class values!
    Indeed, it would be very nice to write as you have done, but it is not possible.
    However, you can use different libraries to achieve the desired effect.
    This is how it looks if you use
    fclabels:

    {-# LANGUAGE TemplateHaskell, TypeOperators #-}
    module StateExample where
    
    import Control.Monad.State hiding (modify)
    import Data.Label (mkLabels)
    import Data.Label.Pure ((:->))
    import Data.Label.PureM
    
    data GlobState = GlobState { _c1 :: Int , _c2 :: Int , _c3 :: Int } deriving Show
    $(mkLabels [''GlobState])
    
    newGlobState:: GlobState
    newGlobState = GlobState { _c1 = 0, _c2 = 0, _c3 = 0 }
    
    incr :: (GlobState :-> Int) -> State GlobState ()
    incr x = modify x (+1)
    
    main :: IO ()
    main = do
        let a = flip execState newGlobState $ do
            incr c1
            incr c2
            incr c1
        print a
    

    There are some magic parts here. We define the GlobState with the same
    record names but prependend with an underscore. Then the function
    mkLabels uses TemplateHaskell to define “lenses” for every field
    in the record. These lenses will have the same name but without the underscore. The argument (GlobState :-> Int) to incr is such a
    lens, and we can use the modify function from Data.Label.PureM
    which updates records defined this way inside the state monad. We hide
    modify from Control.Monad.State, to avoid collision.

    You can look at the other
    functions in the
    documentation for
    PureM

    for other functions usable with state monads, as
    gets and puts.

    If you do not have fclabels installed, but you have the cabal executable from the cabal-install package (which you get if you install the Haskell Platform), you can install fclabels by simply running:

     cabal install fclabels
    

    If this is the first time you run cabal, you first need to update the database:

     cabal update
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working on this problem for 2 days now and it's an
I have been working on this problem for a while now. I am trying
Very odd problem as this is working perfectly on our old Classic ASP site.
Does anyone see a problem with this, its not working saying bad file descriptor
I'm concerned that this might be working on an NP-Complete problem. I'm hoping someone
I'm working this source code: #include <string> #include <vector> #include <iostream> #include <istream> #include
i am new in programming under linux and trying to get working this code:
I have a little archer game I'm working on, and previously in my code
This question is a followup to a previous question I had about discovering unused
Eclipse can’t see my Android Device on Window.Previously I had been working fine.Suddenly I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.