开发者

How to fix a type error when trying to compile two Haskell files? [duplicate]

This question already has an answer here: What is the monomorphism restriction? 开发者_JS百科 (1 answer) Closed 13 hours ago.

I'm coding a program in haskell and the file Main.hs calls the file Enigma.hs but when i try to compile Main it outputs an error

Main.hs :

import Enigma

enigma1 = (SimpleEnigma rotor1 rotor2 rotor3 reflectorB (0,0,25))
plugboard = [('F','T'),('D','U'),('V','A'),('K','W'),('H','Z'),('I','X')] 
enigma2 = (SteckeredEnigma rotor1 rotor2 rotor3 reflectorB (0,0,25) plugboard)
crib1 = "WETTERVORHERSAGEBISKAYA"
message1 = "RWIVTYRESXBFOGKUHQBAISE"

crib2 = "TURINGBOMBEHASKELLSIMULATIONSTOP"
message2 = "YZCSDCVUFVJAAEMVILWRVSQZFCBPJFVYHUUPHLAPJMTMFNLURRADJFCBRBXBCUSSXVYWAPQIRCUVVNODKELDMNNQHYFEFOZPBUIPWKPXIYPKQHMVOAVXFVDCKMZOULMTQNUFBVHFUSXYCYPWFKBYW"

{- Function that will print "No result!" if Maybe type contains Nothing, or the
 - contents of the "Just" part otherwise. -}
printMaybe = maybe (putStrLn "No result!") print

{- This is the type of thing that will happen when testing your code. Note
 - that (1) You must have your code in a module called "Enigma". (2) The functions
 - encodeMessage, longestMenu and breakEnigma are expecting arguments in a
 - particular format. Make sure you write your types so that they are compatible.
 -
 - NOTE: The actual contents of the main function when testing your code will be
 - different to this. You SHOULD NOT submit this file, only Enigma.hs.
 -}
main = do
    print "First a test of encodeMessage: "
    print (encodeMessage "Here is a test input string." enigma1)
    print "And another test of encodeMessage: "
    print (encodeMessage "Here is a test input string." enigma2)

Enigma.hs

 data Enigma = SimpleEnigma Rotor Rotor Rotor Reflector Offsets
                | SteckeredEnigma Rotor Rotor Rotor Reflector Offsets Stecker

  encodeMessage :: [Char] -> Enigma -> [Char]
  encodeMessage [] _ = ""
  encodeMessage (x:xs) (SimpleEnigma lr mr rr rft oft) = encodeLetter x (SimpleEnigma lr mr rr rft oft) : encodeMessage xs (SimpleEnigma lr mr rr rft oft) 
  encodeMessage (x:xs) (SteckeredEnigma lr mr rr rft oft stecker) = encodeLetter x (SteckeredEnigma lr mr rr rft oft stecker) : encodeMessage xs (SteckeredEnigma lr mr rr rft oft stecker) 
  

enigma.hs works on its own, any ideas ?

I tried to run the program and enigma will compile however main won't. Any ideas how to fix this ?

Main.hs:14:44: error:
    * Ambiguous type variable `a0' arising from a use of `print'
      prevents the constraint `(Show a0)' from being solved.
      Relevant bindings include
        printMaybe :: Maybe a0 -> IO () (bound at Main.hs:14:1)
      Probable fix: use a type annotation to specify what `a0' should be.
      These potential instances exist:
        instance Show Ordering -- Defined in `GHC.Show'
        instance Show Integer -- Defined in `GHC.Show'
        instance Show a => Show (Maybe a) -- Defined in `GHC.Show'
        ...plus 22 others
        ...plus 13 instances involving out-of-scope types
        (use -fprint-potential-instances to see them all)
    * In the second argument of `maybe', namely `print'
      In the expression: maybe (putStrLn "No result!") print
      In an equation for `printMaybe':
          printMaybe = maybe (putStrLn "No result!") print
   |
14 | printMaybe = maybe (putStrLn "No result!") print
   |                                            ^^^^^
Failed, one module loaded.


This is the monomorphism restriction. Either add an explicit argument to printMaybe:

printMaybe thing = maybe (putStrLn "No result!") print thing

or leave the definition as-is but add a top-level type signature:

printMaybe :: (Show a) => Maybe a -> IO ()
printMaybe = maybe (putStrLn "No result!") print

or turn off the monomorphism restriction:

{-# LANGUAGE NoMonomorphismRestriction #-}

printMaybe = maybe (putStrLn "No result!") print

All three approaches will work fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