开发者

Is it possible to define new ADTs in GHCi

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 the开发者_C百科re 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).


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


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


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

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


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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