Show Constraint type in haskell










0















I am trying to use show function to print to the console value of zer or one, but I can not do it. Here is my code:



-# LANGUAGE NoMonomorphismRestriction #-

import Control.Arrow
import Data.List
import qualified Data.Map as M
import Data.Function

class Eq a => Bits a where
zer :: a
one :: a

instance Bits Int where
zer = 0
one = 1

instance Bits Bool where
zer = False
one = True

instance Bits Char where
zer = '0'
one = '1'


I am trying to use function show to convert zer or one to the string.
So I tried it:



k = zer
show k


but I got this error



<interactive>:10:1: error:
• Ambiguous type variable ‘a0’ arising from a use of ‘show’
prevents the constraint ‘(Show a0)’ from being solved.
Probable fix: use a type annotation to specify what ‘a0’ should be.
These potential instances exist:
instance (Show k, Show a) => Show (M.Map k a)
-- Defined in ‘containers-0.5.7.1:Data.Map.Base’
instance Show Ordering -- Defined in ‘GHC.Show’
instance Show Integer -- Defined in ‘GHC.Show’
...plus 24 others
...plus 11 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the expression: show zer
In an equation for ‘it’: it = show zer


so i tried to create instance for show. So I added this to my code:



instance (Show a) => Show (Bits a) where
show zer = "0"
show one = "1"


But I got another error



main.hs:25:28: error:
• Expected a type, but ‘Bits a’ has kind ‘Constraint’
• In the first argument of ‘Show’, namely ‘Bits a’
In the instance declaration for ‘Show (Bits a)’


Can you tell me what I am doing wrong?










share|improve this question



















  • 1





    What String do you expect to get out of show k?

    – Daniel Wagner
    Nov 15 '18 at 1:01
















0















I am trying to use show function to print to the console value of zer or one, but I can not do it. Here is my code:



-# LANGUAGE NoMonomorphismRestriction #-

import Control.Arrow
import Data.List
import qualified Data.Map as M
import Data.Function

class Eq a => Bits a where
zer :: a
one :: a

instance Bits Int where
zer = 0
one = 1

instance Bits Bool where
zer = False
one = True

instance Bits Char where
zer = '0'
one = '1'


I am trying to use function show to convert zer or one to the string.
So I tried it:



k = zer
show k


but I got this error



<interactive>:10:1: error:
• Ambiguous type variable ‘a0’ arising from a use of ‘show’
prevents the constraint ‘(Show a0)’ from being solved.
Probable fix: use a type annotation to specify what ‘a0’ should be.
These potential instances exist:
instance (Show k, Show a) => Show (M.Map k a)
-- Defined in ‘containers-0.5.7.1:Data.Map.Base’
instance Show Ordering -- Defined in ‘GHC.Show’
instance Show Integer -- Defined in ‘GHC.Show’
...plus 24 others
...plus 11 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the expression: show zer
In an equation for ‘it’: it = show zer


so i tried to create instance for show. So I added this to my code:



instance (Show a) => Show (Bits a) where
show zer = "0"
show one = "1"


But I got another error



main.hs:25:28: error:
• Expected a type, but ‘Bits a’ has kind ‘Constraint’
• In the first argument of ‘Show’, namely ‘Bits a’
In the instance declaration for ‘Show (Bits a)’


Can you tell me what I am doing wrong?










share|improve this question



















  • 1





    What String do you expect to get out of show k?

    – Daniel Wagner
    Nov 15 '18 at 1:01














0












0








0








I am trying to use show function to print to the console value of zer or one, but I can not do it. Here is my code:



-# LANGUAGE NoMonomorphismRestriction #-

import Control.Arrow
import Data.List
import qualified Data.Map as M
import Data.Function

class Eq a => Bits a where
zer :: a
one :: a

instance Bits Int where
zer = 0
one = 1

instance Bits Bool where
zer = False
one = True

instance Bits Char where
zer = '0'
one = '1'


I am trying to use function show to convert zer or one to the string.
So I tried it:



k = zer
show k


but I got this error



