No instance for ariving from use of 'next' - haskell

I noticed that a function I wrote is now not working, despite using it successfully on other occasions.
My test file (made one just to test this issue) looks like this:
import System.Random
generator = next . snd
This results in the error
No instance for (RandomGen g0) arising from a use of ‘next’
The type variable ‘g0’ is ambiguous
Relevant bindings include
generator :: (a, g0) -> (Int, g0) (bound at Test.hs:2:1)
Note: there is a potential instance available:
instance RandomGen StdGen -- Defined in ‘System.Random’
In the first argument of ‘(.)’, namely ‘next’
In the expression: next . snd
In an equation for ‘generator’: generator = next . snd
The weird thing is that if I open ghci and type:
import System.Random
let generator = next . snd
Everything works just fine. What exactly am I missing?
Edit: Also tried this and it works just fine:
generator something = next (snd something)

this is because of the Monomorphism Restriction
It's a technical issue (please explore the link if you are interested in the details) and normally you'll never see because you add signatures or write down the arguments (not point-free style) in your modules - and in GHCi it's disabled - you got a bit unlucky here.
for newer versions of GHC this is enabled by default for compiled modules but disabled for GHCi (so it will use defaults as epsilonhalbe told you)
To get the same behavior you can run
:set -XMonomorphismRestriction
in GHCi

I think you need to add a type signature, ghci is using defaults therefore this does not happen there

Related

Why do I get 'variable not in scope' for Just ord <*> Nothing?

I'm learning Haskell from a popular book.
It includes the following ghci command:
ghci> Just ord <*> Nothing
Nothing
When I run this in ghci I get:
<interactive>:1:6: error:
• Variable not in scope: ord :: a0 -> b
• Perhaps you meant one of these:
‘or’ (imported from Prelude), ‘odd’ (imported from Prelude)
I think there is a typo, either due to an author mistake or the version of Haskell changing the syntax.
My question is: Why do I get variable not in scope for Just ord <*> Nothing?
A quick search for "ord" on Hoogle reveals that it lives in the Data.Char module. (I have no idea whether it was always there, or whether it only recently got moved there.) So you just need to import Data.Char into your ghci session.
ghci> import Data.Char
ghci> Just ord <*> Nothing
Nothing

how to see the implemented code of a function of a module in haskell?

