Although I agree with the other answer that the question smells, I think it is worth giving a direct answer anyway. You could put a type assertion in your test suite like this:
module Test where
check_type_of_length :: Foldable t => t a -> Int
check_type_of_length = length
If length
can not be given the type you requested, the module will not compile. However, this is not a perfect test: if the type of length
is actually more polymorphic than the type you demand, this will still "pass"; thus, for example, the following module still compiles, even though undefined
does not have exactly the type Foldable t => t a -> Int
.
module Test where
check_type_of_length :: Foldable t => t a -> Int
check_type_of_length = undefined
We can make the test a bit more precise if you want to check a monomorphic type. The idea is to use Typeable
, as suggested by @dfeuer. That would look like this:
module Test where
import Data.Typeable
test_type_of_not :: Bool
test_type_of_not = typeOf not == typeOf (undefined :: Bool -> Bool)
If not
has the monomorphic type Bool -> Bool
, then test_type_of_not
will be True
; if it has a different monomorphic type, it will be False
; and if not
suddenly becomes polymorphic, the module will fail to compile with an ambiguity error.
I do not know an analogous way to test that a polymorphic type does not become more polymorphic; you could perhaps do something nasty like running a short bash script that does
echo :t length | ghci
and checking its output, or something more direct with the GHC API. However, an approach like this is likely to be very fragile.