Is it possible to use the type signature of a function in the type signature of another function?

I have a function f :: String -> String.
I am defining a function g that uses the function f.
Instead of defining g :: String -> (String -> String) -> String where the expression in bold represents the function f, how can I do something like g :: String -> typeof (f) -> String ? I am thinking that this would be useful in writing the types signatures if I use multiple functions taking other functions as arguments, which would be really long? Or is there some other way to achieve this?

If the problem is length, would type synonyms do?

type Mutate = String -> String
g :: String -> Mutate -> String
2 Likes

Right! That’s exactly what I want. I had not seen the use of a synonym for function type signature. Thanks very much!

3 Likes

If you’re worried about long lines, it’s worth noting that it’s common to split a type signature across multiple lines. eg:

foldr :: Foldable t 
      => (a -> b -> b)  -- folding function
      -> b              -- initial accumulator
      -> t a            -- structure to be folded
      -> b

This also has the advantage of allowing you to document the arguments. Personally I would usually prefer full explicitness about the type of function arguments, rather than having to go off and read a type synonym to understand what’s going on.

2 Likes

And if you use Haddock markup, you’ll even see the documentation in the generated html. :wink:

In your example, since the comments come after the thing you’re documenting, just replace -- with -- ^

1 Like