artella
artella

Reputation: 5118

Finding out which module a function belongs to

In ghci (haskell) is there a command which will tell me which module (out of the loaded modules) a function belongs to. e.g. if the function is called whichMod, then it would work as follows :

Prelude>whichMod take
Prelude
Prelude>whichMod sort
Data.List

Upvotes: 3

Views: 584

Answers (3)

josejuan
josejuan

Reputation: 9566

Yes, exists that function:

Prelude> :! hoogle sort
Data.List sort :: Ord a => [a] -> [a]
Data.List sortBy :: (a -> a -> Ordering) -> [a] -> [a]
Data.ByteString sort :: ByteString -> ByteString
Data.ByteString.Char8 sort :: ByteString -> ByteString
Data.Sequence sort :: Ord a => Seq a -> Seq a
package sort-by-pinyin
Data.Sequence sortBy :: (a -> a -> Ordering) -> Seq a -> Seq a
GHC.Exts sortWith :: Ord b => (a -> b) -> [a] -> [a]
package sorty
package cabal-sort
package external-sort
Data.Graph.Inductive.Internal.Heap heapsort :: Ord a => [a] -> [a]
package heapsort
package natural-sort
package NaturalSort
Data.Graph topSort :: Graph -> [Vertex]
Data.Graph.Inductive.Query.DFS topsort :: Graph gr => gr a b -> [Node]
Data.Graph.Inductive.Query.DFS topsort' :: Graph gr => gr a b -> [a]
Data.Sequence unstableSort :: Ord a => Seq a -> Seq a
Data.Sequence unstableSortBy :: (a -> a -> Ordering) -> Seq a -> Seq a
Prelude> _

you can define a alias into your "~/.ghci" configuration file

:def hoogle \str -> return $ ":! hoogle \"" ++ str ++ "\""

then, you can write

Prelude> :hoogle sort
Data.List sort :: Ord a => [a] -> [a]
Data.List sortBy :: (a -> a -> Ordering) -> [a] -> [a]
Data.ByteString sort :: ByteString -> ByteString
Data.ByteString.Char8 sort :: ByteString -> ByteString
Data.Sequence sort :: Ord a => Seq a -> Seq a
package sort-by-pinyin
Data.Sequence sortBy :: (a -> a -> Ordering) -> Seq a -> Seq a
GHC.Exts sortWith :: Ord b => (a -> b) -> [a] -> [a]
package sorty
package cabal-sort
package external-sort
Data.Graph.Inductive.Internal.Heap heapsort :: Ord a => [a] -> [a]
package heapsort
package natural-sort
package NaturalSort
Data.Graph topSort :: Graph -> [Vertex]
Data.Graph.Inductive.Query.DFS topsort :: Graph gr => gr a b -> [Node]
Data.Graph.Inductive.Query.DFS topsort' :: Graph gr => gr a b -> [a]
Data.Sequence unstableSort :: Ord a => Seq a -> Seq a
Data.Sequence unstableSortBy :: (a -> a -> Ordering) -> Seq a -> Seq a
Prelude> _

Upvotes: 6

dave4420
dave4420

Reputation: 47042

You want the :i command (short for :info).

Prelude> :i take
take :: Int -> [a] -> [a]   -- Defined in GHC.List
Prelude> :i sort

Top level: Not in scope: `sort'
Prelude> :m +Data.List
Prelude Data.List> :i sort
sort :: Ord a => [a] -> [a]     -- Defined in Data.List

As you suggest, it only works if the function is in a currently loaded module.

Note that you are told which module the function is originally defined in. e.g. take is defined in GHC.List (at least in my copy of ghc), but re-exported from the prelude. You are not told which module(s) you imported it from.

Upvotes: 8

Jan
Jan

Reputation: 11726

Prelude> :info take
take :: Int -> [a] -> [a]   -- Defined in `GHC.List'

Upvotes: 3

Related Questions