How to create a list of string with the Prelude.functions?
Asked Answered
C

2

1

I look for something equivalent to what we can have in Python doing this::

>>> print([func for func in dir(__builtins__) if func[0].islower()])
['abs', 'all', 'any', 'ascii', 'bin', 'bool', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr',
 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len',
 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod',
'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']
>>> print(len.__doc__)
Return the number of items in a container.
>>> 

This is not a question about "how to get info from a library's contents" like they talk here: Is there a way to see the list of functions in a module, in GHCI?

It's about how to have create a list with all the functions names (so strings) provided by Prelude before any additional import.

I can see this exact list using completion after Prelude.

    Prelude> Prelude.
    Display all 256 possibilities? (y or n)
    Prelude.!!                      Prelude.RealFrac                Prelude.fromEnum                Prelude.readLn
    Prelude.$                       Prelude.Right                   Prelude.fromInteger             Prelude.readParen
    Prelude.$!                      Prelude.Semigroup               Prelude.fromIntegral            Prelude.reads
    Prelude.&&                      Prelude.Show                    Prelude.fromRational            Prelude.readsPrec
    Prelude.*                       Prelude.ShowS                   Prelude.fst                     Prelude.realToFrac
    Prelude.**                      Prelude.String                  Prelude.gcd                     Prelude.recip
    Prelude.*>                      Prelude.Traversable             Prelude.getChar                 Prelude.rem
    Prelude.+                       Prelude.True                    Prelude.getContents             Prelude.repeat
    Prelude.++                      Prelude.Word                    Prelude.getLine                 Prelude.replicate
    Prelude.-                       Prelude.^                       Prelude.head                    Prelude.return
    Prelude..                       Prelude.^^                      Prelude.id                      Prelude.reverse
    Prelude./                       Prelude.abs                     Prelude.init                    Prelude.round
    Prelude./=                      Prelude.acos                    Prelude.interact                Prelude.scaleFloat
    Prelude.<                       Prelude.acosh                   Prelude.ioError                 Prelude.scanl
    Prelude.<$                      Prelude.all                     Prelude.isDenormalized          Prelude.scanl1
    Prelude.<$>                     Prelude.and                     Prelude.isIEEE                  Prelude.scanr
    Prelude.<*                      Prelude.any                     Prelude.isInfinite              Prelude.scanr1
    Prelude.<*>                     Prelude.appendFile              Prelude.isNaN                   Prelude.seq
    Prelude.<=                      Prelude.asTypeOf                Prelude.isNegativeZero          Prelude.sequence
    Prelude.<>                      Prelude.asin                    Prelude.iterate                 Prelude.sequenceA
    Prelude.=<<                     Prelude.asinh                   Prelude.last                    Prelude.sequence_
    Prelude.==                      Prelude.atan                    Prelude.lcm                     Prelude.show
    Prelude.>                       Prelude.atan2                   Prelude.length                  Prelude.showChar
    Prelude.>=                      Prelude.atanh                   Prelude.lex                     Prelude.showList
    Prelude.>>                      Prelude.break                   Prelude.lines                   Prelude.showParen
    Prelude.>>=                     Prelude.ceiling                 Prelude.log                     Prelude.showString
    Prelude.Applicative             Prelude.compare                 Prelude.logBase                 Prelude.shows
    Prelude.Bool                    Prelude.concat                  Prelude.lookup                  Prelude.showsPrec
    Prelude.Bounded                 Prelude.concatMap               Prelude.map                     Prelude.significand
    Prelude.Char                    Prelude.const                   Prelude.mapM                    Prelude.signum
    Prelude.Double                  Prelude.cos                     Prelude.mapM_                   Prelude.sin
    Prelude.EQ                      Prelude.cosh                    Prelude.mappend                 Prelude.sinh
    Prelude.Either                  Prelude.curry                   Prelude.max                     Prelude.snd
    Prelude.Enum                    Prelude.cycle                   Prelude.maxBound                Prelude.span
    Prelude.Eq                      Prelude.decodeFloat             Prelude.maximum                 Prelude.splitAt
    Prelude.False                   Prelude.div                     Prelude.maybe                   Prelude.sqrt
    Prelude.FilePath                Prelude.divMod                  Prelude.mconcat                 Prelude.subtract
    Prelude.Float                   Prelude.drop                    Prelude.mempty                  Prelude.succ
    Prelude.Floating                Prelude.dropWhile               Prelude.min                     Prelude.sum
    Prelude.Foldable                Prelude.either                  Prelude.minBound                Prelude.tail
    Prelude.Fractional              Prelude.elem                    Prelude.minimum                 Prelude.take
    Prelude.Functor                 Prelude.encodeFloat             Prelude.mod                     Prelude.takeWhile
    Prelude.GT                      Prelude.enumFrom                Prelude.negate                  Prelude.tan
    Prelude.IO                      Prelude.enumFromThen            Prelude.not                     Prelude.tanh
    Prelude.IOError                 Prelude.enumFromThenTo          Prelude.notElem                 Prelude.toEnum
    Prelude.Int                     Prelude.enumFromTo              Prelude.null                    Prelude.toInteger
    Prelude.Integer                 Prelude.error                   Prelude.odd                     Prelude.toRational
    Prelude.Integral                Prelude.errorWithoutStackTrace  Prelude.or                      Prelude.traverse
    Prelude.Just                    Prelude.even                    Prelude.otherwise               Prelude.truncate
    Prelude.LT                      Prelude.exp                     Prelude.pi                      Prelude.uncurry
    Prelude.Left                    Prelude.exponent                Prelude.pred                    Prelude.undefined
    Prelude.Maybe                   Prelude.fail                    Prelude.print                   Prelude.unlines
    Prelude.Monad                   Prelude.filter                  Prelude.product                 Prelude.until
    Prelude.MonadFail               Prelude.flip                    Prelude.properFraction          Prelude.unwords
    Prelude.Monoid                  Prelude.floatDigits             Prelude.pure                    Prelude.unzip
    Prelude.Nothing                 Prelude.floatRadix              Prelude.putChar                 Prelude.unzip3
    Prelude.Num                     Prelude.floatRange              Prelude.putStr                  Prelude.userError
    Prelude.Ord                     Prelude.floor                   Prelude.putStrLn                Prelude.words
    Prelude.Ordering                Prelude.fmap                    Prelude.quot                    Prelude.writeFile
    Prelude.Rational                Prelude.foldMap                 Prelude.quotRem                 Prelude.zip
    Prelude.Read                    Prelude.foldl                   Prelude.read                    Prelude.zip3
    Prelude.ReadS                   Prelude.foldl1                  Prelude.readFile                Prelude.zipWith
    Prelude.Real                    Prelude.foldr                   Prelude.readIO                  Prelude.zipWith3
    Prelude.RealFloat               Prelude.foldr1                  Prelude.readList                Prelude.||
    Prelude> Prelude.