I am a newbie to Haskell and I know The Haskell standard library is split into modules, each of them contains functions and types that are somehow related and serve some common purpose. I would like to see the implementation(code) of those library functions.where can I see that ? is there any command in ghci so that I can see the implementation or provide me any resources to learn about modules.
Thank you
Probably the most convenient way to do it, is use Hackage. You can for instance inspect the map function, by clicking Source on the right side of the function signature. This then will show the highlighted code fragment. For instance:
map :: (a -> b) -> [a] -> [b]
{-# NOINLINE [0] map #-}
-- We want the RULEs "map" and "map/coerce" to fire first.
-- map is recursive, so won't inline anyway,
-- but saying so is more explicit, and silences warnings
map _ [] = []
map f (x:xs) = f x : map f xs
You can also use Hoogle to search functions by name or signature, and by clicking the results, you will be redirected to the relevant hackage page.

How to approach compilation errors with internal Text modules?

I have this problem:
Couldn't match expected type ‘case-insensitive-1.2.0.5:Data.CaseInsensitive.Internal.CI
Text’
with actual type ‘Text’
In the first argument of ‘named’, namely ‘n’
because:
Prelude Text.XML.Lens> :t named
named
:: Applicative f =>
case-insensitive-1.2.0.5:Data.CaseInsensitive.Internal.CI
Data.Text.Internal.Text
-> (Element -> f Element) -> Element -> f Element
My code imports Data.Text and relies on OverloadedStrings. What steps should I take to resolve issues like this? What are short term and long term fixes?
Thanks to commenters as I was able to find out why I'm having this problem.
Short Answer: Pay close attention to types and make sure you understand them (read documentation of any module you see in error message).
Long Answer:
Notice that the type contains spaces and GHC may break them down to multiple lines. CI.Text is means something different than CI Text.
‘case-insensitive-1.2.0.5:Data.CaseInsensitive.Internal.CI
Text’
Text here is not a type synonym of re-exported internal Text (common practice in libraries). CI is a type constructor and you cannot import it (for a reason - you have to stop and read documentation of anything you touch). You will understand why you can import CI type and smart-constructors like mk instead.

Printing Text.Pandoc.writers into ghci : No Show instance

What am I trying to do ?
Print under ghci the association list of formats and writers.
See doc :
writers :: [(String, Writer)]
Association list of formats and writers.
What has been tried
zurgl>>>import Text.Pandoc as P
zurgl>>>P.writers
<interactive>:20:1:
No instance for (Show (WriterOptions -> Pandoc -> [Char]))
arising from a use of `print'
Possible fix:
add an instance declaration for
(Show (WriterOptions -> Pandoc -> [Char]))
In a stmt of an interactive GHCi command: print it
I expected the corresponding show instance to be imported automatically, but it seems that's not the case. And I must admit I don't have any clue how to define an instance declaration for (Show (WriterOptions -> Pandoc -> [Char]). As a workaround, I've tried to import additional module of the Pandoc library, but still no Show instance available.
Then Should I define this instance by myself ?
If yes, have you any tips to share with me to complete this task.
If I shouldn't what's the issue ?
Thanks in advance for your help.
EDIT
Ok, I guess I saw my missunderstanding :
Doing :
zurgl>>>map (\x-> fst x) P.writers
["native","json","html","html5","html+lhs","html5+lhs","s5","slidy","slideous","dzslides","docbook","opendocument","latex","latex+lhs","beamer","beamer+lhs","context","texinfo","man","markdown","markdown+lhs","plain","rst","rst+lhs","mediawiki","textile","rtf","org","asciidoc"]
I think it make no sens to try to Show the second stuff in my tuples. It souhld be something like a function then we can't show it.
I guess it should be the problem.
What I tried to do make no sense as the tuple contain two different type.
The first one being an identifier (of type string) for a specific writer, the second one being the writer itself (then a function). For sure, if I try to print all of them it will fail as there is no Show instance for function.
Then to retrieve the list of available writer in Pandoc (with the aims to call the corresponding function dynamically), we just have to retrieve the list of identifier, as :
zurgl>>>map fst P.writers
["native","json","html","html5","html+lhs","html5+lhs","s5","slidy","slideous","dzslides","docbook","opendocument","latex","latex+lhs","beamer","beamer+lhs","context","texinfo","man","markdown","markdown+lhs","plain","rst","rst+lhs","mediawiki","textile","rtf","org","asciidoc"]

Transforming a function to point-free style changes its type

I'm beginning Haskell... I tried to write the following trivial function in two different ways, letting Haskell decide the types, and the type system does something different in each case. What is the explanation for that behavior?
Prelude> let f x = 2 * x
Prelude> let g = (2*)
Prelude> :info f
f :: Num a => a -> a -- Defined at <interactive>:1:5
Prelude> :info g
g :: Integer -> Integer -- Defined at <interactive>:1:5
Thanks!
This is known as the monomorphism restriction.
Basically, it means that top-level bindings that look like x = are forced to be non-polymorphic, unless you specify a type signature. Bindings with arguments, i.e. f x = are not affected. See the link for details as to why this restriction exists.
Usually, you get an error message when the restriction is applied, but in this case GHCi is able to use type defaulting to change the type Num a => a to Integer.
The easiest way to dodge it is to either use an explicit type signature, or put
{-# LANGUAGE NoMonomorphismRestriction #-}
at the top of your module, or run GHCi with -XNoMonomorphismRestriction.
As others have pointed out, this is caused by something called the "Monomorphism Restriction".
MR can be useful for writers of Haskell compilers, and there is controversy about whether or not it is worthwhile to have in the language in general. But there is one thing everyone agrees: at the GHCi prompt, MR is nothing but a nuisance.
MR will probably be turned off by default in this context in an upcoming version of GHC. For now, you should disable it in GHCi by creating a text file called ".ghci" in your home directory that contains a line like this:
:set -XNoMonomorphismRestriction
Because the definition of g doesn't explicitly name its arguments, you run into the monomorphism restriction, preventing g from being polymorphic and (in this case) causing GHC to default to Integer.

Resources