Is it possible to refer to type variables in comparison with a pattern?

The following code (which is not intended to use anything useful) compiles fine:

{-# LANGUAGE ScopedTypeVariables #-}
import System.Random

uselessFunction :: (RandomGen g) => g -> [Int]
uselessFunction gen = 
  let (value::Int, newGen) = (random gen)
  in (uselessFunction newGen)

Is it possible to use type variables in comparison with a sample in the following spirit (code not compiled):

{-# LANGUAGE ScopedTypeVariables #-}
import System.Random

uselessFunction :: (RandomGen g, Random a) => g -> [a]
uselessFunction gen = 
  let (value::a, newGen) = (random gen)
  in (uselessFunction newGen)
+4
source share
2 answers

You already noticed that the extension ScopedTypeVariablesallows you to add type annotations to templates. But for the main purpose of the extension, so that the type variable is locally bounded so that you can reference it inside the function, you must also declare it with forall in the type declaration, for example:

uselessFunction :: forall a g. (RandomGen g, Random a) => g -> [a]

, GHC, .

+8
+2

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


All Articles