<interactive>:10:1: error:
• Ambiguous type variable ‘a0’ arising from a use of ‘show’
prevents the constraint ‘(Show a0)’ from being solved.
Probable fix: use a type annotation to specify what ‘a0’ should be.
These potential instances exist:
instance (Show k, Show a) => Show (M.Map k a)
-- Defined in ‘containers-0.5.7.1:Data.Map.Base’
instance Show Ordering -- Defined in ‘GHC.Show’
instance Show Integer -- Defined in ‘GHC.Show’
...plus 24 others
...plus 11 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the expression: show zer
In an equation for ‘it’: it = show zer


so i tried to create instance for show. So I added this to my code:



instance (Show a) => Show (Bits a) where
show zer = "0"
show one = "1"


But I got another error



main.hs:25:28: error:
• Expected a type, but ‘Bits a’ has kind ‘Constraint’
• In the first argument of ‘Show’, namely ‘Bits a’
In the instance declaration for ‘Show (Bits a)’


Can you tell me what I am doing wrong?










share|improve this question
















I am trying to use show function to print to the console value of zer or one, but I can not do it. Here is my code:



-# LANGUAGE NoMonomorphismRestriction #-

import Control.Arrow
import Data.List
import qualified Data.Map as M
import Data.Function

class Eq a => Bits a where
zer :: a
one :: a

instance Bits Int where
zer = 0
one = 1

instance Bits Bool where
zer = False
one = True

instance Bits Char where
zer = '0'
one = '1'


I am trying to use function show to convert zer or one to the string.
So I tried it:



k = zer
show k


but I got this error



<interactive>:10:1: error:
• Ambiguous type variable ‘a0’ arising from a use of ‘show’
prevents the constraint ‘(Show a0)’ from being solved.
Probable fix: use a type annotation to specify what ‘a0’ should be.
These potential instances exist:
instance (Show k, Show a) => Show (M.Map k a)
-- Defined in ‘containers-0.5.7.1:Data.Map.Base’
instance Show Ordering -- Defined in ‘GHC.Show’
instance Show Integer -- Defined in ‘GHC.Show’
...plus 24 others
...plus 11 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
• In the expression: show zer
In an equation for ‘it’: it = show zer


so i tried to create instance for show. So I added this to my code:



instance (Show a) => Show (Bits a) where
show zer = "0"
show one = "1"


But I got another error



main.hs:25:28: error:
• Expected a type, but ‘Bits a’ has kind ‘Constraint’
• In the first argument of ‘Show’, namely ‘Bits a’
In the instance declaration for ‘Show (Bits a)’


Can you tell me what I am doing wrong?







haskell functional-programming constraints bit






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 0:39







lukas kiss

















asked Nov 15 '18 at 0:31









lukas kisslukas kiss

1179




1179







  • 1





    What String do you expect to get out of show k?

    – Daniel Wagner
    Nov 15 '18 at 1:01













  • 1





    What String do you expect to get out of show k?

    – Daniel Wagner
    Nov 15 '18 at 1:01








1




1





What String do you expect to get out of show k?

– Daniel Wagner
Nov 15 '18 at 1:01






What String do you expect to get out of show k?

– Daniel Wagner
Nov 15 '18 at 1:01













1 Answer
1






active

oldest

votes


















1














You're trying to make a class an instance of a class, rather than making a type an instance of a class. Compare:



Show a => Show (Bits a) -- Invalid


to



Show a => Show (Maybe a) -- Valid


where Maybe is a datatype whereas Bits is a class name.




I don't think it's possible to express "anything that has a Bits instance has a Show instance", because it can lead to overlapping instances: if you could define something like that, then when you use show :: Int -> String the compiler wouldn't know whether to use the Prelude's instance of Show Int or the show that would be defined by Int being an instance of Bits.



A messy workaround could be to enforce "the other direction": that every instance of Bits must be an instance of Show, which would allow you to use a's Show instance rather than your own one:



class (Show a, Eq a) => Bits a where
zer :: a
one :: a

main = print (zer :: Int)


although this requires an explicit type signature to resolve the ambiguity in the type of zer at the call site.






