2012-12-05 57 views
1

在ghci(haskell)中是否有一個命令會告訴我一個函數屬於哪個模塊(加載模塊之外)。例如如果函數被調用whichMod,那麼它會工作如下:找出函數屬於哪個模塊

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

對於當前會話中未加載的函數,thre始終[hoogle](http://www.haskell.org/hoogl E /?hoogle =排序)。您可以構建一個hoogle客戶端並將其添加到ghci。 [GOA](http://www.haskell.org/haskellwiki/GHC/GHCi#GHCi_on_Acid)包可爲您提供幫助。 –

+0

@ n.m。嗯,看起來很有趣。試圖安裝GOA,但後來我得到了可怕的cabal消息「cabal:下列軟件包很可能被重新安裝打破:」。也許我會回到這個時候,我對cabal有更多的經驗!謝謝 – artella

+0

我不知道,我使用gentoo及其haskell覆蓋。我只是說'emerge goa'。嘗試發佈協同工作的版本。 –

回答

5

你想要: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 

正如你所建議的那樣,它只在函數處於當前加載的模塊中時才起作用。

請注意,您會被告知函數最初是在哪個模塊中定義的。 takeGHC.List中定義(至少在我的ghc副本中),但是從前奏中重新導出。你不知道你從哪個模塊導入它。

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

謝謝。不知道哪個答案先來了,但給了+1這個:) – artella

5

是的,存在功能:

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

非常感謝,這真的很有用。我之前聽說過hoogle,但現在我知道如何使用它!:) – artella

+0

我很樂意返回一些收到的援助;) – josejuan

相關問題