Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search for the types of many functions in one go

Tags:

haskell

ghci

How can I search for the types of length, take, drop, splitAt, !! and replicate all in one go, without entering in :t length, :t take, :t drop, etc, for all of these functions?

like image 356
user65526 Avatar asked Mar 16 '18 11:03

user65526


1 Answers

ghci includes a :def command. The command has the form :def <name> <function>, where <name> is the new command to define, and <function> is a Haskell function of type String -> IO String saying how to convert arguments to the new command into an existing chain of commands. We can use this to our advantage: we'll make a new command, :manyt, which takes a list of names and runs :t on each. For simplicity, I'll split on spaces; but if you want to ask for the types of many expressions rather than just names you may want to do some more complicated delimiting/parsing. So, in ~/.ghci, add a line like this:

:def manyt (\s -> Prelude.return (Prelude.unlines [":t " Prelude.++ n | n <- Prelude.words s]))

(The excessive Prelude qualification is so that this works even when -XNoImplicitPrelude is enabled.) Try it out:

> :manyt length take drop splitAt (!!) replicate
length :: Foldable t => t a -> Int
take :: Int -> [a] -> [a]
drop :: Int -> [a] -> [a]
splitAt :: Int -> [a] -> ([a], [a])
(!!) :: [a] -> Int -> a
replicate :: Int -> a -> [a]
like image 132
Daniel Wagner Avatar answered Oct 14 '22 07:10

Daniel Wagner