share|improve this answer

























  • Thanks man, now I am trying to calculate length [zer, one, zer], but I get Ambiguous type variable ‘a0’ arising from a use of ‘zer’. I thought that it should work normally as calculating length of [undefined, undefined, undefined].

    – lukas kiss
    Nov 15 '18 at 10:44











  • @lukaskiss Nope, any use of zer or one will need enough type constraints that the compiler can infer precisely what type is used: here you'll need eg. [zer :: Int, one, zer] or [zer, one, zer] :: [Int]. The type of undefined isn't ambiguous, it's well defined as a, which is why that example works: the type of [zer, one, zer] is Bits a => [a], where a is ambiguous. New problems should be asked as new separate questions though.

    – hnefatl
    Nov 15 '18 at 10:54











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%2f53310800%2fshow-constraint-type-in-haskell%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









1














You're trying to make a class an instance of a class, rather than making a type an instance of a class. Compare:



Show a => Show (Bits a) -- Invalid


to



Show a => Show (Maybe a) -- Valid


where Maybe is a datatype whereas Bits is a class name.




I don't think it's possible to express "anything that has a Bits instance has a Show instance", because it can lead to overlapping instances: if you could define something like that, then when you use show :: Int -> String the compiler wouldn't know whether to use the Prelude's instance of Show Int or the show that would be defined by Int being an instance of Bits.



A messy workaround could be to enforce "the other direction": that every instance of Bits must be an instance of Show, which would allow you to use a's Show instance rather than your own one:



class (Show a, Eq a) => Bits a where
zer :: a
one :: a

main = print (zer :: Int)


although this requires an explicit type signature to resolve the ambiguity in the type of zer at the call site.






share|improve this answer

























  • Thanks man, now I am trying to calculate length [zer, one, zer], but I get Ambiguous type variable ‘a0’ arising from a use of ‘zer’. I thought that it should work normally as calculating length of [undefined, undefined, undefined].

    – lukas kiss
    Nov 15 '18 at 10:44











  • @lukaskiss Nope, any use of zer or one will need enough type constraints that the compiler can infer precisely what type is used: here you'll need eg. [zer :: Int, one, zer] or [zer, one, zer] :: [Int]. The type of undefined isn't ambiguous, it's well defined as a, which is why that example works: the type of [zer, one, zer] is Bits a => [a], where a is ambiguous. New problems should be asked as new separate questions though.

    – hnefatl
    Nov 15 '18 at 10:54
















1














You're trying to make a class an instance of a class, rather than making a type an instance of a class. Compare:



Show a => Show (Bits a) -- Invalid


to



Show a => Show (Maybe a) -- Valid


where Maybe is a datatype whereas Bits is a class name.




I don't think it's possible to express "anything that has a Bits instance has a Show instance", because it can lead to overlapping instances: if you could define something like that, then when you use show :: Int -> String the compiler wouldn't know whether to use the Prelude's instance of Show Int or the show that would be defined by Int being an instance of Bits.



A messy workaround could be to enforce "the other direction": that every instance of Bits must be an instance of Show, which would allow you to use a's Show instance rather than your own one:



class (Show a, Eq a) => Bits a where
zer :: a
one :: a

main = print (zer :: Int)


although this requires an explicit type signature to resolve the ambiguity in the type of zer at the call site.






share|improve this answer

























  • Thanks man, now I am trying to calculate length [zer, one, zer], but I get Ambiguous type variable ‘a0’ arising from a use of ‘zer’. I thought that it should work normally as calculating length of [undefined, undefined, undefined].

    – lukas kiss
    Nov 15 '18 at 10:44











  • @lukaskiss Nope, any use of zer or one will need enough type constraints that the compiler can infer precisely what type is used: here you'll need eg. [zer :: Int, one, zer] or [zer, one, zer] :: [Int]. The type of undefined isn't ambiguous, it's well defined as a, which is why that example works: the type of [zer, one, zer] is Bits a => [a], where a is ambiguous. New problems should be asked as new separate questions though.

    – hnefatl
    Nov 15 '18 at 10:54














1












1








1







You're trying to make a class an instance of a class, rather than making a type an instance of a class. Compare:



Show a => Show (Bits a) -- Invalid


to



Show a => Show (Maybe a) -- Valid


where Maybe is a datatype whereas Bits is a class name.




I don't think it's possible to express "anything that has a Bits instance has a Show instance", because it can lead to overlapping instances: if you could define something like that, then when you use show :: Int -> String the compiler wouldn't know whether to use the Prelude's instance of Show Int or the show that would be defined by Int being an instance of Bits.



