How to write fmap for a Functor that has a function in it's data type definition?Functor fmap, pattern match function values, haskellHaving trouble writing my fmapHow can a time function exist in functional programming?Deriving functor instance, not on last type argumentHow to make the function type constructor instance of functor? Duplicated instanceGHC 7.10.x migration: why can't one write “pure = return” in the instance Applicative?Can I Create a Functor Data Type That 'Looks' Like an Int?Isn't it redundant for Control.Lens.Setter to wrap types in functors?Recursion schemes using `Fix` on a data-type that's already a Functor?How to implement a instance of Functor for a self defined data in haskell`(<*>)` definition for the Applicative functor?

Is this statement about cut time correct?

Why did the Dothraki not follow Jon?

What does $!# mean in Shell scripting?

Who decides how to classify a novel?

A steel cutting sword?

My players want to grind XP but we're using milestone advancement

Time complexity of an algorithm: Is it important to state the base of the logarithm?

In the 3D Zeldas, is it faster to roll or to simply walk?

Pirate democracy at its finest

Sankey diagram: not getting the hang of it

What are the meaning and grammar of "Crying isn't like you"?

Dad jokes are fun

Does this strict reading of the rules allow both Extra Attack and the Thirsting Blade warlock invocation to be used together?

Why aren't space telescopes put in GEO?

Website returning plaintext password

Which European Languages are not Indo-European?

Is it legal to meet with potential future employers in the UK, whilst visiting from the USA

What was the idiom for something that we take without a doubt?

USPS Back Room - Trespassing?

Did 20% of US soldiers in Vietnam use heroin, 95% of whom quit afterwards?

Why didn't Thanos use the Time Stone to stop the Avengers' plan?

Best material to absorb as much light as possible

Could a 19.25mm revolver actually exist?

The art of clickbait captions



How to write fmap for a Functor that has a function in it's data type definition?


Functor fmap, pattern match function values, haskellHaving trouble writing my fmapHow can a time function exist in functional programming?Deriving functor instance, not on last type argumentHow to make the function type constructor instance of functor? Duplicated instanceGHC 7.10.x migration: why can't one write “pure = return” in the instance Applicative?Can I Create a Functor Data Type That 'Looks' Like an Int?Isn't it redundant for Control.Lens.Setter to wrap types in functors?Recursion schemes using `Fix` on a data-type that's already a Functor?How to implement a instance of Functor for a self defined data in haskell`(<*>)` definition for the Applicative functor?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I have data defined as below:



data FGM a = Pure a | GMAction Integer Integer (PlayerMsg -> FGM a)
data PlayerMsg = Guess Integer | Surrender
deriving (Eq, Show)


FGM represents a number guessing game where the 2 integers are the upper and lower bounds, a is the value to be returned in the end, and playermsg is the guess that the player makes.
I would like to make FGM a Functor, so I'm trying to define a fmap for it. Problem is I don't know how do deal with the (PlayerMsg -> FGM a) part of FGM.



From my understanding of the testcases given, for fmap g (someFGM), g should only be applied to Pure, if it's GMAction, fmap should use PlayerMsg and generate the next FGM until it reaches a Pure.



This is what I've tried:



instance Functor FGM where
fmap g (Pure a) = Pure (g a)
fmap g (GMAction upper lower thing) = GMAction upper lower (fmap g (x -> case (thing x) of
Pure a -> Pure a
GMAction u l t -> GMAction u l t))


But when I try to run test on this I get this error:



Occurs check: cannot construct the infinite type:
b ~ FGM b
Expected type: PlayerMsg -> FGM b
Actual type: PlayerMsg -> b
In the third argument of 'GMAction', namely '(fmap g (x -> case (thing x) of
Pure a -> Pure a
GMAction u l t -> GMAction u l t))'
In the expression:...


I've also tried



fmap g (GMAction upper lower thing) = GMAction upper lower (fmap g thing)


and got the same error.



