Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I define a new type in ghci?

Tags:

haskell

ghci

I get an error in ghci when I try to define a new type:

Prelude> data Point = Pt Int Int <interactive>:1:0: parse error on input `data' Prelude> let data Point = Pt Int Int <interactive>:1:4: parse error on input `data'

What am I doing wrong?

like image 346
titaniumdecoy Avatar asked Jun 27 '10 06:06

titaniumdecoy


People also ask

How do you define a function in ghci?

Simply type a let followed by a newline: let ⏎. Then fac 0 = 1 ⏎. Then fac n = n * fac (n-1) ⏎ ⏎ and you're done!

How do I load files into ghci?

From the command line, enter "ghci" (or "ghci -W") followed by an optional filename to load. Note: We recommend using "ghci -W", which tells GHC to output useful warning messages in more situations. These warnings help to avoid common programming errors.

How do I run Haskell with ghci?

Open a command window and navigate to the directory where you want to keep your Haskell source files. Run Haskell by typing ghci or ghci MyFile. hs. (The "i" in "GHCi" stands for "interactive", as opposed to compiling and producing an executable file.)

How do I quit ghci Haskell?

Quits GHCi. You can also quit by typing control-D at the prompt. Attempts to reload the current target set (see :load ) if any of the modules in the set, or any dependent module, has changed.


2 Answers

titaniumdecoy, I remember being helped with this sort of GHCi mystery when I learned the frequently made point that writing things like 'let square x = x * x' inside the GHCi is like writing let a = f b with do notation in the IO monad -- say in this sort of example:

palindromize :: IO () palindromize = do   a <- readFile "foo.txt"   let b = reverse a   putStrLn (a ++ b) 

Similarly, when you redefine an expression in the GHCi, it's sort of like doing the following in do notation, which is perfectly legitimate:

makeMess :: IO () makeMess = do    a <- readFile "foo.txt"    let b = reverse a    putStrLn (a ++ b)    let b = a    putStrLn (a ++ b) 

No one would declare a data type in the middle of such a sequence, but would do it elsewhere in the module. I might have guessed that there was some sort of theoretical objection, but Don S.'s remark suggests there isn't one.

like image 96
applicative Avatar answered Sep 29 '22 13:09

applicative


It is possible since GHC 7.4.1.

like image 21
Alexey Romanov Avatar answered Sep 29 '22 14:09

Alexey Romanov