Is there a function in Haskell standard library (or some other modules / packages) that can take a String
literal such as "+"
, "head"
etc., then turn those into their corresponding Haskell functions, such as +
, head
?
Hoogle did not produce any immediately useful answers. A dummy example for such a function would work like this:
eval :: String -> (a -> b)
eval "+" = (+)
eval "head" = head
p.s. Given that "corresponding" here can mean different things under different context (+
may no longer be defined as numeric addition for types that are not Num
bers) but is necessary to answer this question, let's assume that the "corresponding" function for a string is the function that type checks and could be evaluated in the environment where the string is being turned into this function, a.k.a. use the lexical scope to lookup the value of this function.
If there is no such function, is there a deep reason (e.g. from lambda calculus, type theory etc.) why, and is it impossible to implement in any strict static typed language? If it is impossible in Haskell, what is actually a recommended practice to problems such as this? Imagine implementing a linear algebra library where strings either eval into functions that create values (e.g. vect2
is a function that creates a two dimensional function), or (binary, for simplicity) operations that map from them unto some other type (we could optionally provide type annotations for these, such as eval "innerProduct" :: Double
, eval "matrixProduct" :: Matrix
etc.)
More generally, a String
type has a kind *
, but a function ->
has a kind * -> * -> *
. Assuming that we can provide the first two kinds *
from the lexical scope, it should be possible, right?