I tried to search for examples with data that has a function as parameter like PlayerMsg -> FGM a, but can't find anything. this is kind of similar but didn't work for me(it's my first failed example). Can someone help me define this fmap or point me at where to learn about this?










share|improve this question



















  • 1





    Side note: the lambda with the case expression amounts to thing.

    – duplode
    Mar 24 at 2:26

















1















I have data defined as below:



data FGM a = Pure a | GMAction Integer Integer (PlayerMsg -> FGM a)
data PlayerMsg = Guess Integer | Surrender
deriving (Eq, Show)


FGM represents a number guessing game where the 2 integers are the upper and lower bounds, a is the value to be returned in the end, and playermsg is the guess that the player makes.
I would like to make FGM a Functor, so I'm trying to define a fmap for it. Problem is I don't know how do deal with the (PlayerMsg -> FGM a) part of FGM.



From my understanding of the testcases given, for fmap g (someFGM), g should only be applied to Pure, if it's GMAction, fmap should use PlayerMsg and generate the next FGM until it reaches a Pure.



This is what I've tried:



instance Functor FGM where
fmap g (Pure a) = Pure (g a)
fmap g (GMAction upper lower thing) = GMAction upper lower (fmap g (x -> case (thing x) of
Pure a -> Pure a
GMAction u l t -> GMAction u l t))


But when I try to run test on this I get this error:



Occurs check: cannot construct the infinite type:
b ~ FGM b
Expected type: PlayerMsg -> FGM b
Actual type: PlayerMsg -> b
In the third argument of 'GMAction', namely '(fmap g (x -> case (thing x) of
Pure a -> Pure a
GMAction u l t -> GMAction u l t))'
In the expression:...


I've also tried



fmap g (GMAction upper lower thing) = GMAction upper lower (fmap g thing)


and got the same error.



I tried to search for examples with data that has a function as parameter like PlayerMsg -> FGM a, but can't find anything. this is kind of similar but didn't work for me(it's my first failed example). Can someone help me define this fmap or point me at where to learn about this?










share|improve this question



















  • 1





    Side note: the lambda with the case expression amounts to thing.

    – duplode
    Mar 24 at 2:26













1












1








1








I have data defined as below:



data FGM a = Pure a | GMAction Integer Integer (PlayerMsg -> FGM a)
data PlayerMsg = Guess Integer | Surrender
deriving (Eq, Show)


FGM represents a number guessing game where the 2 integers are the upper and lower bounds, a is the value to be returned in the end, and playermsg is the guess that the player makes.
I would like to make FGM a Functor, so I'm trying to define a fmap for it. Problem is I don't know how do deal with the (PlayerMsg -> FGM a) part of FGM.



From my understanding of the testcases given, for fmap g (someFGM), g should only be applied to Pure, if it's GMAction, fmap should use PlayerMsg and generate the next FGM until it reaches a Pure.



This is what I've tried:



instance Functor FGM where
fmap g (Pure a) = Pure (g a)
fmap g (GMAction upper lower thing) = GMAction upper lower (fmap g (x -> case (thing x) of
Pure a -> Pure a
GMAction u l t -> GMAction u l t))


But when I try to run test on this I get this error:



Occurs check: cannot construct the infinite type:
b ~ FGM b
Expected type: PlayerMsg -> FGM b
Actual type: PlayerMsg -> b
In the third argument of 'GMAction', namely '(fmap g (x -> case (thing x) of
Pure a -> Pure a
GMAction u l t -> GMAction u l t))'
In the expression:...


I've also tried



fmap g (GMAction upper lower thing) = GMAction upper lower (fmap g thing)


and got the same error.



I tried to search for examples with data that has a function as parameter like PlayerMsg -> FGM a, but can't find anything. this is kind of similar but didn't work for me(it's my first failed example). Can someone help me define this fmap or point me at where to learn about this?










share|improve this question
















I have data defined as below:



data FGM a = Pure a | GMAction Integer Integer (PlayerMsg -> FGM a)
data PlayerMsg = Guess Integer | Surrender
deriving (Eq, Show)


FGM represents a number guessing game where the 2 integers are the upper and lower bounds, a is the value to be returned in the end, and playermsg is the guess that the player makes.
I would like to make FGM a Functor, so I'm trying to define a fmap for it. Problem is I don't know how do deal with the (PlayerMsg -> FGM a) part of FGM.



From my understanding of the testcases given, for fmap g (someFGM), g should only be applied to Pure, if it's GMAction, fmap should use PlayerMsg and generate the next FGM until it reaches a Pure.



This is what I've tried:



instance Functor FGM where
fmap g (Pure a) = Pure (g a)
fmap g (GMAction upper lower thing) = GMAction upper lower (fmap g (x -> case (thing x) of
Pure a -> Pure a
GMAction u l t -> GMAction u l t))


But when I try to run test on this I get this error:



Occurs check: cannot construct the infinite type:
b ~ FGM b
Expected type: PlayerMsg -> FGM b
Actual type: PlayerMsg -> b
In the third argument of 'GMAction', namely '(fmap g (x -> case (thing x) of
Pure a -> Pure a
GMAction u l t -> GMAction u l t))'
In the expression:...


