Making instance of ApplicativeOrphaned instances in HaskellEq instance has some strange comparisonsWhy can I not make String an instance of a typeclass?Good examples of Not a Functor/Functor/Applicative/Monad?Confused by the meaning of the 'Alternative' type class and its relationship to other type classesinstance Alternative ZipList in Haskell?How to test the homomorphism law of an Applicative instance?Partially applying a type in a type constructorInstance applicative on datatype `List`Why is this applicative instance unlawful?
Can SQL Server create collisions in system generated constraint names?
How did Captain America manage to do this?
Does a large simulator bay have standard public address announcements?
Phrase for the opposite of "foolproof"
Is there a way to generate a list of distinct numbers such that no two subsets ever have an equal sum?
Is Diceware more secure than a long passphrase?
What's the name of these pliers?
How to write a column outside the braces in a matrix?
How exactly does Hawking radiation decrease the mass of black holes?
Is the claim "Employers won't employ people with no 'social media presence'" realistic?
How to limit Drive Letters Windows assigns to new removable USB drives
Extension of 2-adic valuation to the real numbers
Dynamic SOQL query relationship with field visibility for Users
How to denote matrix elements succinctly?
What are the characteristics of a typeless programming language?
What happened to Captain America in Endgame?
Aliens crash on Earth and go into stasis to wait for technology to fix their ship
Why does nature favour the Laplacian?
Two field separators (colon and space) in awk
Map of water taps to fill bottles
I preordered a game on my Xbox while on the home screen of my friend's account. Which of us owns the game?
What does ゆーか mean?
How can Republicans who favour free markets, consistently express anger when they don't like the outcome of that choice?
Is there really no use for MD5 anymore?
Making instance of Applicative
Orphaned instances in HaskellEq instance has some strange comparisonsWhy can I not make String an instance of a typeclass?Good examples of Not a Functor/Functor/Applicative/Monad?Confused by the meaning of the 'Alternative' type class and its relationship to other type classesinstance Alternative ZipList in Haskell?How to test the homomorphism law of an Applicative instance?Partially applying a type in a type constructorInstance applicative on datatype `List`Why is this applicative instance unlawful?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Still not a hundred percent shure how to make instances of the more complex types. Have this:
data CouldBe a = Is a | Lost deriving (Show, Ord)
Made an instance of Functor
, using Maybe
as an example:
instance Functor CouldBe where
fmap f (Is x) = Is (f x)
fmap f Lost = Lost
For doing something like this:
tupleCouldBe :: CouldBe a -> CouldBe b -> CouldBe (a,b)
tupleCouldBe x y = (,) <$> x <*> y
CouldBe
needs to be an instance of Applicative
, but how would you go about that? Sure I can look it up and copy it, but I want to learn the process behind it and finally end up with the instance
declaration of CouldBe.
haskell typeclass applicative
|
show 1 more comment
Still not a hundred percent shure how to make instances of the more complex types. Have this:
data CouldBe a = Is a | Lost deriving (Show, Ord)
Made an instance of Functor
, using Maybe
as an example:
instance Functor CouldBe where
fmap f (Is x) = Is (f x)
fmap f Lost = Lost
For doing something like this:
tupleCouldBe :: CouldBe a -> CouldBe b -> CouldBe (a,b)
tupleCouldBe x y = (,) <$> x <*> y
CouldBe
needs to be an instance of Applicative
, but how would you go about that? Sure I can look it up and copy it, but I want to learn the process behind it and finally end up with the instance
declaration of CouldBe.
haskell typeclass applicative
2
Start by writing down the functions you need to define to makeCouldBe
an instance ofApplicative
, with their type signatures (specialised toCouldBe
). You should find there's really only one sensible solution if you let the types guide you. And yourFunctor
instance shows you how to handleLost
.
– Robin Zigmond
Mar 9 at 9:15
4
Pattern match as much as possible and then construct the results in the only way that type checks. This works often.
– luqui
Mar 9 at 9:17
2
Logically I'd be definingpure
and<*>
, correct? I think pure should be simple likepure a = Is a
, but doesn't<*>
require more variants...
– Madderote
Mar 9 at 9:28
1
Try pattern matching on each side of<*>
; this should give four different cases. Then use @RobinZigmond's advice and 'follow the types' for each case. One other thing which could help is typed holes: if you don't know what to put somewhere, use an underscore_
, and GHC will give you the type which should be there. They're incredibly useful in these sort of 'follow the types' situations.
– bradrn
Mar 9 at 9:44
1
Note that yourCouldBe
is isomorphic to Haskell's built-inMaybe
type, so any time you want to know how to write an instance for your type, you can just look how the corresponding instance forMaybe
is written.
– Joseph Sible
Mar 9 at 16:43
|
show 1 more comment
Still not a hundred percent shure how to make instances of the more complex types. Have this:
data CouldBe a = Is a | Lost deriving (Show, Ord)
Made an instance of Functor
, using Maybe
as an example:
instance Functor CouldBe where
fmap f (Is x) = Is (f x)
fmap f Lost = Lost
For doing something like this:
tupleCouldBe :: CouldBe a -> CouldBe b -> CouldBe (a,b)
tupleCouldBe x y = (,) <$> x <*> y
CouldBe
needs to be an instance of Applicative
, but how would you go about that? Sure I can look it up and copy it, but I want to learn the process behind it and finally end up with the instance
declaration of CouldBe.
haskell typeclass applicative
Still not a hundred percent shure how to make instances of the more complex types. Have this:
data CouldBe a = Is a | Lost deriving (Show, Ord)
Made an instance of Functor
, using Maybe
as an example:
instance Functor CouldBe where
fmap f (Is x) = Is (f x)
fmap f Lost = Lost
For doing something like this:
tupleCouldBe :: CouldBe a -> CouldBe b -> CouldBe (a,b)
tupleCouldBe x y = (,) <$> x <*> y
CouldBe
needs to be an instance of Applicative
, but how would you go about that? Sure I can look it up and copy it, but I want to learn the process behind it and finally end up with the instance
declaration of CouldBe.
haskell typeclass applicative
haskell typeclass applicative
asked Mar 9 at 9:06
MadderoteMadderote
350211
350211
2
Start by writing down the functions you need to define to makeCouldBe
an instance ofApplicative
, with their type signatures (specialised toCouldBe
). You should find there's really only one sensible solution if you let the types guide you. And yourFunctor
instance shows you how to handleLost
.
– Robin Zigmond
Mar 9 at 9:15
4
Pattern match as much as possible and then construct the results in the only way that type checks. This works often.
– luqui
Mar 9 at 9:17
2
Logically I'd be definingpure
and<*>
, correct? I think pure should be simple likepure a = Is a
, but doesn't<*>
require more variants...
– Madderote
Mar 9 at 9:28
1
Try pattern matching on each side of<*>
; this should give four different cases. Then use @RobinZigmond's advice and 'follow the types' for each case. One other thing which could help is typed holes: if you don't know what to put somewhere, use an underscore_
, and GHC will give you the type which should be there. They're incredibly useful in these sort of 'follow the types' situations.
– bradrn
Mar 9 at 9:44
1
Note that yourCouldBe
is isomorphic to Haskell's built-inMaybe
type, so any time you want to know how to write an instance for your type, you can just look how the corresponding instance forMaybe
is written.
– Joseph Sible
Mar 9 at 16:43
|
show 1 more comment
2
Start by writing down the functions you need to define to makeCouldBe
an instance ofApplicative
, with their type signatures (specialised toCouldBe
). You should find there's really only one sensible solution if you let the types guide you. And yourFunctor
instance shows you how to handleLost
.
– Robin Zigmond
Mar 9 at 9:15
4
Pattern match as much as possible and then construct the results in the only way that type checks. This works often.
– luqui
Mar 9 at 9:17
2
Logically I'd be definingpure
and<*>
, correct? I think pure should be simple likepure a = Is a
, but doesn't<*>
require more variants...
– Madderote
Mar 9 at 9:28
1
Try pattern matching on each side of<*>
; this should give four different cases. Then use @RobinZigmond's advice and 'follow the types' for each case. One other thing which could help is typed holes: if you don't know what to put somewhere, use an underscore_
, and GHC will give you the type which should be there. They're incredibly useful in these sort of 'follow the types' situations.
– bradrn
Mar 9 at 9:44
1
Note that yourCouldBe
is isomorphic to Haskell's built-inMaybe
type, so any time you want to know how to write an instance for your type, you can just look how the corresponding instance forMaybe
is written.
– Joseph Sible
Mar 9 at 16:43
2
2
Start by writing down the functions you need to define to make
CouldBe
an instance of Applicative
, with their type signatures (specialised to CouldBe
). You should find there's really only one sensible solution if you let the types guide you. And your Functor
instance shows you how to handle Lost
.– Robin Zigmond
Mar 9 at 9:15
Start by writing down the functions you need to define to make
CouldBe
an instance of Applicative
, with their type signatures (specialised to CouldBe
). You should find there's really only one sensible solution if you let the types guide you. And your Functor
instance shows you how to handle Lost
.– Robin Zigmond
Mar 9 at 9:15
4
4
Pattern match as much as possible and then construct the results in the only way that type checks. This works often.
– luqui
Mar 9 at 9:17
Pattern match as much as possible and then construct the results in the only way that type checks. This works often.
– luqui
Mar 9 at 9:17
2
2
Logically I'd be defining
pure
and <*>
, correct? I think pure should be simple like pure a = Is a
, but doesn't <*>
require more variants...– Madderote
Mar 9 at 9:28
Logically I'd be defining
pure
and <*>
, correct? I think pure should be simple like pure a = Is a
, but doesn't <*>
require more variants...– Madderote
Mar 9 at 9:28
1
1
Try pattern matching on each side of
<*>
; this should give four different cases. Then use @RobinZigmond's advice and 'follow the types' for each case. One other thing which could help is typed holes: if you don't know what to put somewhere, use an underscore _
, and GHC will give you the type which should be there. They're incredibly useful in these sort of 'follow the types' situations.– bradrn
Mar 9 at 9:44
Try pattern matching on each side of
<*>
; this should give four different cases. Then use @RobinZigmond's advice and 'follow the types' for each case. One other thing which could help is typed holes: if you don't know what to put somewhere, use an underscore _
, and GHC will give you the type which should be there. They're incredibly useful in these sort of 'follow the types' situations.– bradrn
Mar 9 at 9:44
1
1
Note that your
CouldBe
is isomorphic to Haskell's built-in Maybe
type, so any time you want to know how to write an instance for your type, you can just look how the corresponding instance for Maybe
is written.– Joseph Sible
Mar 9 at 16:43
Note that your
CouldBe
is isomorphic to Haskell's built-in Maybe
type, so any time you want to know how to write an instance for your type, you can just look how the corresponding instance for Maybe
is written.– Joseph Sible
Mar 9 at 16:43
|
show 1 more comment
1 Answer
1
active
oldest
votes
You just write it out, following the types:
instance Applicative CouldBe where
-
Minimal complete definition:
pure, ((<*>)
pure a = fa
where
fa = ....
liftA2 abc fa fb = fc
where
fc = ....
According to
data CouldBe a = Is a | Lost
our toolset is
Is :: a -> CouldBe a
Lost :: CouldBe a
but we can also use pattern matching, e.g.
couldBe is lost (Is a) = is a
couldBe is lost (Lost) = lost
couldBe :: ? -> ? -> CouldBe a -> b
couldBe :: ? -> b -> CouldBe a -> b
couldBe :: (a -> b) -> b -> CouldBe a -> b
So,
-- pure :: a -> f a
pure :: a -> CouldBe a
matches up with
Is :: a -> CouldBe a
so we define
pure a = Is a
Then, for liftA2
, we follow the data cases:
-- liftA2 :: (a -> b -> c) -> f a -> f b -> f c
-- liftA2 :: (a -> b -> c) -> CouldBe a -> CouldBe b -> CouldBe c
liftA2 abc Lost _ = ...
liftA2 abc _ Lost = ...
liftA2 abc (Is a) (Is b) = fc
where
c = abc a b
fc = .... -- create an `f c` from `c`:
-- do we have a `c -> CouldBe c` ?
-- do we have an `a -> CouldBe a` ? (it's the same type)
But in the first two cases we don't have an a
or a b
; so we have to come up with a CouldBe c
out of nothing. We do have this tool in our toolset as well.
Having completed all the missing pieces, we can substitute the expressions directly into the definitions, eliminating all the unneeded interim values / variables.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55075707%2fmaking-instance-of-applicative%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You just write it out, following the types:
instance Applicative CouldBe where
-
Minimal complete definition:
pure, ((<*>)
pure a = fa
where
fa = ....
liftA2 abc fa fb = fc
where
fc = ....
According to
data CouldBe a = Is a | Lost
our toolset is
Is :: a -> CouldBe a
Lost :: CouldBe a
but we can also use pattern matching, e.g.
couldBe is lost (Is a) = is a
couldBe is lost (Lost) = lost
couldBe :: ? -> ? -> CouldBe a -> b
couldBe :: ? -> b -> CouldBe a -> b
couldBe :: (a -> b) -> b -> CouldBe a -> b
So,
-- pure :: a -> f a
pure :: a -> CouldBe a
matches up with
Is :: a -> CouldBe a
so we define
pure a = Is a
Then, for liftA2
, we follow the data cases:
-- liftA2 :: (a -> b -> c) -> f a -> f b -> f c
-- liftA2 :: (a -> b -> c) -> CouldBe a -> CouldBe b -> CouldBe c
liftA2 abc Lost _ = ...
liftA2 abc _ Lost = ...
liftA2 abc (Is a) (Is b) = fc
where
c = abc a b
fc = .... -- create an `f c` from `c`:
-- do we have a `c -> CouldBe c` ?
-- do we have an `a -> CouldBe a` ? (it's the same type)
But in the first two cases we don't have an a
or a b
; so we have to come up with a CouldBe c
out of nothing. We do have this tool in our toolset as well.
Having completed all the missing pieces, we can substitute the expressions directly into the definitions, eliminating all the unneeded interim values / variables.
add a comment |
You just write it out, following the types:
instance Applicative CouldBe where
-
Minimal complete definition:
pure, ((<*>)
pure a = fa
where
fa = ....
liftA2 abc fa fb = fc
where
fc = ....
According to
data CouldBe a = Is a | Lost
our toolset is
Is :: a -> CouldBe a
Lost :: CouldBe a
but we can also use pattern matching, e.g.
couldBe is lost (Is a) = is a
couldBe is lost (Lost) = lost
couldBe :: ? -> ? -> CouldBe a -> b
couldBe :: ? -> b -> CouldBe a -> b
couldBe :: (a -> b) -> b -> CouldBe a -> b
So,
-- pure :: a -> f a
pure :: a -> CouldBe a
matches up with
Is :: a -> CouldBe a
so we define
pure a = Is a
Then, for liftA2
, we follow the data cases:
-- liftA2 :: (a -> b -> c) -> f a -> f b -> f c
-- liftA2 :: (a -> b -> c) -> CouldBe a -> CouldBe b -> CouldBe c
liftA2 abc Lost _ = ...
liftA2 abc _ Lost = ...
liftA2 abc (Is a) (Is b) = fc
where
c = abc a b
fc = .... -- create an `f c` from `c`:
-- do we have a `c -> CouldBe c` ?
-- do we have an `a -> CouldBe a` ? (it's the same type)
But in the first two cases we don't have an a
or a b
; so we have to come up with a CouldBe c
out of nothing. We do have this tool in our toolset as well.
Having completed all the missing pieces, we can substitute the expressions directly into the definitions, eliminating all the unneeded interim values / variables.
add a comment |
You just write it out, following the types:
instance Applicative CouldBe where
-
Minimal complete definition:
pure, ((<*>)
pure a = fa
where
fa = ....
liftA2 abc fa fb = fc
where
fc = ....
According to
data CouldBe a = Is a | Lost
our toolset is
Is :: a -> CouldBe a
Lost :: CouldBe a
but we can also use pattern matching, e.g.
couldBe is lost (Is a) = is a
couldBe is lost (Lost) = lost
couldBe :: ? -> ? -> CouldBe a -> b
couldBe :: ? -> b -> CouldBe a -> b
couldBe :: (a -> b) -> b -> CouldBe a -> b
So,
-- pure :: a -> f a
pure :: a -> CouldBe a
matches up with
Is :: a -> CouldBe a
so we define
pure a = Is a
Then, for liftA2
, we follow the data cases:
-- liftA2 :: (a -> b -> c) -> f a -> f b -> f c
-- liftA2 :: (a -> b -> c) -> CouldBe a -> CouldBe b -> CouldBe c
liftA2 abc Lost _ = ...
liftA2 abc _ Lost = ...
liftA2 abc (Is a) (Is b) = fc
where
c = abc a b
fc = .... -- create an `f c` from `c`:
-- do we have a `c -> CouldBe c` ?
-- do we have an `a -> CouldBe a` ? (it's the same type)
But in the first two cases we don't have an a
or a b
; so we have to come up with a CouldBe c
out of nothing. We do have this tool in our toolset as well.
Having completed all the missing pieces, we can substitute the expressions directly into the definitions, eliminating all the unneeded interim values / variables.
You just write it out, following the types:
instance Applicative CouldBe where
-
Minimal complete definition:
pure, ((<*>)
pure a = fa
where
fa = ....
liftA2 abc fa fb = fc
where
fc = ....
According to
data CouldBe a = Is a | Lost
our toolset is
Is :: a -> CouldBe a
Lost :: CouldBe a
but we can also use pattern matching, e.g.
couldBe is lost (Is a) = is a
couldBe is lost (Lost) = lost
couldBe :: ? -> ? -> CouldBe a -> b
couldBe :: ? -> b -> CouldBe a -> b
couldBe :: (a -> b) -> b -> CouldBe a -> b
So,
-- pure :: a -> f a
pure :: a -> CouldBe a
matches up with
Is :: a -> CouldBe a
so we define
pure a = Is a
Then, for liftA2
, we follow the data cases:
-- liftA2 :: (a -> b -> c) -> f a -> f b -> f c
-- liftA2 :: (a -> b -> c) -> CouldBe a -> CouldBe b -> CouldBe c
liftA2 abc Lost _ = ...
liftA2 abc _ Lost = ...
liftA2 abc (Is a) (Is b) = fc
where
c = abc a b
fc = .... -- create an `f c` from `c`:
-- do we have a `c -> CouldBe c` ?
-- do we have an `a -> CouldBe a` ? (it's the same type)
But in the first two cases we don't have an a
or a b
; so we have to come up with a CouldBe c
out of nothing. We do have this tool in our toolset as well.
Having completed all the missing pieces, we can substitute the expressions directly into the definitions, eliminating all the unneeded interim values / variables.
edited Mar 11 at 10:46
answered Mar 9 at 10:39
Will NessWill Ness
47k469127
47k469127
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55075707%2fmaking-instance-of-applicative%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
2
Start by writing down the functions you need to define to make
CouldBe
an instance ofApplicative
, with their type signatures (specialised toCouldBe
). You should find there's really only one sensible solution if you let the types guide you. And yourFunctor
instance shows you how to handleLost
.– Robin Zigmond
Mar 9 at 9:15
4
Pattern match as much as possible and then construct the results in the only way that type checks. This works often.
– luqui
Mar 9 at 9:17
2
Logically I'd be defining
pure
and<*>
, correct? I think pure should be simple likepure a = Is a
, but doesn't<*>
require more variants...– Madderote
Mar 9 at 9:28
1
Try pattern matching on each side of
<*>
; this should give four different cases. Then use @RobinZigmond's advice and 'follow the types' for each case. One other thing which could help is typed holes: if you don't know what to put somewhere, use an underscore_
, and GHC will give you the type which should be there. They're incredibly useful in these sort of 'follow the types' situations.– bradrn
Mar 9 at 9:44
1
Note that your
CouldBe
is isomorphic to Haskell's built-inMaybe
type, so any time you want to know how to write an instance for your type, you can just look how the corresponding instance forMaybe
is written.– Joseph Sible
Mar 9 at 16:43