So right now I do this manualy with copy past & cleaning on vim and then copying this list back on Prelude like this:

Prelude> all_prelude_functions = ["acos", "acosh", "all", "and", "any", "appendFile", "asTypeOf", "asin", "asinh", "atan", "atan2", "atanh", "break", "ceiling", "compare", "concat", "concatMap"]
Prelude> all_prelude_functions
["acos","acosh","all","and","any","appendFile","asTypeOf","asin","asinh","atan","atan2","atanh","break","ceiling","compare","concat","concatMap"]
Prelude> 

I would like to create this list directly inside Prelude

Casuistry answered 2/8, 2017 at 14:24 Comment(5)
Possible duplicate of Is there a way to see the list of functions in a module, in GHCI?Numismatics
Well, you could copy paste the list from GHCI into a gigantic string and store that in your .hs file. Why do you need such a list? What do you want to do with it?Ginnygino
Define what you mean by “builtin”. Haskell doesn't really have “builtins”. You mean, the available definitions in an empty module or REPL? That's just the Prelude module, but nothing in there is really special language-wise – it's just what's imported unless you sayimport Prelude () to prevent that. ...John
...Or do you mean, the “fundamental building blocks”, the stuff you couldn't define yourself in a library but only by rebuilding the compiler? That would be GHC.Prim, but in there are only very awkward to use low-level operations. It isn't supposed to be used in normal Haskell at all, and in fact isn't related to what the Haskell standard defines.John
IOW, everything in Haskell, excluding the (few) keywords, is just a library function/type/typeclass. Which is great, for instance it allows you to search for anything, including “builtins”, infix operators etc., with the specialised Haskell search engines, both by name and by type.John
H
3

