Using Haskell "Possibly," enter Ads [initial question]

I started experimenting with Haskell and have a problem. qqq is a function that should print one line if it is called "Nothing", and print other things if called with "Just something".

The first attempt seems to work:

qqq Nothing = print "There isn't anything to be printed." qqq (Just x) = print "There is something to be printed." >> print x main :: IO () main = qqq (Just 43) 

But:

  • when I try to do main = qqq (Nothing) , it fails ("An ambiguous variable like" a0 "in the constraint: (Show a0), arising from using" qqq ")
  • When I want to add a type signature in case of failure:
    • qqq :: Maybe x => x -> IO () β†’ Type constructor 'Maybe' used as a class β†’ But isn’t it?
    • qqq :: (Maybe x) -> IO () . Now the signature itself looks like a success. But main = qqq (Just 43) begins to fail with a mysterious error (Show a0) , as in the case of main = qqq (Nothing) .

Questions:

  • Why is a qqq call with Nothing so different from a call with Just 43 ?
  • What is (Show a0) ? It is only mentioned in error messages. Any attempts to use it lead to something like "Show not in volume."
  • What is the correct type signature for this? How to make a Haskell signature of the type of print it inferred? Expect something like:
 f 0 = 2 fx = (f (x-1)) + 3 main = print get_type_as_string(f) -- prints "Number -> Number" 
+6
source share
1 answer

qqq type:

 qqq :: Show a => Maybe a -> IO () 

This means that qqq takes one parameter of type Maybe a and returns an IO action without a value, while the restriction a implements the Show class. To find out what Show , you can use :i Show in ghci.

Show is a type that requires that the type value can be converted to a string. qqq has a limitation because print wants to print the value ( print is of type Show a => a -> IO () ). Maybe is not a type, but a data type. You can learn more about typeclasses here .

You can let the GHC output the type signature by typing the function in the .hs file, then loading the file with ghci ( ghci Myfile.hs ), and then type :t qqq to display the type. You can also define a function in an interactive session with let qqq n = case n of { Nothing -> print "abc"; Just x -> print "def" >> print x } let qqq n = case n of { Nothing -> print "abc"; Just x -> print "def" >> print x } (this looks a bit different because the function definition should be on the same line in ghci, but the meaning is the same).

When the main qqq calls with qqq (Just 43) , it becomes clear that the concrete type Maybe a is numeric (ghci is Integer by default), so qqq has the concrete type Maybe Integer -> IO () . However, the main qqq calls with qqq Nothing , a can be any (this is ambiguous), and ghci reports an error.

+9
source

Source: https://habr.com/ru/post/892885/


All Articles