1

I was trying to define a value with specific type like "Int", but seems Prelude doesn't support it?

Prelude> let c::Int = 4
Prelude| 

<interactive>:49:8:
    Illegal type signature: ‘Int’
      Perhaps you intended to use ScopedTypeVariables
    In a pattern type-signature
Prelude> let [Int]c=4
Prelude| 

<interactive>:51:5: Parse error in pattern: [Int]

How to correct it?

vik santata
  • 2,989
  • 8
  • 30
  • 52
  • 2
    Type signatures can appear on the own line (in let, where, or top level declarations) or in expressions. Type signatures are not for the left hand side of a binding. – Thomas M. DuBuisson Feb 29 '16 at 00:43
  • Possible duplicate of [Multi-line commands in GHCi](http://stackoverflow.com/questions/8443035/multi-line-commands-in-ghci) – Antal Spector-Zabusky Feb 29 '16 at 01:33
  • 2
    "How to correct it?" - the error says how - "Perhaps you intended to use ScopedTypeVariables". Enable ScopedTypeVariables in ghci with `:set -XScopedTypeVariables`. That is, if you want to use this particular syntax. – user2407038 Feb 29 '16 at 02:10
  • I came here thinking somebody wanted to put a `type` declaration inside a `let` block. I just went to check the Report to see if you can legally do that... You can't. (You *can*, however, put a fixity declaration in there... Weird, eh?) – MathematicalOrchid Feb 29 '16 at 14:55

2 Answers2

8

In a Haskell file, type declarations are provided separately from definitions, usually on the line before:

c :: Int    -- Type declaration
c = 4       -- Definition

This holds true for local definitions, as well as global ones; you just need to make sure the indentation lines up. So in that case, we have

let c :: Int
    c = 4
in c + c

In Haskell, newlines and indentation can be replaced with braces and semicolons, and sometimes the braces can be elided. In GHCi, where entering multiple-line input requires some extra machinery, you'll usually want the semicolon-separated variant; to wit, that'll be

let c :: Int ; c = 4

(The lack of an in is because GHCi behaves a bit like a do block; this Stack Overflow question has more information.)

However, it looks from your prompt like you have :set +m turned on, so you can use the multi-line option too:

Prelude> let c :: Int
Prelude|     c = 4
Prelude|
Prelude>

(Also, if you want to add a type annotation afterward, let c = 4 ; c :: Int works fine; it's just not the best style for a file you're working on.)


Also, an important note: you aren't using "Prelude", you're using GHCi, GHC's interactive Haskell environment. Prelude is the module that is imported by default in all Haskell programs (it provides the definitions of Bool, Eq, (.), and so on). GHCi's default prompt contains the list of all modules currently imported, so by default it's Prelude>; however, if you type import Data.Function, the prompt will change to Prelude Data.Function>.

Community
  • 1
  • 1
Antal Spector-Zabusky
  • 36,191
  • 7
  • 77
  • 140
5

This will do it

let x = 1::Int
jamshidh
  • 12,002
  • 17
  • 31