Finding out which module a function belongs to

淺唱寂寞╮ 提交于 2019-12-23 15:43:18

问题


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

回答1:


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.




回答2:


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> _



回答3:


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


来源:https://stackoverflow.com/questions/13722203/finding-out-which-module-a-function-belongs-to

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!