I've also tried



fmap g (GMAction upper lower thing) = GMAction upper lower (fmap g thing)


and got the same error.



I tried to search for examples with data that has a function as parameter like PlayerMsg -> FGM a, but can't find anything. this is kind of similar but didn't work for me(it's my first failed example). Can someone help me define this fmap or point me at where to learn about this?







haskell






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 24 at 2:23









duplode

25.1k45296




25.1k45296










asked Mar 24 at 2:11









Ceilvia CCeilvia C

485




485







  • 1





    Side note: the lambda with the case expression amounts to thing.

    – duplode
    Mar 24 at 2:26












  • 1





    Side note: the lambda with the case expression amounts to thing.

    – duplode
    Mar 24 at 2:26







1




1





Side note: the lambda with the case expression amounts to thing.

– duplode
Mar 24 at 2:26





Side note: the lambda with the case expression amounts to thing.

– duplode
Mar 24 at 2:26












2 Answers
2






active

oldest

votes


















5














Try fmap g (GMAction upper lower thing) = GMAction upper lower ((fmap g) . thing). Note the function composition; this is necessary as you need to fmap over the result of the function.






share|improve this answer

























  • Yay it worked! Thank you! yes that's typo I will edit it

    – Ceilvia C
    Mar 24 at 2:22



















4














GHC's built-in rules for deriving Functor are enough to handle simple cases like this.



-# LANGUAGE DeriveFunctor #-
data FGM a = Pure a | GMAction Integer Integer (PlayerMsg -> FGM a) deriving Functor
data PlayerMsg = Guess Integer | Surrender
deriving (Eq, Show)





share|improve this answer























  • Really - despite my answer above - this is what I usually use as well. It's nice having the computer write code for you!

    – bradrn
    Mar 24 at 7:56











  • Good to know this built-in exist, thanks!

    – Ceilvia C
    Mar 24 at 18:13











  • @CeilviaC: You can also see the code that GHC generates from deriving clauses with the -ddump-deriv flag. (If you’re using Stack, this will generate a .dump-deriv file in your .stack-work directory.)

    – Jon Purdy
    Mar 24 at 20:35











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55320147%2fhow-to-write-fmap-for-a-functor-that-has-a-function-in-its-data-type-definition%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









5














Try fmap g (GMAction upper lower thing) = GMAction upper lower ((fmap g) . thing). Note the function composition; this is necessary as you need to fmap over the result of the function.






share|improve this answer

























  • Yay it worked! Thank you! yes that's typo I will edit it

    – Ceilvia C
    Mar 24 at 2:22
















5














Try fmap g (GMAction upper lower thing) = GMAction upper lower ((fmap g) . thing). Note the function composition; this is necessary as you need to fmap over the result of the function.






share|improve this answer

























  • Yay it worked! Thank you! yes that's typo I will edit it

    – Ceilvia C
    Mar 24 at 2:22














5












5








5







Try fmap g (GMAction upper lower thing) = GMAction upper lower ((fmap g) . thing). Note the function composition; this is necessary as you need to fmap over the result of the function.






share|improve this answer















Try fmap g (GMAction upper lower thing) = GMAction upper lower ((fmap g) . thing). Note the function composition; this is necessary as you need to fmap over the result of the function.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 24 at 2:24









duplode

25.1k45296




25.1k45296










answered Mar 24 at 2:18









bradrnbradrn

2,2331819




2,2331819












  • Yay it worked! Thank you! yes that's typo I will edit it

    – Ceilvia C
    Mar 24 at 2:22


















  • Yay it worked! Thank you! yes that's typo I will edit it

    – Ceilvia C
    Mar 24 at 2:22

















Yay it worked! Thank you! yes that's typo I will edit it

– Ceilvia C
Mar 24 at 2:22






Yay it worked! Thank you! yes that's typo I will edit it

– Ceilvia C
Mar 24 at 2:22














4














GHC's built-in rules for deriving Functor are enough to handle simple cases like this.



-# LANGUAGE DeriveFunctor #-
data FGM a = Pure a | GMAction Integer Integer (PlayerMsg -> FGM a) deriving Functor
data PlayerMsg = Guess Integer | Surrender
deriving (Eq, Show)





share|improve this answer























  • Really - despite my answer above - this is what I usually use as well. It's nice having the computer write code for you!

    – bradrn
    Mar 24 at 7:56











  • Good to know this built-in exist, thanks!

    – Ceilvia C
    Mar 24 at 18:13











  • @CeilviaC: You can also see the code that GHC generates from deriving clauses with the -ddump-deriv flag. (If you’re using Stack, this will generate a .dump-deriv file in your .stack-work directory.)

    – Jon Purdy
    Mar 24 at 20:35















4














GHC's built-in rules for deriving Functor are enough to handle simple cases like this.



-# LANGUAGE DeriveFunctor #-
data FGM a = Pure a | GMAction Integer Integer (PlayerMsg -> FGM a) deriving Functor
data PlayerMsg = Guess Integer | Surrender
deriving (Eq, Show)





share|improve this answer























  • Really - despite my answer above - this is what I usually use as well. It's nice having the computer write code for you!

    – bradrn
    Mar 24 at 7:56











  • Good to know this built-in exist, thanks!

    – Ceilvia C
    Mar 24 at 18:13











  • @CeilviaC: You can also see the code that GHC generates from deriving clauses with the -ddump-deriv flag. (If you’re using Stack, this will generate a .dump-deriv file in your .stack-work directory.)

    – Jon Purdy
    Mar 24 at 20:35













4












4








4







GHC's built-in rules for deriving Functor are enough to handle simple cases like this.



-# LANGUAGE DeriveFunctor #-
data FGM a = Pure a | GMAction Integer Integer (PlayerMsg -> FGM a) deriving Functor
data PlayerMsg = Guess Integer | Surrender
deriving (Eq, Show)





share|improve this answer













GHC's built-in rules for deriving Functor are enough to handle simple cases like this.



-# LANGUAGE DeriveFunctor #-
data FGM a = Pure a | GMAction Integer Integer (PlayerMsg -> FGM a) deriving Functor
data PlayerMsg = Guess Integer | Surrender
deriving (Eq, Show)






share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 24 at 7:27









Daniel WagnerDaniel Wagner

106k7163291




106k7163291












  • Really - despite my answer above - this is what I usually use as well. It's nice having the computer write code for you!

    – bradrn
    Mar 24 at 7:56











  • Good to know this built-in exist, thanks!

    – Ceilvia C
    Mar 24 at 18:13











  • @CeilviaC: You can also see the code that GHC generates from deriving clauses with the -ddump-deriv flag. (If you’re using Stack, this will generate a .dump-deriv file in your .stack-work directory.)

    – Jon Purdy
    Mar 24 at 20:35

















  • Really - despite my answer above - this is what I usually use as well. It's nice having the computer write code for you!

    – bradrn
    Mar 24 at 7:56











  • Good to know this built-in exist, thanks!

    – Ceilvia C
    Mar 24 at 18:13











  • @CeilviaC: You can also see the code that GHC generates from deriving clauses with the -ddump-deriv flag. (If you’re using Stack, this will generate a .dump-deriv file in your .stack-work directory.)

    – Jon Purdy
    Mar 24 at 20:35
















Really - despite my answer above - this is what I usually use as well. It's nice having the computer write code for you!

– bradrn
Mar 24 at 7:56





Really - despite my answer above - this is what I usually use as well. It's nice having the computer write code for you!

– bradrn
Mar 24 at 7:56













Good to know this built-in exist, thanks!

– Ceilvia C
Mar 24 at 18:13





Good to know this built-in exist, thanks!

– Ceilvia C
Mar 24 at 18:13













@CeilviaC: You can also see the code that GHC generates from deriving clauses with the -ddump-deriv flag. (If you’re using Stack, this will generate a .dump-deriv file in your .stack-work directory.)

– Jon Purdy
Mar 24 at 20:35





@CeilviaC: You can also see the code that GHC generates from deriving clauses with the -ddump-deriv flag. (If you’re using Stack, this will generate a .dump-deriv file in your .stack-work directory.)

– Jon Purdy
Mar 24 at 20:35

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55320147%2fhow-to-write-fmap-for-a-functor-that-has-a-function-in-its-data-type-definition%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해