3

While commenting on new features in ghci I wished that ghci had the ability to declare type declaration and declaring new ADT types, someone informed that it was indeed possible, and after searching I found this page which told me I could do

let numUniques' :: (Eq a) => [a] -> Int; numUniques' = length . nub

Apparently that same sort of syntax works for pattern matching (ex. let a 1=True;a 2=False).

Creating ADTs would make it almost perfect? Does anyone know if it is currently possible? Should I just make an ADT scratch file and reload it?

P.S. Does anyone know if there are any plans to do so? Are there feature requests for ghc(i)?

Also I know its open source but I'm not currently smart enough to hack on ghc(i).

Community
  • 1
  • 1
Roman A. Taycher
  • 18,619
  • 19
  • 86
  • 141

4 Answers4

8

This has been added as of GHC version 7.4.1, which was released back in February:

jcp@butler:~$ ghci
GHCi, version 7.6.1: http://www.haskell.org/ghc/  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> data Test = Foo | Bar | Baz deriving (Read, Show)
Prelude> Foo
Foo
Prelude> read "Bar" :: Test
Bar
Prelude> :t Baz
Baz :: Test
javawizard
  • 1,277
  • 1
  • 12
  • 17
7

Note that you can also do explicit multiline code in ghci with :{ and :}: http://www.haskell.org/ghc/docs/6.12.2/html/users_guide/interactive-evaluation.html

sclv
  • 38,665
  • 7
  • 99
  • 204
4

No, you can't define new types in ghci.

So yes, you'll need to put those definitions in a file.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
2

No, but you could define new types in hbi (an earlier interpreter). There's discussion about bringing this back, via a ghci library on hackage.

Don Stewart
  • 137,316
  • 36
  • 365
  • 468
  • 2
    It would need changes deeper in GHC I think; the GHC API doesn't provide enough functionality yet to implement this on top of it. It's not that difficult, just work. – Simon Marlow Oct 04 '10 at 20:04
  • This answer is now out of date, per [@javawizard's answer](https://stackoverflow.com/a/13264468/1523776) – Benjamin Hodgson Jul 29 '21 at 15:25