How can I refer to a variable of the same type in annotations of different types?

Say I have the following:

import Control.Monad.Random foo :: IO Float foo = fmap realToFrac getRandom 

GHC rightly complains that it does not know the type getRandom ; I can fix it by doing

 foo = fmap realToFrac (getRandom :: IO Double) 

However, consider situtation instead, where I have:

 foo :: (Functor m, MonadRandom m) => m Float foo = fmap realToFrac getRandom 

I can not do only

 foo = fmap realToFrac (getRandom :: m Double) 

I have to repeat the MonadRandom constraint:

 foo = fmap realToFrac (getRandom :: MonadRandom m => m Double) 

With a lot of restrictions, this will lead to a lot of extra input. I would rather not do this. I know that I could use ScopedTypeVariables :

 {-# LANGUAGE ScopedTypeVariables #-} import Control.Monad.Random foo :: MonadRandom m => m Float foo = do x :: Double <- getRandom return (realToFrac x) 

but it is also verbose, and besides, I could not apply it in all such situations.

I also know the PartialTypeSignatures suggestion. I think this may allow me to do:

 foo = fmap realToFrac (getRandom :: _ Double) 

but this is not entirely clear from my scan over his (long) description. Unfortunately, PartialTypeSignatures not ready for prime time and as such has not yet been included in the released version of GHC.

I obviously welcome other suggestions, but one solution to this problem that I can think of would be some way to identify m with n in the following:

 foo :: (Functor m, MonadRandom m) => m Float foo = fmap realToFrac (getRandom :: n Double) 

Is it possible?

+5
source share
1 answer

Your intuition was correct: ScopedTypeVariables is the right extension for the job:

 {-# LANGUAGE ScopedTypeVariables #-} import Control.Monad.Random foo :: (Functor m, MonadRandom m) => m Float foo = fmap realToFrac (getRandom :: m Double) 
+8
source

Source: https://habr.com/ru/post/1206791/


All Articles