Starting point:

fn :: [a] -> Int fn = (2 *) . length

Let's say we only want to constrain the return value, then we could write:

fn list = (2 * length list) :: Int

How about restricting only the argument? Easy.

fn list = 2 * length (list :: [Char])

While this works, it would be preferable to have the signatures at the top collected and not scattered around the function body.

This is the closest I could come to this:

fnSig = undefined :: [Char] -> a fn | False = fnSig | True = (* 2) . length

Based on http://okmij.org/ftp/Haskell/partial-signatures.lhs via http://okmij.org/ftp/Haskell/types.html#partial-sigs

However, I'd like a cleaner solution. Something that communicates better that my intent is partial restriction. Something like this, for example:

fn :: [Char] -> a fn = (2 *) . length

Or maybe:

fn :: [Char] -> _ fn = (2 *) . length

Is this possible?

Edit for further clarification:

@GaneshSittampalam Made an important point in a comment below. I am looking for "a half-way house between no type signature at all and having to give a precise one". So, I am not looking for a TypeClass-based answer, I just want GHC to fill in the blanks for the unspecified (or not fully restricted) types of my function.

Edit in response to @WillNess

Yes, something like this...

fn list = 2 * length list where _ = list :: [Char]

...could work, but only for arguments, and only if the function is not point-free. Is there a way to apply this technique to point-free functions or return values?

Edit in response to @Rhymoid

I got inspired, and played around with @Rhymoid's idea, and came up with this:

fn = (2 *) . length where _ = fn `asTypeOf` (undefined :: [Char] -> a) _ = fn `asTypeOf` (undefined :: a -> Int) _ = fn `asTypeOf` (undefined :: a -> b) _ = fn `asTypeOf` (undefined :: a)

This approach also restricts fn's type signature, and doesn't pollute any namespace.

Ordinarily we would only have one of the asTypeOf lines, I just added multiple to showcase how powerful this approach is.

It is a little more clumsy than how I would like it, but I guess it is pretty neat we can do this even without specific syntactic support from the language.

@Rhymoid, if you like it too, please add it to your answer. :)