Function to output function name

2020-02-11 03:48发布

Is it possible in Haskell to implement a function which returns its own function name?

A possible type could be (a -> b) -> String.

4条回答
Fickle 薄情
2楼-- · 2020-02-11 04:17

Am I missing something? This function returns its own function name.

Prelude> let myNameIs::(a->b) -> String; myNameIs f = "myNameIs"
Prelude> :type myNameIs
myNameIs :: (a -> b) -> String
Prelude> myNameIs myNameIs
"myNameIs"
查看更多
混吃等死
3楼-- · 2020-02-11 04:24

You want a function that takes a function argument, and returns the definition site variable name that corresponds to the name of that function?

This isn't possibly without meta-programming, which is usually a sign you're doing something wrong :). But assuming you're not, one way to achieve something in the right direction is via Template Haskell, which can get at unique names (how the compiler names things). E.g.

Prelude Language.Haskell.TH> :set -XTemplateHaskell
Prelude Language.Haskell.TH> let f x y = x + y
Prelude Language.Haskell.TH> $( stringE . show =<< reify 'f )

     "VarI f_1627394057
                (ForallT [PlainTV a_1627394063]
                         [ClassP GHC.Num.Num [VarT a_1627394063]]
                              (AppT (AppT ArrowT (VarT a_1627394063)) 
                                    (AppT (AppT ArrowT (VarT a_1627394063)) 
                                         (VarT a_1627394063)))) 
                         Nothing (Fixity 9 InfixL)"

And now we know a lot about the variable. So you can play games by passing a Name to the function (via 'f) rather than f itself.

You are certainly in the world of reflection and meta-programming though, so it would help to know more about what you are trying to do.

查看更多
不美不萌又怎样
4楼-- · 2020-02-11 04:37

To clarify something mentioned in dons' post: no functions have names in Haskell. There are bindings which may bind functions, but if I had such a function (call it getName) as you seek then what would you expect this to return:

let f x = x
    g   = f
    h   = f
in  getName g == getName h
查看更多
做个烂人
5楼-- · 2020-02-11 04:37

I don't know what you need it for, but maybe a simplistic solution suffices? Like so:

data NamedFunction a b = NamedFunction { 
    name :: String,
    apply :: a -> b
}

timesTwo :: NamedFunction Int Int
timesTwo = NamedFunction "timesTwo" (\x -> 2 * x)

which you can use as follows:

ghci> timesTwo `apply` 7
14
ghci> name timesTwo
"timesTwo"

You can then write your own version of (.):

-- contrast (.)  ::    (b -> c) ->          (a -> b) ->         (a -> c)
compose :: NamedFunction b c -> NamedFunction a b -> NamedFunction a c
compose (NamedFunction n1 f1) (NamedFunction n2 f2) = 
     NamedFunction (n1++ " . " ++ n2) (f1 . f2)

In ghci:

ghci> let f = timesTwo `compose` timesTwo in (f `apply` 7, name f) 
(28,"timesTwo . timesTwo")

You'll have to reimplement your own versions of map, filter and so on, and you're bound to run into other problems later, but maybe this is all you need...

查看更多
登录 后发表回答