Forgive me if this is a dumb question, but is there a typeclass that represents all functions? Like, lets say I have a type like this
data Foo a = Thing a
instance (Show a) => Show (Foo a) where
show (Thing a) = show a
And I want to display Thing (\x -> 1). Maybe do some sort o reflection on the method to pretty print some metadata about this?
Trying it I get the following error
> Thing (\x -> 1)
<interactive>:113:1:
No instance for (Show (t0 -> a0)) arising from a use of `print'
Possible fix: add an instance declaration for (Show (t0 -> a0))
In a stmt of an interactive GHCi command: print it
<interactive>:113:14:
No instance for (Num a0) arising from the literal `1'
The type variable `a0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Note: there are several potential instances:
instance Num Double -- Defined in `GHC.Float'
instance Num Float -- Defined in `GHC.Float'
instance Integral a => Num (GHC.Real.Ratio a)
-- Defined in `GHC.Real'
...plus 11 others
In the expression: 1
In the first argument of `Thing', namely `(\ x -> 1)'
In the expression: Thing (\ x -> 1)
Which makes sense, since there is no instance of show for t0 -> a0 but I am not sure how to describe an instance of this typeclass?
This isn't for anything production, I am just messing around with Haskell and curious what I can and can't do.
Text.Show.Functions. - Thomas M. DuBuissonShow. It seems like it'd be easy, but to the compiler that information is very difficult to obtain after it's been compiled, optimized, and assembled. It's even possible to create new functions at run time, so it makes things pretty complicated. You might be manage it with template haskell that would generate values of type likedata Func a = Func a Stringwhere the function definition could be shoved into the string and the function itself in theaslot. You could then create an easy instance ofShowfor it. But this would be very hard to write. - bheklilrControl.Categoryto add names to functions and compose them with.. Alternatively you can useData.Data'stypeOfto make aShowinstance that just tells you the type of the function, but that doesn't work for polymorphic functions. - not my job