A messy workaround could be to enforce "the other direction": that every instance of Bits must be an instance of Show, which would allow you to use a's Show instance rather than your own one:



class (Show a, Eq a) => Bits a where
zer :: a
one :: a

main = print (zer :: Int)


although this requires an explicit type signature to resolve the ambiguity in the type of zer at the call site.






share|improve this answer















You're trying to make a class an instance of a class, rather than making a type an instance of a class. Compare:



Show a => Show (Bits a) -- Invalid


to



Show a => Show (Maybe a) -- Valid


where Maybe is a datatype whereas Bits is a class name.




I don't think it's possible to express "anything that has a Bits instance has a Show instance", because it can lead to overlapping instances: if you could define something like that, then when you use show :: Int -> String the compiler wouldn't know whether to use the Prelude's instance of Show Int or the show that would be defined by Int being an instance of Bits.



A messy workaround could be to enforce "the other direction": that every instance of Bits must be an instance of Show, which would allow you to use a's Show instance rather than your own one:



class (Show a, Eq a) => Bits a where
zer :: a
one :: a

main = print (zer :: Int)


although this requires an explicit type signature to resolve the ambiguity in the type of zer at the call site.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 1:03

























answered Nov 15 '18 at 0:49









hnefatlhnefatl

4,50421731




4,50421731












  • Thanks man, now I am trying to calculate length [zer, one, zer], but I get Ambiguous type variable ‘a0’ arising from a use of ‘zer’. I thought that it should work normally as calculating length of [undefined, undefined, undefined].

    – lukas kiss
    Nov 15 '18 at 10:44











  • @lukaskiss Nope, any use of zer or one will need enough type constraints that the compiler can infer precisely what type is used: here you'll need eg. [zer :: Int, one, zer] or [zer, one, zer] :: [Int]. The type of undefined isn't ambiguous, it's well defined as a, which is why that example works: the type of [zer, one, zer] is Bits a => [a], where a is ambiguous. New problems should be asked as new separate questions though.

    – hnefatl
    Nov 15 '18 at 10:54


















  • Thanks man, now I am trying to calculate length [zer, one, zer], but I get Ambiguous type variable ‘a0’ arising from a use of ‘zer’. I thought that it should work normally as calculating length of [undefined, undefined, undefined].

    – lukas kiss
    Nov 15 '18 at 10:44











  • @lukaskiss Nope, any use of zer or one will need enough type constraints that the compiler can infer precisely what type is used: here you'll need eg. [zer :: Int, one, zer] or [zer, one, zer] :: [Int]. The type of undefined isn't ambiguous, it's well defined as a, which is why that example works: the type of [zer, one, zer] is Bits a => [a], where a is ambiguous. New problems should be asked as new separate questions though.

    – hnefatl
    Nov 15 '18 at 10:54

















Thanks man, now I am trying to calculate length [zer, one, zer], but I get Ambiguous type variable ‘a0’ arising from a use of ‘zer’. I thought that it should work normally as calculating length of [undefined, undefined, undefined].

– lukas kiss
Nov 15 '18 at 10:44





Thanks man, now I am trying to calculate length [zer, one, zer], but I get Ambiguous type variable ‘a0’ arising from a use of ‘zer’. I thought that it should work normally as calculating length of [undefined, undefined, undefined].

– lukas kiss
Nov 15 '18 at 10:44













@lukaskiss Nope, any use of zer or one will need enough type constraints that the compiler can infer precisely what type is used: here you'll need eg. [zer :: Int, one, zer] or [zer, one, zer] :: [Int]. The type of undefined isn't ambiguous, it's well defined as a, which is why that example works: the type of [zer, one, zer] is Bits a => [a], where a is ambiguous. New problems should be asked as new separate questions though.

– hnefatl
Nov 15 '18 at 10:54






@lukaskiss Nope, any use of zer or one will need enough type constraints that the compiler can infer precisely what type is used: here you'll need eg. [zer :: Int, one, zer] or [zer, one, zer] :: [Int]. The type of undefined isn't ambiguous, it's well defined as a, which is why that example works: the type of [zer, one, zer] is Bits a => [a], where a is ambiguous. New problems should be asked as new separate questions though.

– hnefatl
Nov 15 '18 at 10:54




















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%2f53310800%2fshow-constraint-type-in-haskell%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

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo