3

在 ghci (haskell) 中有一个命令会告诉我一个函数属于哪个模块(在加载的模块中)。例如,如果该函数被称为 whichMod,那么它将按如下方式工作:

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

3 回答 3

7

您想要:i命令(的缩写: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

正如您所建议的,它仅在函数位于当前加载的模块中时才有效。

请注意,您被告知该函数最初定义在哪个模块中。例如take,定义在GHC.List(至少在我的 ghc 副本中),但从前奏中重新导出。不会告诉您从哪个模块导入它。

于 2012-12-05T11:28:37.863 回答
6

是的,存在该功能:

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

您可以在“~/.ghci”配置文件中定义一个别名

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

然后,你可以写

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> _
于 2012-12-05T12:32:42.127 回答
3
Prelude> :info take
take :: Int -> [a] -> [a]   -- Defined in `GHC.List'
于 2012-12-05T11:28:27.893 回答