Ghci - interesting compilation online?

The following type of program checks if I specify on the command line (for example, ghci file.hs):

import Data.Ratio
foo = let x = [1..]
          y = (1%2) + (head x)
       in y

However, if I enter it interactively, I get an error like:

Prelude> import Data.Ratio
Prelude Data.Ratio> let x = [1..]
Prelude Data.Ratio> let y = (1%2) + (head x)
<interactive>:1:23:
    Couldn't match expected type `Ratio a0' with actual type `Integer'

It seems xto be impatiently printed as [Integer]opposed to the more general (Num t, Enum t) => [t].

Is there anything I can do about it? Are there other situations where the interactive mode will be different from the batch?

+5
source share
2 answers

, .. x = ..., , , GHC non-polymorphic, , defaulting, . ( GHCi , ).

, let x = [1..], defaulting, [Integer], Integer .

:

  • . , .

    > let x = [1..] :: [Rational]
    
  • . , .

    > let f = (+)
    > :t f
    f :: Integer -> Integer -> Integer
    > let f x y = x + y
    > :t f
    f :: Num a => a -> a -> a
    
  • . , let. GHC , , x [Rational].

    > let x = [1..]; y = 1%2 + head x
    > :t x
    x :: [Ratio Integer]
    
  • . , -, . a Integer, a Num a => a, , . , .

    , .

    > :set -XNoMonomorphismRestriction
    > let x = [1..]
    > :t x
    x :: (Num t, Enum t) => [t]
    

    , , .ghci .

+10

- , x :

let x = [1..] :: [Ratio Int]

:

Data.Ratio Prelude> let x = [1..] :: [Ratio Int]
Data.Ratio Prelude> let y = (1%2) + (head x)
Data.Ratio Prelude> y
3 % 2
Data.Ratio Prelude>
+4

All Articles