In GHCi, you can type :browse Prelude to list everything exported by the Prelude module.

Output from my local installation:

Prelude> :browse Prelude
(!!) :: [a] -> Int -> a
($) ::
  forall (r :: GHC.Types.RuntimeRep) a (b :: TYPE r).
  (a -> b) -> a -> b
($!) :: (a -> b) -> a -> b
(&&) :: Bool -> Bool -> Bool
(++) :: [a] -> [a] -> [a]
(.) :: (b -> c) -> (a -> b) -> a -> c
(<$>) :: Functor f => (a -> b) -> f a -> f b
(=<<) :: Monad m => (a -> m b) -> m a -> m b
class Functor f => Applicative (f :: * -> *) where
  pure :: a -> f a
  (<*>) :: f (a -> b) -> f a -> f b
  (*>) :: f a -> f b -> f b
  (<*) :: f a -> f b -> f a
  {-# MINIMAL pure, (<*>) #-}
data Bool = False | True
class Bounded a where
  minBound :: a
  maxBound :: a
  {-# MINIMAL minBound, maxBound #-}
data Char = GHC.Types.C# GHC.Prim.Char#
data Double = GHC.Types.D# GHC.Prim.Double#
data Either a b = Left a | Right b
class Enum a where
  succ :: a -> a
  pred :: a -> a
  toEnum :: Int -> a
  fromEnum :: a -> Int
  enumFrom :: a -> [a]
  enumFromThen :: a -> a -> [a]
  enumFromTo :: a -> a -> [a]
  enumFromThenTo :: a -> a -> a -> [a]
  {-# MINIMAL toEnum, fromEnum #-}
class Eq a where
  (==) :: a -> a -> Bool
  (/=) :: a -> a -> Bool
  {-# MINIMAL (==) | (/=) #-}
type FilePath = String
data Float = GHC.Types.F# GHC.Prim.Float#
class Fractional a => Floating a where
  pi :: a
  exp :: a -> a
  log :: a -> a
  sqrt :: a -> a
  (**) :: a -> a -> a
  logBase :: a -> a -> a
  sin :: a -> a
  cos :: a -> a
  tan :: a -> a
  asin :: a -> a
  acos :: a -> a
  atan :: a -> a
  sinh :: a -> a
  cosh :: a -> a
  tanh :: a -> a
  asinh :: a -> a
  acosh :: a -> a
  atanh :: a -> a
  GHC.Float.log1p :: a -> a
  GHC.Float.expm1 :: a -> a
  GHC.Float.log1pexp :: a -> a
  GHC.Float.log1mexp :: a -> a
  {-# MINIMAL pi, exp, log, sin, cos, asin, acos, atan, sinh, cosh,
              asinh, acosh, atanh #-}
class Foldable (t :: * -> *) where
  Data.Foldable.fold :: Monoid m => t m -> m
  foldMap :: Monoid m => (a -> m) -> t a -> m
  foldr :: (a -> b -> b) -> b -> t a -> b
  Data.Foldable.foldr' :: (a -> b -> b) -> b -> t a -> b
  foldl :: (b -> a -> b) -> b -> t a -> b
  Data.Foldable.foldl' :: (b -> a -> b) -> b -> t a -> b
  foldr1 :: (a -> a -> a) -> t a -> a
  foldl1 :: (a -> a -> a) -> t a -> a
  Data.Foldable.toList :: t a -> [a]
  null :: t a -> Bool
  length :: t a -> Int
  elem :: Eq a => a -> t a -> Bool
  maximum :: Ord a => t a -> a
  minimum :: Ord a => t a -> a
  sum :: Num a => t a -> a
  product :: Num a => t a -> a
  {-# MINIMAL foldMap | foldr #-}
class Num a => Fractional a where
  (/) :: a -> a -> a
  recip :: a -> a
  fromRational :: Rational -> a
  {-# MINIMAL fromRational, (recip | (/)) #-}
class Functor (f :: * -> *) where
  fmap :: (a -> b) -> f a -> f b
  (<$) :: a -> f b -> f a
  {-# MINIMAL fmap #-}
newtype IO a
  = GHC.Types.IO (GHC.Prim.State# GHC.Prim.RealWorld
                  -> (# GHC.Prim.State# GHC.Prim.RealWorld, a #))
type IOError = GHC.IO.Exception.IOException
data Int = GHC.Types.I# GHC.Prim.Int#
data Integer
  = integer-gmp-1.0.0.1:GHC.Integer.Type.S# !GHC.Prim.Int#
  | integer-gmp-1.0.0.1:GHC.Integer.Type.Jp# {-# UNPACK #-}integer-gmp-1.0.0.1:GHC.Integer.Type.BigNat
  | integer-gmp-1.0.0.1:GHC.Integer.Type.Jn# {-# UNPACK #-}integer-gmp-1.0.0.1:GHC.Integer.Type.BigNat
class (Real a, Enum a) => Integral a where
  quot :: a -> a -> a
  rem :: a -> a -> a
  div :: a -> a -> a
  mod :: a -> a -> a
  quotRem :: a -> a -> (a, a)
  divMod :: a -> a -> (a, a)
  toInteger :: a -> Integer
  {-# MINIMAL quotRem, toInteger #-}
data Maybe a = Nothing | Just a
class Applicative m => Monad (m :: * -> *) where
  (>>=) :: m a -> (a -> m b) -> m b
  (>>) :: m a -> m b -> m b
  return :: a -> m a
  fail :: String -> m a
  {-# MINIMAL (>>=) #-}
class Monoid a where
  mempty :: a
  mappend :: a -> a -> a
  mconcat :: [a] -> a
  {-# MINIMAL mempty, mappend #-}
class Num a where
  (+) :: a -> a -> a
  (-) :: a -> a -> a
  (*) :: a -> a -> a
  negate :: a -> a
  abs :: a -> a
  signum :: a -> a
  fromInteger :: Integer -> a
  {-# MINIMAL (+), (*), abs, signum, fromInteger, (negate | (-)) #-}
class Eq a => Ord a where
  compare :: a -> a -> Ordering
  (<) :: a -> a -> Bool
  (<=) :: a -> a -> Bool
  (>) :: a -> a -> Bool
  (>=) :: a -> a -> Bool
  max :: a -> a -> a
  min :: a -> a -> a
  {-# MINIMAL compare | (<=) #-}
data Ordering = LT | EQ | GT
type Rational = GHC.Real.Ratio Integer
class Read a where
  readsPrec :: Int -> ReadS a
  readList :: ReadS [a]
  GHC.Read.readPrec :: Text.ParserCombinators.ReadPrec.ReadPrec a
  GHC.Read.readListPrec :: Text.ParserCombinators.ReadPrec.ReadPrec
                             [a]
  {-# MINIMAL readsPrec | readPrec #-}
type ReadS a = String -> [(a, String)]
class (Num a, Ord a) => Real a where
  toRational :: a -> Rational
  {-# MINIMAL toRational #-}
class (RealFrac a, Floating a) => RealFloat a where
  floatRadix :: a -> Integer
  floatDigits :: a -> Int
  floatRange :: a -> (Int, Int)
  decodeFloat :: a -> (Integer, Int)
  encodeFloat :: Integer -> Int -> a
  exponent :: a -> Int
  significand :: a -> a
  scaleFloat :: Int -> a -> a
  isNaN :: a -> Bool
  isInfinite :: a -> Bool
  isDenormalized :: a -> Bool
  isNegativeZero :: a -> Bool
  isIEEE :: a -> Bool
  atan2 :: a -> a -> a
  {-# MINIMAL floatRadix, floatDigits, floatRange, decodeFloat,
              encodeFloat, isNaN, isInfinite, isDenormalized, isNegativeZero,
              isIEEE #-}
class (Real a, Fractional a) => RealFrac a where
  properFraction :: Integral b => a -> (b, a)
  truncate :: Integral b => a -> b
  round :: Integral b => a -> b
  ceiling :: Integral b => a -> b
  floor :: Integral b => a -> b
  {-# MINIMAL properFraction #-}
class Show a where
  showsPrec :: Int -> a -> ShowS
  show :: a -> String
  showList :: [a] -> ShowS
  {-# MINIMAL showsPrec | show #-}
type ShowS = String -> String
type String = [Char]
class (Functor t, Foldable t) => Traversable (t :: * -> *) where
  traverse :: Applicative f => (a -> f b) -> t a -> f (t b)
  sequenceA :: Applicative f => t (f a) -> f (t a)
  mapM :: Monad m => (a -> m b) -> t a -> m (t b)
  sequence :: Monad m => t (m a) -> m (t a)
  {-# MINIMAL traverse | sequenceA #-}
data Word = GHC.Types.W# GHC.Prim.Word#
(^) :: (Num a, Integral b) => a -> b -> a
(^^) :: (Fractional a, Integral b) => a -> b -> a
all :: Foldable t => (a -> Bool) -> t a -> Bool
and :: Foldable t => t Bool -> Bool
any :: Foldable t => (a -> Bool) -> t a -> Bool
appendFile :: FilePath -> String -> IO ()
asTypeOf :: a -> a -> a
break :: (a -> Bool) -> [a] -> ([a], [a])
concat :: Foldable t => t [a] -> [a]
concatMap :: Foldable t => (a -> [b]) -> t a -> [b]
const :: a -> b -> a
curry :: ((a, b) -> c) -> a -> b -> c
cycle :: [a] -> [a]
drop :: Int -> [a] -> [a]
dropWhile :: (a -> Bool) -> [a] -> [a]
either :: (a -> c) -> (b -> c) -> Either a b -> c
error ::
  forall (r :: GHC.Types.RuntimeRep) (a :: TYPE r).
  GHC.Stack.Types.HasCallStack =>
  [Char] -> a
errorWithoutStackTrace ::
  forall (r :: GHC.Types.RuntimeRep) (a :: TYPE r). [Char] -> a
even :: Integral a => a -> Bool
filter :: (a -> Bool) -> [a] -> [a]
flip :: (a -> b -> c) -> b -> a -> c
fromIntegral :: (Integral a, Num b) => a -> b
fst :: (a, b) -> a
gcd :: Integral a => a -> a -> a
getChar :: IO Char
getContents :: IO String
getLine :: IO String
head :: [a] -> a
id :: a -> a
init :: [a] -> [a]
interact :: (String -> String) -> IO ()
ioError :: IOError -> IO a
iterate :: (a -> a) -> a -> [a]
last :: [a] -> a
lcm :: Integral a => a -> a -> a
lex :: ReadS String
lines :: String -> [String]
lookup :: Eq a => a -> [(a, b)] -> Maybe b
map :: (a -> b) -> [a] -> [b]
mapM_ :: (Foldable t, Monad m) => (a -> m b) -> t a -> m ()
maybe :: b -> (a -> b) -> Maybe a -> b
not :: Bool -> Bool
notElem :: (Foldable t, Eq a) => a -> t a -> Bool
odd :: Integral a => a -> Bool
or :: Foldable t => t Bool -> Bool
otherwise :: Bool
print :: Show a => a -> IO ()
putChar :: Char -> IO ()
putStr :: String -> IO ()
putStrLn :: String -> IO ()
read :: Read a => String -> a
readFile :: FilePath -> IO String
readIO :: Read a => String -> IO a
readLn :: Read a => IO a
readParen :: Bool -> ReadS a -> ReadS a
reads :: Read a => ReadS a
realToFrac :: (Real a, Fractional b) => a -> b
repeat :: a -> [a]
replicate :: Int -> a -> [a]
reverse :: [a] -> [a]
scanl :: (b -> a -> b) -> b -> [a] -> [b]
scanl1 :: (a -> a -> a) -> [a] -> [a]
scanr :: (a -> b -> b) -> b -> [a] -> [b]
scanr1 :: (a -> a -> a) -> [a] -> [a]
seq :: a -> b -> b
sequence_ :: (Foldable t, Monad m) => t (m a) -> m ()
showChar :: Char -> ShowS
showParen :: Bool -> ShowS -> ShowS
showString :: String -> ShowS
shows :: Show a => a -> ShowS
snd :: (a, b) -> b
span :: (a -> Bool) -> [a] -> ([a], [a])
splitAt :: Int -> [a] -> ([a], [a])
subtract :: Num a => a -> a -> a
tail :: [a] -> [a]
take :: Int -> [a] -> [a]
takeWhile :: (a -> Bool) -> [a] -> [a]
uncurry :: (a -> b -> c) -> (a, b) -> c
undefined ::
  forall (r :: GHC.Types.RuntimeRep) (a :: TYPE r).
  GHC.Stack.Types.HasCallStack =>
  a
unlines :: [String] -> String
until :: (a -> Bool) -> (a -> a) -> a -> a
unwords :: [String] -> String
unzip :: [(a, b)] -> ([a], [b])
unzip3 :: [(a, b, c)] -> ([a], [b], [c])
userError :: String -> IOError
words :: String -> [String]
writeFile :: FilePath -> String -> IO ()
zip :: [a] -> [b] -> [(a, b)]
zip3 :: [a] -> [b] -> [c] -> [(a, b, c)]
zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
zipWith3 :: (a -> b -> c -> d) -> [a] -> [b] -> [c] -> [d]
(||) :: Bool -> Bool -> Bool
Herrin answered 2/8, 2017 at 14:27 Comment(7)
Yes In Prelude we have this help, but outside from prelude in a test.hs file I want to get the liste ['odd', 'break', 'pi', ...]Casuistry
If you want to get a list of the names of available functions programmatically you'll have to go through the GHC api or a wrapper thereof; which shouldn't be too much of an issue because otherwise there's not very many useful things you could do with such a list.Dotted
@Casuistry What do you want to do with that list? What's the larger problem you're working on solving?Copra
@DavidYoung I've updated my question with yours remarquesCasuistry
Thank you @chad-gilbert, is it possible to put all this stdout in a string, so could regeps it to get my expected list of strings ?Casuistry
@Casuistry This still does not answer the question of what you want to use this for. The reason I ask is that I strongly suspect this is an XY problem, and if we knew the ultimate problem you are trying to solve, we would be able to help you much more easily. What is the end goal you are trying to achieve with this?Copra
To train myself with Haskell I try to create markdown document that format for all Prelude function a short one liner explanation and one example of how to use it. Then I will make that for Data.TextCasuistry
G
1

As I show in my answer to your question How to know what are the full list of function availlable in an import? you can use ghc-mod.

Replace in the above referenced answer the line

out <- run "ghc-mod" ["browse", "-d", "Data.List"]

with

out <- run "ghc-mod" ["browse", "-d", "Prelude"]

Grallatorial answered 3/8, 2017 at 15:23 Comment(1)
I'm trying to use it but ghc-mod deprecated for haskell-ide-engine deprecated for haskell-language-serverCasuistry

© 2022 - 2024 — McMap. All rights reserved.